inline LL read(){ LL x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; }
inlinevoidwrite(LL x){ if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); }
intmain(){ LL n, x; n = read(); vector<LL> cnt(MX); for (int i = 0; i < n; i++) { x = read(); cnt[x]++; } queue<LL> q1, q2; for (int i = 0; i < MX; i++) { for (int j = 0; j < cnt[i]; j++) { q1.push(i); } }
auto pop = [&]() { LL res = 0; if (q2.empty() || (!q1.empty() && q1.front() < q2.front())) { res = q1.front(); q1.pop(); } else { res = q2.front(); q2.pop(); } return res; };
LL ans = 0; while (q1.size() + q2.size() > 1) { LL x = pop(), y = pop(); ans += x + y; q2.push(x + y); } write(ans); return0; }