Pagini recente » Cod sursa (job #2534018) | Cod sursa (job #795980) | Cod sursa (job #415324) | Cod sursa (job #1591576) | Cod sursa (job #750608)
Cod sursa(job #750608)
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
const int inf = 1 << 30;
int g, w, sol = inf;
int best[5050];
int main()
{
int i, a, b;
in >> g >> w;
memset(best, -1, sizeof(best));
best[0] = 0;
while(g--){
in >> a >> b;
for(i = w - 1; i >= 0; --i)
if(best[i] != -1)
if(i + a < w){
if((best[i] + b < best[i + a]) || (best[i + a] == -1))
best[i + a] = best[i] + b;
}
else
if(best[i] + b < sol)
sol = best[i] + b;
}
out << (sol == inf ? -1 : sol);
return 0;
}