Cod sursa(job #3241332)

Utilizator SilviuC25Silviu Chisalita SilviuC25 Data 29 august 2024 09:00:58
Problema Energii Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAX_NUM = 1e3;
const int MAX_W = 5e3;

int g, w, e[MAX_NUM], c[MAX_NUM];
vector<int> minCost(MAX_W, INT_MAX);

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    fin >> g >> w;
    for (int i = 0; i < g; ++i) {
        fin >> e[i] >> c[i];
    }
    minCost[0] = 0;
    for (int i = 0; i < g; ++i) {
        for (int j = w - 1; j >= 0; --j) {
            if (minCost[j] != INT_MAX) {
                int currentW = min(e[i] + j, w);
                minCost[currentW] = min(minCost[currentW], minCost[j] + c[i]);
            }
        }
    }
    if (minCost[w] != INT_MAX) {
        fout << minCost[w];
    } else {
        fout << -1;
    }
    return 0;
}