Cod sursa(job #2615840)

Utilizator luci.tosaTosa Lucian luci.tosa Data 15 mai 2020 17:55:36
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <iostream>
#include <fstream>
#define INF 1000000000
using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");
int n, w, v[5005];
int main() {
    fin >> n >> w;
    for(int i = 1; i <= w; i++)
        v[i] = INF;
    for(int i = 1; i <= n; i++) {
        int x, y;
        fin >> x >> y;
        for(int j = w; j >= 0; j--) {
            int next = j + x;
            if(next > w)
                next = w;
            v[next] = min(v[j] + y, v[next]);
        }
    }
    if(v[w] != INF)
        fout << v[w];
    else
        fout << -1;
    return 0;
}