Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

// ll dp[3010][3010][5];
vector<VVL> dp(3010, VVL(3010, VL(5, 0)));
ll tbl[3010][3010];

void init() {
  rep(i, 0, 3010) rep(j, 0, 3010) {
    // rep(k, 0, 5) dp[i][j][k] = 0;
    tbl[i][j] = 0;
  }
}

int main() {

  init();

  // input
  ll R, C, K;
  cin >> R >> C >> K;
  VL r(K), c(K), v(K);
  rep(i, 0, K) {
    cin >> r[i] >> c[i] >> v[i];
    tbl[r[i] - 1][c[i] - 1] = v[i];
  }

  rep(i, 0, R + 1) {
    rep(j, 0, C + 1) {
      // (i, j) -> (i+1, j)
      if (i < R) {
        // i, j で取らない
        rep(k, 0, 4) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        // i, j で取る
        rep(k, 0, 3) dp[i + 1][j][0] =
            max(dp[i + 1][j][0], dp[i][j][k] + tbl[i][j]);
      }
      // (i, j) -> (i, j+1)
      if (j < C) {
        // i, jで取らない
        rep(k, 0, 4) dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // i, jで取る
        rep(k, 0, 3) dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + tbl[i][j]);
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[R][C][i], ans);
  cout << ans << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

// ll dp[3010][3010][5];
vector<VVL> dp(5, VVL(3010, VL(3010, 0)));
ll tbl[3010][3010];

void init() {
  rep(i, 0, 3010) rep(j, 0, 3010) {
    // rep(k, 0, 5) dp[i][j][k] = 0;
    tbl[i][j] = 0;
  }
}

int main() {

  init();

  // input
  ll R, C, K;
  cin >> R >> C >> K;
  VL r(K), c(K), v(K);
  rep(i, 0, K) {
    cin >> r[i] >> c[i] >> v[i];
    tbl[r[i] - 1][c[i] - 1] = v[i];
  }

  rep(i, 0, R + 1) {
    rep(j, 0, C + 1) {
      // (i, j) -> (i+1, j)
      if (i < R) {
        // i, j で取らない
        rep(k, 0, 4) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        // i, j で取る
        rep(k, 0, 3) dp[i + 1][j][0] =
            max(dp[i + 1][j][0], dp[i][j][k] + tbl[i][j]);
      }
      // (i, j) -> (i, j+1)
      if (j < C) {
        // i, jで取らない
        rep(k, 0, 4) dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // i, jで取る
        rep(k, 0, 3) dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + tbl[i][j]);
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[R][C][i], ans);
  cout << ans << endl;
  return 0;
}