Pagini recente » Cod sursa (job #1930145) | Cod sursa (job #164785) | Cod sursa (job #1207899) | Cod sursa (job #1264882) | Cod sursa (job #678860)
Cod sursa(job #678860)
#include<fstream>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int W,G,C[1002],E[1002],cost[1002][5002];
int const inf=32000000;
void cit(){
int i;
fin>>G>>W;
for(i=1;i<=G;i++){
fin>>E[i]>>C[i];
if(E[i]>W) E[i]=W;
}
for(i=1;i<=W;i++) cost[0][i]=inf;
}
void calc(){
int i,j,k,s_max=0;
for(i=1;i<=G;i++){
s_max+=E[i];
for(j=1;j<=W;j++){
if(E[i]<=j){
if(cost[i-1][j-E[i]]!=inf) k=cost[i-1][j-E[i]]+C[i];
else k=C[i];
if(j>s_max) cost[i][j]=k;
else{
if(k<cost[i-1][j]) cost[i][j]=k;
else cost[i][j]=cost[i-1][j];
}
}
else cost[i][j]=cost[i-1][j];
}
}
}
int main(){
cit();
calc();
if(cost[G][W] != inf) fout<<cost[G][W]<<endl;
else fout<<-1;
return 0;
}