Cod sursa(job #3311477)

Utilizator hurjui12AlexandruHurjui Alexandru-Mihai hurjui12Alexandru Data 22 septembrie 2025 17:31:40
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;

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

const int nmax = 5000, gmax = 10000;
int w[nmax+1], p[nmax+1];
int dp[gmax+1];
// dp[i][j] (i este subinteles) = profitul maxim pe care il pot obtine din primele i elemente, stiind ca aceste elemente au greutatea CEL MULT j

void solve() {
    int n, g, i, j;
    fin >> n >> g;
    for (i = 1; i<=n; i++)
        fin >> w[i] >> p[i];

    // stare initiala: dp[0][j] = 0 pentru toate 0 <= j <= g
    for (i = 1; i<=n; i++)
        for (j = g; j>=w[i]; j--)
            dp[j] = max(dp[j], dp[j-w[i]] + p[i]);

    fout << dp[g];
}

int main() {
    solve();
    return 0;
}