Cod sursa(job #2801161)

Utilizator Ion.AAlexandru Ion Ion.A Data 15 noiembrie 2021 11:14:49
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>

using namespace std;

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

const int NMAX = 1e5 + 1;
int n, g;
int d[NMAX];

int main()
{
    in >> n >> g;
    for (int i = 1; i <= g; i++)
    {
        d[i] = -1;
    }
    for (int i = 1; i <= n; i++)
    {
        int cost, profit;
        in >> cost >> profit;
        for (int j = g - cost; j >= 0; j--)
        {
            if (d[j] != -1 && (d[j + cost] == -1 || d[j + cost] < d[j] + profit))
            {
                d[j + cost] = d[j] + profit;
            }
        }
    }
    int maxi = -10;
    for (int i = 1; i <= g; i++)
    {
        maxi = max(maxi, d[i]);
    }
    out << maxi;
    return 0;
}