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

/*
    Author - Param Kothari
    Ah shit, here we go again
*/

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mk make_pair

typedef long long ll;
typedef long double ld;

ll dp[3003][3003][4] = {}, a[3003][3003] = {};

void solve() {
  int r, c, q;
  cin >> r >> c >> q;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < q; i++) {
    ll ri, ci, vi;
    cin >> ri >> ci >> vi;
    a[ri][ci] = vi;
  }
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[1][1][0] = 0;
  dp[1][1][1] = a[1][1];
  for (int i = 2; i <= c; i++) {
    for (int j = 1; j <= min(i, 3); j++) {
      dp[1][i][j] = max(dp[1][i][j], dp[1][i - 1][j - 1] + a[1][i]);
      dp[1][i][j] = max(dp[1][i][j], dp[1][i - 1][j]);
    }
  }
  for (int i = 2; i <= r; i++) {
    for (int j = 1; j <= 3; j++) {
      dp[i][1][0] = max(dp[i][1][0], dp[i - 1][1][j]);
      dp[i][1][1] = max(dp[i][1][1], dp[i - 1][1][j] + a[i][1]);
    }
  }
  for (int i = 2; i <= r; i++) {
    for (int j = 2; j <= c; j++) {
      for (int k = 1; k <= min(j, 3); k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= 3; i++) {
    res = max(res, dp[r][c][i]);
  }
  cout << res << "\\n";
  return;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

/*
    Author - Param Kothari
    Ah shit, here we go again
*/

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mk make_pair

typedef long long ll;
typedef long double ld;

void solve() {
  int r, c, q;
  cin >> r >> c >> q;
  ll a[3003][3003];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < q; i++) {
    ll ri, ci, vi;
    cin >> ri >> ci >> vi;
    a[ri][ci] = vi;
  }
  ll dp[3003][3003][4];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[1][1][0] = 0;
  dp[1][1][1] = a[1][1];
  for (int i = 2; i <= c; i++) {
    for (int j = 1; j <= min(i, 3); j++) {
      dp[1][i][j] = max(dp[1][i][j], dp[1][i - 1][j - 1] + a[1][i]);
      dp[1][i][j] = max(dp[1][i][j], dp[1][i - 1][j]);
    }
  }
  for (int i = 2; i <= r; i++) {
    for (int j = 1; j <= 3; j++) {
      dp[i][1][0] = max(dp[i][1][0], dp[i - 1][1][j]);
      dp[i][1][1] = max(dp[i][1][1], dp[i - 1][1][j] + a[i][1]);
    }
  }
  for (int i = 2; i <= r; i++) {
    for (int j = 2; j <= c; j++) {
      for (int k = 1; k <= min(j, 3); k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= 3; i++) {
    res = max(res, dp[r][c][i]);
  }
  cout << res << "\\n";
  return;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}