Cod sursa(job #1905879)

Utilizator CronosClausCarare Claudiu CronosClaus Data 6 martie 2017 11:27:32
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <bits/stdc++.h>
#define INF 13000000

using namespace std;

long g, w, e, c, costs[ 100 * 1000 ];

int main()
{
    ifstream cin("energii.in");
    ofstream cout("energii.out");
    cin >> g >> w;
    for (long i = 0; i <= w; i++)
        costs[ i ] = INF;
    for (long i = 0; i < g; ++i){
            cin >> 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)
        cout << costs[w] <<'\n';
    else
        cout<<"-1" << endl;
    return 0;
}