Cod sursa(job #3127999)

Utilizator chipsSabou Ioan Alexandru chips Data 8 mai 2023 09:22:56
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 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[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 = g; j >= 1; j--){
            if(j - w[i] >= 0){
                dp[j] = max(dp[j], p[i] + dp[j-w[i]]);
            }
        }
    }
    cout << dp[g] << endl;
    return 0;
}