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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
using pll = pair<ll, ll>;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vpii = vector<pii>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
vector<pair<ll, pll>> buf;
pll m;
void dfs(int f, int pos, int cnt, ll s) {
  if (buf.size() == pos) {
    m = max(m, pll(cnt, s));
    return;
  }
  if (buf.size() - pos + cnt <= m.first)
    return;
  if (!(f & buf[pos].second.first))
    dfs(f | 1 << pos, pos + 1, cnt + 1, s | 1ll << pos);
  dfs(f, pos + 1, cnt, s);
}
int main() {
  int n;
  cin >> n;
  vi in(n);
  for (auto &a : in)
    cin >> a;
  sort(ALL(in));
  ll fa = 1ll << 60;
  do {
    buf.clear();
    rep(i, n) {
      int a = in[i];
      m = pii();
      dfs(0, 0, 0, 0);
      ll f = m.second;
      ll s = 0;
      int k = 0;
      int mf = 0;
      for (int i = 0; 1ll << i <= f; i++)
        if (f & 1ll << i)
          if (buf[i].first > 1)
            mf |= 1ll << (buf.size() + k++);
          else
            mf |= 1ll << i;
      if (a > m.first)
        mf |= 1 << buf.size() + k;
      int l = 0;
      for (int i = 0; f >= 1ll << i && a; i++) {
        if (f & 1ll << i) {
          if (buf[i].first == 1) {
            buf[i].second.first |= mf;
          } else {
            buf.push_back(make_pair(
                1, pii(buf[i].second.first | mf, buf[i].second.second)));
            buf[i].first--;
            for (auto &a : buf)
              if (a.second.first & 1ll << i)
                a.second.first |= 1ll << buf.size() - 1;
            buf[i].second.first |= 1 << i;
          }
          a--;
        }
      }
      if (a) {
        buf.push_back(make_pair(a, pll(mf, buf.size())));
      }
    }
    ll ans = 0;
    for (auto &a : buf)
      ans += a.first;
    fa = min(fa, ans);
  } while (next_permutation(ALL(in)));
  cout << fa << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
using pll = pair<ll, ll>;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vpii = vector<pii>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
vector<pair<ll, pll>> buf;
pll m;
void dfs(int f, int pos, int cnt, ll s) {
  if (buf.size() == pos) {
    m = max(m, pll(cnt, s));
    return;
  }
  if (buf.size() - pos + cnt <= m.first)
    return;
  if (!(f & buf[pos].second.first))
    dfs(f | 1 << pos, pos + 1, cnt + 1, s | 1ll << pos);
  dfs(f, pos + 1, cnt, s);
}
int main() {
  int n;
  cin >> n;
  vi in(n);
  for (auto &a : in)
    cin >> a;
  sort(ALL(in));
  ll fa = 1ll << 60;
  do {
    rep(i, n) {
      int a = in[i];
      m = pii();
      dfs(0, 0, 0, 0);
      ll f = m.second;
      ll s = 0;
      int k = 0;
      int mf = 0;
      for (int i = 0; 1ll << i <= f; i++)
        if (f & 1ll << i)
          if (buf[i].first > 1)
            mf |= 1ll << (buf.size() + k++);
          else
            mf |= 1ll << i;
      if (a > m.first)
        mf |= 1 << buf.size() + k;
      int l = 0;
      for (int i = 0; f >= 1ll << i && a; i++) {
        if (f & 1ll << i) {
          if (buf[i].first == 1) {
            buf[i].second.first |= mf;
          } else {
            buf.push_back(make_pair(
                1, pii(buf[i].second.first | mf, buf[i].second.second)));
            buf[i].first--;
            buf[i].second.first |= 1 << i;
          }
          a--;
        }
      }
      if (a) {
        buf.push_back(make_pair(a, pll(mf, buf.size())));
      }
    }
    ll ans = 0;
    for (auto &a : buf)
      ans += a.first;
    fa = min(fa, ans);
  } while (next_permutation(ALL(in)));
  cout << fa << endl;
}