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

#include <bits/stdc++.h>
using namespace std;

#define rep(ver, n) rep2(ver, 0, n)
#define rep2(ver, m, n) for (int ver = m; ver < (n); ver++)
#define loop while (true)
#define dup(x, y) (((x) + (y)-1) / (y))
#define all(v) (v).begin(), (v).end()
#define debug(x) std::cerr << #x << ": " << x << "\\n"
#define debug2(x, y)                                                           \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\\n"
#define debug3(x, y, z)                                                        \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z        \\
            << ": " << z << "\\n"

typedef long long ll;
typedef pair<int, int> P;

ll dp[3010][3010][4] = {};

int main() {
  int r, c, k, v[3010][3010] = {};
  cin >> r >> c >> k;
  rep(i, k) {
    int r1, c1;
    cin >> r1 >> c1;
    cin >> v[r1 - 1][c1 - 1];
  }

  dp[0][0][1] = v[0][0];

  rep(i, r) {
    rep(j, c) {
      int val = v[i][j];
      int c0 = j, r0 = i, c1 = c0 - 1, r1 = r0 - 1;
      dp[r0][c0][1] = val;
      if (c1 >= 0) {
        rep(k, 4) { dp[r0][c0][k] = max(dp[r0][c0][k], dp[r0][c1][k]); }
        rep(k, 3) {
          dp[r0][c0][k + 1] = max(dp[r0][c0][k + 1], dp[r0][c1][k] + val);
        }
      }
      if (r1 >= 0) {
        rep(k, 4) {
          dp[r0][c0][0] = max(dp[r0][c0][0], dp[r1][c0][k]);
          dp[r0][c0][1] = max(dp[r0][c0][1], dp[r1][c0][k] + val);
        }
      }
    }
  }

  ll ans = 0;
  rep(i, 4) { ans = max(ans, dp[r - 1][c - 1][i]); }

  cout << ans;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(ver, n) rep2(ver, 0, n)
#define rep2(ver, m, n) for (int ver = m; ver < (n); ver++)
#define loop while (true)
#define dup(x, y) (((x) + (y)-1) / (y))
#define all(v) (v).begin(), (v).end()
#define debug(x) std::cerr << #x << ": " << x << "\\n"
#define debug2(x, y)                                                           \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\\n"
#define debug3(x, y, z)                                                        \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z        \\
            << ": " << z << "\\n"

typedef long long ll;
typedef pair<int, int> P;

int main() {
  int r, c, k, v[3010][3010] = {};
  cin >> r >> c >> k;
  rep(i, k) {
    int r1, c1;
    cin >> r1 >> c1;
    cin >> v[r1 - 1][c1 - 1];
  }

  ll dp[3010][3010][4] = {};

  dp[0][0][1] = v[0][0];

  rep(i, r) {
    rep(j, c) {
      int val = v[i][j];
      int c0 = j, r0 = i, c1 = c0 - 1, r1 = r0 - 1;
      dp[r0][c0][1] = v[r0][c0];
      if (c1 >= 0) {
        rep(k, 4) { dp[r0][c0][k] = max(dp[r0][c0][k], dp[r0][c1][k]); }
        rep(k, 3) {
          dp[r0][c0][k + 1] = max(dp[r0][c0][k + 1], dp[r0][c1][k] + val);
        }
      }
      if (r1 >= 0) {
        rep(k, 4) {
          dp[r0][c0][0] = max(dp[r0][c0][0], dp[r1][c0][k]);
          dp[r0][c0][1] = max(dp[r0][c0][1], dp[r1][c0][k] + val);
        }
      }
    }
  }

  ll ans = dp[r - 1][c - 1][0];
  rep(i, 4) { ans = max(ans, dp[r - 1][c - 1][i]); }

  cout << ans;

  return 0;
}