Cod sursa(job #2050008)

Utilizator skeniaTirla Ovidiu skenia Data 27 octombrie 2017 21:39:26
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");

int length, necessary, dynamicCost[5002];

int main() {
    fin >> length >> necessary;
    for (int iter = 0; iter < length; ++iter) {
        int energy, cost;
        fin >> energy >> cost;
        if (dynamicCost[energy] == 0)
            dynamicCost[energy] = cost;
        dynamicCost[energy] = min(dynamicCost[energy], cost);
        if (iter + 1 - energy > 0 and dynamicCost[iter + 1 - energy] > 0)
            dynamicCost[energy] = min(dynamicCost[energy], dynamicCost[iter + 1 - energy] + cost);
    }
    if (dynamicCost[necessary] == 0)
        fout << "-1";
    else
        fout << dynamicCost[necessary] << '\n';
    return 0;
}