Cod sursa(job #3280056)

Utilizator AndreiDragosDavidDragos Andrei David AndreiDragosDavid Data 25 februarie 2025 11:49:52
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

int n, g;
vector<int> dp;
vector<pair<int, int>> a;

int main() {
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);
    cin.tie(0) -> sync_with_stdio(0);
    cin >> n >> g;
    dp.resize(g+1, 0);
    a.resize(n);

    for(auto& it : a)
        cin >> it.first >> it.second;

    for(int i=0; i<n; ++i){
        for(int j=g; j>=a[i].first; --j){
            dp[j] = max(a[i].second + dp[j-a[i].first], dp[j]);
        }
    }

    cout << dp[g];

    return 0;
}