Cod sursa(job #605553)
#include<stdio.h>
#define INF -1
typedef struct generator{
int pow;
int cost;
};
int main(){
FILE *f, *g;
f=fopen("energii.in", "r");
g=fopen("energii.out", "w");
generator gen[1001];
int G, W;
int cost[20001];
fscanf(f, "%d %d", &G, &W);
for(int i=0; i<=20000; i++){
cost[i]=INF;
}
cost[0]=0;
for(int i=0; i<G; i++){
fscanf(f, "%d %d", &gen[i].pow, &gen[i].cost);
for(int j=20000; j>=gen[i].pow; j--){
if(cost[j-gen[i].pow]!=-1 && ((cost[j-gen[i].pow]+gen[i].cost<cost[j]) || cost[j]==INF)){
cost[j]=cost[j-gen[i].pow]+gen[i].cost;
}
}
}
fclose(f);
int min=99999;
for(int i=W; i<20000; i++){
if(cost[i]<min && cost[i]!=INF){
min=cost[i];
}
}
if(min==99999){
fprintf(g, "-1");
}else{
fprintf(g, "%d", min);
}
fclose(g);
return 0;
}