1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <bits/stdc++.h> using namespace std;
using ll = long long; const int maxv = 1e5 + 50; const int maxn = 1e7 + 50;
int cnt[ maxv ]; int q1[ maxn ], h1 = 1, t1 = 0; ll q2[ maxn ], h2 = 1, t2 = 0;
ll get( ) { if( h1 > t1 ) return q2[ h2 ++ ]; if( h2 > t2 ) return q1[ h1 ++ ]; return q1[ h1 ] < q2[ h2 ] ? q1[ h1 ++ ] : q2[ h2 ++ ]; }
int main( ) { int n; scanf( "%d", &n ); for( int i = 1; i <= n; ++ i ) { int x; scanf( "%d", &x ); cnt[ x ] ++; }
for( int i = 1; i < maxv; ++ i ) { while( cnt[i] -- ) q1[ ++ t1 ] = i; }
ll ans = 0; for( int i = 1; i < n; ++ i ) { ll x = get( ); ll y = get( ); ans += x + y; q2[ ++ t2 ] = x + y; }
printf( "%lld\n", ans ); return 0; }
|