#include <bits/stdc++.h>
using namespace std;
int n;
long long a[500010], b[500010], c[500010], tp[500010], ct, s, r;
long long cmp(long long x, long long y) { return x > y; }
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    r += a[i];
  }
  sort(a + 1, a + n + 1, cmp);
  memcpy(b, a, sizeof(b));
  int p = unique(a + 1, a + n + 1) - a - 1;
  int t = 1;
  for (int i = 1; i <= p; i++)
    while (t <= n && a[i] <= b[t]) {
      c[i]++;
      t++;
    }
  for (int i = 1; i <= p; i++) {
    long long rm = s - 2 * q.size();
    rm = min(rm, c[i]);
    for (int j = 1; j <= rm; j++) tp[++ct] = a[i];
    rm = min(s, c[i]) - rm;
    for (int j = 1; j <= rm; j += 2) {
      long long x = q.top();
      q.pop();
      if (x < a[i]) {
        tp[++ct] = a[i];
        if (j != rm) tp[++ct] = a[i];
      } else {
        tp[++ct] = x;
        if (j != rm && 2 * a[i] > x) tp[++ct] = 2 * a[i] - x;
      }
    }
    while (ct) {
      q.push(tp[ct]);
      ct--;
    }
    s += c[i];
  }
  while (!q.empty()) {
    r -= q.top();
    q.pop();
  }
  printf("%lld", r);
}
