Pagini recente » Cod sursa (job #2364922) | Cod sursa (job #1015124) | Cod sursa (job #2958919) | Cod sursa (job #3291415) | Cod sursa (job #1895343)
#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("energie.in");
ofstream g("energie.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];
}
}
}
//printf(out, "%d", D[n][w]);
if( st >= w ) g << st;
else g << -1;
return 0;
}