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

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

const long long MOD = static_cast<long long>(1e9 + 7);
const long long INFINITE = static_cast<long long>(1e16);
const bool IS_DEBUG = true;

#undef int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> items(R, vector<long long>(C, 0));
  for (int c = 0; c < K; c++) {
    long long int x, y, v;
    cin >> x >> y >> v;
    items[x - 1][y - 1] = v;
  }
  vector<vector<vector<long long>>> dp(
      R, vector<vector<long long>>(C, vector<long long>(4, 0)));
  long long res = 0;

  for (int x = 0; x < R; x++) {
    for (int y = 0; y < C; y++) {
      for (int c2 = 0; c2 < 4; c2++) {
        if (c2 > 0)
          dp[x][y][c2] = max(dp[x][y][c2 - 1], dp[x][y][c2]);
        if (x > 0) {
          dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3]);
          if (c2 != 0) {
            dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3] + items[x][y]);
          }
        }
        if (y > 0) {
          dp[x][y][c2] = max(dp[x][y][c2], dp[x][y - 1][c2]);
          if (c2 > 0) {
            dp[x][y][c2] =
                max(dp[x][y][c2], dp[x][y - 1][c2 - 1] + items[x][y]);
          }
        }
        if (x == 0 && y == 0 && c2 > 0) {
          dp[x][y][c2] = max(dp[x][y][c2], items[x][y]);
        }
        res = max(res, dp[x][y][c2]);
        // cout<<x<<" "<<y<<" "<<c2<<" "<<dp[x][y][c2]<<endl;
      }
    }
  }
  cout << res;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

const long long MOD = static_cast<long long>(1e9 + 7);
const long long INFINITE = static_cast<long long>(1e16);
const bool IS_DEBUG = true;

#undef int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> items(R, vector<long long>(C, 0));
  for (int c = 0; c < K; c++) {
    long long int x, y, v;
    cin >> x >> y >> v;
    items[x - 1][y - 1] = v;
  }
  vector<vector<vector<long long>>> dp(
      R, vector<vector<long long>>(C, vector<long long>(10, 0)));
  long long res = 0;

  for (int x = 0; x < R; x++) {
    for (int y = 0; y < C; y++) {
      for (int c2 = 0; c2 < 4; c2++) {
        if (c2 > 0)
          dp[x][y][c2] = max(dp[x][y][c2 - 1], dp[x][y][c2]);
        if (x > 0) {
          dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3]);
          if (c2 != 0) {
            dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3] + items[x][y]);
          }
        }
        if (y > 0) {
          dp[x][y][c2] = max(dp[x][y][c2], dp[x][y - 1][c2]);
          if (c2 > 0) {
            dp[x][y][c2] =
                max(dp[x][y][c2], dp[x][y - 1][c2 - 1] + items[x][y]);
          }
        }
        if (x == 0 && y == 0 && c2 > 0) {
          dp[x][y][c2] = max(dp[x][y][c2], items[x][y]);
        }
        res = max(res, dp[x][y][c2]);
        // cout<<x<<" "<<y<<" "<<c2<<" "<<dp[x][y][c2]<<endl;
      }
    }
  }
  cout << res;
}