Pagini recente » Cod sursa (job #1618344) | Cod sursa (job #2128359) | Cod sursa (job #2128306) | Cod sursa (job #3352265) | Cod sursa (job #3344207)
#include <bits/stdc++.h>
int weight[5001];
int profit[5001];
std::ifstream fin("rucsac.in");
std::ofstream fout("rucsac.out");
int dp[5001];
int main()
{
int n, G;
fin >> n >> G;
for(int i = 1; i <= n; i++)
{
fin >> weight[i] >> profit[i];
}
for(int i = 1; i <= n; i++)
{
for(int g = G; g >= 1; g--)
{
if(g >= weight[i])
{
dp[g] = std::max(dp[g], dp[g - weight[i]] + profit[i]);
}
}
}
fout << dp[G];
}