#include <bits/stdc++.h>
using namespace std;
map<int, int> a;
multiset<long long> G;
int x, y, ed, st, n, M, i, j;
int b[500011];
long long ans;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    ans += x;
    a[x]++;
  }
  M = 0;
  for (map<int, int>::reverse_iterator it = a.rbegin(); it != a.rend(); it++) {
    x = it->first;
    y = it->second;
    ed = min(M, (M + y) / 2);
    st = max(0, ed - y);
    for (i = ed - 1; i >= st; i--) {
      if (i < G.size()) {
        b[i] = *G.begin();
        G.erase(G.begin());
      } else
        b[i] = 0;
    }
    for (i = st, j = M - i; i < j && i < ed; i++) {
      if (b[i] < x)
        b[i] = x;
      else if (--j < ed)
        b[j] = max(2 * x - b[i], 0);
    }
    for (i = st; i < ed; i++) G.insert(b[i]);
    M += y;
  }
  for (multiset<long long>::iterator it = G.begin(); it != G.end(); it++)
    ans -= *it;
  printf("%I64d\n", ans);
}
