#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int n;
priority_queue<int, vector<int>, greater<int> > pq;
map<int, int, greater<int> > mp;
map<int, int, greater<int> >::iterator it;
int F[N], A[N];
long long ans = 0;
void dbgout(priority_queue<int, vector<int>, greater<int> > q) {
  if (q.empty()) {
    puts("Queue is Empty");
    return;
  }
  while (q.empty() == false) {
    printf("%d ", q.top());
    q.pop();
  }
  puts("");
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    ans += a;
    mp[a]++;
  }
  int all = 0, free = 0, len;
  for (it = mp.begin(); it != mp.end(); ++it) {
    int val = it->first;
    int cur = it->second;
    int ct = min(all - 2 * free, cur);
    vector<int> up = vector<int>(ct, val);
    cur -= ct;
    while (pq.size() && cur) {
      if (pq.top() < val) {
        pq.pop();
        up.push_back(val);
        cur--;
        if (cur) {
          up.push_back(val);
          cur--;
        }
      } else if (pq.top() < 2 * val && cur > 1) {
        up.push_back(2 * val - pq.top());
        up.push_back(pq.top());
        pq.pop();
        cur -= 2;
      } else
        break;
    }
    for (int i = 0; i < up.size(); ++i) pq.push(up[i]);
    free = pq.size();
    all += it->second;
  }
  while (pq.size()) {
    ans -= pq.top();
    pq.pop();
  }
  cout << ans << endl;
  return 0;
}
