#include <bits/stdc++.h>
using namespace std;
int n, ls[500010], a[500010], cnt[500010];
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), ls[i] = a[i], ans += a[i];
  sort(ls + 1, ls + n + 1);
  int ex = unique(ls + 1, ls + n + 1) - ls;
  for (int i = 1; i <= n; i++) cnt[lower_bound(ls + 1, ls + ex, a[i]) - ls]++;
  int pre = 0;
  for (int i = ex - 1; i >= 0; i--) {
    int C = min(cnt[i], pre - 2 * (int)q.size());
    vector<long long> tmp;
    for (int j = C + 1; j <= cnt[i]; j += 2) {
      if (q.empty()) break;
      long long t = q.top();
      q.pop();
      if (t >= ls[i]) {
        tmp.push_back(t);
        if (j != cnt[i] && 2 * ls[i] > t) tmp.push_back(2 * ls[i] - t);
      } else {
        tmp.push_back(ls[i]);
        if (j != cnt[i]) tmp.push_back(ls[i]);
      }
    }
    for (int j = 1; j <= C; j++) q.push(ls[i]);
    for (auto &x : tmp) q.push(x);
    pre += cnt[i];
  }
  while (!q.empty()) ans -= q.top(), q.pop();
  printf("%lld\n", ans);
  return 0;
}
