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

#include <bits/stdc++.h>
using namespace std;
#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 ordered_set_pair                                                       \\
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,           \\
       tree_order_statistics_node_update>
#define ordered_set_mutiset                                                    \\
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \\
       tree_order_statistics_node_update>
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> pi;
#define PI 3.1415926535897932384
#define FOR(i, vv, n) for (ll i = vv; i < n; i++)
#define FORR(i, n, vv) for (ll i = n - 1; i >= vv; i--)
#define ve vector
#define maxind(v) (max_element(v.begin(), v.end()) - v.begin())
#define minind(v) (min_element(v.begin(), v.end()) - v.begin())
#define maxe(v) *max_element(v.begin(), v.end())
#define mine(v) *min_element(v.begin(), v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define M 1000000007ll
#define INF 1000000000000000000ll
#define PRECISE cout.precision(18);
#define all(v) v.begin(), v.end()
#define BS(v, n) binary_search(all(v), n)
#define srt(v) sort(v.begin(), v.end())
#define rsrt(v) sort(all(v), greater<ll>())
#define uni(v) v.resize(unique(all(v)) - v.begin())
#define F first
#define S second
#define GET(i, p) get<p>(i)

ll dp[3000][3000][4];

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  FAST
      // PRECISE

      ll n,
      m, k;
  cin >> n >> m >> k;
  ve<ve<ll>> v(n, ve<ll>(m));
  FOR(i, 0, k) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    v[x][y] = z;
  }

  FOR(i, 0, n) {
    FOR(j, 0, m) {
      FOR(k, 0, 4) {
        dp[i][j][k] = 0;
        if (k > 0)
          dp[i][j][k] = v[i][j];
        if (i - 1 >= 0) {
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3] + v[i][j]);
          else
            dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
        }
      }
    }
  }

  cout << dp[n - 1][m - 1][3];

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#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 ordered_set_pair                                                       \\
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,           \\
       tree_order_statistics_node_update>
#define ordered_set_mutiset                                                    \\
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \\
       tree_order_statistics_node_update>
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> pi;
#define PI 3.1415926535897932384
#define FOR(i, vv, n) for (ll i = vv; i < n; i++)
#define FORR(i, n, vv) for (ll i = n - 1; i >= vv; i--)
#define ve vector
#define maxind(v) (max_element(v.begin(), v.end()) - v.begin())
#define minind(v) (min_element(v.begin(), v.end()) - v.begin())
#define maxe(v) *max_element(v.begin(), v.end())
#define mine(v) *min_element(v.begin(), v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define M 1000000007ll
#define INF 1000000000000000000ll
#define PRECISE cout.precision(18);
#define all(v) v.begin(), v.end()
#define BS(v, n) binary_search(all(v), n)
#define srt(v) sort(v.begin(), v.end())
#define rsrt(v) sort(all(v), greater<ll>())
#define uni(v) v.resize(unique(all(v)) - v.begin())
#define F first
#define S second
#define GET(i, p) get<p>(i)

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  FAST
      // PRECISE

      ll n,
      m, k;
  cin >> n >> m >> k;
  ve<ve<ll>> v(n, ve<ll>(m));
  FOR(i, 0, k) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    v[x][y] = z;
  }

  ll dp[n][m][4];
  FOR(i, 0, n) {
    FOR(j, 0, m) {
      FOR(k, 0, 4) {
        dp[i][j][k] = 0;

        if (k > 0)
          dp[i][j][k] = v[i][j];
        if (i - 1 >= 0) {
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3] + v[i][j]);
          else
            dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
        }
      }
    }
  }

  cout << dp[n - 1][m - 1][3];

  return 0;
}