Cod sursa(job #1324722)

Utilizator cautionPopescu Teodor caution Data 22 ianuarie 2015 18:48:29
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.58 kb
#include <fstream>
#define INF 13000000
using namespace std;
int main()
{
    ifstream in("energii.in");
    ofstream out("energii.out");
    long g, w, e, c, *costs;
    in>>g>>w;
    costs=new long[w+1];
    for(long i=0; i<=w; ++i)
        costs[i]=INF;
    for(long i=0; i<g; ++i)
    {
        in>>e>>c;
        for(long j=w; j>e; --j)
            if(c+costs[j-e]<costs[j]) costs[j]=c+costs[j-e];
        for(long j=0; j<=e&&j<=w; ++j)
            if(costs[j]>c) costs[j]=c;
    }
    if(costs[w]!=INF) out<<costs[w]<<endl;
    else out<<"-1"<<endl;
    return 0;
}