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

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

string s[10];
string dp[1 << 10][10];
int main() {
  int n;
  string inf;
  rep(i, 100) inf += 'z';
  while (scanf("%d", &n), n) {
    rep(i, n) cin >> s[i];
    rep(i, 1 << n) rep(j, n) dp[i][j] = inf;
    rep(i, n) dp[1 << i][i] = s[i];
    rep(i, 1 << n) {
      rep(j, n) {
        if (!(i >> j & 1))
          continue;
        if (dp[i][j] == inf)
          continue;
        rep(k, n) {
          if (i >> k & 1)
            continue;
          string t = dp[i | 1 << k][k];
          string p = dp[i][j];
          int d = s[k].size();
          while ((int)(p + s[k].substr(d)).find(s[k]) == -1)
            d--;
          p += s[k].substr(d);
          if (t.size() > p.size())
            dp[i | 1 << k][k] = p;
          else if (t.size() == p.size())
            dp[i | 1 << k][k] = min(dp[i | 1 << k][k], p);
        }
      }
    }
    string ans = inf;
    rep(i, n) {
      string t = dp[(1 << n) - 1][i];
      if (ans.size() > t.size())
        ans = t;
      else if (ans.size() == t.size())
        ans = min(ans, t);
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

string s[10];
int d[10][10], p[10];
string dp[1 << 10][10];
int main() {
  int n;
  while (scanf("%d", &n), n) {
    rep(i, n) cin >> s[i];
    rep(i, n) p[i] = i;
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      int k = min(s[i].size(), s[j].size());
      while (s[i].substr(s[i].size() - k) != s[j].substr(0, k))
        k--;
      d[i][j] = k;
    }
    rep(i, 1 << n) rep(j, n) rep(k, 1000) dp[i][j] += 'z';
    rep(i, n) dp[1 << i][i] = s[i];
    rep(i, 1 << n) {
      rep(j, n) {
        if (!(i >> j & 1))
          continue;
        rep(k, n) {
          if (i >> k & 1)
            continue;
          string t = dp[i | 1 << k][k];
          string p = dp[i][j] + s[k].substr(d[j][k]);
          if (t.size() > p.size())
            dp[i | 1 << k][k] = p;
          else if (t.size() == p.size())
            dp[i | 1 << k][k] = min(dp[i | 1 << k][k], p);
        }
      }
    }
    string ans;
    rep(i, 1000) ans += 'z';
    rep(i, n) {
      string t = dp[(1 << n) - 1][i];
      if (ans.size() > t.size())
        ans = t;
      else if (ans.size() == t.size())
        ans = min(ans, t);
    }
    cout << ans << endl;
  }
}
Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAXN = 11;
int N;
string S[MAXN];
string rem[MAXN][MAXN];
string mem[1 << MAXN][MAXN];

bool lt(const string &a, const string &b) {
  return a.size() < b.size() || a.size() == b.size() && a < b;
}

string rec(int used, int prev) {
  string &res = mem[used][prev];
  if (isalpha(res[0]))
    return res;
  if (used == (1 << N) - 1)
    return res = "";
  for (int i = 0; i < N; ++i) {
    if (used >> i & 1)
      continue;
    string tmp = rem[prev][i] + rec(used | (1 << i), i);
    if (lt(tmp, res))
      res = tmp;
  }
  return res;
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i < N; ++i)
      cin >> S[i];
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        if (i == j)
          continue;
        if (S[j].find(S[i]) != string::npos) {
          swap(S[i], S[--N]);
          --i;
          break;
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      rem[N][i] = S[i];
      for (int j = 0; j < N; ++j) {
        if (i == j)
          continue;
        for (int k = 0; k <= S[i].size(); ++k) {
          int l = S[i].size() - k;
          if (S[i].substr(k, l) == S[j].substr(0, l)) {
            rem[i][j] = "";
            while (l < S[j].size())
              rem[i][j] += S[j][l++];
            break;
          }
        }
      }
    }
    fill(mem[0], mem[1 << MAXN], string(1000, '#'));
    cout << rec(0, N) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int MAXN = 11;
int N;
string S[MAXN];

bool lt(const string &a, const string &b) {
  return a.size() < b.size() || a.size() == b.size() && a < b;
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i < N; ++i)
      cin >> S[i];
    sort(S, S + N);
    string ans(1000, ' ');
    do {
      try {
        string T = S[0];
        for (int i = 1; i < N; ++i) {
          if (lt(ans, T))
            throw 0;
          for (int j = 0; j <= T.size(); ++j) {
            if (j != T.size() && T[j] != S[i][0])
              continue;
            int len, k;
            if (T.size() < j + S[i].size()) {
              len = T.size() - j;
              k = T.size() - j;
            } else {
              len = S[i].size();
              k = S[i].size();
            }
            string a = T.substr(j, len);
            string b = S[i].substr(0, len);
            if (a == b) {
              while (k < S[i].size())
                T += S[i][k++];
              break;
            }
          }
        }
        if (lt(T, ans))
          ans = T;
      } catch (...) {
      }
    } while (next_permutation(S, S + N));
    cout << ans << endl;
  }
  return 0;
}
Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

string sss(vector<string> v) {
loop:
  REP(i, v.size()) {
    REP(j, v.size()) if (i != j) {
      if (string::npos != v[i].find(v[j])) {
        v.erase(v.begin() + j);
        goto loop;
      }
    }
  }

  int n = v.size();
  vector<vector<int>> same(n, vector<int>(n));

  REP(i, n) REP(j, n) {
    for (int ii = 0; ii <= v[i].size(); ii++) {
      bool ok = true;
      for (int jj = 0; ii + jj < v[i].size() && jj < v[j].size(); jj++) {
        if (v[i][ii + jj] != v[j][jj]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        same[i][j] = ii;
        break;
      }
    }
  }

  string ret = "";
  REP(i, n) ret += v[i];
  vector<int> pm(n);
  REP(i, n) pm[i] = i;

  do {
    int len = v[pm[0]].size();
    REP(i, n - 1) {
      int a = pm[i + 1];
      int b = pm[i];
      len += v[a].size() - (v[b].size() - same[b][a]);
    }

    if (len <= ret.size()) {
      string tmp = v[pm[0]];

      REP(i, n - 1) {
        int a = pm[i + 1];
        int b = pm[i];

        tmp += v[a].substr(v[b].size() - same[b][a]);
      }

      if (tmp.size() < ret.size())
        ret = tmp;
      else if (tmp.size() == ret.size()) {
        if (ret > tmp)
          ret = tmp;
      }
    }
  } while (next_permutation(pm.begin(), pm.end()));

  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> v(n);
    REP(i, n) cin >> v[i];
    cout << sss(v) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

string sss(vector<string> v) {
loop:
  REP(i, v.size()) {
    REP(j, v.size()) if (i != j) {
      if (string::npos != v[i].find(v[j])) {
        v.erase(v.begin() + j);
        goto loop;
      }
    }
  }

  int n = v.size();
  vector<vector<int>> same(n, vector<int>(n));

  REP(i, n) REP(j, n) {
    for (int ii = 0; ii <= v[i].size(); ii++) {
      bool ok = true;
      for (int jj = 0; ii + jj < v[i].size() && jj < v[j].size(); jj++) {
        if (v[i][ii + jj] != v[j][jj]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        same[i][j] = ii;
        break;
      }
    }
  }

  string ret = "";
  REP(i, n) ret += v[i];
  vector<int> pm(n);
  REP(i, n) pm[i] = i;

  do {
    string tmp = v[pm[0]];

    REP(i, n - 1) {
      int a = pm[i + 1];
      int b = pm[i];

      tmp += v[a].substr(v[b].size() - same[b][a]);
    }

    if (tmp.size() < ret.size())
      ret = tmp;
    else if (tmp.size() == ret.size()) {
      if (ret > tmp)
        ret = tmp;
    }
  } while (next_permutation(pm.begin(), pm.end()));

  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> v(n);
    REP(i, n) cin >> v[i];
    cout << sss(v) << endl;
  }
  return 0;
}
Problem p01150 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int n;
  while (cin >> n && n > 0) {
    if (n <= 16) {
      cout << 0 << endl;
      continue;
    }
    LL f = n;
    REP(i, 7) f *= i + 1;

    if (n % 2 == 0) {

      n /= 2;
      vector<LL> dp1(n);
      vector<LL> dp2(n);
      dp1[0] = 1;

      REP(_, 7) {
        vector<LL> next1(n);
        vector<LL> next2(n);
        REP(i, n) {
          if (dp1[i]) {
            for (int j = i + 2; j < n; j++) {
              next1[j] += dp1[i];
            }
            for (int j = i + 1; j < n; j++) {
              next2[j] += dp1[i];
            }
          }
          if (dp2[i]) {
            for (int j = i + 2; j < n; j++) {
              next2[j] += dp2[i];
            }
            for (int j = i + 1; j < n; j++) {
              next1[j] += dp2[i];
            }
          }
        }
        dp1.swap(next1);
        dp2.swap(next2);
      }
      LL ans = 0;
      REP(i, n - 1) ans += dp1[i];
      REP(i, n) ans += dp2[i];
      cout << f * ans << endl;
    } else {
      n--;
      vector<LL> dp(n);
      dp[0] = 1;
      REP(_, 7) {
        vector<LL> next(n);
        REP(i, n) if (dp[i]) {
          for (int j = i + 2; j < n; j++) {
            next[j] += dp[i];
          }
        }
        dp.swap(next);
      }
      LL ans = 0;
      REP(i, n) ans += dp[i];
      cout << f * ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int n;
  while (cin >> n && n > 0) {
    LL f = n;
    REP(i, 7) f *= i + 1;

    if (n % 2 == 0) {

      n /= 2;
      vector<LL> dp1(n);
      vector<LL> dp2(n);
      dp1[0] = 1;

      REP(_, 7) {
        vector<LL> next1(n);
        vector<LL> next2(n);
        REP(i, n) {
          if (dp1[i]) {
            for (int j = i + 2; j < n; j++) {
              next1[j] += dp1[i];
            }
            for (int j = i + 1; j < n; j++) {
              next2[j] += dp1[i];
            }
          }
          if (dp2[i]) {
            for (int j = i + 2; j < n; j++) {
              next2[j] += dp1[i];
            }
            for (int j = i + 1; j < n; j++) {
              next1[j] += dp1[i];
            }
          }
        }
        dp1.swap(next1);
        dp2.swap(next2);
      }
      LL ans = 0;
      REP(i, n - 1) ans += dp1[i];
      REP(i, n) ans += dp2[i];
      cout << f * ans << endl;
    } else {
      n--;
      vector<LL> dp(n);
      dp[0] = 1;
      REP(_, 7) {
        vector<LL> next(n);
        REP(i, n) if (dp[i]) {
          for (int j = i + 2; j < n; j++) {
            next[j] += dp[i];
          }
        }
        dp.swap(next);
      }
      LL ans = 0;
      REP(i, n) ans += dp[i];
      cout << f * ans << endl;
    }
  }
  return 0;
}
Problem p01150 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

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

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

ll _nCr[300][300];
ll nCr(int n, int r) {
  if (_nCr[n][r] != -1)
    return _nCr[n][r];
  return (r == 0 || n == r) ? 1
                            : (_nCr[n][r] = nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll _fact[300];

ll fact(ll n) {
  if (_fact[n] != -1)
    return _fact[n];
  return (n <= 1 ? 1 : (_fact[n] = n * fact(n - 1)));
}

struct Comparer {
  bool operator()(const bitset<71> &b1, const bitset<71> &b2) const {
    return b1.to_ulong() < b2.to_ulong();
  }
};

ll dp[101][10][2][2];
ll seats;

ll dfs(int pos, int used, bool here, bool opposite) {
  if (used >= 8)
    return 1;
  if (pos >= seats / 2)
    return 0;
  if (dp[pos][used][here][opposite] != -1)
    return dp[pos][used][here][opposite];
  if (pos + 1 == seats / 2)
    here = false;

  ll res = 0;
  res += dfs(pos + 1, used, true, true);
  if (here)
    res += dfs(pos + 1, used + 1, false, true);
  if (opposite)
    res += dfs(pos + 1, used + 1, true, false);

  return (dp[pos][used][here][opposite] = res);
}

int main() {

  memset(_nCr, -1, sizeof(_nCr));
  memset(_fact, -1, sizeof(_fact));
  while (~scanf("%lld", &seats)) {
    if (seats == 0)
      break;

    if (seats <= 16) {
      printf("%d\\n", 0);
    } else if (seats % 2 != 0) {
      printf("%lld\\n", seats * nCr(seats - 9, 7) * fact(7));
    } else {
      memset(dp, -1, sizeof(dp));
      printf("%lld\\n", seats * dfs(1, 1, false, true) * fact(7));
    }
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

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

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

ll nCr(int n, int r) {
  return (r == 0 || n == r) ? 1 : nCr(n - 1, r) + nCr(n - 1, r - 1);
}

ll fact(ll n) { return (n <= 1 ? 1 : n * fact(n - 1)); }

int main() {
  ll seats;
  while (~scanf("%lld", &seats)) {
    if (seats == 0)
      break;

    if (seats <= 16) {
      printf("%d\\n", 0);
    } else if (seats % 2 != 0) {
      printf("%lld\\n", seats * nCr(seats - 9, 7) * fact(7));
    } else {
      printf("%lld\\n", seats * nCr(seats - 16, 8) * fact(7));
    }
  }
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// bool ok[100];
int kind[14];

int n;
int num[100];
bool fin;
int mem[100];

const int MAX_PRIME = 1000;
void dfs(int sum, int cnt) {
  if (cnt == 1) {
    for (int i = 1; i < 14; i++) {
      if (kind[i] > 0) {
        mem[n - cnt] = i;
        break;
      }
    }
    fin = true;
    return;
  }
  for (int i = 1; i < 14; i++) {
    if (kind[i] == 0)
      continue;
    int nsum = sum - i;
    if (nsum % i == 0) {
      kind[i]--;
      mem[-cnt + n] = i;
      dfs(nsum, cnt - 1);
      if (fin)
        return;
      kind[i]++;
    }
  }
}
// void dfs(int sum,int cnt){
//     if(cnt==n){
//         fin=true;
//         return;
//     }
//     for(int i = 0; i < n; i++){
//         if(ok[i])
//             continue;
//         int nsum=sum+num[i];
//         if(sum%num[i]==0){
//             ok[i]=true;
//             mem[cnt]=num[i];
//             dfs(nsum,cnt+1);
//             if(fin)
//                 return;
//             ok[i]=false;
//         }
//     }
// }

int main() {
  while (cin >> n && n != 0) {
    int sum = 0;
    fin = false;
    fill(kind, kind + 14, 0);
    for (int i = 0; i < n; i++) {
      cin >> num[i];
      kind[num[i]]++;
      sum += num[i];
    }
    dfs(sum, n);
    if (fin) {
      for (int i = 0; i < n; i++) {
        cout << mem[n - 1 - i];
        if (i == n - 1)
          cout << endl;
        else
          cout << " ";
      }
    } else
      cout << "No" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

bool ok[100];
int n;
int num[100];
bool fin;
int mem[100];

const int MAX_PRIME = 1000;
void dfs(int sum, int cnt) {
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (!ok[i]) {
        mem[n - cnt] = num[i];
        break;
      }
    }
    fin = true;
    return;
  }
  for (int i = 0; i < n; i++) {
    if (ok[i])
      continue;
    int nsum = sum - num[i];
    if (nsum % num[i] == 0) {
      ok[i] = true;
      mem[-cnt + n] = num[i];
      dfs(nsum, cnt - 1);
      if (fin)
        return;
      ok[i] = false;
    }
  }
}
// void dfs(int sum,int cnt){
//     if(cnt==n){
//         fin=true;
//         return;
//     }
//     for(int i = 0; i < n; i++){
//         if(ok[i])
//             continue;
//         int nsum=sum+num[i];
//         if(sum%num[i]==0){
//             ok[i]=true;
//             mem[cnt]=num[i];
//             dfs(nsum,cnt+1);
//             if(fin)
//                 return;
//             ok[i]=false;
//         }
//     }
// }

int main() {
  while (cin >> n && n != 0) {
    int sum = 0;
    fin = false;
    for (int i = 0; i < n; i++) {
      cin >> num[i];
      sum += num[i];
    }
    fill(ok, ok + 100, false);
    dfs(sum, n);
    if (fin) {
      for (int i = 0; i < n; i++) {
        cout << mem[n - 1 - i];
        if (i == n - 1)
          cout << endl;
        else
          cout << " ";
      }
    } else
      cout << "No" << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
vector<int> a[365];
int b[14];
stack<int> c;
bool fi(int p) {
  int i;
  if (!p) {
    cout << c.top();
    c.pop();
    while (!c.empty()) {
      cout << " " << c.top();
      c.pop();
    }
    cout << endl;
    return true;
  }
  for (i = 0; i < (int)a[p].size(); ++i) {
    if (!b[a[p][i]])
      continue;
    --b[a[p][i]];
    c.push(a[p][i]);
    if (fi(p - a[p][i]))
      return true;
    c.pop();
    ++b[a[p][i]];
  }
  return false;
}

int main() {
  int i, j;
  // vector<int> a[365];
  for (i = 1; i < 365; ++i) {
    for (j = 1; j <= 13; ++j) {
      if (i < j || (i - j) % j)
        continue;
      a[i].push_back(j);
    }
  }
  int n;
  while (cin >> n, n) {
    // int b[14];
    memset(b, 0, sizeof(b));
    int sm = 0;
    for (i = 0; i < n; ++i) {
      int p;
      cin >> p;
      ++b[p];
      sm += p;
    }
    if (!fi(sm))
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[52], b[52], c[52], n;
int fn(int p, int q) {
  int i;
  if (p == n) {
    return 1;
  } else {
    for (i = 0; i < n; i++) {
      if (b[i] == 0 && q % a[i] == 0) {
        b[i] = -1;
        c[p] = a[i];
        if (fn(p + 1, q + a[i]))
          return 1;
        b[i] = 0;
      }
    }
    return 0;
  }
}
int main() {
  int i;
  while (cin >> n, n) {
    for (i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    memset(b, 0, sizeof(b));
    for (i = 0; i < n; i++) {
      b[i] = -1;
      c[0] = a[i];
      if (fn(1, a[i]))
        break;
      b[i] = 0;
    }
    if (i == n) {
      cout << "No" << endl;
    } else {
      for (i = 0; i < n - 1; i++)
        cout << c[i] << " ";
      cout << c[i] << endl;
    }
  }
  return 0;
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vi out, in;
int n;
bool f(int a, int t) {
  if (a == 0) {
    rep(i, n) {
      if (i)
        cout << " ";
      cout << out[n - 1 - i];
    }
    cout << endl;
    return true;
  }
  loop(i, 1, 14) if (in[i] && (a - i) % i == 0) {
    in[i]--;
    out[t] = i;
    if (f(a - i, t + 1))
      return true;
    in[i]++;
  }
  return false;
}
int main() {
  while (cin >> n, n) {
    out = vi(n);
    in = vi(14);
    int sum = 0;
    rep(i, n) {
      int a;
      cin >> a;
      sum += a;
      in[a]++;
    }
    if (!f(sum, 0))
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vi out, in, visit;
int n;
bool f(int a, int t) {
  if (a == 0) {
    rep(i, n) {
      if (i)
        cout << " ";
      cout << out[n - 1 - i];
    }
    cout << endl;
    return true;
  }
  rep(i, n) if (visit[i] == 0 && (a - in[i]) % in[i] == 0) {
    visit[i] = true;
    out[t] = in[i];
    if (f(a - in[i], t + 1))
      return true;
    visit[i] = false;
  }
  return false;
}
int main() {
  while (cin >> n, n) {
    in = out = visit = vi(n);
    rep(i, n) cin >> in[i];
    int sum = 0;
    rep(i, n) sum += in[i];
    if (!f(sum, 0))
      cout << "No" << endl;
  }
}