Cod sursa(job #3357926)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 21:53:35
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

struct elem {
    int weight, profit;
};

const int DIM = 50001;
elem v[DIM];
int curr[DIM], last[DIM];

int main() {
    int n, G;
    fin >> n >> G;

    for (int i = 1; i <= n; i++)
        fin >> v[i].weight >> v[i].profit;

    for (int i = 0; i <= G; i++)
        last[i] = 0;

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= G; j++) {
            curr[j] = last[j];

            if (v[i].weight <= j)
                curr[j] = max(curr[j], last[j - v[i].weight] + v[i].profit);
        }

        for (int j = 0; j <= G; j++)
            last[j] = curr[j];
    }

    fout << last[G];

    return 0;
}