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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\\n"
#define pll pair<ll, ll>
#define f first
#define s second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define int ll
#define sz(x) (ll) x.size()
#define all(x) (x.begin(), x.end())
using namespace std;

const ll INF = 1e12;
const ll N = (3000 + 5); // TODO : change value as per problem
const ll MOD = 1e9 + 7;

int a[N][N];
int dp[N][N][4];
void solve() {
  int n, m, K;
  cin >> n >> m >> K;
  for (int ii = 1; ii <= K; ii++) {
    int i, j, v;
    cin >> i >> j >> v;
    a[i][j] = v;
  }
  for (int i = 1; i <= n; i++) {

    dp[i][1][0] = max(dp[i - 1][1][0], dp[i - 1][1][1]);
    if (a[i][1] != 0)
      dp[i][1][1] = a[i][1] + max(dp[i - 1][1][0], dp[i - 1][1][1]);
  }
  for (int i = 2; i <= m; i++) {
    for (int j = 1; j <= min(3ll, i); j++) {
      if (a[1][i] != 0 && (j == 1 || dp[1][i - 1][j - 1] != 0))
        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]);
    }
  }
  // cout << dp[1][2][2] << endl;
  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= m; j++) {
      for (int k = 1; k <= min(3ll, j); k++) {
        if (a[i][j] != 0 && (k == 1 || dp[i][j - 1][k - 1] != 0))
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        int ma = 0;
        for (int kk = 0; kk <= 3; kk++)
          ma = max(ma, dp[i - 1][j][kk]);
        if (a[i][j] != 0 && k == 1)
          dp[i][j][k] = max(dp[i][j][k], ma + a[i][j]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
      int ma = 0;
      for (int l = 0; l <= 3; l++)
        ma = max(ma, dp[i - 1][j][l]);
      dp[i][j][0] = max(dp[i][j][0], max(dp[i][j - 1][0], ma));
    }
  }
  // cout << dp[2][1][1] << endl;
  // cout << dp[2][2][2] << endl;
  int ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[n][m][i]);
  }
  cout << ans << endl;
}
signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  // freopen(".in","r",stdin);freopen(".out","w",stdout);

  ll tt = 1;
  // cin >> tt;
  while (tt--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\\n"
#define pll pair<ll, ll>
#define f first
#define s second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define int ll
#define sz(x) (ll) x.size()
#define all(x) (x.begin(), x.end())
using namespace std;

const ll INF = 1e12;
const ll N = (300 + 5); // TODO : change value as per problem
const ll MOD = 1e9 + 7;

int a[N][N];
int dp[N][N][4];
void solve() {
  int n, m, K;
  cin >> n >> m >> K;
  for (int ii = 1; ii <= K; ii++) {
    int i, j, v;
    cin >> i >> j >> v;
    a[i][j] = v;
  }
  for (int i = 1; i <= n; i++) {
    dp[i][1][1] = a[i][1];
  }

  for (int i = 2; i <= m; i++) {
    for (int j = 1; j <= min(i, 3ll); j++) {
      if (a[1][i] != 0)
        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]);
    }
  }
  // cout << dp[1][2][2] << endl;
  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= m; j++) {
      for (int k = 1; k <= min(3ll, j); k++) {
        if (a[i][j] != 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        int ma = 0;
        for (int kk = 0; kk <= 3; kk++)
          ma = max(ma, dp[i - 1][j][kk]);
        if (a[i][j] != 0 && k == 1)
          dp[i][j][k] = max(dp[i][j][k], ma + a[i][j]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
      int ma = 0;
      for (int l = 0; l <= 3; l++)
        ma = max(ma, dp[i - 1][j][l]);
      dp[i][j][0] = max(dp[i][j][0], max(dp[i][j - 1][0], ma));
    }
  }
  // cout << dp[2][1][1] << endl;
  // cout << dp[2][2][2] << endl;
  int ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[n][m][i]);
  }
  cout << ans << endl;
}
signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  // freopen(".in","r",stdin);freopen(".out","w",stdout);

  ll tt = 1;
  // cin >> tt;
  while (tt--) {
    solve();
  }
}