Cod sursa(job #2932995)

Utilizator Torna3oVirtopeanu Andrei Torna3o Data 4 noiembrie 2022 14:19:17
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int prof[10001];
int main()
{
    int n, G, i, j, g, p, rez=0;
    in >> n >> G;
    for(i = 1; i <= G; i++)
        prof[i] = -999999999;
    for(i = 1; i <= n; i++)
    {
        in >> g >> p;
        for(j = G - g; j >= 0; j--)
        {
            if(prof[j] + p > prof[j + g])
            {
                prof[j + g] = prof[j] + p;
            }
        }
    }
    for(i = G; i >= 1; i--)
        rez = max(rez, prof[i]);
    out << rez;
    return 0;
}