//见《挑战程序设计竞赛》第2.2.4节
#include<stdio.h>
#include<algorithm>
#define MAX_N 20000
using namespace std;
typedef long long ll;
int N;
int L[MAX_N];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", L + i);
ll ans = 0;
while (N > 1) {
int mii1 = 0, mii2 = 1;
if (L[mii1] > L[mii2]) swap(mii1, mii2);
for (int i = 2; i < N; i++) {
if (L[i] < L[mii1]) {
mii2 = mii1;
mii1 = i;
}
else if (L[i] < L[mii2]) {
mii2 = i;
}
}
int t = L[mii1] + L[mii2];
ans += t;
if (mii1 == N - 1) swap(mii1, mii2);
L[mii1] = t;
L[mii2] = L[N - 1];
N--;
}
printf("%lld\n", ans);
return 0;
}
//见《挑战程序设计竞赛》第2.4.2节
#include<cstdio>
#include<queue>
#include<functional>
using namespace std;
typedef long long ll;
int main() {
int N, L;
ll ans = 0;
priority_queue<int, vector<int>, greater<int> > que;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &L);
que.push(L);
}
while (que.size() > 1) {
int l1, l2;
l1 = que.top();
que.pop();
l2 = que.top();
que.pop();
ans += l1 + l2;
que.push(l1 + l2);
}
printf("%lld\n", ans);
getchar();
return 0;
}