#include <bits/stdc++.h>
using namespace std;
int n, a[500001], maxn[500001], cnt[500001];
map<int, int> mp;
multiset<int> val;
long long ans;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    ans += a[i];
    ++mp[a[i]];
  }
  int pair = 0, res = cnt[0] = mp.rbegin()->second, k = 1;
  for (auto it = ++mp.rbegin(); it != mp.rend(); ++it) {
    cnt[k] = (cnt[k - 1] + it->second);
    if (res >= it->second) {
      pair += it->second;
      res -= it->second;
      maxn[k] = pair;
    } else {
      if (pair * 2 + res < it->second)
        pair = pair * 2 + res;
      else
        pair = cnt[k] / 2;
      res = cnt[k] - 2 * pair;
      maxn[k] = pair;
    }
    ++k;
  }
  res = cnt[0];
  k = 1;
  pair = 0;
  for (auto it = ++mp.rbegin(); it != mp.rend(); ++it) {
    int beg = max(0, maxn[k] - it->second);
    int ecnt = 0;
    int i = beg;
    for (; i < (int)val.size(); ++i) {
      if (it->first >= *val.begin()) {
        val.erase(val.begin());
        ++ecnt;
        --it->second;
        --i;
        --pair;
        ++res;
      } else
        break;
    }
    vector<int> tmp;
    for (auto itt = val.begin(); itt != val.end() && i < (int)val.size();
         ++itt) {
      tmp.push_back(*itt - it->first);
      ++i;
    }
    while (ecnt--) {
      val.insert(it->first);
      ++i;
      ++pair;
    }
    for (int sz = val.size(); it->second && i < sz + res && i < maxn[k]; ++i) {
      val.insert(it->first);
      --it->second;
      ++pair;
    }
    for (int j = 0; i < maxn[k] && it->second && j < (int)tmp.size(); ++i, ++j)
      if (it->first - tmp[j] >= 0) {
        val.insert(it->first - tmp[j]);
        --it->second;
        ++pair;
      }
    for (; i < maxn[k] && it->second; ++i) {
      val.insert(it->first);
      ++pair;
    }
    res = cnt[k] - 2 * pair;
    ++k;
  }
  for (auto it : val) ans -= it;
  printf("%lld\n", ans);
}
