#include <bits/stdc++.h>
using namespace std;
const int N = 500001;
int n, num[N], sum;
long long ans;
int a[N], b[N], cnt;
priority_queue<int, vector<int>, greater<int> > q;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), ans += a[i];
  sort(a + 1, a + n + 1, greater<int>());
  for (int i = 1; i <= n; i++) {
    if (i == 1 || a[i] != a[i - 1]) b[++cnt] = a[i];
    num[cnt]++;
  }
  for (int i = 1; i <= cnt; i++) {
    vector<int> s;
    int m = min((int)(sum - q.size() * 2), num[i]);
    for (int j = 1; j <= m; j++) s.push_back(b[i]);
    m = min(sum, num[i]) - m;
    for (int j = 1; j <= m; j += 2) {
      int u = q.top();
      q.pop();
      if (u < b[i]) {
        s.push_back(b[i]);
        if (j < m) s.push_back(b[i]);
      } else {
        s.push_back(u);
        if (j < m && 2 * b[i] > u) s.push_back(2 * b[i] - u);
      }
    }
    for (int j = 0; j < s.size(); j++) q.push(s[j]);
    sum += num[i];
  }
  while (!q.empty()) ans -= q.top(), q.pop();
  printf("%lld", ans);
  return 0;
}
