Pagini recente » Cod sursa (job #2036405) | Cod sursa (job #2797321) | Cod sursa (job #2108828) | Cod sursa (job #1932350) | Cod sursa (job #1040742)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
unsigned short profit[5001][10001]; short int n, g[5001],G,c[10001];
ofstream out("rucsac.out");
void citeste()
{ ifstream f("rucsac.in");
f>>n>>G;
int i;
for(i=1;i<=n;i++)
f>>g[i]>>c[i];
}
void calculprofit()
{ int i,j,x;
for(i=1;i<=n;i++)
for(j=1;j<=G;j++)
{ if(j<g[i]) //nu e loc pt obiectul i in rucsac
profit[i][j]=profit[i-1][j];
else
{ x=profit[i-1][j-g[i]]+c[i];
if(x>profit[i-1][j]) profit[i][j]=x;
else profit[i][j]=profit[i-1][j];
}
}
}
int main()
{ citeste();
calculprofit();
out<<profit[n][G]<<endl;
return 0;
}