Cod sursa(job #1127332)

Utilizator robert_stefanRobert Stefan robert_stefan Data 27 februarie 2014 12:01:07
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#define IN "energii.in"
#define OUT "energii.out"
#define NMAX 1005
#define WMAX 5005
#define INF 0x3f3f3f3f3f3f3f

using namespace std;

ifstream in(IN);
ofstream out(OUT);

int n, w, en[NMAX], cost[NMAX], l[WMAX], ll[WMAX];

int main()
{
    int i, j;
    in>>n>>w;
    for(i=1; i<=n; ++i)
        in>>en[i]>>cost[i];
    for(i=1; i<=w; ++i)
        l[i]=INF;
    for(i=1; i<=n; ++i)
    {
        for(j=1; j<=w; ++j)
            if(en[i]>=j)
                ll[j]=min(l[j], cost[i]);
            else
                ll[j]=min(l[j], l[j-en[i]] + cost[i]);
        for(j=1; j<=w; ++j)
            l[j]=ll[j];
    }
    if(ll[w]==INF)
        out<<"-1\n";
    else
        out<<ll[w]<<'\n';
    in.close();
    out.close();
    return 0;
}