Cod sursa(job #1668558)

Utilizator narcios_neculaNarcis Necula narcios_necula Data 29 martie 2016 21:15:49
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <fstream>
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
int x,y,n,ge,i,j,s,mi,b[500009];
int main()
{
    f >> n >> ge;
    f >> x >> y;
    s = x;
    b[x] = y;
    for(i = 2; i <= n; ++i)
    {
        f >> x >> y;
        for(j = s; j >= 1; --j)
            if(b[j] > 0)
                if(b[j + x] < b[j] + y || b[j + x] == 0)
                    b[j + x] = b[j] + y;
        s += x;
        if(b[x] == 0 || b[x] > y)
            b[x] = y;
    }
    if(b[ge] == 0)
        g << -1 << '\n';
    else
        g << b[ge] << '\n';
        return 0;
}