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

#pragma GCC optimize("Ofast")
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <complex>   // complex
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define enld '\\n'
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
// constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int 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;
}
// ---------------------------------------------------------------------------

ll dp[3001][3001][4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  for (int i = 0; i < 3001; i++) {
    for (int j = 0; j < 3001; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<ll>> G(R, vector<ll>(C, 0));
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    G[r][c] = v;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        // 行移動
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        if (k != 3) {
          chmax(dp[i + 1][j][0], dp[i][j][k] + G[i][j]);
        }
        // 列移動
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k != 3) {
          chmax(dp[i][j + 1][k + 1], dp[i][j][k] + G[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++) {
    chmax(ans, dp[R][C - 1][i]);
    chmax(ans, dp[R - 1][C][i]);
  }
  cout << ans << "\\n";
  return 0;
}


Error: MLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <complex>   // complex
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define enld '\\n'
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
// constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int 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;
}
// ---------------------------------------------------------------------------

ll dp[3001][3001][4][4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  for (int i = 0; i < 3001; i++) {
    for (int j = 0; j < 3001; j++) {
      for (int k = 0; k < 4; k++) {
        for (int l = 0; l < 4; l++) {
          dp[i][j][k][l] = 0;
        }
      }
    }
  }
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<ll>> G(R, vector<ll>(C, 0));
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    G[r][c] = v;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        for (int l = 0; l <= 3; l++) {
          // 行移動
          chmax(dp[i + 1][j][k][0], dp[i][j][k][l]);
          if (l != 3 && k != 3) {
            chmax(dp[i + 1][j][k + 1][0], dp[i][j][k][l] + G[i][j]);
          }
          // 列移動
          chmax(dp[i][j + 1][0][l], dp[i][j][k][l]);
          if (k != 3 && l != 3) {
            chmax(dp[i][j + 1][0][l + 1], dp[i][j][k][l] + G[i][j]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++) {
    for (int j = 0; j <= 3; j++) {
      chmax(ans, dp[R][C - 1][i][j]);
      chmax(ans, dp[R - 1][C][i][j]);
    }
  }
  cout << ans << "\\n";
  return 0;
}