#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 500005;
priority_queue<ll, vector<ll>, greater<ll>> pq;
int n, a[N], b[N], cnt[N], idx, top, stk[N];
ll sum;
int main() {
  ios::sync_with_stdio(false);
  ll ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], ans += a[i];
  sort(a + 1, a + n + 1, [](int a, int b) { return a > b; });
  for (int i = 1; i <= n; ++cnt[idx], i++)
    if (i == 1 || a[i] != a[i - 1]) b[++idx] = a[i];
  for (int i = 1; i <= n; i++) {
    top = 0;
    ll mx = min(sum - ll(pq.size() * 2), ll(cnt[i]));
    for (int j = 1; j <= mx; j++) stk[++top] = b[i];
    mx = min(sum, ll(cnt[i])) - mx;
    for (int j = 1; j <= mx; j += 2) {
      ll t = pq.top();
      pq.pop();
      if (t < b[i]) {
        stk[++top] = b[i];
        if (j < mx) stk[++top] = b[i];
      } else {
        stk[++top] = t;
        if (j < mx && b[i] * 2 > t) stk[++top] = b[i] * 2 - t;
      }
    }
    for (int j = 1; j <= top; j++) pq.push(stk[j]);
    sum += cnt[i];
  }
  while (!pq.empty()) ans -= pq.top(), pq.pop();
  cout << ans << endl;
  return 0;
}
