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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

const ld pi = acos(-1);
const ll inf = (ll)1e18;
const int mod = 1000000007;
// const int mod=998244353;

int n, m, k;
const int N = 3e3 + 10;

ll dp[N][N][4];
int val[N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q, t, u, v, h;
  ll x, a, b, c, d, y, w, z, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    cin >> u >> v >> val[u][v];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][1] = dp[i][j][0] + val[i][j];
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + val[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + val[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[n][m][i]);
  }
  cout << ans << el;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

const ld pi = acos(-1);
const ll inf = (ll)1e18;
const int mod = 1000000007;
// const int mod=998244353;

int n, m, k;
const int N = 1e6 + 10;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q, t, u, v, h;
  ll x, a, b, c, d, y, w, z, k;
  cin >> n >> m >> k;
  ll dp[n + 1][m + 1][4];
  int val[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  memset(val, 0, sizeof(val));
  for (int i = 1; i <= k; i++) {
    cin >> u >> v >> val[u][v];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][1] = dp[i][j][0] + val[i][j];
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + val[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + val[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[n][m][i]);
  }
  cout << ans << el;

  return 0;
}