Pagini recente » Profil arghirediana | Cod sursa (job #286609) | Cod sursa (job #2059850) | Cod sursa (job #2359463) | Cod sursa (job #925497)
Cod sursa(job #925497)
#include <fstream>
#define MMAX (1<<26)
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
int G, w, e[1005], c[1005], cost[1005][1005];
int main()
{
int i,j;
f >> G >> w;
for(i = 1; i <= G; ++i)
{
f >> e[i] >> c[i];
}
for(i = 0; i <= G; ++i) cost[i][0] = 1<<30;
for(j = 0; j <= w; ++j) cost[0][j] = 1<<30;
for(i = 1; i <= G; ++i)
{
for(j = 1; j <= w; ++j)
{
if(j > e[i])
{
cost[i][j] = min(cost[i - 1][j], cost[i - 1][j - e[i]] + c[i]);
}
else
{
cost[i][j] = min(cost[i - 1][j], c[i]);
}
}
}
if(cost[G][w] != 1<<30)
{
g << cost[G][w] << '\n';
}
else
{
g << -1 << '\n';
}
}