#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000 + 10;
map<int, int> count1;
int cmp[MAX];
int main() {
  long long base_cost = 0;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    base_cost += a;
    ++count1[a];
  }
  int last = 0, sum = 0;
  multiset<int> ans;
  for (map<int, int>::reverse_iterator it = count1.rbegin();
       it != count1.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) {
        cmp[u] = *ans.begin();
        ans.erase(ans.begin());
      } else
        cmp[u] = 0;
    }
    for (; now <= next && cmp[now] > val; ++now)
      ;
    if (now != next + 1) {
      int id = now - 1;
      for (; now <= next; ++now) {
        int tot = val - cmp[now];
        if (sum - now + 1 != now) tot += val - cmp[sum - now + 1];
        if (now <= sum - id)
          cmp[now] = val;
        else
          cmp[now] = max(0, tot);
      }
    }
    for (int i = max(1, next - num * 2 + 1); i <= next; i++) ans.insert(cmp[i]);
    sum += count;
    last = next;
  }
  while (ans.size()) {
    base_cost -= *ans.begin();
    ans.erase(ans.begin());
  }
  printf("%I64d\n", base_cost);
  return 0;
}
