Pagini recente » Cod sursa (job #270518) | Cod sursa (job #3166910) | Clasament aasfa | Cod sursa (job #107999) | Cod sursa (job #2970594)
#include<fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int N=5e3;
const int G=1e4;
int profit[G+1];
struct obiecte
{
int g;
int p;
}v[N+1];
int main()
{
int n, G, maxim=-1;
fin >> n >> G;
for(int i=0; i<n; i++)
{
fin >> v[i].g >> v[i].p;
}
for(int i=0; i<G; i++)
profit[i]=-1;
profit[0]=0;
for(int i=0; i<n; i++)
{
for(int j=G-v[i].g; j>=0; j--)
{
if(profit[j]!=-1 && profit[j]+v[i].p > profit[j+v[i].g])
{
profit[j+v[i].g]=profit[j] + v[i].p;
}
}
}
for(int i=1; i<=G; i++)
if(profit[i]>maxim)maxim=profit[i];
fout << maxim;
return 0;
}