공부기록/백준

[백준] 1715번 카드 정렬하기

메델 2023. 11. 22. 19:20
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    	Scanner kb = new Scanner(System.in);
    	int n = kb.nextInt();
    	int answer = 0;
    	PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
    	
    	for(int i=0; i<n; i++) {
    		pq.add(kb.nextInt());
    	}
    	
    	while(pq.size()>1) {
    		int a = pq.remove();
    		int b = pq.remove();
    		answer += a+ b;
    		pq.add(a+b);
    	}
    	
    	System.out.println(answer);

    	
    }
}