#include <bits/stdc++.h>
using namespace std;
int n, tot, sum, rnt;
int cnt[501000], s[501000], tmp[501000];
long long ans;
struct cmp1 {
  bool operator()(int a, int b) { return a > b; }
};
inline bool cmp(int a, int b) { return a > b; }
priority_queue<int, vector<int>, cmp1> q;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c > '9' || c < '0') c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x;
}
void pre() {
  n = read();
  for (register int i = 1; i <= n; i++) s[i] = read(), ans += s[i];
  sort(s + 1, s + n + 1, cmp);
  for (register int i = 1; i <= n; i++)
    if (s[i] != s[i + 1])
      s[++tot] = s[i], ++cnt[tot];
    else
      ++cnt[tot + 1];
}
long long t[501000], top;
inline void check() {
  top = 0;
  while (!q.empty()) t[++top] = q.top(), q.pop();
  for (int i = top; i; i--) printf("%lld ", t[i]), q.push(t[i]);
  if (top)
    printf("\n");
  else
    printf("0\n");
}
void work() {
  for (register int i = 1; i <= tot; i++) {
    int have = sum - 2 * q.size();
    have = min(have, cnt[i]);
    for (register int j = 1; j <= have; j++) tmp[++rnt] = s[i];
    have = min(sum, cnt[i]) - have;
    for (register int j = 1; j <= have; j += 2) {
      long long x = q.top();
      q.pop();
      if (x < s[i]) {
        tmp[++rnt] = s[i];
        if (j < have) tmp[++rnt] = s[i];
      } else {
        tmp[++rnt] = x;
        if (j < have && 2 * s[i] - x > 0) tmp[++rnt] = 2 * s[i] - x;
      }
    }
    for (; rnt; --rnt) q.push(tmp[rnt]);
    sum += cnt[i];
  }
  while (!q.empty()) ans -= q.top(), q.pop();
  printf("%lld\n", ans);
}
int main() {
  pre();
  work();
  fclose(stdin);
  return 0;
}
