Pagini recente » Cod sursa (job #3129363) | Cod sursa (job #2758773) | Cod sursa (job #801009) | Cod sursa (job #2219057) | Cod sursa (job #1895367)
#include<bits/stdc++.h>
#define oo 0x3f3f3f3f
using namespace std;
int n, w, D[5001][5001], Val[1001], Cost[1001], st;
int main()
{
ifstream f("energii.in");
ofstream g("energii.out");
f >> n >> w;
for(int i = 1; i <= n; i++)
f >> Cost[i] >> Val[i];
//border
for(int i = 0; i <= n; i ++)
D[i][0] = oo;
for(int i = 0; i <= w; i ++)
D[0][i] = oo;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= w; j ++)
{
if( Cost[j] <= j )
{
if( D[i-1][j - Cost[i]] == oo ){
D[i][j] = min( D[i-1][j], Val[j] );
st += D[i][j];
}
else{
D[i][j] = min(D[i-1][j], D[i-1][j - Cost[i]] + Val[j]);
st += D[i][j];
}
}else D[i][j] = D[i-1][j];
}
//printf(out, "%d", D[n][w]);
if( st >= w ) g << st;
else g << -1;
return 0;
}