Cod sursa(job #3318116)

Utilizator MihneaStoicaMihnea Teodor Stoica MihneaStoica Data 27 octombrie 2025 09:48:12
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;

/**
 * Problem: Problema rucsacului
 * URL: https://www.infoarena.ro/problema/rucsac
 * TL: 200 ms
 * ML: 12 MB
 *
 * Good Luck!
 */
using ll = long long;
// #define int ll
void tc() {
    int n, g; cin >> n >> g;
    vector<int> w(n + 1), p(n + 1);
    for (int i = 1; i <= n; i ++) {
        cin >> w[i] >> p[i];
    }

    vector<int> dp(g + 1);
    for (int i = 1; i <= n; i ++) {
        for (int j = g - w[i]; j >= 0; j --) {
            dp[j + w[i]] = max(dp[j + w[i]], dp[j] + p[i]);
        }
    }

    cout << dp[g] << '\n';
}

#define MULTITEST 0
#define FILEIO 1
#define FILE "rucsac"

signed main() {
#if FILEIO && !defined(LOCAL)
    (void)!freopen(FILE ".in", "r", stdin); (void)!freopen(FILE ".out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#if MULTITEST
    int T; cin >> T; while (T --) tc();
#else
    tc();
#endif
    return 0;
}