Cod sursa(job #3345028)

Utilizator codeQTStreche Andrei-Claudiu codeQT Data 7 martie 2026 17:42:03
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main()
{
    int n, g, weight, profit;
    fin >> n >> g;
    vector<vector<int>> dp(2, vector<int>(g + 1, 0));

    for (int i = 1; i <= n; i++) {
        fin >> weight >> profit;
        for (int j = 1; j <= g; j++) {
            if (j >= weight) {
                dp[1][j] = max(dp[0][j - weight] + profit, dp[0][j]);
            } else {
                dp[1][j] = dp[0][j];
            }
        }
        for (int j = 0; j < g; j++) {
            dp[0][j] = dp[1][j];
        }
    }
    
    fout << dp[1][g];

    return 0;
}