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 rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

using vec = vector<ll>;
using vvec = vector<vec>;
using vvvec = vector<vvec>;

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  vvec c(H, vec(W, 0));
  rep(i, N) {
    int x, y;
    ll v;
    cin >> x >> y >> v;
    x--, y--;
    c[x][y] = v;
  }
  vvvec dp(H, vvec(W, vec(4, 0)));
  int dx[] = {1, 0}, dy[] = {0, 1};
  rep(i, H) {
    rep(j, W) {
      rep(k, 4) {
        rep(m, 2) {
          int ni = i + dx[m], nj = j + dy[m];
          if (ni >= H || nj >= W)
            continue;
          rep(n, 2) {
            if (k + n > 3)
              continue;
            int nk;
            if (m == 0)
              nk = 0;
            else
              nk = k + n;
            ll add = c[i][j] * n;
            chmax(dp[ni][nj][nk], dp[i][j][k] + add);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) { chmax(ans, dp[H - 1][W - 1][k] + (k < 3 ? c[H - 1][W - 1] : 0)); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  ll c[H][W];
  fill(c[0], c[H], 0);
  rep(i, N) {
    int x, y;
    ll v;
    cin >> x >> y >> v;
    x--, y--;
    c[x][y] = v;
  }
  ll dp[H][W][4];
  rep(i, H) rep(j, W) rep(k, 4) dp[i][j][k] = -INF;
  dp[0][0][0] = 0;
  int dx[] = {1, 0}, dy[] = {0, 1};
  rep(i, H) {
    rep(j, W) {
      rep(k, 4) {
        rep(m, 2) {
          int ni = i + dx[m], nj = j + dy[m];
          if (ni >= H || nj >= W)
            continue;
          rep(n, 2) {
            if (k + n > 3)
              continue;
            int nk;
            if (m == 0)
              nk = 0;
            else
              nk = k + n;
            int add = n ? c[i][j] : 0;
            chmax(dp[ni][nj][nk], dp[i][j][k] + add);
          }
        }
      }
    }
  }
  ll ans = -INF;
  rep(k, 3) { chmax(ans, dp[H - 1][W - 1][k] + (k < 3 ? c[H - 1][W - 1] : 0)); }
  cout << ans << endl;
}