Pagini recente » Cod sursa (job #2386291) | Cod sursa (job #390382) | Cod sursa (job #2618477) | Cod sursa (job #3195954) | Cod sursa (job #2491013)
#include<bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int weight[5000],profit[5000];
int n,max_weight;
int dp[10001][5001];
void read()
{
in>>n>>max_weight;
for(int i=0; i<n; i++)
{
in>>weight[i]>>profit[i];
}
}
int knapsack()
{
for(int i=1; i<=n; i++)
{
for(int g=0; g<=max_weight; g++)
{
dp[i][g]=dp[i-1][g];
if(weight[i-1]<=g)
{
dp[i][g]=max(dp[i][g],dp[i-1][g-weight[i-1]]+profit[i-1]);
}
}
}
return dp[n][max_weight];
}
int main()
{
read();
out<<knapsack();
}