#include <bits/stdc++.h>
using namespace std;
int n, top, T, sum, k[500100];
long long ans, a[500100], heap[500100], sta[500100];
void down(int x) {
  for (int y = x + x, z = y + 1; y <= top;
       swap(heap[x], heap[y]), x = y, y = x + x, z = y + 1) {
    if (y < top && heap[z] < heap[y]) y = z;
    if (heap[x] <= heap[y]) break;
  }
}
void up(int x) {
  for (int y = x >> 1; y; swap(heap[x], heap[y]), x = y, y = x >> 1)
    if (heap[y] <= heap[x]) break;
}
void put(long long v) {
  heap[++top] = v;
  up(top);
}
long long get() {
  if (!top) return 0;
  long long r = heap[1];
  heap[1] = heap[top--];
  down(1);
  return r;
}
int main() {
  scanf("%d", &n);
  for (int i = 1, E = n; i <= E; i++) scanf("%lld", &a[i]), ans += a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1, E = n / 2; i <= E; i++) swap(a[i], a[n + 1 - i]);
  for (int i = 1, E = n; i <= E; i++)
    if (i == 1)
      k[n = 1] = 1;
    else if (a[i] ^ a[i - 1])
      k[++n] = 1, a[n] = a[i];
    else
      ++k[n];
  for (int i = 1, E = n; i <= E; i++) {
    int lim = min(k[i], sum - 2 * top);
    for (int j = 1, E = lim; j <= E; j++) sta[++T] = a[i];
    lim = min(k[i], sum) - lim;
    for (int j = 1; j <= lim; j += 2) {
      long long x = get();
      if (x < a[i]) {
        sta[++T] = a[i];
        if (j ^ lim) sta[++T] = a[i];
      } else {
        sta[++T] = x;
        if (a[i] * 2 > x && j ^ lim) sta[++T] = a[i] * 2 - x;
      }
    }
    sum += k[i];
    while (T) put(sta[T--]);
  }
  while (top) ans -= get();
  printf("%lld", ans);
  return 0;
}
