Cod sursa(job #3196392)

Utilizator rapidu36Victor Manz rapidu36 Data 23 ianuarie 2024 19:48:41
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>

using namespace std;

const int N = 5000;
const int K = 10000;

int g[N], p[N], profit[K+1];

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