#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
priority_queue<int, vector<int>, greater<int> > Q;
long long ans;
int a[510000], b[510000], n, c[510000];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), ans += a[i];
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);
  Q.push(1 << 30);
  for (int i = 1; i <= n;) {
    int j;
    for (j = i; j <= n && a[i] == a[j]; ++j)
      ;
    int now = min(i - 1, (j - 1) / 2), l = now - min(j - i, now),
        r = Q.size() - 1;
    if (l && l - 1 + min(j - i, i - 1 - 2 * l + 2) == now) l--;
    while (Q.size() >= l + 1) {
      b[Q.size() - 1] = Q.top();
      Q.pop();
    }
    if (b[l] <= a[i]) {
      Q.push(b[l]);
      for (int k = l + 1; k <= now; ++k) Q.push(a[i]);
    } else {
      if (l + 1 + min(j - i, i - 1 - 2 * l - 2) == now) {
        Q.push(b[l]);
        l++;
      }
      for (int k = l; k <= now; ++k) c[k] = a[i];
      for (int k = l; k <= r; ++k)
        if (b[k] > a[i]) c[now - (k - l) + 1] = 2 * a[i] - b[k];
      for (int k = l; k <= now; ++k) Q.push(max(b[k], c[k]));
    }
    i = j;
  }
  while (!Q.empty()) ans -= Q.top(), Q.pop();
  cout << ans + (1 << 30) << endl;
}
