Pagini recente » Cod sursa (job #553061) | Borderou de evaluare (job #2022797) | Cod sursa (job #2705230) | Cod sursa (job #3234827) | Cod sursa (job #2716716)
#include<fstream>
#include<algorithm>
#define N 5050
using namespace std;
ifstream f("rucsac.in");
ofstream out("rucsac.out");
int v[N],n, gmax, pmax=-10, smg, smp;
struct nod
{
int g,p;
};
nod a[N];
bool comp(const nod &x, const nod &y)
{
if(x.g != y.g)
return (x.g < y.g);
return x.p > y.p;
}
void bkt(int k)
{
if(k>n)
{
if(smp > pmax)
pmax = smp;
return;
}
if(smg +a[k].g <= gmax)
{
smg += a[k].g;
smp += a[k].p;
v[k] = 1;
bkt(k+1);
v[k] = 0;
smp -= a[k].p;
smg -= a[k].g;
}
v[k] = 0;
bkt(k+1);
}
int main()
{
f>>n>>gmax;
for(int i =1; i<=n; ++i)
{
f>>a[i].g>>a[i].p;
}
sort(a+1, a+n+1, comp);
bkt(1);
out<<pmax;
}