Cod sursa(job #2716716)

Utilizator doru.nituNitu Doru Constantin doru.nitu Data 5 martie 2021 16:07:09
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#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;
}