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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll dp[3005][3005][4];
int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll ma[r][c];
  rep(i, r) { rep(j, c) ma[i][j] = 0; }
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    ma[a][b] = c;
  }

  rep(i, r + 5) {
    rep(j, c + 5) { rep(k, 4) dp[i][j][k] = 0; }
  }

  rep(i, r) {
    rep(j, c) {
      rep(k, 4) {
        ll base = 0;
        rep(ww, 4) chmax(base, dp[i][j + 1][ww]);

        if (ma[i][j] == 0) {

          if (k == 0)
            dp[i + 1][j + 1][k] = max(base, dp[i + 1][j][k]);
          else
            dp[i + 1][j + 1][k] = dp[i + 1][j][k];
        }

        else {
          if (k == 0)
            dp[i + 1][j + 1][k] = max(base, dp[i + 1][j][k]);
          else if (k == 1)
            dp[i + 1][j + 1][k] =
                max(base + ma[i][j],
                    max(dp[i + 1][j][k - 1] + ma[i][j], dp[i + 1][j][k]));
          else
            dp[i + 1][j + 1][k] =
                max(dp[i + 1][j][k], dp[i + 1][j][k - 1] + ma[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) chmax(ans, dp[r][c][i]);
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll ma[r][c];
  rep(i, r) { rep(j, c) ma[i][j] = 0; }
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    ma[a][b] = c;
  }
  ll dp[r + 5][c + 5][4];
  rep(i, r + 5) {
    rep(j, c + 5) { rep(k, 4) dp[i][j][k] = 0; }
  }

  rep(i, r) {
    rep(j, c) {
      rep(k, 4) {
        ll base = 0;
        rep(ww, 4) chmax(base, dp[i][j + 1][ww]);

        if (ma[i][j] == 0) {

          if (k == 0)
            dp[i + 1][j + 1][k] = max(base, dp[i + 1][j][k]);
          else
            dp[i + 1][j + 1][k] = dp[i + 1][j][k];
        }

        else {
          if (k == 0)
            dp[i + 1][j + 1][k] = max(base, dp[i + 1][j][k]);
          else if (k == 1)
            dp[i + 1][j + 1][k] =
                max(base + ma[i][j],
                    max(dp[i + 1][j][k - 1] + ma[i][j], dp[i + 1][j][k]));
          else
            dp[i + 1][j + 1][k] =
                max(dp[i + 1][j][k], dp[i + 1][j][k - 1] + ma[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) chmax(ans, dp[r][c][i]);
  cout << ans << endl;

  return 0;
}