Cod sursa(job #3364567)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 5 septembrie 2026 15:55:41
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;

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

const int nmax = 5000;
const int gmax = 10000;
int n, g, w[nmax + 5], p[nmax + 5], dp[3][gmax + 5];

// dp[0][w] = 0
// dp[1][w] = 
// dp[2][w] = 
// dp[3][w] = 

int main()
{
    fin >> n >> g;
    for (int i = 1; i <= n; i++)
    {
        fin >> w[i] >> p[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= g; j++)
        {
            dp[i % 2][j] = dp[(i - 1) % 2][j];
            if (w[i] <= j)
            {
                dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][j - w[i]] + p[i]);
            }
        }
    }
    fout << dp[n % 2][g];
    return 0;
}