Cod sursa(job #1189266)

Utilizator andreiagAndrei Galusca andreiag Data 21 mai 2014 23:58:01
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
const int Wmax = 5005;
const int INF = 0x3f3f3f3f;

int dp[Wmax];

int main()
{
    ifstream f ("energii.in");
    ofstream g ("energii.out");

    memset(dp, INF, sizeof(dp));
    dp[0] = 0;

    int N, W, e, p;
    f >> N >> W;
    for (int i = 0; i < N; i++)
    {
        f >> e >> p;
        for (int x = W; x >= 0; x--)
            if (x - e >= 0 && dp[x] > dp[x-e] + p)
                dp[x] = dp[x-e] + p;
            else if (x - e < 0 && dp[x] > p)
                dp[x] = p;
    }
    if (dp[W] == INF)
        g << -1 << '\n';
    else
        g << dp[W] << '\n';

    return 0;
}