Pagini recente » Cod sursa (job #189051) | Cod sursa (job #2703750) | Cod sursa (job #2646688) | Cod sursa (job #2352689) | Cod sursa (job #1844329)
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("energii.in");
ofstream fout("energii.out");
int main() {
int g,w,eg[1002],cg[1002];
int cost[1005][5005];
f >> g >> w;
for(int i = 1; i <= g; i++) {
f >> eg[i] >> cg[i];
}
for(int i = 0; i <= g; i++) {
for(int j = 0; j <= w; j++)
cost[i][j] = 100000;
}
for(int i = 1; i <= g; i++) {
for(int j = 1; j <= w; j++) {
if(j > eg[i]) {
cost[i][j] = min(cost[i-1][j],cg[i] + cost[i-1][j - eg[i]]);
}
else {
cost[i][j] = min(cost[i - 1][j],cg[i]);
}
}
}
int min = cost[g][w];
if(min == 100000)
fout << "-1";
else
fout << min;
f.close();
fout.close();
return 0;
}