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

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

int main() {
  FAST /**/

      ll n,
      m, k;
  cin >> n >> m >> k;

  ll ***arr = new ll **[n];
  rep(n) arr[i] = new ll *[m];
  rep(n) repi(j, m) arr[i][j] = new ll[4];
  // memset(arr,0,sizeof(arr));
  rep(k) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--, c--;
    arr[r][c][0] = v;
  }

  for (ll i = n - 1; i > -1; i--) {
    for (ll j = m - 1; j > -1; j--) {
      ll val = arr[i][j][0];
      arr[i][j][0] = 0;
      if (i + j == (n + m - 2)) {
        arr[i][j][1] = val;
        continue;
      }
      if (i + 1 < n) {
        ll maxi = max({arr[i + 1][j][0], arr[i + 1][j][1], arr[i + 1][j][2],
                       arr[i + 1][j][3]});
        arr[i][j][0] = maxi;
        arr[i][j][1] = maxi + val;
      }

      for (ll st = 0; st < 4; st++) {
        if (j + 1 < m) {
          arr[i][j][st] = max(arr[i][j][st], arr[i][j + 1][st]);
          if (st > 0)
            arr[i][j][st] = max(arr[i][j][st], arr[i][j + 1][st - 1] + val);
        }
      }
    }
  }

  ll ans = max({arr[0][0][0], arr[0][0][1], arr[0][0][2], arr[0][0][3]});
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

int main() {
  FAST /**/

      ll n,
      m, k;
  cin >> n >> m >> k;

  ll arr[n][m];
  memset(arr, 0, sizeof(arr));
  rep(k) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--, c--;
    arr[r][c] = v;
  }

  ll dp[n][m][4];
  memset(dp, 0, sizeof(dp));
  for (ll i = n - 1; i > -1; i--) {
    for (ll j = m - 1; j > -1; j--) {
      if (i + j == (n + m - 2)) {
        dp[i][j][1] = arr[i][j];
        continue;
      }
      if (i + 1 < n) {
        ll maxi = max({dp[i + 1][j][0], dp[i + 1][j][1], dp[i + 1][j][2],
                       dp[i + 1][j][3]});
        dp[i][j][0] = maxi;
        dp[i][j][1] = maxi + arr[i][j];
      }

      for (ll st = 0; st < 4; st++) {
        if (j + 1 < m) {
          dp[i][j][st] = max(dp[i][j][st], dp[i][j + 1][st]);
          if (st > 0)
            dp[i][j][st] = max(dp[i][j][st], dp[i][j + 1][st - 1] + arr[i][j]);
        }
      }
    }
  }

  ll ans = max({dp[0][0][0], dp[0][0][1], dp[0][0][2], dp[0][0][3]});
  cout << ans;
  return 0;
}