Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\n"
#define TO_STRING(VariableName) #VariableName

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  rep(i, (int)v.size() - 1) cout << v[i] << " ";
  cout << v[(int)v.size() - 1];
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <class T> void debag(const T &obj) { cout << obj << endl; }

bool solve() {
  ll r, c, k;
  cin >> r >> c >> k;
  swap(r, c);
  vector<vector<ll>> items(r, vector<ll>(c, 0));
  rep(i, k) {
    ll a, b, v;
    cin >> a >> b >> v;
    swap(a, b);
    a--;
    b--;
    items[a][b] = v;
  }

  ll dp[r + 1][c + 1][5];
  rep(i, r) rep(j, c) rep(k, 5) dp[i][j][k] = 0;
  rep(j, c) rep(i, r) rep(k, 4) {
    // use
    if (i + 1 < r and k < 3)
      chmax(dp[i + 1][j][k + 1], dp[i][j][k] + items[i][j]);
    if (j + 1 < c and k < 3)
      chmax(dp[i][j + 1][0], dp[i][j][k] + items[i][j]);

    // notuse
    if (i + 1 < r)
      chmax(dp[i + 1][j][k], dp[i][j][k]);
    if (j + 1 < c)
      chmax(dp[i][j + 1][0], dp[i][j][k]);
  }

  // rep(i,r)rep(j,c)cout<<i<<" "<<j<<" "<<dp[i][j][0]<<endl;

  ll ans = 0;
  rep(k, 3) chmax(ans, dp[r - 1][c - 1][k] + items[r - 1][c - 1]);
  chmax(ans, dp[r - 1][c - 1][3]);

  cout << ans << endl;
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(30);
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\n"
#define TO_STRING(VariableName) #VariableName

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  rep(i, (int)v.size() - 1) cout << v[i] << " ";
  cout << v[(int)v.size() - 1];
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <class T> void debag(const T &obj) { cout << obj << endl; }

bool solve() {
  ll r, c, k;
  cin >> r >> c >> k;
  swap(r, c);
  vector<vector<ll>> items(r, vector<ll>(c, 0));
  rep(i, k) {
    ll a, b, v;
    cin >> a >> b >> v;
    swap(a, b);
    a--;
    b--;
    items[a][b] = v;
  }

  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(5, 0)));
  rep(i, r) rep(j, c) rep(k, 5) dp[i][j][k] = 0;
  rep(j, c) rep(i, r) rep(k, 4) {
    // use
    if (i + 1 < r and k < 3)
      chmax(dp[i + 1][j][k + 1], dp[i][j][k] + items[i][j]);
    if (j + 1 < c and k < 3)
      chmax(dp[i][j + 1][0], dp[i][j][k] + items[i][j]);

    // notuse
    if (i + 1 < r)
      chmax(dp[i + 1][j][k], dp[i][j][k]);
    if (j + 1 < c)
      chmax(dp[i][j + 1][0], dp[i][j][k]);
  }

  // rep(i,r)rep(j,c)cout<<i<<" "<<j<<" "<<dp[i][j][0]<<endl;

  ll ans = 0;
  rep(k, 3) chmax(ans, dp[r - 1][c - 1][k] + items[r - 1][c - 1]);
  chmax(ans, dp[r - 1][c - 1][3]);

  cout << ans << endl;
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(30);
  solve();
}