Cod sursa(job #2470920)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 9 octombrie 2019 21:09:25
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 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 = w; j >= 0; --j){
           int k = j + gen[i].enrg;
           k = min(k, w);
           dp[k] = min(dp[k], dp[k - gen[i].enrg] + gen[i].cost);
        }
    }
    fout << dp[w];
    return 0;
}