#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long _x = 0;
  int _ch = getchar(), _f = 1;
  for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar())
    ;
  if (_ch == '-') {
    _f = 0;
    _ch = getchar();
  }
  for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0';
  return _f ? _x : -_x;
}
void write(long long _x) {
  if (_x >= 10)
    write(_x / 10), putchar(_x % 10 + '0');
  else
    putchar(_x + '0');
}
inline void wrt(long long _x, char _p) {
  if (_x < 0) putchar('-'), _x = -_x;
  write(_x);
  if (_p) putchar(_p);
}
int heap[500005], tot;
inline void push(int x) {
  heap[++tot] = x;
  push_heap(heap + 1, heap + 1 + tot, greater<int>());
}
inline void pop() {
  pop_heap(heap + 1, heap + 1 + tot, greater<int>());
  tot--;
}
inline int top() { return heap[1]; }
int n, v[500005], num[500005], a[500005], m;
int tmp[500005], tt;
long long ans = 0;
void In() {
  for (int i = int(1); i <= (int)(tt); i++) push(tmp[i]);
  tt = 0;
}
int main() {
  n = rd();
  for (int i = int(1); i <= (int)(n); i++) a[i] = rd(), ans += a[i];
  sort(a + 1, a + 1 + n);
  for (int i = int(n); i >= (int)(1); i--) {
    if (a[i] != a[i + 1])
      v[++m] = a[i], num[m] = 1;
    else
      num[m]++;
  }
  for (int i = 1, sum = 0; i <= m; sum += num[i], i++, In()) {
    int S = min(sum - tot * 2, num[i]);
    for (int j = int(1); j <= (int)(S); j++) tmp[++tt] = v[i];
    S = min(num[i], sum) - S;
    for (; S > 0; S -= 2) {
      int now = top();
      pop();
      if (now < v[i]) {
        tmp[++tt] = v[i];
        if (S > 1) tmp[++tt] = v[i];
      } else {
        tmp[++tt] = now;
        if (2 * v[i] > now && S > 1) tmp[++tt] = 2 * v[i] - now;
      }
    }
  }
  for (int i = int(1); i <= (int)(tot); i++) ans -= heap[i];
  wrt(ans, '\n');
}
