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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD; 木の連結成分の個数は頂点数ー辺の数

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int r, c, kk;
  cin >> r >> c >> kk;
  ll a[r][c];
  rep(i, r) rep(j, c) a[i][j] = 0;
  rep(i, kk) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    ll v;
    cin >> v;
    a[x][y] = v;
  }
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  if (a[0][0] > 0)
    dp[0][0][1] = a[0][0];

  rep(i, r) {
    rep(j, c) {
      rep(k, 4) {
        if (k == 0) {
          if (i > 0)
            chmax(dp[i][j][1], dp[i - 1][j][0] + a[i][j]);
          if (j > 0)
            chmax(dp[i][j][1], dp[i][j - 1][0] + a[i][j]);
        } else {
          if (i > 0)
            chmax(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
          if (j > 0 && k <= 2)
            chmax(dp[i][j][k + 1], dp[i][j - 1][k] + a[i][j]);
        }
        if (i > 0)
          chmax(dp[i][j][k], dp[i - 1][j][k]);
        if (j > 0)
          chmax(dp[i][j][k], dp[i][j - 1][k]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) chmax(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD; 木の連結成分の個数は頂点数ー辺の数

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int r, c, k;
  cin >> r >> c >> k;
  ll a[r][c];
  rep(i, r) rep(j, c) a[i][j] = 0;
  rep(i, k) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    ll v;
    cin >> v;
    a[x][y] = v;
  }
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(k, 0)));
  if (a[0][0] > 0)
    dp[0][0][1] = a[0][0];

  rep(i, r) {
    rep(j, c) {
      rep(k, 4) {
        if (k == 0) {
          if (i > 0)
            chmax(dp[i][j][1], dp[i - 1][j][0] + a[i][j]);
          if (j > 0)
            chmax(dp[i][j][1], dp[i][j - 1][0] + a[i][j]);
        } else {
          if (i > 0)
            chmax(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
          if (j > 0 && k <= 2)
            chmax(dp[i][j][k + 1], dp[i][j - 1][k] + a[i][j]);
        }
        if (i > 0)
          chmax(dp[i][j][k], dp[i - 1][j][k]);
        if (j > 0)
          chmax(dp[i][j][k], dp[i][j - 1][k]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) chmax(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}