
이 문제는 value = (값1+ 값2)를 total에 total +=value 를 해주고
우선순위 큐에 add(value)를 해줘야 한다.
while(!pq.isEmpty()){
int a = pq.poll();
if(pq.size()>0){
int b = pq.poll();
total += a+b;
pq.add(a+b);
} //else 해서 1개 남앗을때는 고려할 필요가 없다.
}
또한 큐에서는 계산시 2개가 빠지고 결과값 1개가 추가 되므로 서서히 -1이 되니까
결국 값이 2개있을때까지만 계속 계산을 반복해주면 된다.
import java.util.*;
import java.io.*;
public class J1715 {
public static void main(String[] args)throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Integer> pq = new PriorityQueue();
int index = Integer.parseInt(buffer.readLine());
for (int i = 0; i < index; i++) {
pq.add(Integer.parseInt(buffer.readLine()));
}
int total = 0;
while(!pq.isEmpty()){
int a = pq.poll();
if(pq.size()>0){
int b = pq.poll();
total += a+b;
pq.add(a+b);
}
}
System.out.println(total);
}
}