Cod sursa(job #2836488)

Utilizator RaresCelescuRares Celescu RaresCelescu Data 20 ianuarie 2022 15:25:34
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>

using namespace std;

const int K=10001;
const int N=5001;

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

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