Pagini recente » Cod sursa (job #139824) | Borderou de evaluare (job #3291567) | Cod sursa (job #2875602) | Cod sursa (job #1338912) | Cod sursa (job #2721128)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int DP[2][10005];
int n,G;
int main()
{
int i,j,W,P,l=1;
fin>>n>>G;
for(i = 1; i <= n; i++){
fin>>W>>P;
for(j = 1; j < W; j++){
DP[l][j] = max(DP[1-l][j],DP[l][j-1]);
}
for(j = W; j <= G; j++){
DP[l][j] = max( max( DP[1-l][j], DP[1-l][j-W]+P ) , DP[l][j-1]);
}
l = 1-l;
}
fout<<DP[1-l][G]<<'\n';
return 0;
}