func_code_string: #include <bits/stdc++.h>   using namespace std;   void rd(int &x) {     x = 0;     int f = 1;     char ch = (getchar());     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = (getchar());     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = (getchar());     x *= f;   }   const int N = 5e5 + 5;   int n, m;   int a[N], c[N], tmp[N];   priority_queue<int, vector<int>, greater<int> > Q;   int main() {     rd(n);     long long ans = 0;     for (int i = 1; i <= n; ++i) rd(a[i]), ans += a[i];     sort(a + 1, a + n + 1, greater<int>());     for (int i = 1; i <= n; ++i) {       int j = i;       a[++m] = a[i];       while (j <= n && a[j + 1] == a[i]) ++j;       c[m] = j - i + 1, i = j;     }     n = m;     int cnt = 0, tmp0 = 0;     for (int i = 1; i <= n; ++i) {       int res = min(cnt - 2 * (int)Q.size(), c[i]),           ok = min(c[i] - res, cnt - res);       tmp0 = 0;       for (int j = 1; j <= res; ++j) tmp[++tmp0] = a[i];       for (int j = 1; j <= ok; j += 2) {         int val = Q.top();         Q.pop();         if (val < a[i]) {           tmp[++tmp0] = a[i];           if (ok - j) tmp[++tmp0] = a[i];         } else {           tmp[++tmp0] = val;           if (ok - j && 2 * a[i] - val > 0) tmp[++tmp0] = 2 * a[i] - val;         }       }       for (int j = 1; j <= tmp0; ++j) Q.push(tmp[j]);       cnt += c[i];     }     while (!Q.empty()) ans -= Q.top(), Q.pop();     cout << ans;     return 0;   }   