Cod sursa(job #2970601)

Utilizator Mihai-VijulieVijulie Mihai Mihai-Vijulie Data 25 ianuarie 2023 16:33:22
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>

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

int main()
{
    int p[5001], g[5001], profit[10001], n, k;
    fin >> n >> k;
    for(int i = 1; i <= n; i++){
        fin >> g[i] >> p[i];
    }

    for(int j = 1; j<= k; j++){
        profit[j] = -1;
    }
    profit[0] = 0;
    for(int i = 0; i <= n;i++){
        ///actualizez vectorul profit folosind obiectul i
        for(int j = k-g[i]; j >= 0; j--){
            if(profit[j] != -1 && profit[j] + p[i] > j + g[i]){
                profit[j + g[i]] = profit[j] + p[i];
            }
        }
    }
    fout<<profit[k];
    return 0;
}