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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long MODPOW(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

ll mp[3001][3001];
ll dp[3001][3001][4];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  fill(mp[0], mp[0] + (r + 1) * (c + 1), 0);
  for (ll i = 0; i < k; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    mp[a][b] = c;
  }
  fill(dp[0][0], dp[0][0] + (r + 1) * (c + 1) * 4, 0);

  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      for (ll k = 0; k < 4; k++) {
        chmax(dp[i][j][0], max(dp[i][j - 1][0], dp[i - 1][j][k]));
      }
      for (k = 1; k < 4; k++) {
        dp[i][j][k] = dp[i][j - 1][k];
      }
      if (mp[i][j] != 0) {
        chmax(dp[i][j][3], dp[i][j][2] + mp[i][j]);
        chmax(dp[i][j][2], dp[i][j][1] + mp[i][j]);
        chmax(dp[i][j][1], dp[i][j][0] + mp[i][j]);
      }
    }
  }
  for (ll i = 1; i < 4; i++) {
    chmax(dp[r][c][i], dp[r][c][i - 1]);
  }
  cout << dp[r][c][3] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long MODPOW(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll mp[r + 1][c + 1];
  fill(mp[0], mp[0] + (r + 1) * (c + 1), 0);
  for (ll i = 0; i < k; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    mp[a][b] = c;
  }
  ll dp[r + 1][c + 1][4];
  fill(dp[0][0], dp[0][0] + (r + 1) * (c + 1) * 4, 0);

  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      for (ll k = 0; k < 4; k++) {
        chmax(dp[i][j][0], max(dp[i][j - 1][0], dp[i - 1][j][k]));
      }
      for (k = 1; k < 4; k++) {
        dp[i][j][k] = dp[i][j - 1][k];
      }
      if (mp[i][j] != 0) {
        chmax(dp[i][j][3], dp[i][j][2] + mp[i][j]);
        chmax(dp[i][j][2], dp[i][j][1] + mp[i][j]);
        chmax(dp[i][j][1], dp[i][j][0] + mp[i][j]);
      }
    }
  }
  for (ll i = 1; i < 4; i++) {
    chmax(dp[r][c][i], dp[r][c][i - 1]);
  }
  cout << dp[r][c][3] << endl;
}