Cod sursa(job #2434223)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 1 iulie 2019 11:59:59
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>
#include <set>

using namespace std;

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

int costM[5005];
set<int> ToCheck, toAdd;

int main()
{
    int n, need;
    fin >> n >> need;

    for(int i = 1; i <= 5001; ++i)
        costM[i] = 10000;

    for(int i = 1; i <= n; ++i){
        int cant, cost;
        fin >> cant >> cost;

        costM[cant] = min(costM[cant], cost);
        for(auto it : ToCheck){
            costM[it+ cant] = min(costM[it + cant], costM[it] + cost);
            toAdd.insert(it+ cant);
        }

        ToCheck.insert(toAdd.begin(), toAdd.end());
        ToCheck.insert(cant);
        toAdd.clear();
    }

    fout << costM[need] << '\n';
    return 0;
}