Cod sursa(job #2720715)

Utilizator LuBoNILuput Bogdan LuBoNI Data 11 martie 2021 10:48:50
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n, g, w[5001], p[5001], d[5001][10001], Pmax;
int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");
    fin >> n >> g;
    for (int i = 1; i <= n; i++)
        fin >> w[i] >> p[i];
    for (int i = 1; i <= n; i++)
        for (int cw = 0; cw <= g; cw++)
        {
            if (w[i] <= cw)
            d[i][cw] = max(d[i-1][cw], d[i-1][cw-w[i]] + p[i]);
        }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= g; j++)
            cout << d[i][j] << " ";
        cout << endl;
    }
    cout << endl
         << d[n][g];
    Pmax = d[n][g];
    fout << Pmax;
    return 0;
}
/*
6 10
3 7
3 4
1 2
1 9
2 4
1 5
*/