Cod sursa(job #2470916)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 9 octombrie 2019 21:07:20
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1005, MAXG = 10000005;

#define enrg first
#define cost second

pair<int, int> gen[MAXN];
int dp[MAXG];

int main()
{
    ifstream fin("energii.in");
    ofstream fout("energii.out");
    int n, w, tot = 0;
    fin >> n >> w;
    for(int i = 1; i <= n; ++i){
        fin >> gen[i].enrg >> gen[i].cost;
        tot += gen[i].enrg;
    }
    sort(gen + 1, gen + n + 1);
    if(tot < w){
        fout << -1;
        return 0;
    }
    for(int i = 1; i <= tot; ++i) dp[i] = 1e9;
    for(int i = 1; i <= n; ++i){
        for(int j = tot; j >= gen[i].enrg; --j)
            dp[min(j, w)] = min(dp[min(w, j)], dp[min(w, j) - gen[i].enrg] + gen[i].cost);
    }
    fout << dp[w];
    return 0;
}