Problem p02586 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define reps(i, s, n) for (int i = s, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;
typedef long double ld;
const char sp = ' ';

using namespace std;

static ll dp[3001][3001][4];
static ll te[3001][3001];
int main(void) {

  ll R, C, K;
  cin >> R >> C >> K;
  ll r, c, v;

  MEMSET(te, 0);
  MEMSET(dp, 0);
  rep(i, K) {
    cin >> r >> c >> v;
    te[c - 1][r - 1] = v;
  }
  dp[0][0][1] = te[0][0];
  rep(j, R) rep(i, C) {
    rep1(k, 3) dp[i + 1][j][k] =
        max({dp[i + 1][j][k], dp[i][j][k], dp[i][j][k - 1] + te[i + 1][j]});
    dp[i + 1][j][0] = max({dp[i + 1][j][0], dp[i][j][0]});
    rep(k, 4) dp[i][j + 1][0] = max(dp[i][j][k], dp[i][j + 1][0]);
    rep(k, 4) dp[i][j + 1][1] =
        max(dp[i][j][k] + te[i][j + 1], dp[i][j + 1][1]);
  }
  ll Maxim = 0;
  rep(i, 4) { Maxim = max(Maxim, dp[C - 1][R - 1][i]); }
  cout << Maxim << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define reps(i, s, n) for (int i = s, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;
typedef long double ld;
const char sp = ' ';

using namespace std;

static unsigned long int dp[3001][3001][4][4];
static unsigned long int te[3001][3001];
int main(void) {

  ll R, C, K;
  cin >> R >> C >> K;
  ll r, c, v;

  MEMSET(te, 0);
  MEMSET(dp, 0);
  rep(i, K) {
    cin >> r >> c >> v;
    te[c - 1][r - 1] = v;
  }
  dp[0][0][1][1] = te[0][0];
  rep(j, R) rep(i, C) {
    dp[i + 1][j][0][0] = max({dp[i][j][0][0], dp[i][j][0][1], dp[i][j][0][2],
                              dp[i][j][0][3], dp[i + 1][j][0][0]});
    rep1(k, 3) dp[i + 1][j][k][0] = max(
        {dp[i + 1][j][k][0], dp[i][j][k][0], dp[i][j][k][1], dp[i][j][k][2],
         dp[i][j][k][3], dp[i][j][k - 1][0] + te[i + 1][j],
         dp[i][j][k - 1][1] + te[i + 1][j], dp[i][j][k - 1][2] + te[i + 1][j],
         dp[i][j][k - 1][3] + te[i + 1][j]});
    dp[i][j + 1][0][0] = max({dp[i][j][0][0], dp[i][j][1][0], dp[i][j][3][0],
                              dp[i][j][2][0], dp[i + 1][j][0][0]});
    rep1(k, 3) dp[i][j + 1][0][k] = max(
        {dp[i][j + 1][0][k], dp[i][j][0][k], dp[i][j][1][k], dp[i][j][2][k],
         dp[i][j][3][k], dp[i][j][0][k - 1] + te[i][j + 1],
         dp[i][j][1][k - 1] + te[i][j + 1], dp[i][j][2][k - 1] + te[i][j + 1],
         dp[i][j][3][k - 1] + te[i][j + 1]});
  }
  unsigned long int Maxim = 0;
  rep(i, 4) rep(j, 4) { Maxim = max(Maxim, dp[C - 1][R - 1][i][j]); }
  cout << Maxim + 1 << endl;
  return 0;
}