Cod sursa(job #2132637)

Utilizator Neamtu_StefanStefan Neamtu Neamtu_Stefan Data 15 februarie 2018 22:19:50
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>

#define gr first
#define pr second

std :: ifstream fin;
std :: ofstream fout;

int main()
{
    fin.open("rucsac.in");
    fout.open("rucsac.out");

    int n, g;
    fin >> n >> g;

    std :: vector <std :: pair <int, int> > obiecte(n+2);

    for(int i=1; i<=n; ++i)
        fin >> obiecte[i].gr >> obiecte[i].pr;

    std :: vector <int> l1(g+2), l2(g+2);

    for(int i=1; i<=g; ++i)
        if (obiecte[1].gr > i)
            l1[i]=0;
        else
            l1[i]=obiecte[1].pr;

    for(int i=2; i<=n;l1=l2, ++i)
        for(int j=1; j<=g; ++j)
            if (j-obiecte[i].gr>=0)
                l2[j]=std :: max(l1[j], l1[j-obiecte[i].gr] + obiecte[i].pr);
            else
                l2[j]=l1[j];

    fout << l2[g];

    return 0;
}