#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n;
long long sum;
int main() {
  map<int, int> t;
  int x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    t[x]++;
    sum += x;
  }
  multiset<int> a;
  int total = 0;
  for (map<int, int>::reverse_iterator p = t.rbegin(); p != t.rend(); p++) {
    int value = p->first, num = p->second, last = a.size();
    int maxnum = min((total + num) / 2, min(total, num + last)),
        add = min(num, maxnum), left = total - last * 2;
    multiset<int>::iterator cur = a.begin();
    for (int i = 1; i <= last - maxnum + add; i++) cur++;
    vector<int> temp(a.begin(), cur);
    reverse(temp.begin(), temp.end());
    a.erase(a.begin(), cur);
    int tot = 0;
    for (int i = 1; i <= last - maxnum + add; i++)
      if (value > temp[i - 1]) {
        a.insert(value);
        left++;
      } else {
        tot = i;
        a.insert(temp[i - 1]);
      }
    for (int i = last + 1; i <= maxnum; i++)
      if (left) {
        a.insert(value);
        left--;
      } else {
        a.insert(value + value - temp[--tot]);
      }
    total += num;
  }
  for (multiset<int>::reverse_iterator i = a.rbegin(); i != a.rend(); i++)
    if (*i < 0)
      break;
    else
      sum -= *i;
  printf("%I64d\n", sum);
  return 0;
}
