Cod sursa(job #1757031)

Utilizator xSliveSergiu xSlive Data 14 septembrie 2016 11:09:04
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main(){
    int nrGen,energMax;
    int generator,costG;

    ifstream f("energii.in");
    ofstream g("energii.out");
    f >> nrGen >> energMax;
    vector<int> cost(energMax + 1,10010);
    for(int i=0;i<nrGen;i++){
        f >> generator >> costG;
        for(int j = energMax;j>=0;j--){
            if(j <= generator)   cost[j] = min(cost[j],costG);
            else cost[j] = min(cost[j],cost[j - generator] + costG);
        }
    }
    if(cost[energMax] == 10010)   g << "-1";
    else g << cost[energMax];
    f.close();
    g.close();
    return 0;
}