Cod sursa(job #1265759)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 17 noiembrie 2014 18:49:06
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>

using namespace std;

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

const int gmax = 1000 , wmax = 5000 , infinit = 10000001;
int v[wmax + 1] , e[gmax + 1] , c[gmax + 1];
int g , w;

int main()
{
    in >> g >> w;

    for(int i = 1 ; i <= g ; i++)
        in >> e[i] >> c[i];

    for(int i = 1 ; i <= w ; i++)
        v[i] = infinit;

    for(int i = 1 ; i <= g ; i++)
        for(int j = w - 1 ; j >= 0 ; j--)
            if(v[j] + c[i] < v[j + e[i]])
                v[j + e[i]] = v[j] + c[i];

    if(v[w] == infinit)
        out << "-1\n";
    else
        out << v[w] << '\n';
    return 0;
}