Cod sursa(job #2331561)

Utilizator vladsftVlad Safta vladsft Data 29 ianuarie 2019 18:17:49
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>

using namespace std;

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

int g[5001], p[5001], profit[10001];
int main()
{
    int n, k, mx = 0;
    in >> n >> k;
    for (int i = 0; i < n; i++)
        in >> g[i] >> p[i];
    for (int i = 1; i <= k; i++)
        profit[i] = -1;
    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 = 0; i <= k; i++)
    {
        if (mx < profit[i])
            mx = profit[i];
    }
    out << mx;
    return 0;
}