Cod sursa(job #3352231)

Utilizator eric_dragosDragos Eric eric_dragos Data 25 aprilie 2026 12:09:09
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int Nmax = 1e4+5;
int n, g;
// int weights[Nmax], vals[Nmax], dp[Nmax][Nmax];
vector<int> weights, vals;
vector<vector<int>> dp;
void citire(){
    fin >> n >> g;
    weights.resize(n+1);
    vals.resize(n+1);
    dp.resize(n+1, vector<int>(g+1));
    for(int i = 1; i<=n; i++) {
        fin >> weights[i] >> vals[i];
    }
}
void solve(){
    for(int i = 1; i<=n; i++){
        for(int j = 1; j<=g; j++){
            if(weights[i] > j) dp[i][j] = dp[i-1][j];
            else{
                dp[i][j] = max(dp[i-1][j], vals[i] + dp[i-1][j-weights[i]]);
            }
        }
    }
}
int main(){
    citire();
    solve();
    fout << dp[n][g] << '\n';

    return 0;
}