Problem:
m
頂点の完全グラフがある．最初，完全グラフの辺には色は塗られていない．すぬけ君は，各
i
(1 ≤
i
≤
n
) について次の操作を行った: 完全グラフから
a
i
個の頂点を選び，選ばれた頂点同士を結ぶ辺すべてを色
i
でぬる．複数個の色が塗られた辺はなかった．
m
として考えられる最小値を求めよ．
Constraints
1 ≤
n
≤ 5
2 ≤
a
i
≤ 10
9
Input
n
a
1
. . .
a
n
Output
m
の最小値を一行に出力せよ．
Sample Input 1
2
3
3
Sample Output 1
5
たとえば，頂点1, 2, 3, 4, 5 からなるグラフがあった場合，次のように色を塗ることができる．
頂点1, 2, 3 を選びその間を結ぶ辺を色1 でぬる．
頂点1, 4, 5 を選びその間を結ぶ辺を色2 でぬる．
Sample Input 2
5
2
3
4
5
6
Sample Output 2
12

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;cin >> n;
  vi a(n);cin >> a;
  if(n == 1){
    cout << a[0] << endl;
    return 0;
  }
  ll res = 0;
  rep(i,0,n)if(a[i] >= n-1)res += a[i]-(n-1),a[i] = n-1;
  vi msk(n);
  auto dfs = [&](auto &dfs,int m,int d)->bool{
    if(d == n)return true;
    rep(i,0,1 << m){
      if(__builtin_popcount(i) != a[d])continue;
      bool is = true;
      rep(j,0,d)if(__builtin_popcount(msk[j] & i) >= 2){is = false;break;}
      if(is){
        msk[d] = i;
        if(dfs(dfs,m,d+1))return true;
      }
    }
    return false;
  };
  rep(i,0,20)if(dfs(dfs,i,0)){
    cout << res + i << endl;
    break;
  }
  

  return 0;
}
accuracy: 1
time_ms: 1750
memory_kb: 3444
final_rank: 9

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// #ifndef ONLINE_JUDGE
// #define _GLIBCXX_DEBUG // 配列外参照のエラー
// #endif

// スタンダードライブラリ
#include <bits/stdc++.h>
using namespace std;

// 型関連
using ll = long long;
using lint = long long;
using pll = pair<ll, ll>;
using plll = tuple<ll, ll, ll>;
using pii = pair<int, int>;
using piii = tuple<ll, ll, ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;

template <class T> using prique = priority_queue<T>;
template <class T> void chmin(T &a, T b) {
    if (a > b)
        a = b;
}
template <class T> void chmax(T &a, T b) {
    if (a < b)
        a = b;
}

#define all(v) v.begin(), v.end()

// 型定数
constexpr ll mod = 998244353;
constexpr ll MOD = 1000000007;
int INF32 = 2e9;
ll INF64 = 9e18;
#define endl '\n';

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/////////////////////////// print関連
template <typename T> void print(vector<T> A);
void print();
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail);
void print(string S);
template <typename T> void print(vector<vector<T>> &F);

inline void Yes(bool f);

///////////////////ここから//////////////////////
bool solve() {
    int N;
    cin >> N;
    vector<ll> A(N);
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        if (A[i] > 4) {
            ans += A[i] - 4;
            A[i] = 4;
        }
    }

    if (N == 1) {
        print(A[0] + ans);
        return true;
    }
    vvi F(N, vi(10));
    

    vvi E(N, vi(N));

    int ans_base = 10;
    auto rec = [&](auto &rec, int idx) {
        int i = idx / 10;
        int j = idx % 10;
        if (j == 0) {
            int cnt = 0;
            for (int k = 0; k < 10; k++) {
                cnt += F[i - 1][k];
            }
            if (cnt != A[i - 1])
                return;
        }
        if (idx == N * 10) {
            int cnt = 0;
            for (int k = 0; k < 10; k++) {
                for (int l = 0; l < N; l++) {
                    if (F[l][k]) {
                        cnt++;
                        break;
                    }
                }
            }
            chmin(ans_base, cnt);
            return;
        }

        rec(rec, idx + 1);

        bool ok = true;
        unordered_set<int> se;
        for (int l = 0; l < N; l++) {
            if (F[l][j]) {
                se.insert(l);
                if (E[l][i]) {
                    ok = false;
                    break;
                }
            }
        }
        if (ok) {
            F[i][j] = 1;
            for (auto l : se) {
                E[i][l] = 1;
                E[l][i] = 1;
            }
            rec(rec, idx + 1);
            for (auto l : se) {
                E[i][l] = 0;
                E[l][i] = 0;
            }
            F[i][j] = 0;
        }
        return;
    };


    for (int i = 0; i < A[0]; i++) {
        F[0][i] = 1;
    }
    for (int i = 0; i < A[1] - 1; i++) {
        F[1][9 - i] = 1;
    }
    F[1][0] = 1;
    rec(rec, 20);

    F.assign(N, vector<int>(10, 0));
    for (int i = 0; i < A[0]; i++) {
        F[0][i] = 1;
    }
    for (int i = 0; i < A[1]; i++) {
        F[1][9 - i] = 1;
    }
    E.assign(N, vector<int>(N));
    rec(rec, 20);

    print(ans_base+ans);
    

    return true;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    solve();
}

////////////////////print/////////////////////////
// 1次元ベクトルを出力する
template <typename T> void print(vector<T> A) {
    if (A.size() == 0) {
        return;
    }
    for (size_t i = 0; i < A.size() - 1; i++) {
        cout << A[i] << ' ';
    }
    cout << A[A.size() - 1] << endl;
    return;
}

// 2次元ベクトルを出力する
template <typename T> void print(vector<vector<T>> &F) {
    for (size_t i = 0; i < F.size(); i++) {
        for (size_t j = 0; j < F[i].size(); j++) {
            cout << F[i][j];
            if (j < F[i].size() - 1) {
                cout << ' ';
            }
        }
        cout << endl;
    }
}

// 空白行を出力するprint
void print() { cout << endl; }

// 数値・文字列を空白区切りで出力する. print(a,b)など
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}

void print(string S) { cout << S << endl; }

//////////////出力関連

inline void Yes(bool f) {
    if (f) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 180
memory_kb: 3584
final_rank: 7

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001
long long ans = 80000000000;

void dfs(vector<long long> a,vector<int> t,vector<pair<pair<int,int>,pair<int,int>>> ng, long long S){
	//cout<<a.size()<<endl;
	if(a.size()==0){
		ans = min(ans,S);
		return;
	}
	long long A = a.back();
	a.pop_back();
	
	int maxi = 1;
	rep(i,t.size()){
		maxi *= (t[i]+1);
	}
	
	rep(i,maxi){
		
		vector<int> tt(t.size());
		long long temp = i;
		long long cnt = 0;
		rep(j,t.size()){
			tt[j] = temp%(t[j]+1);
			temp /= t[j]+1;
			if(tt[j]!=t[j])cnt++;
		}
		bool f = true;
		rep(j,ng.size()){
			if(tt[ng[j].first.first]==ng[j].first.second && tt[ng[j].second.first]==ng[j].second.second){
				f = false;
			}
		}
		if(!f)continue;
		long long need = A - cnt;
		if(need<0)continue;
		t.push_back(min((int)need,(int)a.size()));
		cnt = 0;
		rep(j,tt.size()){
			rep(k,tt.size()){
				if(j>=k)continue;
				if(tt[j]==t[j])continue;
				if(tt[k]==t[k])continue;
				ng.emplace_back(make_pair(j,tt[j]),make_pair(k,tt[k]));
				cnt++;
			}
		}
		
		rep(j,tt.size()){
			if(tt[j]==t[j])continue;
			rep(k,t.back()){
				ng.emplace_back(make_pair(j,tt[j]),make_pair(tt.size(),k));
				cnt++;
			}
		}
		dfs(a,t,ng,S + need);
		rep(j,cnt)tt.pop_back();
		t.pop_back();
	}
	
	
}

int main(){
	
	int n;
	cin>>n;
	
	vector<long long> a(n);
	rep(i,n)cin>>a[i];
	sort(a.begin(),a.end());
	
	do{
		
		vector<int> t;
		vector<pair<pair<int,int>,pair<int,int>>> ng;
		dfs(a,t,ng,0);
	}
	while(next_permutation(a.begin(),a.end()));
	
	cout<<ans<<endl;
	
	return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 3492
final_rank: 6

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/all>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

int main(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
    sort(all(a));
    ll ans = accumulate(all(a),0LL);
    rep(i,n) chmin(a[i],4);
    vvl b(n);
    int res = 0;
    int id = 0;
    vl bits;
	auto dfs = [&](auto &&dfs, int now, int now2) -> void {
        if(now == n){
            chmax(res,accumulate(all(a),0)-id);
            return;
        }
        if(now == now2){
            int sz = b[now].size();
            while(b[now].size() < a[now]) b[now].push_back(id++);
            int bit = 0;
            rep(i,a[now]) bit |= 1<<b[now][i];
            bits.push_back(bit);
            dfs(dfs,now+1,0);
            bits.pop_back();
            while(b[now].size() > sz) b[now].pop_back(), id--;
        }else{
            rep(k,a[now2]){
                if(b[now].size() < a[now]){
                    bool f = true;
                    for(auto x : b[now]) if(x == b[now2][k]) f = false;
                    b[now].push_back(b[now2][k]);
                    int bit = 0;
                    for(auto x : b[now]) bit |= 1<<x;
                    for(auto x : bits) if(pcount(x&bit) >= 2) f = false;
                    if(f) dfs(dfs,now,now2+1);
                    b[now].pop_back();
                }
            }
            dfs(dfs,now,now2+1);
        }
    };
    do{
        dfs(dfs,0,0);
    }while(next_permutation(all(a)));
    ans -= res;
    cout << ans << endl;
}
accuracy: 1
time_ms: 560
memory_kb: 3484
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

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

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------ End of template --------------

#define endl "\n"

int n;
int ans = INF;
int V = 0;
vector<ll> a;
vector<vector<int>> choose(5);
vector<int> belong(30);
vector<vector<int>> clique(5);

void efs(int, int);

void dfs(int id) {
  if (id == n) {
    chmin(ans, V);
    return;
  }
  choose[id].assign(id, -1);
  efs(0, id);
}

void efs(int id, int m) {
  if (id == m) {
    int cnt = 0;
    for (int i = 0; i < choose[m].size(); i++) {
      if (choose[m][i] != -1) cnt++;
      for (int j = i + 1; j < choose[m].size(); j++) {
        int p = choose[m][i], q = choose[m][j];
        if (p == -1 || q == -1) continue;
        if (popcount(belong[p] & belong[q]) > 0) return;
      }
    }

    if (cnt > a[m]) return;

    clique[m].clear();
    for (int v : choose[m]) {
      if (v != -1) clique[m].push_back(v);
    }
    int V_old = V;
    for (int i = 0; i < a[m] - cnt; i++) { clique[m].push_back(V++); }
    for (int v : clique[m]) belong[v] ^= (1 << m);
    dfs(m + 1);
    for (int v : clique[m]) belong[v] ^= (1 << m);
    V = V_old;
    return;
  }

  for (int i = -1; i < a[id]; i++) {
    if (i == -1)
      choose[m][id] = -1;
    else
      choose[m][id] = clique[id][i];
    efs(id + 1, m);
  }
}

void solve() {
  cin >> n;
  a.resize(n);
  cin >> a;
  sort(all(a));

  ll base = 0ll;
  for (int i = 0; i < n; i++) {
    if (a[i] > 5) {
      base += a[i] - 5;
      a[i] = 5;
    }
  }

  reverse(all(a));

  dfs(0);

  cout << ans + base << endl;

  return;
}

int main() {
  fastio();
  solve();
  // int t;
  // cin >> t;
  // while (t--) solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}
accuracy: 1
time_ms: 470
memory_kb: 3472
final_rank: 5

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-8, PI = acos(-1);
//ここから編集    
typedef string::const_iterator State;

int arr[101010];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(6);

    
    ll n; cin >> n;
    ll ans = 0;
    vector<ll> a(n);
    REP(i,n) cin >> a[i];


        set<pair<int, int>> st[10];
        for(int i=0; i<n; i++){
            if(i == 0){
                for(int j=1; j<=min(n,a[i]); j++) arr[j] |= (1<<i);
                ans += a[0];
            }else{
                int mask = 0;
                map<int, vector<int>> mp;
                for(int j=1; j<=100000LL; j++) {
                    if(arr[j] == 0) continue;
                    for(int k=(1<<5)-1; k>=0; k--){
                        if(k & arr[j]) continue;
                        
                        if(mp[k|arr[j]].size() < mp[k].size()+1){
                            mp[k|arr[j]] = mp[k];
                            mp[k|arr[j]].push_back(j);
                        }
                    }
                }
                ll t = 0, mx=0;
                for(auto e: mp){
                    if(mx < e.second.size()){
                        t = e.first;
                        mx = e.second.size();
                    }
                }

                ans += max(0LL,a[i]-mx);
                for(auto e: mp[t]){
                    
                    arr[e] |= (1<<i);
                }
                int tmp = max(0LL, a[i]-mx);
                for(int j=1; j<=100000; j++){
                    if(tmp == 0) break;
                    if(arr[j] == 0){
                        arr[j] |= (1<<i);
                        tmp--;
                    }
                }               
            }
        }
    
    cout << ans << endl;

    
    return 0;
}
accuracy: 0.05172413793103448
time_ms: 20
memory_kb: 3504
final_rank: 19

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

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

unsigned long long xor128() {
    static unsigned long long rx = 123456789, ry = 362436069, rz = 521288629, rw = 88675123;
    unsigned long long rt = (rx ^ (rx << 11));
    rx = ry;
    ry = rz;
    rz = rw;
    return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}

int n,a[5];
int A[5][5];

bool used[16][16];

int score(int m){
  int sum=0;
  unordered_set<int>st;
  
  for(int i=0;i<n;i++){
    for(int j=0;j<a[i];j++){
      for(int k=0;k<a[i];k++){
	if(j==k) continue;
	if(st.count((A[i][j]<<10)+A[i][k])) sum ++ ;
	else st.insert((A[i][j]<<10)+A[i][k]);
      }
    }
  }

  return sum;
}

bool can(int m){

  if(m<a[n-1]) return 0;

  for(int i=0;i<n;i++){
    for(int j=0;j<a[i];j++){
      A[i][j] = j;
    }
  }

  int cur = score(m);
  
  for(int i=0;i<10000;i++){
    int x = xor128()%n;
    int y = xor128()%a[x];
    int z = xor128()%m;
    if(A[x][y]==z){continue;}
    int f=0;
    for(int j=0;j<a[x];j++)if(A[x][j]==z){f=1;break;}
    if(f==1){continue;}

    int tmp = A[x][y];
    A[x][y] = z;
    int nex = score(m);
    if(nex<=cur){
      cur = nex;
      if(cur==0) return 1;
    }
    else{
      A[x][y] = tmp;
    }
  }
  return 0;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++) cin>>a[i];

  if(n==1){
    cout<<a[0]<<endl;
  }
  else if(n==2){
    cout<<a[0]+a[1]-1<<endl;
  }
  else if(n==3){
    cout<<a[0]+a[1]+a[2]-3<<endl;
  }
  else{
    sort(a,a+n);
    long long ans = 0;
    for(int i=0;i<n;i++){
      if(a[i]>4){
	ans += a[i]-4;
	a[i]=4;
      }
    }
    for(int i=1;i<=16;i++){
      //cout<<(can(i))<<' '<<i<<endl;
      if(can(i)){
	cout<<ans+i<<endl;
	break;
      }
    }
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3472
final_rank: 4

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	while (N < 5) {
		N++;
		v.push_back(0);
	}
	long long int ans = accumulate(v.begin(), v.end(), 0LL);
	vector<vector<vector<int>>>num(5);
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			num[0].push_back({ i, j });
		}
	}
	num[1].push_back({ 0, 1, 2 });
	for (int i = 0; i < 3; i++) {
		for (int j = 3; j < N; j++) {
			num[1].push_back({ i, j });
		}
	}
	num[1].push_back({ 3, 4 });
	num[2].push_back({ 0, 1, 2, 3 });
	for (int i = 0; i < 4; i++)num[2].push_back({ i, 4 });
	num[3].push_back({ 0, 1, 2, 3, 4 });
	num[4].push_back({ 0,1,2 });
	num[4].push_back({ 2,3,4 });
	num[4].push_back({ 0,3 });
	num[4].push_back({ 0,4 });
	num[4].push_back({ 1,3 });
	num[4].push_back({ 1,4 });
	sort(v.begin(), v.end());
	int minus = 0;
	bool flag =false;
	do {
		for (auto i : num) {
			if (i.size() == 10 && flag)continue;
		//	cout << i.size() << endl;
			flag = true;
			vector<int>p(i.size());
			for (int j = 0; j < i.size(); j++)p[j] = j;
			do {
				int box = 0;
				auto w = v;
				for (auto j : p) {
					bool ok = true;
					for (auto k : i[j]) {
						ok &= !!w[k];
					}
					if (ok) {
						box += i[j].size() - 1;
						for (auto k : i[j])w[k]--;
					}
				}
				minus = max(minus, box);
			} while (next_permutation(p.begin(), p.end()));
		}
	} while (next_permutation(v.begin(), v.end()));
	cout << ans - minus << endl;
}
accuracy: 1
time_ms: 520
memory_kb: 3244
final_rank: 3

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	while (N < 5) {
		N++;
		v.push_back(0);
	}
	long long int ans = accumulate(v.begin(), v.end(), 0LL);
	vector<vector<vector<int>>>num(4);
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			num[0].push_back({ i, j });
		}
	}
	num[1].push_back({ 0, 1, 2 });
	for (int i = 0; i < 3; i++) {
		for (int j = 3; j < N; j++) {
			num[1].push_back({ i, j });
		}
	}
	num[1].push_back({ 3, 4 });
	num[1].push_back({ 2,3,4 });
	num[2].push_back({ 0, 1, 2, 3 });
	for (int i = 0; i < 4; i++)num[2].push_back({ i, 4 });
	num[3].push_back({ 0, 1, 2, 3, 4 });
	sort(v.begin(), v.end());
	int minus = 0;
	bool flag =false;
	do {
		for (auto i : num) {
			if (i.size() == 10 && flag)continue;
		//	cout << i.size() << endl;
			flag = true;
			vector<int>p(i.size());
			for (int j = 0; j < i.size(); j++)p[j] = j;
			do {
				int box = 0;
				auto w = v;
				for (auto j : p) {
					bool ok = true;
					for (auto k : i[j]) {
						ok &= !!w[k];
					}
					if (ok) {
						box += i[j].size() - 1;
						for (auto k : i[j])w[k]--;
					}
				}
				minus = max(minus, box);
			} while (next_permutation(p.begin(), p.end()));
		}
	} while (next_permutation(v.begin(), v.end()));
	cout << ans - minus << endl;
}
accuracy: 0.034482758620689655
time_ms: 1390
memory_kb: 3172
final_rank: 20

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	while (N < 5) {
		N++;
		v.push_back(0);
	}
	long long int ans = accumulate(v.begin(), v.end(), 0LL);
	vector<vector<vector<int>>>num(4);
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			num[0].push_back({ i, j });
		}
	}
	num[1].push_back({ 0, 1, 2 });
	for (int i = 0; i < 3; i++) {
		for (int j = 3; j < N; j++) {
			num[1].push_back({ i, j });
		}
	}
	num[1].push_back({ 3, 4 });
	num[2].push_back({ 0, 1, 2, 3 });
	for (int i = 0; i < 4; i++)num[2].push_back({ i, 4 });
	num[3].push_back({ 0, 1, 2, 3, 4 });
	sort(v.begin(), v.end());
	int minus = 0;
	bool flag =false;
	do {
		for (auto i : num) {
			if (i.size() == 10 && flag)continue;
		//	cout << i.size() << endl;
			flag = true;
			vector<int>p(i.size());
			for (int j = 0; j < i.size(); j++)p[j] = j;
			do {
				int box = 0;
				auto w = v;
				for (auto j : p) {
					bool ok = true;
					for (auto k : i[j]) {
						ok &= !!w[k];
					}
					if (ok) {
						box += i[j].size() - 1;
						for (auto k : i[j])w[k]--;
					}
				}
				minus = max(minus, box);
			} while (next_permutation(p.begin(), p.end()));
		}
	} while (next_permutation(v.begin(), v.end()));
	cout << ans - minus << endl;
}
accuracy: 0.5862068965517241
time_ms: 520
memory_kb: 3244
final_rank: 11

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	long long int ans = accumulate(v.begin(), v.end(), 0LL);
	vector<pair<int, int>>num;
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			num.emplace_back(i, j);
		}
	}
	vector<int>p(num.size());
	for (int i = 0; i < num.size(); i++)p[i] = i;
	int minus = 0;
	do {
		auto w = v;
		int box = 0;
		for (auto i : p) {
			if (w[num[i].first] && w[num[i].second]) {
				w[num[i].first]--;
				w[num[i].second]--;
				box++;
			}
		}
		minus = max(minus, box);
	} while (next_permutation(p.begin(), p.end()));
	cout << ans - minus << endl;
}
accuracy: 0.27586206896551724
time_ms: 160
memory_kb: 3188
final_rank: 17

code_snippet: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define ALL(x) x.begin(), x.end()

using namespace std;

typedef long long ll;

bool LT(double a,double b) { return !equals(a,b) && a < b; }
bool LTE(double a,double b) { return equals(a,b) || a < b; }

int n;
vector<ll> a;
ll mini;

void dfs(int used, map<int,ll> rem, ll cost) {
  if( used == ((1<<n)-1) ) {
    mini = min(mini,cost);
    return;
  }
  if( cost >= mini ) return;
  rep(i,n) {
    if( (used>>i) & 1 ) continue;
    int nused = used | (1<<i);
    rep(j,(1<<n)) {
      if( ~used & j ) continue;
      map<int,ll> nrem = rem;
      ll ncost = cost;
      nrem[i] = a[i];
      ll coef = a[i];
      rep(k,n) {
	if( coef <= 0 ) break;
	if( ( ( j >> k ) & 1 ) && nrem[k] >= 1 ) {
	  --nrem[k];
	  --nrem[i];
	  --coef;
	}
      }
      assert( coef >= 0 );
      dfs(nused,nrem,ncost + coef);
    }
  }
}

/*
  3 3 4 3 3
  --
  4
  (4)
  --
  4 2
  (3) (2)
  --
  4 2 1
  (2) (1) (2)
  --
  4 2 1 0
  (1) (0) (1) (0)
  --
  4 2 1 0 1
  ()
  

 */

void compute() {
  mini = LLONG_MAX;
  map<int,ll> rem;
  dfs(0,rem,0);
  cout << mini << endl;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i,n) cin >> a[i];
  compute();
  return 0;
}
accuracy: 0.27586206896551724
time_ms: 20
memory_kb: 3120
final_rank: 13

code_snippet: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define ALL(x) x.begin(), x.end()

using namespace std;

typedef long long ll;

bool LT(double a,double b) { return !equals(a,b) && a < b; }
bool LTE(double a,double b) { return equals(a,b) || a < b; }

int n;
vector<ll> a;
ll mini;

void dfs(int used, map<int,ll> rem, ll cost) {
  if( used == ((1<<n)-1) ) {
    mini = min(mini,cost);
    return;
  }
  if( cost >= mini ) return;
  rep(i,n) {
    if( (used>>i) & 1 ) continue;
    rep(j,(1<<n)) {
      if( ~used & j ) continue;
      map<int,ll> nrem = rem;
      ll ncost = cost;
      nrem[i] = a[i];
      int nused = used | (1<<i);
      ll coef = a[i];
      rep(k,n) {
	if( coef <= 0 ) break;
	if( ( (used>>k) & 1 ) && nrem[k] >= 1 ) {
	  --nrem[k];
	  --nrem[i];
	  --coef;
	}
      }
      assert( coef >= 0 );
      dfs(nused,nrem,ncost + coef);
    }
  }
  
}

void compute() {
  mini = LLONG_MAX;
  map<int,ll> rem;
  dfs(0,rem,0);
  cout << mini << endl;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i,n) cin >> a[i];
  compute();
  return 0;
}
accuracy: 0.27586206896551724
time_ms: 30
memory_kb: 3116
final_rank: 12

code_snippet: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define ALL(x) x.begin(), x.end()

using namespace std;

typedef long long ll;

bool LT(double a,double b) { return !equals(a,b) && a < b; }
bool LTE(double a,double b) { return equals(a,b) || a < b; }

int n;
vector<ll> a;
ll mini;

void dfs(int used, map<int,ll> rem, ll cost) {
  if( used == ((1<<n)-1) ) {
    mini = min(mini,cost);
    return;
  }
  if( cost >= mini ) return;
  rep(i,n) {
    if( (used>>i) & 1 ) continue;
    rep(j,(1<<n)) {
      if( ~used & j ) continue;
      map<int,ll> nrem = rem;
      ll ncost = cost + a[i];
      nrem[i] = a[i];
      int nused = used | (1<<i);
      rep(k,n) {
	if( ( (used>>k) & 1 ) && nrem[k] >= 1 ) {
	  --nrem[k];
	  --nrem[i];
	  --ncost;
	}
      }
      dfs(nused,nrem,ncost);
    }
  }
  
}

void compute() {
  mini = LLONG_MAX;
  map<int,ll> rem;
  dfs(0,rem,0);
  cout << mini << endl;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i,n) cin >> a[i];
  compute();
  return 0;
}
accuracy: 0.27586206896551724
time_ms: 30
memory_kb: 3120
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

bool used[20][20];
void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(a.begin(), a.end(),greater<int>());
	ll ans = a[0];
	int d = a[0]; if (d >= 4)d = 4;
	int m = d;
	rep(i, m) {
		rep(j, m) {
			used[i][j] = true;
		}
	}
	rep1(i, n - 1) {
		int ma = 0; int chk = 0;
		rep(j, (1 << m)) {
			vector<int> v;
			rep(k, m) {
				if (j&(1 << k)) {
					v.push_back(k);
				}
			}
			bool f = true;
			rep(k, v.size()) {
				Rep(l, k + 1, v.size()) {
					if (used[v[k]][v[l]])f = false;
				}
			}
			if (f) {
				if (ma < v.size()) {
					ma = v.size();
					chk = j;
				}
			}
		}
		vector<int> v;
		rep(j, m) {
			if (chk&(1 << j))v.push_back(j);
		}
		if (a[i] - ma < 0) {
			rep(j, ma - a[i]) {
				v.pop_back();
			}
			ma = v.size();
		}
		ll dif = a[i] - ma;
		ans += dif;
		if (dif >= 4)dif = 4;
		
		Rep(i, m, m + dif) {
			v.push_back(i);
		}
		m += dif;
		rep(j, v.size()) {
			rep(k, v.size()) {
				used[v[j]][v[k]] = true;
			}
		}
	}
	cout << ans << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3200
final_rank: 2

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

bool used[20][20];
void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(a.begin(), a.end(),greater<int>());
	ll ans = a[0];
	int d = a[0]; if (d >= 4)d = 4;
	int m = d;
	rep(i, m) {
		rep(j, m) {
			used[i][j] = true;
		}
	}
	rep1(i, n - 1) {
		int ma = 0; int chk = 0;
		rep(j, (1 << m)) {
			vector<int> v;
			rep(k, m) {
				if (j&(1 << k)) {
					v.push_back(k);
				}
			}
			bool f = true;
			rep(k, v.size()) {
				Rep(l, k + 1, v.size()) {
					if (used[v[k]][v[l]])f = false;
				}
			}
			if (f) {
				if (ma < v.size()) {
					ma = v.size();
					chk = j;
				}
			}
		}
		vector<int> v;
		rep(j, m) {
			if (chk&(1 << j))v.push_back(j);
		}
		if (a[i] - ma < 0) {
			rep(i, ma - a[i]) {
				v.pop_back();
			}
			ma = v.size();
		}
		ll dif = a[i] - ma;
		ans += dif;
		if (dif >= 4)dif = 4;
		
		Rep(i, m, m + dif) {
			v.push_back(i);
		}
		m += dif;
		rep(j, v.size()) {
			rep(k, v.size()) {
				used[v[j]][v[k]] = true;
			}
		}
	}
	cout << ans << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 0.15517241379310345
time_ms: 10
memory_kb: 3124
final_rank: 18

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

bool used[20][20];
void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(a.begin(), a.end());
	ll ans = a[0];
	int d = a[0]; if (d >= 4)d = 4;
	int m = d;
	rep(i, m) {
		rep(j, m) {
			used[i][j] = true;
		}
	}
	rep1(i, n - 1) {
		int ma = 0; int chk = 0;
		rep(j, (1 << m)) {
			vector<int> v;
			rep(k, m) {
				if (j&(1 << k)) {
					v.push_back(k);
				}
			}
			bool f = true;
			rep(k, v.size()) {
				Rep(l, k + 1, v.size()) {
					if (used[v[k]][v[l]])f = false;
				}
			}
			if (f) {
				if (ma < v.size()) {
					ma = v.size();
					chk = j;
				}
			}
		}
		vector<int> v;
		rep(j, m) {
			if (chk&(1 << j))v.push_back(j);
		}
		if (a[i] - ma < 0) {
			rep(i, ma - a[i]) {
				v.pop_back();
			}
			ma = v.size();
		}
		ll dif = a[i] - ma;
		ans += dif;
		if (dif >= 4)dif = 4;
		
		Rep(i, m, m + dif) {
			v.push_back(i);
		}
		m += dif;
		rep(j, v.size()) {
			rep(k, v.size()) {
				used[v[j]][v[k]] = true;
			}
		}
	}
	cout << ans << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 0.27586206896551724
time_ms: 10
memory_kb: 3200
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

bool used[20][20];
void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(a.begin(), a.end());
	ll ans = a[0];
	int d = a[0]; if (d >= 4)d = 4;
	int m = d;
	rep(i, m) {
		rep(j, m) {
			used[i][j] = true;
		}
	}
	rep1(i, n - 1) {
		int ma = 0; int chk = 0;
		rep(j, (1 << m)) {
			vector<int> v;
			rep(k, m) {
				if (j&(1 << k)) {
					v.push_back(k);
				}
			}
			bool f = true;
			rep(k, v.size()) {
				Rep(l, k + 1, v.size()) {
					if (used[v[k]][v[l]])f = false;
				}
			}
			if (f) {
				if (ma < v.size()) {
					ma = v.size();
					chk = j;
				}
			}
		}
		ll dif = max(0, a[i] - ma);
		ans += dif;
		if (dif >= 4)dif = 4;
		vector<int> v;
		rep(j, m) {
			if (chk&(1 << j))v.push_back(j);
		}
		Rep(i, m, m + dif) {
			v.push_back(i);
		}
		m += dif;
		rep(j, v.size()) {
			rep(k, v.size()) {
				used[v[j]][v[k]] = true;
			}
		}
	}
	cout << ans << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 0.27586206896551724
time_ms: 10
memory_kb: 3200
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,aa[5];
ll sum,ans;
int main(){
	cin>>n;
	for(int i=0;i<n;i++) cin>>aa[i],sum+=aa[i];
	{
		vector<int>M,N;
		for(int j=0;j<(1<<n);j++){
			if(__builtin_popcount(j) >= 2 && __builtin_popcount(j) <= 3) M.pb(j);
			else if(__builtin_popcount(j) >= 4) N.pb(j);
		}
		int sz = M.size();
		for(int j=0;j<(1<<sz);j++){
			vector<int>vec; int cnt[5]={}; ll C=0;
			rep(k,sz) if(((j>>k)&1)) vec.pb(M[k]);
			rep(k,vec.size())for(int a=k+1;a<vec.size();a++){
				int v = (vec[k]&vec[a]);
				if(__builtin_popcount(v) >= 2) goto fail;
			}
			rep(k,vec.size()){
				rep(a,n) if(((vec[k]>>a)&1)) cnt[a]++;
			}
			rep(a,n) if(cnt[a] > aa[a]) goto fail;
			rep(a,n) C += cnt[a];
			C -= vec.size();
			ans = max(ans,C);
			for(int k=0;k<N.size();k++){
				bool ok = 1;
				rep(a,n) if(((N[k]>>a)&1)&&cnt[a]>=aa[a]) ok=0;
				if(!ok) continue;
				rep(kk,vec.size()) if(__builtin_popcount(vec[kk]&N[k])>=2) ok=0;
				if(!ok) continue;
				ll D = C;
				D += __builtin_popcount(N[k])-1;
				ans = max(ans,D);
			}
			fail:;
		}
	}
	cout<<sum-ans<<endl;
}
accuracy: 1
time_ms: 190
memory_kb: 3144
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,aa[5];
ll sum,ans;
int main(){
	cin>>n;
	for(int i=0;i<n;i++) cin>>aa[i],sum+=aa[i];
	{
		vector<int>M;
		for(int j=0;j<(1<<n);j++){
			if(__builtin_popcount(j) >= 2 && __builtin_popcount(j) <= 3) M.pb(j);
		}
		int sz = M.size();
		for(int j=0;j<(1<<sz);j++){
			vector<int>vec; int cnt[5]={}; ll C=0;
			rep(k,sz) if(((j>>k)&1)) vec.pb(M[k]);
			rep(k,vec.size())for(int a=k+1;a<vec.size();a++){
				int v = (vec[k]&vec[a]);
				if(__builtin_popcount(v) >= 2) goto fail;
			}
			rep(k,vec.size()){
				rep(a,n) if(((vec[k]>>a)&1)) cnt[a]++;
			}
			rep(a,n) if(cnt[a] > aa[a]) goto fail;
			rep(a,n) C += cnt[a];
			C -= vec.size();
			ans = max(ans,C);
			fail:;
		}
	}
	cout<<sum-ans<<endl;
}
accuracy: 0.896551724137931
time_ms: 190
memory_kb: 3144
final_rank: 10