#include <bits/stdc++.h>   using namespace std;   int n, a[500005], tot, s[500005], c[500005], tmp[1500005], num;   priority_queue<int> q;   bool cmp(int x, int y) { return x > y; }   int main() {     long long ans = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), ans += a[i];     sort(a + 1, a + n + 1, cmp);     for (int i = 1; i <= n; i++) {       if (i == 1 || a[i] != a[i - 1])         s[++tot] = a[i], c[tot]++;       else         c[tot]++;     }     int sum = 0;     for (int i = 1; i <= tot; i++) {       int pt = min((int)(sum - (int)2 * q.size()), c[i]);       int rst = min(c[i] - pt, sum - pt);       num = 0;       for (int j = 1; j <= pt; j++) tmp[++num] = s[i];       for (int j = 1; j <= rst; j += 2) {         int k = -q.top();         q.pop();         if (k < s[i]) {           tmp[++num] = s[i];           if (j < rst) tmp[++num] = s[i];         } else {           tmp[++num] = k;           if (j < rst) tmp[++num] = 2 * s[i] - k;         }       }       for (int j = 1; j <= num; j++)         if (tmp[j] >= 0) q.push(-tmp[j]);       sum += c[i];     }     while (!q.empty()) ans += q.top(), q.pop();     printf( %lld n , ans);   }   