#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  };
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
const int N = 5e5 + 5;
int n, tot, pre;
long long v[N];
bool mycmp(int x, int y) { return x > y; }
struct node {
  long long v;
  int num;
} a[N];
long long ans;
priority_queue<long long, vector<long long>, greater<long long> > q;
queue<long long> que;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) v[i] = read(), ans += v[i];
  sort(v + 1, v + n + 1, mycmp);
  a[++tot] = {v[1], 1};
  for (int i = 2; i <= n; i++) {
    if (v[i] == v[i - 1])
      a[tot].num++;
    else
      a[++tot] = {v[i], 1};
  }
  for (int i = 1; i <= tot; i++) {
    long long val = a[i].v;
    int num = a[i].num;
    int l = q.size() * 2;
    int t = max(min(pre - l, num), 0);
    for (int j = 1; j <= t; j++) que.push(val);
    for (int j = t; j < num && !q.empty(); j += 2) {
      long long x = q.top();
      q.pop();
      if (x < val) {
        que.push(val);
        if (j != num - 1) que.push(val);
      } else {
        que.push(x);
        if (j != num - 1 && val * 2 >= x) que.push(val * 2 - x);
      }
    }
    while (!que.empty()) {
      q.push(que.front());
      que.pop();
    }
    pre += num;
  }
  while (!q.empty()) {
    ans -= q.top();
    q.pop();
  }
  printf("%lld", ans);
  return 0;
}
