Cod sursa(job #3320346)

Utilizator rapidu36Victor Manz rapidu36 Data 5 noiembrie 2025 10:27:40
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <vector>

using namespace std;

struct obiect
{
    int g, p;
};

int main()
{
    ifstream in("rucsac.in");
    ofstream out("rucsac.out");
    int n, k;
    in >> n >> k;
    vector <obiect> v(n);
    vector <int> profit(k + 1, -1);
    for (auto &ob: v)
    {
        in >> ob.g >> ob.p;
    }
    profit[0] = 0;
    for (auto ob: v)
    {
        for (int j = k; j >= ob.g; j--)
        {
            if (profit[j-ob.g] != -1)
            {
                profit[j] = max(profit[j], profit[j-ob.g] + ob.p);
            }
        }
    }
    int profit_max = -1;
    for (auto p: profit)
    {
        profit_max = max(profit_max, p);
    }
    out << profit_max << "\n";
    in.close();
    out.close();
    return 0;
}