Pagini recente » Cod sursa (job #2555143) | Cod sursa (job #2084638) | Cod sursa (job #2879106) | Cod sursa (job #704327) | Cod sursa (job #2695472)
#include <iostream>
#include <fstream>
std::ifstream in("rucsac.in");
std::ofstream out("rucsac.out");
const int N=5001;
const int W=10001;
/// dp[i][j] este valoarea maxima ce se poate obtine alegand din primele i obiecte cu suma greutatilor <=j
int dp[N][W];
int val[N];
int wt[N];
int main()
{
int n, w;
in>>n>>w;
for(int i=1; i<=n; i++)
{
in>>wt[i]>>val[i];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=w; j++)
{
if(i==0 || j==0)
{
dp[i][j]=0;
}
else
{
if(wt[i]<=j)
{
dp[i][j]=std::max(dp[i-1][j], val[i]+dp[i-1][j-wt[i]]);
}
else
{
dp[i][j]=dp[i-1][j];
}
}
}
}
out<<dp[n][w];
}