Cod sursa(job #1496063)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 4 octombrie 2015 11:14:26
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

const int Wmax = 5000;
const int Inf = 0x3F3F3F3F;

int minCost[Wmax + 1];

int main()
{
    ifstream in("energii.in");
    ofstream out("energii.out");
    int n, w;
    int energy, cost;

    memset(minCost, Inf, sizeof(minCost));

    in >> n >> w;
    minCost[0] = 0;
    for ( int i = 0; i < n; i++ )
    {
        in >> energy >> cost;
        for ( int j = w - 1; j >= 0; j-- )
        {
            if ( minCost[j] != Inf )
            {
                int newEnergy = j + energy;
                if ( newEnergy > w )
                    newEnergy = w;
                if ( minCost[newEnergy] > minCost[j] + cost )
                    minCost[newEnergy] = minCost[j] + cost;
            }
        }
    }
    in.close();
    out << ( minCost[w] == Inf ? -1 : minCost[w] ) << '\n';
    out.close();
    return 0;
}