Cod sursa(job #2941942)

Utilizator Razvan_GabrielRazvan Gabriel Razvan_Gabriel Data 18 noiembrie 2022 16:11:22
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>

using namespace std;

int p[5001], g[5001];
int profit[10001];

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

    int n, k;
    fin >> n >> k;
    for(int i = 0; i < n; i++)
        fin >> g[i] >> p[i];

    profit[0] = 0;
    for(int j = 1; j <= k; j++)
        profit[j] = -1;

    for(int i = 0; i < n; i++){
        for(int j = k - g[i]; j >= 0; j--){
            if(profit[j] != -1)
                profit[j + g[i]] = max(profit[j + g[i]], profit[j] + p[i]);
        }
    }

    int maxim = -1;
    for(int i = 1; i <= k; i++)
        maxim = max(maxim, profit[i]);

    fout << maxim;

    return 0;
}