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

#include <bits/extc++.h>

#ifndef DUMP
#define DUMP(...) (void)0
#endif

using namespace std;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector v(h, vector<int>(w));
  while (k--) {
    int i, j;
    cin >> i >> j;
    --i, --j;
    cin >> v[i][j];
  }
  vector dp(4, vector(h, vector<int64_t>(w, -1e18)));
  dp[0][0][0] = 0;
  dp[1][0][0] = v[0][0];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int x = 0; x < 4; ++x) {
        if (i + 1 < h) {
          dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[x][i][j]);
          dp[1][i + 1][j] = max(dp[1][i + 1][j], dp[x][i][j] + v[i + 1][j]);
        }
        if (j + 1 < w) {
          dp[x][i][j + 1] = max(dp[x][i][j + 1], dp[x][i][j]);
          if (x < 3) {
            dp[x + 1][i][j + 1] =
                max(dp[x + 1][i][j + 1], dp[x][i][j] + v[i][j + 1]);
          }
        }
      }
    }
  }
  auto res = numeric_limits<int64_t>::min();
  for (int x = 0; x < 4; ++x)
    res = max(res, dp[x].back().back());
  cout << res << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll r, c, ko;
  cin >> r >> c >> ko;

  vector<vector<ll>> p(r, vector<ll>(c, -1));
  rep(i, ko) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    p[x][y] = z;
  }

  ll ans[r][c][4];
  rep(i, r) {
    rep(j, c) {
      rep(k, 4) { ans[i][j][k] = -1; }
    }
  }
  if (p[0][0] != 0)
    ans[0][0][1] = p[0][0];
  ans[0][0][0] = 0ll;

  rep(i, r) {
    rep(j, c) {
      rep(k, 4) {
        if (ans[i][j][k] == -1)
          continue;
        if (i + 1 < r) {
          if (p[i + 1][j] != -1) {
            chmax(ans[i + 1][j][1], ans[i][j][k] + p[i + 1][j]);
          }
          chmax(ans[i + 1][j][0], ans[i][j][k]);
        }
        if (j + 1 < c) {
          if (p[i][j + 1] != -1 && k < 3) {
            chmax(ans[i][j + 1][k + 1], ans[i][j][k] + p[i][j + 1]);
          }
          chmax(ans[i][j + 1][k], ans[i][j][k]);
        }
      }
    }
  }

  /*
    rep(i,r){
      rep(j,c){
        ll z=0;
        rep(k,4){
          chmax(z,ans[i][j][k]);
        }
        cout << z << " ";
      }
      cout << endl;
    }
  */

  ll kot = 0;
  rep(i, 4) {
    if (ans[r - 1][c - 1][i] == -1)
      continue;
    chmax(kot, ans[r - 1][c - 1][i]);
  }
  cout << kot << endl;
}