Pagini recente » Cod sursa (job #2277338) | Cod sursa (job #1784324) | Cod sursa (job #2782553) | Cod sursa (job #1386898) | Cod sursa (job #1981698)
#include <iostream>
#include <fstream>
#define MAXN 5001
#define MAXG 10001
using namespace std;
int a,n,gr,greutate[MAXN],profit[MAXN],pmax[MAXN][MAXG],profmax;
int main()
{
ifstream f ("rucsac.in");
ofstream g ("rucsac.out");
f>>n>>gr;
for(int i=1;i<=n;i++)
f>>greutate[i]>>profit[i];
for(int i=1;i<=n;i++)
for(int j=1;j<=gr;j++){
if(j>=greutate[i])pmax[i][j]=max(pmax[i-1][j],pmax[i-1][j-greutate[i]]+profit[i]);
else pmax[i][j]=pmax[i-1][j];
}
for(int i=1;i<=gr;i++)
if(pmax[n][i]>=profmax)
profmax=pmax[n][i];
g<<profmax;
f.close ();
g.close ();
return 0;
}