Cod sursa(job #2941953)

Utilizator alexia._.fFlorete Alexia Maria alexia._.f Data 18 noiembrie 2022 16:17:37
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N_MAX = 5000;
const int P_MAX = 10000;

struct rucsac
{
    int greutate, profit;
};
rucsac v[N_MAX + 5];

int Profit[P_MAX + 5];

int main()
{
    int n, k;
    in >> n >> k;
    for(int i = 0; i < n; i++)
    {
        in >> v[i].greutate >> v[i].profit;
    }

    for(int j = 1; j <= k; j++)
    {
        Profit[j] = -1;
    }

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

    int profit_maxim = 0;
    for(int i = 1; i <= k; i++)
    {
        profit_maxim = max(profit_maxim, Profit[i]);
    }
    out << profit_maxim;

    in.close();
    out.close();
    return 0;
}