Cod sursa(job #3127998)

Utilizator chipsSabou Ioan Alexandru chips Data 8 mai 2023 09:13:17
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>

using namespace std;

void setIO(){
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);
}

#define nmax 5050
#define gmax 10010

int dp[nmax][gmax];
int w[nmax];
int p[nmax];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    setIO();
    int n,g,i,j;
    cin >> n >> g;
    for(i = 1; i <= n; i++){
        cin >> w[i] >> p[i];
    }

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