#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000 + 10;
map<int, int> F;
int b[MAX];
int main() {
  int i;
  int n, a;
  scanf("%d", &n);
  long long ans = 0;
  for ((i) = (1); (i) <= (n); ++(i)) {
    scanf("%d", &a);
    ans += a;
    ++F[a];
  }
  int last = 0, sum = 0;
  multiset<int> hash;
  for (map<int, int>::reverse_iterator it = F.rbegin(); it != F.rend(); ++it) {
    int val = it->first;
    int count = it->second;
    int num = min(sum, count);
    int next = max(num, min(sum - last, num + last));
    int t = (sum - num) / 2;
    if (t <= last) next = max(next, min(sum - t, num + t));
    int now = next - num + 1;
    int tmp = min(num * 2, next);
    int u = next;
    for (; tmp; tmp--, u--) {
      if (u <= last) {
        b[u] = *hash.begin();
        hash.erase(hash.begin());
      } else
        b[u] = 0;
    }
    for (; now <= next && b[now] > val; ++now)
      ;
    if (now == next + 1)
      ;
    else {
      int be = now - 1;
      for (; now <= next; ++now) {
        int tot = val - b[now];
        if (sum - now + 1 != now) tot += val - b[sum - now + 1];
        if (now <= sum - be)
          b[now] = val;
        else
          b[now] = max(0, tot);
      }
    }
    for ((i) = (max(1, next - num * 2 + 1)); (i) <= (next); ++(i))
      hash.insert(b[i]);
    sum += count;
    last = next;
  }
  while (hash.size()) {
    ans -= *hash.begin();
    hash.erase(hash.begin());
  }
  cout << ans << endl;
  return 0;
}
