alexjercan/bugnet huggingface dataset:
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, l, n) for (lint i = l; i < n; i++)
#define rer(i, l, n) for (lint i = l; i <= n; i++)
#define rrep(i, n, l) for (int i = n - 1; i >= l; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int n;
int t[55];
bool dp[55][900][900];

int main() {
  cin >> n;
  int sum = 0;
  rep(i, 0, n) {
    cin >> t[i];
    sum += t[i];
  }
  memset(dp, false, sizeof(dp));
  dp[0][0][0] = true;
  rep(i, 0, n) {
    rep(j, 0, 900) {
      rep(k, 0, 900) {
        if (!dp[i][j][k])
          continue;
        if (j + t[i] < 900)
          dp[i + 1][j + t[i]][k] = true; //?±±????????????
        if (k + t[i] < 900)
          dp[i + 1][j][k + t[i]] = true; //?±±????????????
        dp[i + 1][j][k] = true;          //?±±????????????
      }
    }
  }
  int MIN = 1e8;
  rep(i, 0, 900) {
    rep(j, 0, 900) {
      if (dp[n][i][j]) {
        int tmp = max(i, max(j, sum - i - j));
        MIN = min(MIN, tmp);
      }
    }
  }
  o(MIN);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

bool dp[51][2500][2500];

int main() {
  int n, sum = 0;
  cin >> n;
  vi t(n + 1);
  rep(i, 1, n + 1) {
    cin >> t[i];
    sum += t[i];
  }
  sort(all(t));
  // reverse(all(t));
  memset(dp, 0, sizeof(dp));
  rep(i, 0, 2500) {
    rep(j, 0, 2500) { dp[0][i][j] = 0; }
  }
  rep(i, 1, n + 1) {
    rep(j, 0, 2500) {
      rep(k, 0, 2500) {
        //????????????????????????true??????????????????true??????????????????
        dp[i][j][k] |= dp[i - 1][j - t[i]][k] || dp[i - 1][j][k - t[i]];
        /*if(j>=t[i] && dp[i-1][j-t[i]][k]){
                dp[i][j][k]=true;
        }if(k>=t[i] && dp[i-1][j][k-t[i]]){
                dp[i][j][k]=true;
        }*/
      }
    }
  }
  // o("ok");
  int c, tmp, MIN = 3000;
  rep(i, 0, 2500) {
    rep(j, 0, 2500) {
      if (dp[n][i][j]) {
        c = sum - i - j;
        tmp = max(i, max(j, c));
        MIN = min(MIN, tmp);
      }
    }
  }
  o(MIN);
}
Problem p01868 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

int n, sum = 0;
bool dp[52][901][901];

signed main() {
  cin >> n;
  vector<int> t(n);
  rep(i, 0, n) {
    cin >> t[i];
    sum += t[i];
  }
  sort(all(t));
  rep(i, 0, 51) rep(j, 0, 900) rep(k, 0, 900) dp[i][j][k] = false;
  dp[0][0][0] = true;
  rep(i, 0, n) {
    rep(j, 0, 900) {
      rep(k, 0, 900) {
        if (dp[i][j][k]) {
          if (j + t[i] < 900)
            dp[i + 1][j + t[i]][k] = true;
          if (k + t[i] < 900)
            dp[i + 1][j][k + t[i]] = true;
          dp[i + 1][j][k] = true;
        }
      }
    }
  }
  int MIN = 1e8;
  rep(i, 0, 900) {
    rep(j, 0, 900) {
      if (dp[n][i][j] == false)
        continue;
      int tmp = sum - i - j;
      MIN = min(MIN, max(tmp, max(i, j)));
    }
  }
  cout << MIN << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

int n, sum = 0;
bool dp[51][900][900];

signed main() {
  cin >> n;
  vector<int> t(n);
  rep(i, 0, n) {
    cin >> t[i];
    sum += t[i];
  }
  sort(all(t));
  rep(i, 0, 51) rep(j, 0, 900) rep(k, 0, 900) dp[i][j][k] = false;
  dp[0][0][0] = true;
  rep(i, 0, n) {
    rep(j, 0, 900) {
      rep(k, 0, 900) {
        if (dp[i][j][k]) {
          dp[i + 1][j + t[i]][k] = true;
          dp[i + 1][j][k + t[i]] = true;
          dp[i + 1][j][k] = true;
        }
      }
    }
  }
  int MIN = 1e8;
  rep(i, 0, 900) {
    rep(j, 0, 900) {
      if (dp[n][i][j] == false)
        continue;
      int tmp = sum - i - j;
      MIN = min(MIN, max(tmp, max(i, j)));
    }
  }
  cout << MIN << endl;
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int t[50], sum;
// int sum[3];
bool dp[2][2505][2505];

int main() {
  cin >> n;
  sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    sum += t[i];
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    int ci = i & 1, ni = (i + 1) & 1;
    memset(dp[ni], 0, sizeof(dp[ni]));

    for (int A = 0; A <= 2500; ++A) {
      for (int B = 0; B <= 2500; ++B) {
        if (!dp[ci][A][B])
          continue;
        dp[ni][A][B] = 1;
        dp[ni][A + t[i]][B] = 1;
        dp[ni][A][B + t[i]] = 1;
      }
    }
  }

  int ans = 1e9;
  for (int A = 0; A <= 2500; ++A) {
    for (int B = 0; B <= 2500; ++B) {
      if (!dp[n & 1][A][B])
        continue;
      ans = min(ans, max(A, max(B, sum - A - B)));
    }
  }

  cout << ans << endl;

  /*
  sort(t, t + n);

  for(int i = n-1; i >= 0; i--){
    sort(sum,sum+3);
    sum[0] += t[i];
  }

  sort(sum, sum + 3);

  cout << sum[2] << endl;
  */
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int t[50], sum;
// int sum[3];
bool dp[51][2505][2505];

int main() {
  cin >> n;
  sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    sum += t[i];
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int A = 0; A <= 2500; ++A) {
      for (int B = 0; B <= 2500; ++B) {
        if (!dp[i][A][B])
          continue;
        dp[i + 1][A][B] = 1;
        dp[i + 1][A + t[i]][B] = 1;
        dp[i + 1][A][B + t[i]] = 1;
      }
    }
  }

  int ans = 1e9;
  for (int A = 0; A <= 2500; ++A) {
    for (int B = 0; B <= 2500; ++B) {
      if (!dp[n][A][B])
        continue;
      ans = min(ans, max(A, max(B, sum - A - B)));
    }
  }

  cout << ans << endl;

  /*
  sort(t, t + n);

  for(int i = n-1; i >= 0; i--){
    sort(sum,sum+3);
    sum[0] += t[i];
  }

  sort(sum, sum + 3);

  cout << sum[2] << endl;
  */
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int INF = 1 << 28;

int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> T(N);
  for (auto &i : T)
    cin >> i, sum += i;

  vector<vector<vector<int>>> dp(
      51, vector<vector<int>>(1000, vector<int>(1000, INF)));
  dp[0][0][0] = 0;

  int res = INF;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        if (!dp[i][j][k]) {
          if (j + T[i] < 1000)
            dp[i + 1][j + T[i]][k] = 0;
          if (k + T[i] < 1000)
            dp[i + 1][j][k + T[i]] = 0;
          res = min(res, max(j + T[i], max(k, sum - (j + k + T[i]))));
          res = min(res, max(j, max(k + T[i], sum - (j + k + T[i]))));
          dp[i + 1][j][k] = 0;
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

const int INF = 1 << 28;

int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> T(N);
  for (auto &i : T)
    cin >> i, sum += i;

  vector<vector<vector<int>>> dp(
      55, vector<vector<int>>(1111, vector<int>(1111, INF)));
  dp[0][0][0] = 0;

  int res = INF;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        if (!dp[i][j][k]) {
          if (j + T[i] < 1000)
            dp[i + 1][j + T[i]][k] = 0;
          if (k + T[i] < 1000)
            dp[i + 1][j][k + T[i]] = 0;
          res = min(res, max(j + T[i], max(k, sum - (j + k + T[i]))));
          res = min(res, max(j, max(k + T[i], sum - (j + k + T[i]))));
          dp[i + 1][j][k] = 0;
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[2510][2510];
int dp2[2510][2510];
const int inf = 1000000;
int main() {
  int n;
  cin >> n;
  vector<int> t(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    sum += t[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      for (int k = 0; k <= sum; k++)
        dp[j][k] = dp2[j][k] = inf;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      for (int k = 0; k <= sum; k++) {
        if (j >= t[i]) {
          dp2[j][k] = min(dp2[j][k], dp[j - t[i]][k]);
        }
        if (k >= t[i]) {
          dp2[j][k] = min(dp2[j][k], dp[j][k - t[i]]);
        }
        dp2[j][k] = min(dp2[j][k], dp[j][k] + t[i]);
      }
    }
    for (int j = 0; j <= sum; j++) {
      for (int k = 0; k <= sum; k++) {
        dp[j][k] = dp2[j][k];
        dp2[j][k] = inf;
      }
    }
  }
  int ans = inf;
  for (int i = 0; i <= sum; i++) {
    for (int j = 0; j <= sum; j++) {
      int large = max(dp[i][j], max(i, j));
      ans = min(ans, large);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[60][2510][2510];
const int inf = 1000000;
int main() {
  int n;
  cin >> n;
  vector<int> t(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    sum += t[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      for (int k = 0; k <= sum; k++)
        dp[i][j][k] = inf;
    }
  }
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      for (int k = 0; k <= sum; k++) {
        if (j >= t[i]) {
          dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j - t[i]][k]);
        }
        if (k >= t[i]) {
          dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k - t[i]]);
        }
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k] + t[i]);
      }
    }
  }
  int ans = inf;
  for (int i = 0; i <= sum; i++) {
    for (int j = 0; j <= sum; j++) {
      int large = max(dp[n][i][j], max(i, j));
      ans = min(ans, large);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int MAX_N = 55;
const int MAX_S = 900;

int N;
int T[MAX_N];
bool dp[MAX_N][MAX_S][MAX_S];

signed main() {
  cin >> N;
  int sm = 0;
  rep(i, N) {
    cin >> T[i];
    sm += T[i];
  }

  rep(i, MAX_N) rep(j, MAX_S) rep(k, MAX_S) dp[i][j][k] = false;
  dp[0][0][0] = true;

  rep(i, N) {
    rep(j, MAX_S) {
      rep(k, MAX_S) {
        if (!dp[i][j][k])
          continue;

        if (j + T[i] < MAX_S) {
          dp[i + 1][j + T[i]][k] = true; // ?±±1?????????
        }
        if (k + T[i] < MAX_S) {
          dp[i + 1][j][k + T[i]] = true; // ?±±2?????????
        }
        dp[i + 1][j][k] = true; // ?±±3?????????
      }
    }
  }

  int mi = 99999999;
  rep(j, MAX_S) {
    rep(k, MAX_S) {
      if (dp[N][j][k]) {
        int ma = max(j, k);
        ma = max(ma, sm - j - k);
        mi = min(mi, ma);
      }
    }
  }

  cout << mi << endl;
}

Error: MLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int MAX_N = 55;
const int MAX_S = 2510;

int N;
int T[MAX_N];
bool dp[MAX_N][MAX_S][MAX_S];

signed main() {
  cin >> N;
  int sm = 0;
  rep(i, N) {
    cin >> T[i];
    sm += T[i];
  }

  rep(i, MAX_N) rep(j, MAX_S) rep(k, MAX_S) dp[i][j][k] = false;
  dp[0][0][0] = true;

  rep(i, N) {
    rep(j, MAX_S) {
      rep(k, MAX_S) {
        if (!dp[i][j][k])
          continue;

        dp[i + 1][j + T[i]][k] = true; // ?±±1?????????
        dp[i + 1][j][k + T[i]] = true; // ?±±2?????????
        dp[i + 1][j][k] = true;        // ?±±3?????????
      }
    }
  }

  int mi = 99999999;
  rep(j, MAX_S) {
    rep(k, MAX_S) {
      if (dp[N][j][k]) {
        int ma = max(j, k);
        ma = max(ma, sm - j - k);
        mi = min(mi, ma);
      }
    }
  }

  cout << mi << endl;
}
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>;

int t[55];
set<tii> dp[2];

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

  dp[0].insert(tii(0, 0, 0));

  rep(i, n) {
    int ci = i & 1, ni = ci ^ 1;
    dp[ni].clear();
    for (auto &cur : dp[ci]) {
      rep(j, 3) {
        int a[3];
        tie(a[0], a[1], a[2]) = cur;
        a[j] += t[i];
        sort(a, a + 3);
        dp[ni].insert(tii(a[0], a[1], a[2]));
      }
    }
  }

  int ans = 1 << 20;
  for (auto &cur : dp[n & 1]) {
    int res;
    tie(ignore, ignore, res) = cur;
    chmin(ans, res);
  }
  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>;

int t[55];
set<tii> dp[55];

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

  dp[0].insert(tii(0, 0, 0));

  rep(i, n) {
    for (auto &cur : dp[i]) {
      rep(j, 3) {
        int a[3];
        tie(a[0], a[1], a[2]) = cur;
        a[j] += t[i];
        sort(a, a + 3);
        dp[i + 1].insert(tii(a[0], a[1], a[2]));
      }
    }
  }

  int ans = 1 << 20;
  for (auto &cur : dp[n]) {
    int res;
    tie(ignore, ignore, res) = cur;
    chmin(ans, res);
  }
  cout << ans << endl;
  return 0;
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for (int(i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)
#define RFOR(i, e, s) for (int(i) = (e)-1; (i) >= (int)(s); (i)--)
#define RREP(i, e) RFOR(i, e, 0)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;
typedef priority_queue<int> PQI;
typedef priority_queue<PII> PQII;

const double EPS = 1e-10;
const int INF = 1e9;
const int N = 50;
const int T = 50;
int n;
int t[N];
int dp[2][N * T + 1][N * T + 1];

int main() {
  scanf("%d ", &n);
  REP(i, n) scanf("%d ", t + i);

  int t_sum = 0;
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  int crt, nxt;
  REP(i, n) {
    crt = i & 1;
    nxt = (i + 1) & 1;
    REP(j, t_sum + 1)
    REP(k, t_sum + 1) {
      dp[nxt][j + t[i]][k] |= dp[crt][j][k];
      dp[nxt][j][k + t[i]] |= dp[crt][j][k];
      dp[nxt][j][k] |= dp[crt][j][k];
    }
    t_sum += t[i];
    memset(dp[crt], 0, sizeof(dp[crt]));
  }

  int res = INF;
  REP(j, t_sum + 1)
  REP(k, t_sum + 1) if (dp[nxt][j][k]) res =
      min(res, max(max(j, k), t_sum - j - k));

  printf("%d\\n", res);

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for (int(i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)
#define RFOR(i, e, s) for (int(i) = (e)-1; (i) >= (int)(s); (i)--)
#define RREP(i, e) RFOR(i, e, 0)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;
typedef priority_queue<int> PQI;
typedef priority_queue<PII> PQII;

const double EPS = 1e-10;
const int INF = 1e9;
const int N = 50;
const int T = 50;
int n;
int t[N];
int dp[N + 1][N * T + 1][N * T + 1];

int main() {
  scanf("%d ", &n);
  REP(i, n) scanf("%d ", t + i);

  int t_sum = 0;
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  REP(i, n) {
    REP(j, t_sum + 1)
    REP(k, t_sum + 1) {
      dp[i + 1][j + t[i]][k] |= dp[i][j][k];
      dp[i + 1][j][k + t[i]] |= dp[i][j][k];
      dp[i + 1][j][k] |= dp[i][j][k];
    }
    t_sum += t[i];
  }

  int res = INF;
  REP(j, t_sum + 1)
  REP(k, t_sum + 1) if (dp[n][j][k]) res =
      min(res, max(max(j, k), t_sum - j - k));

  printf("%d\\n", res);

  return 0;
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < n; (i)++)
const int INF = 1e9;

int main() {
  int n;
  cin >> n;
  vector<int> t(n);
  rep(i, n) { cin >> t[i]; }
  map<int, map<int, map<int, int>>> dp[51];
  int limit = INF;

  function<int(int, int, int, int)> dfs = [&](int i, int a, int b, int c) {
    if (i == n) {
      return limit = min(limit, max({a, b, c}));
    }
    int d[3] = {a, b, c};
    sort(d, d + 3);
    a = d[0], b = d[1], c = d[2];
    if (c >= limit)
      return INF;
    if (dp[i].count(a) && dp[i][a].count(b) && dp[i][a][b].count(c)) {
      return dp[i][a][b][c];
    }

    int ret = dfs(i + 1, a + t[i], b, c);
    ret = min(ret, dfs(i + 1, a, b + t[i], c));
    ret = min(ret, dfs(i + 1, a, b, c + t[i]));
    return dp[i][a][b][c] = ret;
  };

  cout << dfs(0, 0, 0, 0) << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < n; (i)++)

int main() {
  int n;
  cin >> n;
  vector<int> t(n);
  rep(i, n) { cin >> t[i]; }
  map<int, map<int, map<int, int>>> dp[51];

  function<int(int, int, int, int)> dfs = [&](int i, int a, int b, int c) {
    if (i == n) {
      return max({a, b, c});
    }
    if (dp[i].count(a) && dp[i][a].count(b) && dp[i][a][b].count(c)) {
      return dp[i][a][b][c];
    }

    int ret = dfs(i + 1, a + t[i], b, c);
    ret = min(ret, dfs(i + 1, a, b + t[i], c));
    ret = min(ret, dfs(i + 1, a, b, c + t[i]));
    return dp[i][a][b][c] = ret;
  };

  cout << dfs(0, 0, 0, 0) << endl;
}
Problem p01868 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int dp[55][1055][1055];
int n, a[55], minx = 10000000;
int main() {
  for (int i = 0; i < 55; i++)
    for (int j = 0; j < 1113025; j++)
      dp[i][j / 1055][j % 1055] = 10000000;
  dp[0][0][0] = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k] + a[i]);
        dp[i + 1][j + a[i]][k] = min(dp[i + 1][j + a[i]][k], dp[i][j][k]);
        dp[i + 1][j][k + a[i]] = min(dp[i + 1][j][k + a[i]], dp[i][j][k]);
      }
    }
  }
  for (int i = 0; i < 1055; i++) {
    for (int j = 0; j < 1055; j++)
      minx = min(minx, max(i, max(j, dp[n][i][j])));
  }
  cout << minx << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int dp[55][1100][1100];
int n, a[55], minx = 10000000;
int main() {
  for (int i = 0; i < 55; i++)
    for (int j = 0; j < 1210000; j++)
      dp[i][j / 1100][j % 1100] = 10000000;
  dp[0][0][0] = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k] + a[i]);
        dp[i + 1][j + a[i]][k] = min(dp[i + 1][j + a[i]][k], dp[i][j][k]);
        dp[i + 1][j][k + a[i]] = min(dp[i + 1][j][k + a[i]], dp[i][j][k]);
      }
    }
  }
  for (int i = 0; i < 1100; i++) {
    for (int j = 0; j < 1100; j++)
      minx = min(minx, max(i, max(j, dp[n][i][j])));
  }
  cout << minx << endl;
  return 0;
}
Problem p01868 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
int N, S, T[50];
bool dp[51][834][1251];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &T[i]), S += T[i];
  dp[0][0][0] = true;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= S / 3; j++) {
      for (int k = 0; k <= S / 2; k++) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j - T[i - 1] >= 0)
          dp[i][j][k] |= dp[i - 1][j - T[i - 1]][k];
        if (k - T[i - 1] >= 0)
          dp[i][j][k] |= dp[i - 1][j][k - T[i - 1]];
      }
    }
  }
  int ret = 999999999;
  for (int i = 0; i <= S / 3; i++) {
    for (int j = 0; j <= S / 2; j++) {
      if (dp[N][i][j]) {
        ret = min(ret, max({i, j, S - i - j}));
      }
    }
  }
  printf("%d\\n", ret);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int N, S, T[50];
bool dp[51][2001][2001];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &T[i]), S += T[i];
  dp[0][0][0] = true;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= S; j++) {
      for (int k = 0; k <= S; k++) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j - T[i - 1] >= 0)
          dp[i][j][k] |= dp[i - 1][j - T[i - 1]][k];
        if (k - T[i - 1] >= 0)
          dp[i][j][k] |= dp[i - 1][j][k - T[i - 1]];
      }
    }
  }
  int ret = 999999999;
  for (int i = 0; i <= S; i++) {
    for (int j = 0; j <= S; j++) {
      if (dp[N][i][j]) {
        ret = min(ret, max({i, j, S - i - j}));
      }
    }
  }
  printf("%d\\n", ret);
  return 0;
}