Cod sursa(job #2546393)

Utilizator Hey_HeyIacovlev Denis Hey_Hey Data 14 februarie 2020 09:56:36
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 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=0; j<=w; j++)
        if(j<=e) costs[j]=min(c,costs[j]);
        else if(c+costs[j-e]<costs[j]) costs[j]=c+costs[j-e];
        
        /*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;
}