Cod sursa(job #2759375)

Utilizator Mihai180315Mihai Smarandache Mihai180315 Data 17 iunie 2021 12:09:46
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;
const int GMAX = 1001;
const int INF = 50000000;

int d[GMAX + 5];

int main()
{
    ifstream fin("energii.in");
    ofstream fout("energii.out");

    int g, w;
    int e, c, ans = INF;
    fin >> g >> w;
    d[0] = 0;
    for (int i = 1; i <= w; ++i) {
        d[i] = INF + 5;
    }
    for (int nr = 1; nr <= g; ++nr) {
        fin >> e >> c;
        for (int i = w - 1; i >= 0; --i) {
            if (d[i] != INF + 5) {
                if (i + e >= w) {
                    if (d[i] + c < ans) {
                        ans = d[i] + c;
                    }
                } else {
                    if (d[i + e] > d[i] + c) {
                        d[i + e] = d[i] + c;
                    }
                }
            }
        }
    }
    fout << ans;
    return 0;
}