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

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define endl '\\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// dp[i][j][cnt] = 座標(i,j), i行目でcnt個拾うときの価値の最大値
ll dp[3010][3010][4] = {};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  V<V<int>> v(h + 2, V<int>(w + 2));
  rep(i, k) {
    int y, x;
    cin >> y >> x;
    int c;
    cin >> c;
    v[y][x] = c;
  }

  rep(i, h + 1) rep(j, w + 1) rep(l, 4) {
    { // ↑
      int ny = i + dy[0];
      int nx = j + dx[0];
      chmax(dp[ny][nx][0], dp[i][j][l]);
      chmax(dp[ny][nx][1], dp[i][j][l] + v[ny][nx]);
    }
    { // →
      int ny = i + dy[1];
      int nx = j + dx[1];
      chmax(dp[ny][nx][l], dp[i][j][l]);
      if (l < 3)
        chmax(dp[ny][nx][l + 1], dp[i][j][l] + v[ny][nx]);
    }
  }
  ll ans = 0;
  rep(l, 4) { chmax(ans, dp[h][w][l]); }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\\n" : "No\\n")
#define endl '\\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// dp[i][j][cnt] = 座標(i,j), i行目でcnt個拾うときの価値の最大値
ll dp[3010][3010][4] = {};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  V<V<int>> v(h + 1, V<int>(w + 1));
  rep(i, k) {
    int y, x;
    cin >> y >> x;
    int c;
    cin >> c;
    v[y][x] = c;
  }

  rep(i, h + 1) rep(j, w + 1) rep(l, 4) {
    { // ↑
      int ny = i + dy[0];
      int nx = j + dx[0];
      chmax(dp[ny][nx][0], dp[i][j][l]);
      chmax(dp[ny][nx][1], dp[i][j][l] + v[ny][nx]);
    }
    { // →
      int ny = i + dy[1];
      int nx = j + dx[1];
      chmax(dp[ny][nx][l], dp[i][j][l]);
      if (l < 3)
        chmax(dp[ny][nx][l + 1], dp[i][j][l] + v[ny][nx]);
    }
  }
  ll ans = 0;
  rep(l, 4) { chmax(ans, dp[h][w][l]); }
  cout << ans << endl;
}