Skip to content

Instantly share code, notes, and snippets.

@alculquicondor
Created November 10, 2013 17:49
Show Gist options
  • Save alculquicondor/7401403 to your computer and use it in GitHub Desktop.
Save alculquicondor/7401403 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <set>
#include <cstring>
#define MAXN 60
using namespace std;
typedef long long ll;
ll A[MAXN];
multiset<ll> S;
void add(ll k, int q) {
while (q--) {
S.insert(k);
}
}
void join(int q) {
multiset<ll>::iterator it;
ll t;
while (q--) {
it = S.begin();
t = *it;
S.erase(it);
it = S.begin();
t += *it;
S.erase(it);
S.insert(t);
}
}
int main() {
ios::sync_with_stdio(0);
int n, x, maxi;
while(cin >> n) {
maxi = 0;
S.clear();
memset(A, 0, sizeof A);
for (int i = 0; i < n; ++i) {
cin >> x;
A[x] ++;
maxi = max(maxi, x);
}
add(1, A[maxi]);
int tmp;
for (int i = maxi-1; i >= 0; --i) {
//cout << "# " << i << " " << A[i] << " " << S.size() << endl;
tmp = *S.rbegin();
join(S.size() / 2);
add(tmp, A[i]);
//cout << "# " <<*S.rbegin() << endl;
}
cout << *S.begin() << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment