#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 500010;
const long long oo = 1e16 + 10;
long long n;
long long a[MAXN];
struct D {
  long long cost, num;
  D() {}
  D(long long cost, long long num) : cost(cost), num(num) {}
};
vector<D> v;
bool cmp(const long long &a, const long long &b) { return a > b; }
priority_queue<int, vector<int>, greater<int> > q1, q2;
void fused() {
  while (!q2.empty()) {
    q1.push(q2.top());
    q2.pop();
  }
}
priority_queue<int, vector<int>, greater<int> > q3;
void work() {
  int N = v.size() - 1;
  int rest = v[1].num;
  for (int i = 2; i <= N; i++) {
    int cnt = v[i].num, c = v[i].cost;
    while (cnt && rest) {
      q2.push(c);
      cnt--;
      rest--;
    }
    while (cnt >= 2 && !q1.empty()) {
      int a = q1.top();
      if (c > a) {
        cnt -= 2;
        q1.pop();
        q2.push(c);
        q2.push(c);
      } else if (2 * c > a) {
        cnt -= 2;
        q1.pop();
        q2.push(a);
        q2.push(2 * c - a);
      } else
        break;
    }
    while (cnt && !q1.empty() && q1.top() < c) {
      q1.pop();
      q2.push(c);
      cnt--;
      rest++;
    }
    fused();
    rest += cnt;
  }
}
long long ans = 0;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i], ans += a[i];
  sort(a + 1, a + n + 1, cmp);
  a[0] = -oo;
  a[n + 1] = -oo;
  v.push_back(D(0, 0));
  long long cnt = 0;
  for (long long i = 1; i <= n; i++) {
    if (a[i] == a[i - 1]) {
      cnt++;
      continue;
    }
    if (cnt) v.push_back(D(a[i - 1], cnt));
    cnt = 1;
  }
  if (cnt) v.push_back(D(a[n], cnt));
  work();
  while (!q1.empty()) {
    ans -= q1.top();
    q1.pop();
  }
  cout << ans << endl;
  return 0;
}
