Pagini recente » Cod sursa (job #2975739) | Cod sursa (job #2624060) | Cod sursa (job #239448) | Cod sursa (job #2720391) | Cod sursa (job #796897)
Cod sursa(job #796897)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
int G, W;
int energie[1001], cost[1001],mat[1001][5001];
int main()
{
int i,j;
f>>G>>W;
for(i = 1; i <= G; i++)
f>>energie[i]>>cost[i];
for(i = 0; i < G; i++)
for(j = 1; j <= W; j++)
mat[i][j] = 10001;
for(i = 1; i <= G; i++){
for(j = 0; j <= W; j++){
mat[i][j] = mat[i-1][j];
if (j < energie[i])
mat[i][j] = min(mat[i][j], cost[i]);
else
mat[i][j] = min(mat[i][j], mat[i-1][j-energie[i]] + cost[i]);
}
}
if(mat[G][W] != 10001)
g<<mat[G][W];
else
g<<"-1"<<"\n";
}