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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<VVD> VVVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<VVS> VVVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<VVB> VVVB;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
typedef vector<VVPII> VVVPII;
typedef vector<PLL> VPLL;
typedef vector<VPLL> VVPLL;
typedef vector<VVPLL> VVVPLL;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0};
const int DY[] = {0, -1, 0, 1};
const double EPS = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

int main(void) {
  int N;
  cin >> N;
  VI ts(N);
  rep(i, N) cin >> ts[i];

  int T = accumulate(ALL(ts), 0) + 1;

  // dp[N][T1][T2] = min(t2)
  VVVI dp(2, VVI(T, VI(T, INF)));
  dp[0][0][0] = 0;

  rep(i, N) {
    int cur = i % 2;
    int next = (i + 1) % 2;
    int t = ts[i];
    dp[next] = VVI(T, VI(T, INF));

    rep(j, T) {
      rep(k, T) {
        if (dp[cur][j][k] == INF)
          continue;
        int cur_t3 = dp[cur][j][k];

        MINUD(dp[next][j + t][k], cur_t3);
        MINUD(dp[next][j][k + t], cur_t3);
        MINUD(dp[next][j][k], cur_t3 + t);
      }
    }
  }

  int ans = INF;
  int i = N % 2;
  rep(j, T) {
    rep(k, T) { MINUD(ans, max({j, k, dp[i][j][k]})); }
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<VVD> VVVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<VVS> VVVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<VVB> VVVB;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
typedef vector<VVPII> VVVPII;
typedef vector<PLL> VPLL;
typedef vector<VPLL> VVPLL;
typedef vector<VVPLL> VVVPLL;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0};
const int DY[] = {0, -1, 0, 1};
const double EPS = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

int main(void) {
  int N;
  cin >> N;
  VI ts(N);
  rep(i, N) cin >> ts[i];

  int T = accumulate(ALL(ts), 0) + 1;

  // dp[N][T1][T2] = min(t2)
  VVVI dp(N + 1, VVI(T, VI(T, INF)));
  dp[0][0][0] = 0;

  rep(i, N) {
    int t = ts[i];

    rep(j, T) {
      rep(k, T) {
        if (dp[i][j][k] == INF)
          continue;
        int cur_t3 = dp[i][j][k];

        MINUD(dp[i + 1][j + t][k], cur_t3);
        MINUD(dp[i + 1][j][k + t], cur_t3);
        MINUD(dp[i + 1][j][k], cur_t3 + t);
      }
    }
  }

  int ans = INF;
  rep(j, T) {
    rep(k, T) { MINUD(ans, max({j, k, dp[N][j][k]})); }
  }

  cout << ans << endl;

  return 0;
}