Cod sursa(job #2635708)

Utilizator sireanu_vladSireanu Vlad sireanu_vlad Data 15 iulie 2020 13:30:43
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int best(int i, int j, int* w, int* p)
{
    if(i == 0 || j == 0)
    {
        //cout << "i=" << i << endl;
        //cout << "j=" << j << endl;
        return 0;
    }
    else if(w[i-1] > j) return best(i - 1, j, w, p);
    return max(best(i - 1, j, w, p), best(i - 1, j - w[i-1], w, p) + p[i-1]);
}

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

    return 0;
}