Problem p03525 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

namespace {
/** whole range */
#define whole(xs) (xs).begin(), (xs).end()
/** unique vector(erase duplicates) */
#define uniq(xs) ((xs).erase(unique((xs).begin(), (xs).end()), (xs).end()))

/** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
template <typename T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  if (xs.empty())
    return os;
  os << xs[0];
  for (auto i = 1; i < xs.size(); i++)
    os << ' ' << xs[i];
  return os;
}

int N;
vector<int> D;
void input() {
  cin >> N;
  D.resize(N);
  for (int i = 0; i < N; i++)
    cin >> D[i];
  D.push_back(0);
}

void solve() {
  vector<int> count(13, 0);
  for (int i = 0; i < D.size(); i++) {
    count[D[i]]++;
  }
  vector<int> X;
  for (int i = 0; i < 13; i++) {
    if (count[i] >= 3) {
      cout << 0 << endl;
      return;
    }
  }
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < min(count[i], 2); j++) {
      X.push_back(i);
    }
  }
  sort(whole(X));
  auto n = X.size();
  auto ans = 0;

  auto f = [&](int bit) {
    vector<int> d(n, 0);
    for (int i = 0; i < n; i++) {
      if (bit & (1 << i)) {
        d[i] = X[i];
      } else {
        d[i] = 24 - X[i];
      }
      if (d[i] < ans)
        return;
      if (i >= 1) {
        int t = abs(d[i] - d[i - 1]);
        if (min(t, 24 - t) < ans)
          return;
      }
    }
    sort(whole(d));
    int ret = 12;
    for (int i = 0; i < n; i++) {
      int j = (i + 1) % n;
      auto t = abs(d[i] - d[j]);
      ret = min(ret, min(t, 24 - t));
      if (ret < ans)
        return;
    }
    ans = max(ans, ret);
  };

  for (int bit = 0; bit < (1 << n); bit++) {
    f(bit);
  }
  cout << ans << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

namespace {
/** whole range */
#define whole(xs) (xs).begin(), (xs).end()
/** unique vector(erase duplicates) */
#define uniq(xs) ((xs).erase(unique((xs).begin(), (xs).end()), (xs).end()))

/** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
template <typename T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  if (xs.empty())
    return os;
  os << xs[0];
  for (auto i = 1; i < xs.size(); i++)
    os << ' ' << xs[i];
  return os;
}

int N;
vector<int> D;
void input() {
  cin >> N;
  D.resize(N);
  for (int i = 0; i < N; i++)
    cin >> D[i];
  D.push_back(0);
}

void solve() {
  vector<int> count(13, 0);
  for (int i = 0; i < D.size(); i++) {
    count[D[i]]++;
  }
  vector<int> X;
  for (int i = 0; i < 13; i++) {
    if (count[i] >= 3) {
      cout << 0 << endl;
      return;
    }
    if (count[i] >= 2 && (i == 0 || i == 12)) {
      cout << 0 << endl;
      return;
    }
  }
  if (count[1] >= 1) {
    cout << 1 << endl;
    return;
  }
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < min(count[i], 2); j++) {
      X.push_back(i);
    }
  }
  sort(whole(X));
  auto n = X.size();
  auto ans = 0;

  auto f = [&](int bit) {
    vector<int> d(n, 0);
    for (int i = 0; i < n; i++) {
      if (bit & (1 << i)) {
        d[i] = X[i];
      } else {
        d[i] = 24 - X[i];
      }
      if (d[i] < ans)
        return;
      if (i >= 1) {
        int t = abs(d[i] - d[i - 1]);
        if (min(t, 24 - t) < ans)
          return;
      }
    }
    sort(whole(d));
    int ret = 12;
    for (int i = 0; i < n; i++) {
      int j = (i + 1) % n;
      auto t = abs(d[i] - d[j]);
      ret = min(ret, min(t, 24 - t));
      if (ret < ans)
        return;
    }
    ans = max(ans, ret);
  };

  for (int bit = 0; bit < (1 << n); bit++) {
    f(bit);
  }
  cout << ans << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}