import java.util.*;import java.io.*;import java.math.*;import java.text.* ;publicclassMain{publicstaticvoidmain(String[] args){try{
BufferedReader br =newBufferedReader(newInputStreamReader(System.in));
String str;while((str = br.readLine())!= null){int n = Integer.parseInt(str);
String[] parts = br.readLine().split(" ");
ArrayList<Integer> list =newArrayList<>();for(int i =0; i < n; i++){
list.add(Integer.parseInt(parts[i]));}
System.out.println(hafu(list));}}catch(IOException e){
e.printStackTrace();}}publicstaticinthafu(ArrayList<Integer> list){
Collections.sort(list);if(list.size()==2)return list.get(0)+list.get(1);int a = list.remove(0);int b = list.remove(0);int n = a + b;
list.add(n);return a + b +hafu(list);}}