Pagini recente » Cod sursa (job #1579736) | Cod sursa (job #2334125) | Cod sursa (job #2379429) | Cod sursa (job #2217192) | Cod sursa (job #3223077)
#include <iostream>
#include <fstream>
using namespace std;
long long dp[2][10001];
struct obiect
{
int gr,profit;
};
obiect ob[5001];
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int main()
{
int n,g;
in>>n>>g;
for(int i=1;i<=n;i++)
in>>ob[i].gr>>ob[i].profit;
dp[1][ob[1].gr]=ob[1].profit;
for(int i=1;i<=n;i++)
{
int lin_c=i%2;
int lin_ant=1-i%2;
for(int j=0;j<=g;j++)
if(j>=ob[i].gr)
dp[lin_c][j]=max(dp[lin_ant][j-ob[i].gr]+ob[i].profit,dp[lin_ant][j]);
else
dp[lin_c][j]=dp[lin_ant][j];
}
int max_profit=-1;
for(int j=0;j<=g;j++)
max_profit=max(max_profit,dp[n%2][j]);
out<<max_profit;
return 0;
}