Pagini recente » Cod sursa (job #2312374) | Borderou de evaluare (job #1900560) | Cod sursa (job #2916510) | Cod sursa (job #1923204) | Cod sursa (job #2970588)
#include<iostream>
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;
}