Cod sursa(job #2971195)

Utilizator cygAlexandruIvanIvan Alexandru cygAlexandruIvan Data 26 ianuarie 2023 19:59:53
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>

using namespace std;
const int N = 5000, G = 10000;

struct obiect{
    int w, p;
};

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

    int n, g, profit[G+1], pmax = -1;
    obiect x[N];

    in >> n >> g;
    for(int i = 0; i < n; i++)
        in >> x[i].w >> x[i].p;

    for(int j = 1; j <= g; j++)
        profit[j] = -1;
    profit[0] = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = g - x[i].w; j >= 0; j--)
        {
            if(profit[j] != -1 && profit[j] + x[i].p > profit[j + x[i].w])
                profit[j + x[i].w] = profit[j] + x[i].p;
        }
    }

    for(int i = 0; i < g; i++)
        pmax = max(profit[i], profit[i+1]);

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