Cod sursa(job #3000221)

Utilizator matthriscuMatt . matthriscu Data 12 martie 2023 10:42:49
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>

using namespace std;

const int MAXN = 5002; // Maximum number of items
const int MAXG = 10002; // Maximum weight of the knapsack

int n, g, d[MAXG], w, p;

int main()
{
    ifstream fin("rucsac.in"); // Input stream
    ofstream fout("rucsac.out"); // Output stream

    fin >> n >> g; // Read number of items and maximum weight of the knapsack

    for (int i = 1; i <= n; i++)
    {
        fin >> w >> p; // Read weight and profit of the current item
        for (int j = g; j >= w; j--) // Iterate over the possible knapsack weights
        {
            d[j] = max(d[j], d[j - w] + p); // Update maximum profit
        }
    }

    fout << d[g]; // Output maximum profit for knapsack of weight g
    return 0;
}