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

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;
using tii = tuple<int, int, int>;

const int limit = 2500;
int t[55];
int dp[2][limit + 10][limit + 10];

int main(void) {
  int n;
  cin >> n;
  rep(i, n) cin >> t[i];

  clr(dp, -1);
  dp[0][0][0] = 0;
  rep(i, n) {
    rep(j, limit + 1) rep(k, limit + 1) {
      if (dp[0][j][k] == -1)
        continue;
      int cur = dp[0][j][k];
      dp[1][j + t[i]][k] = cur;
      dp[1][j][k + t[i]] = cur;
      dp[1][j][k] = cur + t[i];
    }
    rep(j, limit + 1) rep(k, limit + 1) dp[0][j][k] = dp[1][j][k];
    clr(dp[1], -1);
  }

  int ans = limit;
  rep(j, limit + 1) rep(k, limit + 1) {
    if (dp[0][j][k] == -1)
      continue;
    int cmax = max({j, k, dp[0][j][k]});
    chmin(ans, cmax);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;
using tii = tuple<int, int, int>;

const int limit = 1550;
int t[55];
int dp[55][limit + 10][limit + 10];

int main(void) {
  int n;
  cin >> n;
  rep(i, n) cin >> t[i];

  clr(dp, -1);
  dp[0][0][0] = 0;
  rep(i, n) rep(j, limit + 1) rep(k, limit + 1) {
    if (dp[i][j][k] == -1)
      continue;
    int cur = dp[i][j][k];
    dp[i + 1][j + t[i]][k] = cur;
    dp[i + 1][j][k + t[i]] = cur;
    dp[i + 1][j][k] = cur + t[i];
  }

  int ans = limit;
  rep(j, limit + 1) rep(k, limit + 1) {
    if (dp[n][j][k] == -1)
      continue;
    int cmax = max({j, k, dp[n][j][k]});
    chmin(ans, cmax);
  }
  cout << ans << endl;
  return 0;
}