Cod sursa(job #1265786)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 17 noiembrie 2014 19:10:40
Problema Energii Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

using namespace std;

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

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

int main()
{
    int cmax = 0;

    in >> g >> w;

    for(int i = 1 ; i <= g ; i++)
    {
        in >> e[i] >> c[i];
        cmax = max(cmax , c[i]);
    }

    for(int i = 1 ; i <= w + cmax; 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];

    int rez = infinit;

    for(int i = w ; i <= w + cmax; i++)
        rez = min(rez , v[i]);

    if(rez != infinit)
        out << rez << '\n';
    else
        out << "-1\n";
    return 0;
}