import java.io.IOException;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Scanner s=new Scanner(System.in);
StringBuilder out = new StringBuilder("");
while(true){
int cases=s.nextInt();
if(cases==0)
break;
long sumX=0,sumT=0;
PriorityQueue<Long> pq=new PriorityQueue<Long>();
for(int i=0;i<cases;i++){
pq.add(s.nextLong());
}
long sum=0;
while(pq.size()>1){
long x=pq.remove();
long y=pq.remove();
pq.add(x+y);
sum+=x+y;
}
out.append(sum).append("\n");
}
System.out.print(out);
}
}
No comments:
Post a Comment