Cod sursa(job #2760083)

Utilizator CaptnBananaPetcu Tudor CaptnBanana Data 22 iunie 2021 19:43:43
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>

using namespace std;

ifstream f("energii.in");
ofstream g("energii.out");

const int N = 1000, inf = N * N * 10 + 1;
int n, w, e[N], c[N], costMin[5 * N];

int main(){
    f >> n >> w;
    for(int i = 0; i < n; i++)
        f >> e[i] >> c[i];

    f.close();
    for(int j = 1; j <= w; j++)
        costMin[j] = inf;

    for(int i = 0; i < n; i++){
        for(int j = w - 1; j >= 0; j--){
            if(costMin[j] != inf){
                if(j + e[i] >= w)
                    costMin[w] = min(costMin[w], costMin[j] + c[i]);
                else
                    costMin[j + e[i]] = min(costMin[j + e[i]], costMin[j] + c[i]);
            }
        }
    }

    if(costMin[w] == inf)
        g << -1;
    else
        g << costMin[w];

    g.close();
}