Cod sursa(job #2663813)

Utilizator david2003David Ghergut david2003 Data 27 octombrie 2020 13:16:45
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#define limit 1000000000
using namespace std;
ifstream cin("energii.in");
ofstream cout("energii.out");
long long G, W, energie[10001], costul[10001], d[10001];
int main(){
    cin >> G >> W;
    for (int i = 1; i <= G; i++)
        cin >> energie[i] >> costul[i];
    for (int i = 1; i <= W; i++)
        d[i] = limit;
    long long raspuns = limit;
    for (int j = 1; j <= G; j++){
        for (int i = w - 1; i >= 0; i--){
            if (d[i] != limit && i + energie[j] >= W)
                raspuns = min(raspuns, d[i] + costul[i]);
            else if (d[i] != limit)
                d[i + e[j]] = min(d[i + e[j]], d[i] + c[j]);
        }
    }
    if (raspuns != limit)
        cout << raspuns;
    else
        cout << -1;
    return 0;
}