#include <bits/stdc++.h>
using namespace std;
map<int, int> p;
multiset<int> q;
int arr[500010];
int main() {
  int i, j, k, n, m;
  while (~scanf("%d", &n)) {
    long long sum = 0;
    p.clear();
    for (i = 0; i < n; i++) {
      scanf("%d", &j);
      p[j]++;
      sum += j;
    }
    int acc = 0, buff;
    q.clear();
    for (map<int, int>::reverse_iterator i = p.rbegin(); i != p.rend(); i++) {
      int val = i->first, num = i->second;
      buff = min(acc, (acc + num) / 2);
      for (j = buff - 1; j >= (buff > num ? buff - num : 0); j--)
        if (j < q.size()) {
          arr[j] = *q.begin();
          q.erase(q.begin());
        } else
          arr[j] = 0;
      for (j = (buff > num ? buff - num : 0), k = acc - j; j < k && j < buff;
           j++)
        if (arr[j] > val) {
          if (k - 1 < buff)
            arr[k - 1] = (2 * val - arr[j] > 0 ? 2 * val - arr[j] : 0);
          k--;
        } else
          arr[j] = val;
      q.insert(arr + (buff > num ? buff - num : 0), arr + buff);
      acc += num;
    }
    for (multiset<int>::iterator i = q.begin(); i != q.end(); i++) sum -= *i;
    printf("%I64d\n", sum);
  }
  return 0;
}
