Cod sursa(job #1668564)

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