Pagini recente » Cod sursa (job #1485117) | Cod sursa (job #3273684) | Cod sursa (job #2158464) | Cod sursa (job #1034421) | Cod sursa (job #2426531)
#include <bits/stdc++.h>
#define NM 5005
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int w[NM],p[NM],dp[2][NM*2];
int main()
{ int n,gMax;
f>>n>>gMax;
for(int i=1; i<=n; i++) f>>w[i]>>p[i];
int ind=0;
for(int i=1; i<=n; i++,ind=1-ind)
{ for(int j=0; j<=gMax; j++)
{ dp[ind][j]=dp[1-ind][j];
if(w[i]<=j)
dp[ind][j]=max(dp[1-ind][j],dp[1-ind][j-w[i]]+p[i]);
}
}
g<<dp[1][gMax];
return 0;
}