Pagini recente » Cod sursa (job #863339) | Cod sursa (job #2203854) | Cod sursa (job #313197) | Cod sursa (job #19498) | Cod sursa (job #2426034)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("rucsac.in");
ofstream r("rucsac.out");
vector <int> greutati [3002];
int ans[2][3002];
int wmax,k;
int n, g,w,p;
struct obiect
{
int pf;
int kg;
} ob[36003];
bool mycmp (int a ,int b)
{
return a>b;
}
int main()
{
f>>n>>g;
int p0=0;
while (n--)
{
f>>w>>p;
if (w==0)
{
p0+=p;
continue;
}
greutati[w].push_back(p);
wmax=max(wmax,w);
}
for (int i=1; i<=wmax; i++)
{
sort(greutati[i].begin(),greutati[i].end(),mycmp);
int l=greutati[i].size();
int stop=min(l ,g/i);
for (int j=0; j<stop ; j++)
{
ob[++k].kg=i;
ob[k].pf=greutati[i][j];
}
}
int l1=0;
int l2=1;
for (int i=1; i<=k; i++)
{
for (int j=1; j<=g; j++)
{
if (ob[i].kg > j)
ans[l2][j]=ans[l1][j];
ans[l2][j]=max(ans[l1][j], ob[i].pf+ans[l1][j-ob[i].kg]);
}
swap(l1,l2);
}
if (k%2==0)
r<<ans[0][g]+p0;
else r<<ans[1][g]+p0;
}