Cod sursa(job #2020637)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 11 septembrie 2017 01:06:15
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 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];

        for (int j = 1; j < power[i]; j++)
            if (cost[i] < best[j])
                best[j] = cost[i];
    }

    fout << (best[W] == oo ? -1 : best[W]);
    return 0;
}