Cod sursa(job #2989246)

Utilizator raresgherasaRares Gherasa raresgherasa Data 6 martie 2023 11:50:21
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("rucsac.in");
ofstream fout ("rucsac.out");

const int N_MAX = 1e4 + 5;

int dp[N_MAX];
int n, G;

int main(){
   ios_base::sync_with_stdio(false);

    fin >> n >> G;
    fill(dp, dp + G + 1, -1);
    dp[0] = 0;
    for (int i = 1; i <= n; i++){
        int g, p; fin >> g >> p;
        for (int j = G; j > 0; j--){
            if (j - g < 0){
                continue;
            }
            if (dp[j - g] != -1){
                if (dp[j] == -1){
                    dp[j] = dp[j - g] + p;
                }
                else{
                    dp[j] = max(dp[j], dp[j - g] + p);
                }
            }
        }
    }
    fout << dp[G];
}