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

#include <bits/stdc++.h>
using namespace std;
#define FO(x)                                                                  \\
  {                                                                            \\
    freopen(#x ".in", "r", stdin);                                             \\
    freopen(#x ".out", "w", stdout);                                           \\
  }
#define pa pair<int, int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = 0x7fffffff;
const int N = 5e3 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
} /*
 struct Edge{
         int nxt,v;
 //	int w;
 //	int id;
 }e[N<<1];
 void add(int u,int v){
         e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
         e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
 }*/
ll f[N][N][4];
int n, m, k;
ll c[N][N];
int main() {
#ifdef Devil_Gary
  freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
  n = read(), m = read(), k = read();
  for (int i = 1; i <= k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    c[x][y] = z;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k != 0)
          f[i][j][k] = c[i][j] + max(f[i][j - 1][k - 1],
                                     max(max(f[i - 1][j][0], f[i - 1][j][1]),
                                         max(f[i - 1][j][2], f[i - 1][j][3])));
        f[i][j][k] =
            max(f[i][j][k],
                max(f[i][j - 1][k], max(max(f[i - 1][j][0], f[i - 1][j][1]),
                                        max(f[i - 1][j][2], f[i - 1][j][3]))));
      }
    }
  }
  cout << max(max(f[n][m][0], f[n][m][1]), max(f[n][m][2], f[n][m][3]));
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FO(x)                                                                  \\
  {                                                                            \\
    freopen(#x ".in", "r", stdin);                                             \\
    freopen(#x ".out", "w", stdout);                                           \\
  }
#define pa pair<int, int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = 0x7fffffff;
const int N = 5e3 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
} /*
 struct Edge{
         int nxt,v;
 //	int w;
 //	int id;
 }e[N<<1];
 void add(int u,int v){
         e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
         e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
 }*/
ll f[N][N][3];
int n, m, k;
ll c[N][N];
int main() {
#ifdef Devil_Gary
  freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
  n = read(), m = read(), k = read();
  for (int i = 1; i <= k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    c[x][y] = z;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k != 0)
          f[i][j][k] = c[i][j] + max(f[i][j - 1][k - 1],
                                     max(max(f[i - 1][j][0], f[i - 1][j][1]),
                                         max(f[i - 1][j][2], f[i - 1][j][3])));
        f[i][j][k] =
            max(f[i][j][k],
                max(f[i][j - 1][k], max(max(f[i - 1][j][0], f[i - 1][j][1]),
                                        max(f[i - 1][j][2], f[i - 1][j][3]))));
      }
    }
  }
  cout << max(max(f[n][m][0], f[n][m][1]), max(f[n][m][2], f[n][m][3]));
  return 0;
}