Cod sursa(job #1631974)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 5 martie 2016 20:36:05
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#include <algorithm>

using namespace std;

int best[5005];

int main()
{
    ifstream cin("energii.in");
    ofstream cout("energii.out");

    int n = 0, w = 0;
    cin >> n >> w;

    for (int i = 1; i <= w; ++ i)
        best[i] = 1e9;

    int wi, ci;
    while (n --) {
        cin >> wi >> ci;
        for (int i = w; i >= 0; -- i)
            if (ci + best[i] <= best[min(w, i + wi)])
                best[min(w, i + wi)] = ci + best[i];
    }

    if (best[w] < 1e9)
        cout << best[w] << '\n';
    else
        cout << "-1\n";

    cin.close();
    cout.close();
    return 0;
}