Cod sursa(job #2470891)

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