Cod sursa(job #1668582)

Utilizator narcios_neculaNarcis Necula narcios_necula Data 29 martie 2016 21:29:29
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 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;
    if(x <= ge)
        b[x] = y;
    else
        b[ge] = y;
    for(i = 2; i <= n; ++i)
    {
        f >> x >> y;
        for(j = ge - 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;

        if(x <= ge)
        {
            if(b[x] == 0 || b[x] > y)
                b[x] = y;
        }
        else
            b[ge] = y;
    }
    if(b[ge] == 0)
        g << -1 << '\n';
    else
        g << b[ge] << '\n';
        return 0;
}