Cod sursa(job #2970579)

Utilizator karina_antoniuAntoniu Karina karina_antoniu Data 25 ianuarie 2023 16:17:02
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
using namespace std;

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

const int NMAX = 10000;
const int N = 5000;
int p[N + 1], profit[NMAX + 1], g[N + 1];

int main()
{
    int n, k, maxim = 0;
    cin >> n >> k;
    for(int i = 0; i < n; i++)
        cin >> g[i] >> p[i];
    for(int j = 0; 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];
    }
    for(int i = 1; i <= k; i++)
        if(profit[i] > maxim)
            maxim = profit[i];
    cout << maxim << '\n';
    return 0;
}