#include <bits/stdc++.h>
using namespace std;
priority_queue<int, deque<int>, greater<int> > Q;
int a[500050], b[500050], c[500050];
int n, m;
int main() {
  long long ans;
  int i, j, tot, x, y, z;
  scanf("%d", &n);
  ans = 0;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += a[i];
  }
  sort(a + 1, a + n + 1);
  m = 0;
  for (i = 1; i <= n; i++) {
    if ((i == 1) || (a[i] != a[i - 1])) {
      m++;
      a[m] = a[i];
      b[m] = 1;
    } else
      b[m]++;
  }
  tot = 0;
  for (i = m; i >= 1; i--) {
    x = min(tot - int(Q.size() * 2), b[i]);
    for (j = 1; j <= x; j++) c[j] = a[i];
    z = x;
    x = min(tot, b[i]) - x;
    for (j = 1; j <= x; j += 2) {
      y = Q.top();
      Q.pop();
      if (y < a[i]) {
        z++;
        c[z] = a[i];
        if (j != x) {
          z++;
          c[z] = a[i];
        }
      } else {
        z++;
        c[z] = y;
        if ((j != x) && (2 * a[i] - y > 0)) {
          z++;
          c[z] = 2 * a[i] - y;
        }
      }
    }
    for (j = 1; j <= z; j++) Q.push(c[j]);
    tot += b[i];
  }
  while (!Q.empty()) {
    ans -= Q.top();
    Q.pop();
  }
  printf("%I64d\n", ans);
  return 0;
}
