Cod sursa(job #2185863)

Utilizator DawlauAndrei Blahovici Dawlau Data 24 martie 2018 23:19:35
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<fstream>
using namespace std;

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

const int NMAX = 1e3 + 5, maxEnergy = 5e3 + 5, INF = 1e9;

int bestCost[maxEnergy];
int energy[NMAX], cost[NMAX];

int N, W;

inline void readData(){

    fin >> N >> W;

    int idx;
    for(idx = 1; idx <= N; ++idx)
        fin >> energy[idx] >> cost[idx];
}

inline void getBestPrice(){

    fill(bestCost + 2, bestCost + 1 + W, INF);

    int idx, e;

    for(idx = 1; idx <= N; ++idx){

        for(e = W; e >= energy[idx]; --e)
            bestCost[e] = min(bestCost[e], bestCost[e - energy[idx]] + cost[idx]);
        for(e = energy[idx] - 1; e >= 2; --e)
            bestCost[e] = min(bestCost[e], cost[idx]);
    }

    fout << bestCost[W];
}

int main(){

    readData();
    getBestPrice();
}