import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.PrintWriter; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { int n = cin.nextInt(); PriorityQueue < Long > Q = new PriorityQueue < > (); long tot = 0; while(n-- > 0) { Q.offer(cin.nextLong()); } while(Q.size() > 1) { long x = Q.peek(); Q.poll(); long y = Q.peek(); Q.poll(); tot += x + y; Q.offer(x + y); } cout.println(tot); cin.close(); cout.close(); } static Scanner cin = new Scanner(new BufferedInputStream(System.in)); static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out)); static PrintWriter cerr = new PrintWriter(System.err, true); }