Cod sursa(job #2020636)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 11 septembrie 2017 01:02:32
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <bits/stdc++.h>

#define oo 0x3f3f3f3
using namespace std;

int G, W, power[1 << 10], cost[1 << 10];
vector<int>best;

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

int main()
{
    fin >> G; fin.get();
    fin >> W; best.resize(W + 1, oo);
    fin.get();
    for (int i = 1; i <= G; ++i)
        fin >> power[i] >> cost[i];

    best[0] = 0;
    for (int i = 1; i <= G; ++i)
        for (int j = W; j >= power[i]; --j)
            if (best[j] > best[j - power[i]] + cost[i])
                best[j] = best[j - power[i]] + cost[i];
    fout << (best[W] == oo ? -1 : best[W]);
    return 0;
}