Cod sursa(job #3355416)

Utilizator RORO123bBarbulescu Robert RORO123b Data 22 mai 2026 19:36:36
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

int main() {
    int n, G;
    fin >> n >> G;
    vector<int> g(n + 1);
    vector<int> p(n + 1);
    vector<int> dp(G + 1, -1);
    int maxx = -1;

    for (int i = 1; i <= n; i++) {
        fin >> g[i] >> p[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = G - g[i]; j >= 0; j--) {
            dp[j + g[i]] = max(dp[j + g[i]] + p[i], dp[j]);
            maxx = max(dp[j + g[i]], maxx);
        }
    }
    fout << maxx << '\n';
    return 0;
}