#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e5;
int test, n, m, cnt;
multiset<int> heap;
long long ans;
int ins[N + 10];
int a[N + 10];
void Insert(int time, int key) {
  int i = 1;
  for (; i < time; i += 2) {
    if (!heap.size())
      ins[++cnt] = key, ins[++cnt] = key;
    else {
      int u = *heap.begin();
      if (key > u)
        ins[++cnt] = key, ins[++cnt] = key, heap.erase(heap.begin());
      else if (2 * key > u)
        ins[++cnt] = u, ins[++cnt] = 2 * key - u, heap.erase(heap.begin());
    }
  }
  if (i == time) {
    int u = *heap.begin();
    if (key > u) ins[++cnt] = key, heap.erase(heap.begin());
  }
  for (int i = 1; i <= cnt; ++i) heap.insert(ins[i]);
}
int main() {
  scanf("%d\n", &n);
  ans = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), ans += (long long)a[i];
  sort(a + 1, a + n + 1);
  for (int i = n; i >= 1; --i) {
    int j = i;
    for (; j >= 1 && a[j] == a[i]; --j)
      ;
    ++j;
    int add = min(n - i - 2 * (int)heap.size(), i - j + 1);
    for (int k = 1; k <= add; ++k) ins[k] = a[i];
    cnt = add;
    Insert(min(i - j + 1, n - i) - add, a[i]), i = j;
  }
  for (; heap.size();) {
    ans -= (long long)*heap.begin(), heap.erase(heap.begin());
  }
  cout << ans << endl;
  return 0;
}
