#include #include using namespace std; int main() { int n; cin >> n; priority_queue q; while (n--) { int x; cin >> x; q.push(-x); } int res = 0; while (q.size() > 1) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); res -= a + b; q.push(a + b); } printf("%d\n", res); return 0; }