Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int c[14];
vector<int> res;
bool dfs(int left, int sum) {
  if (left == 0 && sum == 0)
    return true;

  for (int i = 1; i <= 13; ++i) {
    if (c[i] && sum % i == 0) {
      --c[i];
      if (dfs(left - 1, sum - i)) {
        res.push_back(i);
        return true;
      }
      ++c[i];
    }
  }

  return false;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    CLEAR(c, 0);
    res.clear();

    rep(i, n) {
      int t;
      scanf("%d", &t);
      ++c[t];
    }

    int s = 0;
    for (int i = 1; i <= 13; ++i)
      s += i * c[i];
    if (dfs(n, s)) {
      for (int i = 0; i < res.size(); ++i)
        printf("%d%c", res[i], i + 1 == res.size() ? '\\n' : ' ');
    } else
      puts("No");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int c[14];
vector<int> res;
bool dfs(int left, int sum) {
  if (left == 0)
    return true;

  for (int i = 1; i <= 13; ++i) {
    if (c[i] && sum % i == 0) {
      --c[i];
      if (dfs(left - 1, sum + i)) {
        res.push_back(i);
        return true;
      }
      ++c[i];
    }
  }
  return false;
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    CLEAR(c, 0);
    rep(i, n) {
      int t;
      scanf("%d", &t);
      ++c[t];
    }

    if (dfs(n, 0)) {
      reverse(all(res));
      for (int i = 0; i < res.size(); ++i)
        printf("%d%c", res[i], i + 1 == res.size() ? '\\n' : ' ');
    } else
      puts("No");
  }
}