Cod sursa(job #3165149)

Utilizator MAlex2019Melintioi George Alexandru MAlex2019 Data 5 noiembrie 2023 15:22:53
Problema Energii Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;

const int MAXG = 1e7;
int solutii[MAXG + 1];
ifstream fin("energii.in");
ofstream fout("energii.out");

int main()
{
    ios_base::sync_with_stdio(false);
    int n, g, i, j, w, p, nou;

    fin >> n >> g;
    for (i = 0; i < n; i++) {
        fin >> p >> w;
        for (j = w; j > 0; j--)
            if (solutii[j] > 0) {
                nou = solutii[j] + p;
                solutii[j + w] = max(nou, solutii[j + w]);
            }
        solutii[w] = max(p, solutii[w]);
        /*for (j = 1; j <= g; j++)
            fout << j << ' ' << solutii[j] << '\n';
        fout << "stop\n\n";*/
    }

    for (int i = 0; i <= 1e7; i++)
        if (solutii[i] >= g) {
            fout << i << '\n';
            return 0;
        }
    fout << "-1\n";

    return 0;
}