Problem p01743 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n;
  cin >> n;
  vector<ll> a;
  ll ans = 0;
  int sum = 0;
  rep(i, 0, n) {
    ll in;
    cin >> in;
    if (in > 4) {
      ans += in - 4;
      in = 4;
    }
    sum += in;
    a.push_back(in);
  }

  sort(a.rbegin(), a.rend());
  int tmp = min(sum - (n - 1), 11);
  int mini = tmp;
  vector<vector<int>> vs(tmp + 1);
  rep(i, 0, 1 << tmp) vs[__builtin_popcount(i)].push_back(i);
  ;
  function<void(int, vector<int>, int)> dfs = [&](int idx, vector<int> v,
                                                  int x) {
    if (idx == n) {
      mini = __builtin_popcount(x);
      return;
    }
    for (int i : vs[a[idx]]) {
      int x_ = x | i;
      if (__builtin_popcount(x_) >= mini)
        continue;
      bool ok = true;
      rep(j, 0, idx) {
        if (__builtin_popcount(i & v[j]) < 2)
          continue;
        ok = false;
        break;
      }
      if (!ok)
        continue;
      vector<int> w = v;
      w.push_back(i);
      dfs(idx + 1, w, x_);
    }
  };
  dfs(0, vector<int>(), 0);
  ans += mini;
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n;
  cin >> n;
  vector<ll> a;
  ll ans = 0;
  int sum = 0;
  rep(i, 0, n) {
    ll in;
    cin >> in;
    if (in > 4) {
      ans += in - 4;
      in = 4;
    }
    sum += in;
    a.push_back(in);
  }

  sort(a.rbegin(), a.rend());
  int tmp = min(sum - (n - 1), 10);
  int mini = tmp;
  vector<vector<int>> vs(tmp + 1);
  rep(i, 0, 1 << tmp) vs[__builtin_popcount(i)].push_back(i);
  ;
  function<void(int, vector<int>, int)> dfs = [&](int idx, vector<int> v,
                                                  int x) {
    if (idx == n) {
      mini = __builtin_popcount(x);
      return;
    }
    for (int i : vs[a[idx]]) {
      int x_ = x | i;
      if (__builtin_popcount(x_) >= mini)
        continue;
      bool ok = true;
      rep(j, 0, idx) {
        if (__builtin_popcount(i & v[j]) < 2)
          continue;
        ok = false;
        break;
      }
      if (!ok)
        continue;
      vector<int> w = v;
      w.push_back(i);
      dfs(idx + 1, w, x_);
    }
  };
  dfs(0, vector<int>(), 0);
  ans += mini;
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}