Pagini recente » Cod sursa (job #565416) | Cod sursa (job #2032577) | Cod sursa (job #2155081) | Cod sursa (job #1016894) | Cod sursa (job #2551797)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct object
{
int weight, value;
};
int n,g;
object lst[5001];
int dp[5001][10001];
void citire()
{
fin>>n>>g;
for(int i=1;i<=n;++i)
{
fin>>lst[i].weight;
fin>>lst[i].value;
}
}
int main()
{
citire();
for(int i=1;i<=n;++i)
{
for(int j=0;j<=g;++j)
{
if(j-lst[i].weight<0)
dp[i][j] = dp[i-1][j];
else
{
int bef = j-lst[i].weight;
dp[i][j] = max(dp[i-1][bef] + lst[i].value, dp[i-1][j]);
}
}
}
fout<<dp[n][g];
return 0;
}