Pagini recente » Cod sursa (job #2963539) | Cod sursa (job #732291) | Cod sursa (job #162865) | Cod sursa (job #382789) | Cod sursa (job #3244385)
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct obj
{
int greutate, profit;
};
int main()
{
int dp[2][10001]={};
obj chestii[5001];
int n,gmax;
fin>>n>>gmax;
for(int i=1;i<=n;i++)
fin>>chestii[i].greutate>>chestii[i].profit;
for(int i=1;i<=n;i++)
{
int lin_c=i%2;
int lin_ant=1-lin_c;
for(int j=0;j<=gmax;j++)
{
if(chestii[i].greutate>j)
dp[lin_c][j]=dp[lin_ant][j];
else
{
dp[lin_c][j]=max(dp[lin_ant][j],chestii[i].profit+dp[lin_ant][j-chestii[i].greutate]);
}
}
}
fout<<dp[0][gmax];
return 0;
}