Problem p01065 (C++)

Buggy submission (status: Runtime Error):
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  if (!v.empty()) {
    out << '[';
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
    out << "\b\b]";
  }
  return out;
}
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  const int B = 350;

  int N, x, y;
  cin >> N >> x >> y;
  vector<int> a(N), b(N), c(N), d(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  vector<vector<vector<int>>> dp(
      N + 1, vector<vector<int>>(305, vector<int>(B + 305, -1)));
  dp[0][x][y + B] = 0;

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < dp[i].size(); ++j) {
      for (int k = 0; k < dp[i][j].size(); ++k) {
        if (dp[i][j][k] < 0)
          continue;
        chmax(dp[i + 1][j][k], dp[i][j][k]);
        if (j >= a[i])
          chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]);
        if (k >= c[i])
          chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]);
      }
    }
  }

  int ma = 0;
  for (int j = 0; j < dp[N].size(); ++j) {
    for (int k = B; k < dp[N][j].size(); ++k) {
      chmax(ma, dp[N][j][k]);
    }
  }
  cout << ma << endl;
}

Fixed submission:
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  if (!v.empty()) {
    out << '[';
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
    out << "\b\b]";
  }
  return out;
}
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  const int B = 350;

  int N, x, y;
  cin >> N >> x >> y;
  vector<int> a(N), b(N), c(N), d(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  vector<vector<vector<int>>> dp(
      N + 1, vector<vector<int>>(305, vector<int>(B + 605, -1)));
  dp[0][x][y + B] = 0;

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < dp[i].size(); ++j) {
      for (int k = 0; k < dp[i][j].size(); ++k) {
        if (dp[i][j][k] < 0)
          continue;
        chmax(dp[i + 1][j][k], dp[i][j][k]);
        if (j >= a[i])
          chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]);
        if (k >= c[i])
          chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]);
      }
    }
  }

  int ma = 0;
  for (int j = 0; j < dp[N].size(); ++j) {
    for (int k = B; k < dp[N][j].size(); ++k) {
      chmax(ma, dp[N][j][k]);
    }
  }
  cout << ma << endl;
}