Cod sursa(job #1916851)

Utilizator JokesWarMihai Baruta JokesWar Data 9 martie 2017 10:31:19
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>

using namespace std;

int n, w, c[1002], e[1002], op[10002], sol, ma;

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

    fin >> n >> w;

    for(int i = 1; i <= n; ++i)
        fin >> e[i] >> c[i];

    sol = 999999;
    ma = 999999;

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j <= w; ++j)
            if(op[j + c[i]] < op[j] + e[i])
            {
                op[j+c[i]] = op[j] + e[i];
                if(j+c[i] < ma && op[j+c[i]] <= sol && op[j+c[i]] >= w)
                {
                    ma = j+c[i];
                    sol = op[j+c[i]];
                }
            }

    if(sol == 999999) fout << -1;
    else fout << ma;

    return 0;
}