Cod sursa(job #2036574)

Utilizator BrandonChris Luntraru Brandon Data 10 octombrie 2017 20:20:24
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>

using namespace std;

const int MaxWgt = 10005;
const int MaxN = 5005;

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

int rucsack[MaxWgt];

class ObjectType {
public:
  int wgt, profit;

  ObjectType(int _wgt = 0, int _profit = 0) {
    wgt = _wgt;
    profit = _profit;
  }
};

ObjectType objectList[MaxN];

int main() {
  int n, wgtCap;
  cin >> n >> wgtCap;
  for (int i = 1; i <= n; ++i) {
    cin >> objectList[i].wgt >> objectList[i].profit;
  }

  for (int currObject = 1; currObject <= n; ++currObject) {
    for (int currWgt = wgtCap; currWgt >= 0; --currWgt) {
      int newWgt = currWgt + objectList[currObject].wgt;
      if (newWgt > wgtCap)  {
        continue;
      }

      rucsack[newWgt] = max(rucsack[newWgt], rucsack[currWgt] + objectList[currObject].profit);
    }
  }

  cout << rucsack[wgtCap] << '\n';
  return 0;
}