Cod sursa(job #2470929)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 9 octombrie 2019 21:13:20
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 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;
    }
    if(tot < w){
        fout << -1;
        return 0;
    }
    sort(gen + 1, gen + n + 1);
    for(int i = 1; i <= w; ++i) dp[i] = 1e9;
    for(int i = 1; i <= n; ++i){
        for(int j = w - 1; j >= 0; --j){
           int k = j + gen[i].enrg;
           k = min(k, w);
           dp[k] = min(dp[k], dp[j] + gen[i].cost);
        }
    }
    fout << dp[w];
    return 0;
}