#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch;
  for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    ;
  int x = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x;
}
const int N = 5e5 + 5;
multiset<int> s;
int n, cnt, a[N];
long long ans, c[N];
struct node {
  int x, y;
} b[N];
bool cmp(int x, int y) { return x > y; }
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  sort(a + 1, a + n + 1, cmp);
  for (int l = 1, r = 0; l <= n; l = r + 1) {
    while (r < n && a[r + 1] == a[l]) r++;
    b[++cnt].x = a[l];
    b[cnt].y = r - l + 1;
    ans += (long long)a[l] * (r - l + 1);
  }
  int now = 0, sz = 0;
  for (int i = 1; i <= cnt; i++) {
    int lst = sz;
    sz = min(now, (now + b[i].y) >> 1);
    int down = max(0, sz - b[i].y);
    for (int j = sz - 1; j >= down; j--)
      if (j < s.size()) {
        c[j] = *s.begin();
        s.erase(s.begin());
      } else
        c[j] = 0;
    int k = now - down;
    for (int j = down; j <= sz - 1; j++) {
      if (j >= k) break;
      if (c[j] < b[i].x)
        c[j] = b[i].x;
      else
        --k, c[k] = max(c[k], max((long long)0, 2 * b[i].x - c[j]));
    }
    for (int j = down; j <= sz - 1; j++) s.insert(c[j]);
    now += b[i].y;
  }
  for (multiset<int>::iterator i = s.begin(); i != s.end(); i++) ans -= *i;
  printf("%I64d\n", ans);
  return 0;
}
