Cod sursa(job #2636691)

Utilizator sireanu_vladSireanu Vlad sireanu_vlad Data 19 iulie 2020 11:13:17
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int w[5010], p[5010], best[5010][10010];


int main()
{
    int n, g;
    in >> n >> g;
    for(int i = 1; i <= n; i++)
    {
        in >> w[i] >> p[i];
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= g; j++)
        {
            if(j >= w[i])
                best[i][j] = max(best[i-1][j], best[i-1][j - w[i]] + p[i]);
            else
                best[i][j] = best[i-1][j];
        }
    }

    out << best[n][g];

    return 0;
}