Cod sursa(job #2294810)

Utilizator AxellbenCretu Alexandru Axellben Data 2 decembrie 2018 20:22:47
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <iostream>
#include <map>
using namespace std;

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

struct object {
  int w, p;
};

int knapsack(object objects[], int N, int G) {
  if (N < 0 || G == 0) {
    return 0;
  } else if (G < objects[N].w) {
    return knapsack(objects, N - 1, G);
  } else {
    int temp1 = knapsack(objects, N - 1, G);
    int temp2 = objects[N].p + knapsack(objects, N - 1, G - objects[N].w);
    return max(temp1, temp2);
  }
}

int main() {
  object objects[5000];
  int N, G;
  fin >> N >> G;
  for (int i = 0; i < N; i++) {
    fin >> objects[i].w >> objects[i].p;
  }
  fout << knapsack(objects, N - 1, G);
  return 0;
}