Pagini recente » Cod sursa (job #40234) | Cod sursa (job #3321606) | Cod sursa (job #794056) | Cod sursa (job #180428) | Cod sursa (job #3318281)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
vector<int> weight, profit, dp;
int n, g, ans, i, j;
int main()
{
fin>> n>> g;
dp.assign(g+1, -1);
weight.resize(n+1);
profit.resize(n+1);
for(i = 1; i <= n; i++)
fin>> weight[i]>> profit[i];
dp[0] = 0;
for(i = 1; i <= n; i++)
{
for(j = g-weight[i]; j >= 0; j--)
{
if(dp[j] != -1 && dp[j+weight[i]] < dp[j] + profit[i])
dp[j+weight[i]] = dp[j] + profit[i];
}
}
for(i = 1; i <= g; i++)
ans = max(ans, dp[i]);
fout<< ans;
fin.close();
fout.close();
return 0;
}