#include <bits/stdc++.h>
using namespace std;
long long n, s, nn, tp, ans, w[500100], b[500100], c[500100], stk[500100];
priority_queue<long long, vector<long long>, greater<long long> > Q;
inline bool cmp(long long u, long long v) { return u > v; }
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &w[i]), ans += w[i];
  sort(w + 1, w + n + 1, cmp);
  for (int i = 1; i <= n; i++, c[nn]++)
    if (i == 1 || w[i] != w[i - 1]) b[++nn] = w[i];
  for (int i = 1; i <= nn; s += c[i++]) {
    tp = 0;
    long long mx = min(s - 2 * (long long)Q.size(), c[i]);
    for (int j = 1; j <= mx; j++) stk[++tp] = b[i];
    mx = min(s, c[i]) - mx;
    for (int j = 1; j <= mx; j += 2) {
      long long x = Q.top();
      Q.pop();
      if (x < b[i]) {
        stk[++tp] = b[i];
        if (j < mx) stk[++tp] = b[i];
      } else {
        stk[++tp] = x;
        if (j < mx && 2 * b[i] > x) stk[++tp] = 2 * b[i] - x;
      }
    }
    for (int j = 1; j <= tp; j++) Q.push(stk[j]);
  }
  while (!Q.empty()) ans -= Q.top(), Q.pop();
  printf("%lld\n", ans);
  return 0;
}
