#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
void update(int price, int Y, int S) {
  int X = q.size(), i, j;
  S = min(S, 2 * X + Y);
  X = min(X, S / 2);
  Y = min(Y, S);
  int cy = Y, cx = (S - cy) / 2;
  while (q.size() > X) q.pop();
  vector<int> removed;
  while (q.size() > cx) {
    int tmp = q.top();
    removed.push_back(tmp);
    q.pop();
  }
  reverse(removed.begin(), removed.end());
  int x = cx, y = 0;
  while (x + y < cx + cy) {
    if (y == 0 && x < X && removed[x - cx] > price) {
      q.push(removed[x - cx]);
      x++;
    } else if (y < Y && 2 * x + y < S) {
      q.push(price);
      y++;
    } else {
      q.push(2 * price - removed[x - cx - 1]);
      x--;
      y += 2;
    }
  }
}
map<int, int> mp;
int main(void) {
  int N, i, x;
  cin >> N;
  long long ans = 0;
  for ((i) = 0; (i) < (int)(N); (i)++) {
    scanf("%d", &x);
    mp[-x]++;
    ans += x;
  }
  int sum = 0;
  for (__typeof((mp).begin()) itr = (mp).begin(); itr != (mp).end(); itr++) {
    update(-(itr->first), (itr->second), sum);
    sum += (itr->second);
  }
  while (!q.empty()) {
    x = q.top();
    q.pop();
    if (x > 0) ans -= x;
  }
  cout << ans << endl;
  return 0;
}
