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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}
vector<vector<vector<vector<ll>>>> dp;
ll f(int mode, char op, bool ud, bool lr) {
  if (op == 'L' || op == 'R') {
    int res = op == 'R' ? 1 : -1;
    if (lr)
      res = -res;
    if (mode == 1 || mode == 2)
      res = -res;
    return res;
  } else {
    int res = op == 'U' ? 1 : -1;
    if (ud)
      res = -res;
    if (mode == 2 || mode == 3)
      res = -res;
    return res;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  const int n = s.size();
  int K;
  cin >> K;
  ll ans = 0;
  REP(mode, 4) {
    dp.assign(2, vector<vector<vector<ll>>>(
                     K + 1, vector<vector<ll>>(2, vector<ll>(2, -inf))));
    dp[0][0][0][0] = 0;
    REP(i, n) {
      dp[i + 1 & 1].assign(K + 1, vector<vector<ll>>(2, vector<ll>(2, -inf)));
      REP(j, K + 1) REP(a, 2) REP(b, 2) {
        if (dp[i & 1][j][a][b] == -inf)
          continue;
        // ???????????????
        dp[i + 1 & 1][j][a][b] = max(dp[i + 1 & 1][j][a][b],
                                     dp[i & 1][j][a][b] + f(mode, s[i], a, b));
        // ??????
        if (j < K) {
          dp[i + 1 & 1][j + 1][!a][b] =
              max(dp[i + 1 & 1][j + 1][!a][b],
                  dp[i & 1][j][a][b] + f(mode, s[i], !a, b));
          dp[i + 1 & 1][j + 1][a][!b] =
              max(dp[i + 1 & 1][j + 1][a][!b],
                  dp[i & 1][j][a][b] + f(mode, s[i], a, !b));
        }
      }
    }
    REP(j, K + 1) REP(a, 2) REP(b, 2) {
      if (dp[n & 1][j][a][b] == -inf)
        continue;
      ans = max(ans, dp[n & 1][j][a][b]);
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}
vector<vector<vector<vector<ll>>>> dp;
ll f(int mode, char op, bool ud, bool lr) {
  if (op == 'L' || op == 'R') {
    int res = op == 'R' ? 1 : -1;
    if (lr)
      res = -res;
    if (mode == 1 || mode == 2)
      res = -res;
    return res;
  } else {
    int res = op == 'U' ? 1 : -1;
    if (ud)
      res = -res;
    if (mode == 2 || mode == 3)
      res = -res;
    return res;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  const int n = s.size();
  int K;
  cin >> K;
  ll ans = 0;
  REP(mode, 4) {
    dp.assign(n + 1, vector<vector<vector<ll>>>(
                         K + 1, vector<vector<ll>>(2, vector<ll>(2, -inf))));
    dp[0][0][0][0] = 0;
    REP(i, n) REP(j, K + 1) REP(a, 2) REP(b, 2) {
      if (dp[i][j][a][b] == -inf)
        continue;
      // ???????????????
      dp[i + 1][j][a][b] =
          max(dp[i + 1][j][a][b], dp[i][j][a][b] + f(mode, s[i], a, b));
      // ??????
      if (j < K) {
        dp[i + 1][j + 1][!a][b] =
            max(dp[i + 1][j + 1][!a][b], dp[i][j][a][b] + f(mode, s[i], !a, b));
        dp[i + 1][j + 1][a][!b] =
            max(dp[i + 1][j + 1][a][!b], dp[i][j][a][b] + f(mode, s[i], a, !b));
      }
    }
    REP(j, K + 1) REP(a, 2) REP(b, 2) {
      if (dp[n][j][a][b] == -inf)
        continue;
      ans = max(ans, dp[n][j][a][b]);
    }
  }
  cout << ans << endl;
}