Pagini recente » Cod sursa (job #963570) | Cod sursa (job #2699664) | Cod sursa (job #1128935) | Cod sursa (job #216824) | Cod sursa (job #403926)
Cod sursa(job #403926)
#include <cstdio>
#include <cstdlib>
int cost[5010];
short gen[1010][2];
const int none=4000000;
int g,w;
int main(){
FILE* fin=fopen("energii.in","r");
freopen("energii.out","wt",stdout);
fscanf(fin,"%u %u",&g,&w);
for(int i=0;i<g;i++){
fscanf(fin,"%hu %hu",&gen[i][0],&gen[i][1]);
}
for(int i=0;i<=w;i++){
cost[i]=none;
}
for(int i=0;i<g;i++){
for(int j=w;j>0;j--){
if(cost[j]!=none){
if(j+gen[i][1]>=w){
if(cost[j]+gen[i][1]<cost[w]){
cost[w]=cost[j]+gen[i][1];
}
}else if(cost[j+gen[i][0]]>cost[j]+gen[i][1]){
cost[j+gen[i][0]]=cost[j]+gen[i][1];
}
}
if(gen[i][0]>=w){
if(gen[i][1]<cost[w]){
cost[w]=gen[i][1];
}
}else if(cost[gen[i][0]]>gen[i][1]){
cost[gen[i][0]]=gen[i][1];
}
}
}
if(cost[w]==none){
printf("-1");
}else{
printf("%u",cost[w]);
}
}