Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define EACH(i, a, b) for (int i = (a); i <= (b); i++)
#define REACH(i, a, b) for (int i = (b); i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(a, x, b) ((a) <= (x) && (x) < (b))

const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;

using ll = long long;
using P = pair<int, int>;
template <class T = int> using V = vector<T>;

template <class T> bool inline chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool inline chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T = int> T inline input() {
  T x;
  cin >> x;
  return (x);
}
template <class T> void inline print(T &x) { cout << x << '\n'; }
#define debug(x) cerr << #x << ": " << x << endl;

const int dxs[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dys[] = {0, -1, 0, 1, 1, -1, -1, 1};

ll dp[3001][3001][4];
int RC[3001][3001];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int R, C, K;
  cin >> R >> C >> K;
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    RC[r][c] = v;
  }
  dp[1][1][1] = dp[1][1][2] = dp[1][1][3] = RC[1][1];

  EACH(i, 1, R) {
    EACH(j, 1, C) {
      REP(k, 4) {
        chmax<ll>(dp[i + 1][j][0], dp[i][j][k]);
        chmax<ll>(dp[i + 1][j][1], dp[i][j][k] + RC[i + 1][j]);
      }
      chmax<ll>(dp[i][j + 1][0], dp[i][j][0]);
      chmax<ll>(dp[i][j + 1][1], max(dp[i][j][1], dp[i][j][0] + RC[i][j + 1]));
      chmax<ll>(dp[i][j + 1][2], max(dp[i][j][2], dp[i][j][1] + RC[i][j + 1]));
      chmax<ll>(dp[i][j + 1][3], max(dp[i][j][3], dp[i][j][2] + RC[i][j + 1]));
    }
  }

  ll ans = 0;
  REP(k, 4) chmax(ans, dp[R][C][k]);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define EACH(i, a, b) for (int i = (a); i <= (b); i++)
#define REACH(i, a, b) for (int i = (b); i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(a, x, b) ((a) <= (x) && (x) < (b))

const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;

using ll = long long;
using P = pair<int, int>;
template <class T = int> using V = vector<T>;

template <class T> bool inline chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool inline chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T = int> T inline input() {
  T x;
  cin >> x;
  return (x);
}
template <class T> void inline print(T &x) { cout << x << '\n'; }
#define debug(x) cerr << #x << ": " << x << endl;

const int dxs[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dys[] = {0, -1, 0, 1, 1, -1, -1, 1};

ll dp[3010][3010][4];
int RC[3001][3001];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int R, C, K;
  cin >> R >> C >> K;
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    RC[r][c] = v;
  }
  dp[1][1][1] = dp[1][1][2] = dp[1][1][3] = RC[1][1];

  EACH(i, 1, R) {
    EACH(j, 1, C) {
      REP(k, 4) {
        chmax<ll>(dp[i + 1][j][0], dp[i][j][k]);
        chmax<ll>(dp[i + 1][j][1], dp[i][j][k] + RC[i + 1][j]);
      }
      chmax<ll>(dp[i][j + 1][0], dp[i][j][0]);
      chmax<ll>(dp[i][j + 1][1], max(dp[i][j][1], dp[i][j][0] + RC[i][j + 1]));
      chmax<ll>(dp[i][j + 1][2], max(dp[i][j][2], dp[i][j][1] + RC[i][j + 1]));
      chmax<ll>(dp[i][j + 1][3], max(dp[i][j][3], dp[i][j][2] + RC[i][j + 1]));
    }
  }

  ll ans = 0;
  REP(k, 4) chmax(ans, dp[R][C][k]);

  cout << ans << endl;

  return 0;
}