Cod sursa(job #1899709)

Utilizator felipeGFilipGherman felipeG Data 2 martie 2017 21:32:49
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream fin ("energii.in");
ofstream fout ("energii.out");

struct obiect{
    short w,p;
};

int n, g, i, j, Pmax[10010], maxi;
obiect ob[5010];


int main()
{
    fin >> n;
    fin >> g;

    for (i = 1; i <= n; ++i)
        fin >> ob[i].w >> ob[i].p;

    for (i = 1; i <= n; ++i)
        for (j = g; j >= ob[i].w; --j)
        {
            Pmax[j] = max(Pmax[j], Pmax[j - ob[i].w] + ob[i].p);
            if (Pmax[j] > maxi)
                 maxi = Pmax[j];
        }

    fout << maxi;
    return 0;
}