Cod sursa(job #1594920)

Utilizator ArceyGeorge Cioroiu Arcey Data 9 februarie 2016 20:15:47
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <cstdio>
#include <iostream>
#include <set>
#include <climits>
#include <map>
#include <algorithm>
#include <list>
#include <vector>
#include <utility>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <string>

using namespace std;

int main() {
  //  freopen("tt.txt", "r", stdin);
    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(0);

    int dp[5001], g, w;
    cin >> g >> w;

    for (int i = w; i > 0; i--) {
        dp[i] = -1;
    }
    dp[0] = 0;

    for (int i = 1; i <= g; i++) {
        int e, c;
        cin >> e >> c;

        for (int j = w; j >= 0; j--) {
            if (dp[j] != -1) {
                int next = j + e;
                if (next > w)
                    next = w;

                if (dp[next] == -1 || dp[next] > dp[j] + c)
                    dp[next] = dp[j] + c;
            }
        }
    }

    cout << dp[w];

    return 0;
}