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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v)                                                               \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end())
// #define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
// const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int field[3010][3010];
ll dp[3010][3010][5];

void solve() {
  int R, C, K;
  cin >> R >> C >> K;
  int r[200010], c[200010], v[200010];
  rep(i, K) {
    cin >> r[i] >> c[i] >> v[i];
    r[i]--, c[i]--;
    field[r[i]][c[i]] += v[i];
  }

  if (field[0][0] > 0) {
    dp[0][0][1] = field[0][0];
  }

  rep(i, R) {
    rep(j, C) {
      if (i + 1 < R) {
        ll Max = 0;
        rep(k, 4) chmax(Max, dp[i][j][k]);
        chmax(dp[i + 1][j][0], Max);
        if (field[i + 1][j] > 0) {
          chmax(dp[i + 1][j][1], Max + field[i + 1][j]);
        }
      }
      if (j + 1 < C) {
        rep(k, 4) {
          chmax(dp[i][j + 1][k], dp[i][j][k]);
          if (k == 3)
            continue;
          if (field[i][j + 1] > 0) {
            chmax(dp[i][j + 1][k + 1], dp[i][j][k] + field[i][j + 1]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) chmax(ans, dp[R - 1][C - 1][i]);
  cout << ans << endl;

  // rep(i,R){
  //     rep(j,C){
  //         cout<<i<<" "<<j<<endl;
  //         rep(k,4){
  //             cout<<dp[i][j][k]<<", ";
  //         }
  //         cout<<endl;
  //     }
  // }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll t = 1;
  rep(i, t) { solve(); }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v)                                                               \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end())
// #define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
// const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int field[3010][3010];
ll dp[3010][3010][4];

void solve() {
  int R, C, K;
  cin >> R >> C >> K;
  int r[5010], c[5010], v[5010];
  rep(i, K) {
    cin >> r[i] >> c[i] >> v[i];
    r[i]--, c[i]--;
    field[r[i]][c[i]] += v[i];
  }

  if (field[0][0] > 0) {
    dp[0][0][1] = field[0][0];
  }

  rep(i, R) {
    rep(j, C) {
      if (i + 1 < R) {
        ll Max = 0;
        rep(k, 4) chmax(Max, dp[i][j][k]);
        chmax(dp[i + 1][j][0], Max);
        if (field[i + 1][j] > 0) {
          chmax(dp[i + 1][j][1], Max + field[i + 1][j]);
        }
      }
      if (j + 1 < C) {
        rep(k, 4) {
          chmax(dp[i][j + 1][k], dp[i][j][k]);
          if (k == 3)
            continue;
          if (field[i][j + 1] > 0) {
            chmax(dp[i][j + 1][k + 1], dp[i][j][k] + field[i][j + 1]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) chmax(ans, dp[R - 1][C - 1][i]);
  cout << ans << endl;

  // rep(i,R){
  //     rep(j,C){
  //         cout<<i<<" "<<j<<endl;
  //         rep(k,4){
  //             cout<<dp[i][j][k]<<", ";
  //         }
  //         cout<<endl;
  //     }
  // }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll t = 1;
  rep(i, t) { solve(); }

  return 0;
}