code:
#include <bits/stdc++.h>
using namespace std;
int n, a, t[100010];
long long ans = 0;
queue<int> q1, q2;
int main (){
scanf ("%d", &n);
for (int i = 1; i <= n; i++)scanf ("%d", &a), t[a]++;
for (int i = 1; i <= 100010; i++){
for (int j = 1; j <= t[i]; j++)q1.push (i);
}
for (int i = 1; i <= n - 1; i++){
int x, y;
if (!q2.empty () && (q1.empty () || q1.front () > q2.front ()))x = q2.front (), q2.pop ();
else x = q1.front (), q1.pop ();
if (!q2.empty () && (q1.empty () || q1.front () > q2.front ()))y = q2.front (), q2.pop ();
else y = q1.front (), q1.pop ();
q2.push (x + y);
ans += x + y;
}
cout << ans;
return 0;
}
data:
P6033.in
8
37461 75360 46736 51097 66867 62178 96430 38866
P6033.out
1404882
record:here