Cod sursa(job #2924110)

Utilizator MAlex2019Melintioi George Alexandru MAlex2019 Data 25 septembrie 2022 16:08:55
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>

#define MAXG 10000

using namespace std;

int solutii[MAXG + 1];

int main()
{
    ios_base::sync_with_stdio(false);
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");
    int n, g, i, j, w, p, nou;

    fin >> n >> g;
    for (i = 0; i < n; i++) {
        fin >> w >> p;
        for (j = g - 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";*/
    }

    nou = 0;
    for (i = 1; i <= g; i++)
        nou = max(nou, solutii[i]);
    fout << nou;

    return 0;
}