Cod sursa(job #2691370)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 28 decembrie 2020 14:07:57
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>

using namespace std;

const int N = 5001;

int cost[N];

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

    int n, w, e, c, s = 0;
    in >> n >> w;
    for (int i = 1; i <= w; ++i)
        cost[i] = -1;
    while (n--) {
        in >> e >> c;
        s += e;
        for (int i = w - 1; i >= 0; --i)
            if (cost[i] >= 0) {
                if (cost[max(w, i + e)] == -1)
                    cost[max(w, i + e)] = cost[i] + c;
                else
                    cost[max(w, i + e)] = min(cost[i + e], cost[i] + c);
            }
    }
    int rez = 1 << 30;
    for (int i = w; i <= s; ++i)
        if (cost[i] > 0)
            rez = min(rez, cost[i]);
    if (rez == 1 << 30)
        out << -1;
    else
        out << rez;

    in.close();
    out.close();
    return 0;
}