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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (int i = a; i < (b); i++)
#define fj(a, b) for (int j = a; j < (b); j++)
#define fk(a, b) for (int k = a; k < (b); k++)
#define lb lower_bound
#define ub upper_bound
// hash base 177013

void rmax(ll &a, ll b) { a = max(a, b); }

ll dp[3002][3002][4];

int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    int n, m, q;
    cin >> n >> m >> q;
    ll mat[n][m];
    memset(mat, 0, sizeof(mat));
    fi(0, q) {
      ll x, y, c;
      cin >> x >> y >> c;
      --x, --y;
      mat[x][y] = c;
    }

    // fi(0,4)
    {
      dp[0][0][0] = 0;
      dp[0][0][1] = mat[0][0];
      dp[0][0][2] = 0;
      dp[0][0][3] = 0;
    }
    fi(0, n) {
      fj(0, m) {
        fk(0, 4) {
          ll v = dp[i][j][k];
          if (i + 1 < n) {
            rmax(dp[i + 1][j][0], v);
            rmax(dp[i + 1][j][1], v + mat[i + 1][j]);
          }
          if (j + 1 < m) {
            rmax(dp[i][j + 1][k], v);
            if (k + 1 <= 3)
              rmax(dp[i][j + 1][k + 1], v + mat[i][j + 1]);
          }
        }
      }
    }
    ll ans = 0;
    fi(0, 4) ans = max(ans, dp[n - 1][m - 1][i]);

    cout << ans;
  }
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (int i = a; i < (b); i++)
#define fj(a, b) for (int j = a; j < (b); j++)
#define fk(a, b) for (int k = a; k < (b); k++)
#define lb lower_bound
#define ub upper_bound
// hash base 177013

void rmax(ll &a, ll b) { a = max(a, b); }

int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    int n, m, q;
    cin >> n >> m >> q;
    ll mat[n + 2][m + 2];
    memset(mat, 0, sizeof(mat));
    fi(0, q) {
      int x, y, c;
      cin >> x >> y >> c;
      --x, --y;
      mat[x][y] = c;
    }
    ll dp[n][m][4];
    memset(dp, -0x3f, sizeof(dp));
    // fi(0,4)
    {
      dp[0][0][0] = 0;
      dp[0][0][1] = mat[0][0];
      dp[0][0][2] = 0;
      dp[0][0][3] = 0;
    }
    fi(0, n) {
      fj(0, m) {
        fk(0, 4) {
          ll v = dp[i][j][k];
          if (i + 1 < n) {
            rmax(dp[i + 1][j][0], v);
            rmax(dp[i + 1][j][1], v + mat[i + 1][j]);
          }
          if (j + 1 < m) {
            rmax(dp[i][j + 1][k], v);
            if (k + 1 <= 3)
              rmax(dp[i][j + 1][k + 1], v + mat[i][j + 1]);
          }
        }
      }
    }
    ll ans = 0;
    fk(0, 4) ans = max(ans, dp[n - 1][m - 1][k]);

    cout << ans;
  }
}