Cod sursa(job #2970631)

Utilizator theodoraoTheodora Oprescu theodorao Data 25 ianuarie 2023 16:57:58
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    ifstream in("rucsac.in");
    ofstream out("rucsac.out");

    int n, k, g[10000], p[10000], profit[10000];
    in >> n >> k;

    for(int i = 0 ; i < n ; i++)
    {
        in >> g[i];
        in >> 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];
            }
        }
    }
    int maxim = 0;
    for(int j = 1; j <= k; j++)
    {
		if (profit[j] > maxim)
            maxim = profit[j];
	}
	out << maxim;
    return 0;
}