Pagini recente » Cod sursa (job #35177) | Cod sursa (job #1642545) | Cod sursa (job #2730476) | Cod sursa (job #2306466) | Cod sursa (job #1121401)
#include <fstream>
#include <algorithm>
using namespace std;
int best[5000][5000],i,n,gr,j,pz,mx;
struct obiect
{
int w,p;
}a[5000];
int cmp (obiect A, obiect B)
{
if (A.w<B.w) return 1;
return 0;
}
int main()
{
ifstream f("rucsac.in");
ofstream g("rucsac.out");
f>>n>>gr;
for (i=1;i<=n;i++)
f>>a[i].w>>a[i].p;
sort (a+1,a+n+1,cmp);
for (i=0;i<=n;i++)
for (j=0;j<=gr;j++)
best[i][j]=-1;
best[0][0]=0;
for (i=1;i<=n;i++)
for (j=0;j<=gr;j++)
{
pz=j-a[i].w;
if (pz>=0)
if(best[i-1][pz]!=-1) best[i][j]=best[i-1][pz]+a[i].p;
if (best[i][j]<best[i-1][j]) best[i][j]=best[i-1][j];
}
mx=-1;
for (i=1;i<=gr;i++)
if (mx<best[n][i]) mx=best[n][i];
g<<mx;
return 0;
}