alexjercan/bugnet huggingface dataset:
Problem p00244 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
};
typedef pair<int, int> Pi;
const int INF = 1 << 30;

int n, m;
vector<edge> graph[200];
int min_cost[200];

int Dijkstra() {
  vector<int> min_cost(200, INF);
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  que.push(Pi(0, 0));
  min_cost[0] = 0;
  while (!que.empty()) {
    int now = que.top().second;
    int cost = que.top().first;
    que.pop();
    if (now % n == n - 1)
      return (cost);
    if (cost > min_cost[now])
      continue;
    for (int i = 0; i < graph[now].size(); i++) {
      edge e = graph[now][i];
      if (cost + e.cost < min_cost[e.to]) {
        min_cost[e.to] = cost + e.cost;
        que.push(Pi(min_cost[e.to], e.to));
      }
    }
  }
  return (-1);
}

int main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      --a, --b;
      graph[a].push_back((edge){b, c});
      graph[b].push_back((edge){a, c});
      graph[a + n].push_back((edge){b + n, c});
      graph[b + n].push_back((edge){a + n, c});
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < graph[i].size(); j++) {
        for (int k = 0; k < graph[graph[i][j].to].size(); k++) {
          int to = graph[graph[i][j].to][k].to;
          if (to < n && i != to)
            graph[i].push_back((edge){to + n, 0});
        }
      }
    }

    cout << Dijkstra() << endl;

    for (int i = 0; i < 200; i++) {
      graph[i].clear();
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
};
typedef pair<int, int> Pi;
const int INF = 1 << 30;

int n, m;
vector<edge> graph[200];
int min_cost[200];

int Dijkstra() {
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  fill_n(min_cost, n + n, INF);
  que.push(make_pair(0, 0));
  min_cost[0] = 0;
  while (!que.empty()) {
    Pi s = que.top();
    que.pop();
    if (s.second % n == n - 1)
      return (s.first);
    if (min_cost[s.second] < s.first)
      continue;
    for (auto e : graph[s.second]) {
      if (min_cost[e.to] > s.first + e.cost) {
        min_cost[e.to] = s.first + e.cost;
        que.push(make_pair(s.first + e.cost, e.to));
      }
    }
  }
  return (-1);
}

int main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      --a, --b;
      graph[a].push_back((edge){b, c});
      graph[b].push_back((edge){a, c});
      graph[a + n].push_back((edge){b + n, c});
      graph[b + n].push_back((edge){a + n, c});
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < graph[i].size(); j++) {
        for (int k = 0; k < graph[graph[i][j].to].size(); k++) {
          int to = graph[graph[i][j].to][k].to;
          if (i != to) {
            graph[i].push_back((edge){to + n, 0});
          }
        }
      }
    }

    cout << Dijkstra() << endl;

    for (int i = 0; i < 200; i++) {
      graph[i].clear();
    }
  }
}
Problem p00246 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<pair<int, long long>, int> dp;
vector<long long> e;

// int dp[51][51][51][51][2];

void gen_pattern(int pos, int r, long long v) {
  if (pos >= 10) {
    if (r == 0) {
      e.push_back(v);
    }
    return;
  }
  for (int i = 0; r - pos * i >= 0; i++) {
    gen_pattern(pos + 1, r - pos * i, v + ((long long)(i) << ((pos - 1) * 7)));
  }
}

int view(long long v) {
  for (int i = 0; i < 9; i++) {
    cout << (v >> (7 * i) & 127) << " ";
  }
  cout << endl;
}

int dfs(int pos, long long v) {
  if (pos >= e.size())
    return 0;
  if (dp.count(make_pair(pos, v)))
    return dp[make_pair(pos, v)];
  long long V = v;
  int ans = 0, c = 0;
  while (1) {
    ans = max(dfs(pos + 1, v) + c, ans);
    c++;
    bool f = true;
    for (int i = 0; i < 9; i++) {
      int s = v >> (7 * i) & 127;
      int x = e[pos] >> (7 * i) & 127;
      if (s < x) {
        f = false;
        break;
      }
    }
    if (!f)
      break;
    v -= e[pos];
  }
  v = V;
  return dp[make_pair(pos, V)] = ans;
}

int main() {
  int n;
  gen_pattern(1, 10, 0);
  while (cin >> n && n) {
    vector<char> v(10);
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      v[t]++;
    }
    int greedy = 0;
    for (int i = 1; i < 5; i++) {
      int u = min(v[i], v[10 - i]);
      greedy += u;
      v[i] -= u;
      v[10 - i] -= u;
    }
    greedy += v[5] / 2;
    v[5] %= 2;
    long long enc = 0;
    for (int i = 1; i < 10; i++) {
      enc += (long long)(v[i]) << (7 * (i - 1));
    }
    dp.clear();
    cout << dfs(0, enc) + greedy << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<pair<int, vector<char>>, int> dp;
vector<vector<char>> e;

// int dp[51][51][51][51][2];

int dfs(int pos, vector<char> &v) {
  if (pos >= e.size())
    return 0;
  if (dp.count(make_pair(pos, v)))
    return dp[make_pair(pos, v)];
  vector<char> V = v;
  int ans = 0, c = 0;
  while (1) {
    ans = max(dfs(pos + 1, v) + c, ans);

    c++;
    for (int i = 0; i < v.size(); i++)
      v[i] -= e[pos][i];

    bool f = true;
    for (int i = 0; i < v.size(); i++)
      if (v[i] < 0)
        f = false;
    if (!f)
      break;
  }
  v = V;
  return dp[make_pair(pos, V)] = ans;
}

vector<char> bt;
void gen_pattern(int pos, int r) {
  if (pos >= 10) {
    if (r == 0) {
      e.push_back(bt);
    }
    return;
  }
  for (int i = 0; r - pos * i >= 0; i++) {
    bt[pos] = i;
    gen_pattern(pos + 1, r - pos * i);
  }
}

int main() {
  int n;
  bt.resize(10);
  gen_pattern(1, 10);
  while (cin >> n && n) {
    vector<char> v(10);
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      v[t]++;
    }
    int greedy = 0;
    for (int i = 1; i < 5; i++) {
      int u = min(v[i], v[10 - i]);
      greedy += u;
      v[i] -= u;
      v[10 - i] -= u;
    }
    dp.clear();
    greedy += v[5] / 2;
    v[5] %= 2;
    cout << dfs(0, v) + greedy << endl;
  }
}
Problem p00246 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int a[11], da[43][11], dn[43], h, v, n, f, an, z;

int list(int n, int k, int g) {
  int i;
  if (k == 10 && n == h) {
    for (i = 0; i < h; i++)
      da[v][a[i]]++;
    dn[v] = h;
    v++;
  }
  if (n == h || k >= 10)
    return 1;
  for (i = g; i < 10; i++) {
    a[n] = i;
    list(n + 1, k + i, i);
  }
  return 0;
}

int dfs(int g, int u, int t) {
  int i, j, k;
  if (an < g)
    an = g;
  if (g + t / dn[u] <= an)
    return 0;
  for (i = u; i < 41; i++) {
    f = 0;
    for (j = 1; j < 10; j++) {
      a[j] -= da[i][j];
      if (a[j] < 0) {
        f = 1;
        for (k = j; k > 0; k--)
          a[k] += da[i][k];
        break;
      }
    }
    if (f == 0) {
      dfs(g + 1, i, t - dn[i]);
      for (j = 1; j < 10; j++)
        a[j] += da[i][j];
    }
  }
  return 0;
}

int main() {
  int i, j;
  v = 0;
  for (i = 0; i < 40; i++)
    for (j = 0; j < 11; j++)
      da[i][j] = 0;
  for (i = 2; i < 11; i++) {
    h = i;
    list(0, 0, 1);
  }
  while (cin >> n && n > 0) {
    for (i = 0; i < 11; i++)
      a[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> j;
      a[j]++;
    }
    v = 0;
    an = 0;
    while (v < 5) {
      while (true) {
        f = 0;
        for (i = 1; i < 10; i++)
          if (a[i] < da[v][i]) {
            f = 1;
            break;
          }
        if (f == 1)
          break;
        for (i = 1; i < 10; i++)
          a[i] -= da[v][i];
        an++;
        n -= 2;
      }
      v++;
    }
    dfs(an, 5, n);
    cout << an << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int a[11], da[43][11], dn[43], h, v, n, f, an, z;

int list(int n, int k, int g) {
  int i;
  if (k == 10 && n == h) {
    for (i = 0; i < h; i++)
      da[v][a[i]]++;
    dn[v] = h;
    v++;
  }
  if (n == h || k >= 10)
    return 1;
  for (i = g; i < 10; i++) {
    a[n] = i;
    list(n + 1, k + i, i);
  }
  return 0;
}

int dfs(int g, int u, int t) {
  int i, j, k;
  if (an < g)
    an = g;
  if (g + t / dn[u] <= an)
    return 0;
  for (i = u; i < 41; i++) {
    f = 0;
    for (j = 1; j < 10; j++) {
      a[j] -= da[i][j];
      if (a[j] < 0) {
        f = 1;
        for (k = j; k > 0; k--)
          a[k] += da[i][k];
        break;
      }
    }
    if (f == 0) {
      dfs(g + 1, i, t - dn[i]);
      for (j = 1; j < 10; j++)
        a[j] += da[i][j];
    }
  }
  return 0;
}

int main() {
  int i, j;
  v = 0;
  for (i = 0; i < 40; i++)
    for (j = 0; j < 11; j++)
      da[i][j] = 0;
  for (i = 2; i < 11; i++) {
    h = i;
    list(0, 0, 1);
  }
  while (cin >> n && n > 0) {
    for (i = 0; i < 11; i++)
      a[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> j;
      a[j]++;
    }
    v = 0;
    an = 0;
    dfs(0, 0, n);
    cout << an << endl;
  }
  return 0;
}
Problem p00246 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int N = 10;
map<vector<int>, int> memo;

vector<vector<int>> listup(int start, int sum, vector<int> list) {
  vector<vector<int>> result;
  if (sum == 0) {
    result.push_back(list);
    return result;
  }
  for (int i = start; i < N; ++i) {
    if (sum < i)
      continue;
    list.push_back(i);
    vector<vector<int>> add = listup(i, sum - i, list);
    result.insert(result.begin(), add.begin(), add.end());
    list.pop_back();
  }
  return result;
}

int dfs(const vector<vector<int>> &list, vector<int> count, int start) {
  if (memo.count(count))
    return memo[count];
  int result = 0;
  for (int i = start; i < list.size(); ++i) {
    bool ok = true;
    for (const auto &v : list[i])
      ok &= 0 <= --count[v];
    if (ok)
      result = max(result, 1 + dfs(list, count, i));
    for (const auto &v : list[i])
      ++count[v];
  }
  return memo[count] = result;
}

int main() {
  vector<vector<int>> list = listup(1, N, vector<int>());

  int n;
  while (cin >> n, n) {
    vector<int> manju(n);
    for (auto &v : manju)
      cin >> v;

    vector<int> count(11);
    for (const auto &v : manju)
      ++count[v];

    int answer = 0;

    for (int i = 1; i < 5; ++i)
      while (count[i] && count[10 - i]) {
        --count[i];
        --count[10 - i];
        ++answer;
      }
    while (count[5] > 2) {
      count[5] -= 2;
      ++answer;
    }

    memo.clear();

    cout << answer + dfs(list, count, 0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int N = 10;
map<vector<int>, int> memo;

vector<vector<int>> listup(int start, int sum, vector<int> list) {
  vector<vector<int>> result;
  if (sum == 0) {
    result.push_back(list);
    return result;
  }
  for (int i = start; i < N; ++i) {
    if (sum < i)
      continue;
    list.push_back(i);
    vector<vector<int>> add = listup(i, sum - i, list);
    result.insert(result.begin(), add.begin(), add.end());
    list.pop_back();
  }
  return result;
}

int dfs(const vector<vector<int>> &list, vector<int> count, int start) {
  if (memo.count(count))
    return memo[count];
  int result = 0;
  for (int i = start; i < list.size(); ++i) {
    bool ok = true;
    for (const auto &v : list[i])
      ok &= 0 <= --count[v];
    if (ok)
      result = max(result, 1 + dfs(list, count, i));
    for (const auto &v : list[i])
      ++count[v];
  }
  return memo[count] = result;
}

int main() {
  vector<vector<int>> list = listup(1, N, vector<int>());

  int n;
  while (cin >> n, n) {
    vector<int> manju(n);
    for (auto &v : manju)
      cin >> v;

    vector<int> count(11);
    for (const auto &v : manju)
      ++count[v];

    memo.clear();

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

#include <algorithm>
#include <cstdio>

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

using namespace std;

int ans; // ツ暫ツ津ィツ嘉ー
void dfs(int i, int sum, int *a, int now = 0) {
  // ツ篠ゥツ鳴セツづ按枝ツ環?づィ
  int rem = sum;
  for (int j = 1; j < 10; j++)
    rem += a[j] * j;
  if (now + rem / 10 <= ans)
    return;

  ans = max(ans, now);

  if (sum == 10) {
    dfs(1, 0, a, now + 1);
    return;
  }
  if (i >= 10)
    return;

  dfs(i + 1, sum, a, now);
  if (a[i] > 0 && sum + i <= 10) {
    a[i]--;
    dfs(i, sum + i, a, now);
    a[i]++;
  }
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    int a[10] = {};
    rep(i, n) {
      int b;
      scanf("%d", &b);
      a[b]++;
    }

    int sum = 0;
    for (int i = 1; i < 5; i++) {
      int c = min(a[i], a[10 - i]);
      sum += c;
      a[i] -= c;
      a[10 - i] -= c;
    }
    { // i==5
      sum += a[5] / 2;
      a[5] %= 2;
    }

    ans = -1;
    dfs(1, 0, a);
    printf("%d\\n", sum + ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>

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

using namespace std;

const int P[] = {-1,     999853, 999863, 999883, 999907,
                 999917, 999931, 999953, 999959, 999961}; // primes

map<int, int> dp[11][11];
int dfs(int i, int sum, int *a) {
  int h = 0;
  for (int j = 1; j < 10; j++)
    h += a[j] * P[j];
  if (dp[i][sum].count(h) > 0)
    return dp[i][sum][h];

  if (sum == 10) {
    return dp[i][sum][h] = 1 + dfs(1, 0, a);
  }
  if (i >= 10)
    return dp[i][sum][h] = 0;

  int res = dfs(i + 1, sum, a);
  if (a[i] > 0 && sum + i <= 10) {
    a[i]--;
    res = max(res, dfs(i, sum + i, a));
    a[i]++;
  }
  return dp[i][sum][h] = res;
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    int a[10] = {};
    rep(i, n) {
      int b;
      scanf("%d", &b);
      a[b]++;
    }

    int sum = 0;
    for (int i = 1; i < 5; i++) {
      int c = min(a[i], a[10 - i]);
      sum += c;
      a[i] -= c;
      a[10 - i] -= c;
    }
    { // i==5
      sum += a[5] / 2;
      a[5] %= 2;
    }
    a[9] = 0; // 9 ツつェツ単ツ禿?づ?残ツづ?づ?づ?使ツつ「ツ督ケツつェツづ按つ「

    rep(i, 11) rep(j, 11) dp[i][j].clear();
    printf("%d\\n", sum + dfs(1, 0, a));
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int n;
string a;
map<string, int> memo;

int rec(string v) {
  if (memo.find(v) != memo.end())
    return memo[v];

  int res = 0;
  for (int i = 8; i >= 1; i--) {
    if (v[i]) {
      if (v[0] + i == 10) {
        v[i]--;
        v[0] = 0;
        res = max(res, rec(v) + 1);
        v[i]++;
        v[0] = 10 - i;
      } else if (v[0] + i < 10) {
        v[i]--;
        v[0] += i;
        res = max(res, rec(v));
        v[i]++;
        v[0] -= i;
      }
    }
  }

  return memo[v] = res;
}

int main() {
  while (cin >> n, n) {
    a.resize(10);
    for (int i = 0; i < 10; i++)
      a[i] = 0;
    int tmp;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      a[tmp]++;
    }

    int res = 0;
    for (int i = 1; i <= 4; i++) {
      tmp = min(a[i], a[10 - i]);
      a[i] -= tmp;
      a[10 - i] -= tmp;
      res += tmp;
    }
    res += a[5] / 2;
    a[5] &= 1;

    memo.clear();
    cout << res + rec(a) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef vector<int> vi;

int n;
vi a(10);
map<vi, int> memo;

int rec(vi v) {
  if (memo.find(v) != memo.end())
    return memo[v];

  int res = 0;
  for (int i = 8; i >= 1; i--) {
    if (v[i]) {
      if (v[0] + i == 10) {
        v[i]--;
        v[0] = 0;
        res = max(res, rec(v) + 1);
        v[i]++;
        v[0] = 10 - i;
      } else if (v[0] + i < 10) {
        v[i]--;
        v[0] += i;
        res = max(res, rec(v));
        v[i]++;
        v[0] -= i;
      }
    }
  }

  return memo[v] = res;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < 10; i++)
      a[i] = 0;
    int tmp;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      a[tmp]++;
    }

    int res = 0;
    for (int i = 1; i <= 4; i++) {
      tmp = min(a[i], a[10 - i]);
      a[i] -= tmp;
      a[10 - i] -= tmp;
      res += tmp;
    }
    res += a[5] / 2;
    a[5] &= 1;

    memo.clear();
    cout << res + rec(a) << endl;
  }
}
Problem p00246 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> a;
void manju(vector<int> &b, int prev, int sum, const vector<int> &x) {
  if (sum > 10)
    return;
  if (sum == 10) {
    if (accumulate(b.begin(), b.end(), 0) > 2)
      a.push_back(b);
    return;
  }

  for (int i = prev; i <= 9; ++i) {
    if (x[i] == 0)
      continue;
    ++b[i];
    sum += i;
    manju(b, i, sum, x);
    sum -= i;
    --b[i];
  }
}

vector<map<vector<int>, int>> memo;
int solve(const vector<int> &x, int k) {
  if (k >= a.size())
    return 0;

  if (memo[k].find(x) != memo[k].end())
    return memo[k][x];

  int ret = solve(x, k + 1);

  vector<int> y = x;
  bool ok = true;
  for (int i = 1; i <= 9; ++i) {
    y[i] -= a[k][i];
    if (y[i] < 0)
      ok = false;
  }
  if (ok)
    ret = max(ret, solve(y, k) + 1);

  return memo[k][x] = ret;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    int ret = 0;
    vector<int> x(10, 0);
    for (int i = 0; i < n; ++i) {
      int y;
      cin >> y;
      if (x[10 - y] > 0) {
        --x[10 - y];
        ++ret;
      } else {
        ++x[y];
      }
    }

    a.clear();
    vector<int> b(10, 0);
    manju(b, 1, 0, x);

    memo.assign(a.size(), map<vector<int>, int>());
    ret += solve(x, 0);
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> a;
void manju(vector<int> &select) {
  int sum = accumulate(select.begin(), select.end(), 0);
  if (sum > 10)
    return;
  if (sum == 10) {
    vector<int> b(10, 0);
    for (int i = 0; i < select.size(); ++i)
      ++b[select[i]];
    a.push_back(b);
    return;
  }

  for (int i = (select.size() == 0 ? 1 : select.back()); i <= 9; ++i) {
    select.push_back(i);
    manju(select);
    select.pop_back();
  }
}

int solve(vector<int> x, int k) {
  if (k >= a.size())
    return 0;

  int ret = solve(x, k + 1);
  for (;;) {
    for (int i = 1; i <= 9; ++i) {
      x[i] -= a[k][i];
      if (x[i] < 0)
        return ret;
    }
    ret = max(ret, solve(x, k) + 1);
  }
}

int main() {
  vector<int> select;
  manju(select);

  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<int> x(10, 0);
    for (int i = 0; i < n; ++i) {
      int y;
      cin >> y;
      ++x[y];
    }

    cout << solve(x, 0) << endl;
  }
}
Problem p00246 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<vector<int>> T;
void init(int i, int s, vector<int> v) {
  if (s > 10)
    return;
  else if (i == 9) {
    if (s == 10)
      T.push_back(v);
    return;
  }
  for (int j = 0; j <= 10; j++) {
    v[i] = j;
    init(i + 1, s + (i + 1) * j, v);
  }
}

int n;
map<vector<int>, int> dp;

int dfs(vector<int> v, int sum) {
  if (sum < 10)
    return dp[v] = 0;
  if (dp.count(v))
    return dp[v];
  int res = 0;
  for (int i = 0; i < T.size(); i++) {
    vector<int> vv = v;
    bool f = true;
    for (int k = 0; k < 9; k++) {
      vv[k] -= T[i][k];
      if (vv[k] < 0)
        f = false;
    }
    if (!f)
      continue;
    if (sum / 10 <= res)
      continue;
    res = max(res, 1 + dfs(vv, sum - 10));
  }
  return dp[v] = res;
}

int main() {
  init(0, 0, vector<int>(9));

  while (scanf("%d", &n), n) {
    dp.clear();
    vector<int> v(9);

    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      v[--a]++;
    }

    int g = 0;
    for (int i = 0; i < 4; i++) {
      int t = min(v[i], v[8 - i]);
      g += t;
      v[i] -= t;
      v[8 - i] -= t;
    }
    g += v[4] / 2;
    v[4] %= 2;

    int sum = 0;
    for (int i = 0; i < 9; i++) {
      sum += (i + 1) * v[i];
    }

    printf("%d\\n", g + dfs(v, sum));
  }
}

Error: TLE

Fixed submission:

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

vector<vector<int>> T;
void init(int i, int s, vector<int> v) {
  if (s > 10)
    return;
  else if (i == 9) {
    if (s == 10)
      T.push_back(v);
    return;
  }
  for (int j = 0; j <= 10; j++) {
    v[i] = j;
    init(i + 1, s + (i + 1) * j, v);
  }
}

int n;
map<vector<int>, int> dp[50];

int dfs(int i, vector<int> v, int sum) {
  /*
  printf("%d", i);
  for (int j = 0; j < 9; j++) printf(" %d", v[j]);
  puts("");
  */

  if (sum < 10)
    return dp[i][v] = 0;

  if (i == T.size())
    return 0;
  if (dp[i].count(v))
    return dp[i][v];
  int res = 0;
  for (int j = 0, f = 1; f; j++) {
    if (j + sum / 10 > res) {
      res = max(res, j + dfs(i + 1, v, sum));
    }
    for (int k = 0; k < 9; k++) {
      v[k] -= T[i][k];
      if (v[k] < 0)
        f = 0;
    }
    sum -= 10;
  }
  return dp[i][v] = res;
}

int main() {
  init(0, 0, vector<int>(9));

  while (scanf("%d", &n), n) {
    for (int i = 0; i < 50; i++)
      dp[i].clear();

    vector<int> v(9);

    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      v[--a]++;
    }

    int g = 0;
    for (int i = 0; i < 4; i++) {
      int t = min(v[i], v[8 - i]);
      g += t;
      v[i] -= t;
      v[8 - i] -= t;
    }
    g += v[4] / 2;
    v[4] %= 2;

    int sum = 0;
    for (int i = 0; i < 9; i++) {
      sum += (i + 1) * v[i];
    }

    printf("%d\\n", g + dfs(0, v, sum));
  }
}