Cod sursa(job #2121326)

Utilizator preda.andreiPreda Andrei preda.andrei Data 3 februarie 2018 16:09:03
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <vector>

using namespace std;

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

  int objects, capacity;
  fin >> objects >> capacity;

  vector<int> profit(capacity + 1, 0);
  int res = 0;

  for (int i = 0; i < objects; ++i) {
    int weight, value;
    fin >> weight >> value;

    for (int j = capacity - weight; j >= 0; --j) {
      if (profit[j] > 0 || j == 0) {
        auto new_profit = profit[j] + value;
        auto new_weight = j + weight;
        profit[new_weight] = max(profit[new_weight], new_profit);
        res = max(res, new_profit);
      }
    }
  }

  fout << res << "\n";
  return 0;
}