Algorithm & SQL/BAEKJOON

백준 JAVA 1715 카드 정렬하기

YoonJong 2022. 8. 1. 13:41
728x90
반응형

package BAEKJOON.Gold.Ⅳ;

import java.util.PriorityQueue;
import java.util.Scanner;

public class NO1715_2 {

    static int n;
    static int [] arr;
    // 우선순위 큐 사용
    // 작은 숫자부터 더해야 나가야한다.
    static PriorityQueue<Integer> Q = new PriorityQueue<>();


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();

        arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            Q.offer(arr[i]);
        }

        int total =0;
        // Q 사이즈가 1이면 출력
        while(Q.size() > 1) {
            int sum = 0;
            sum += Q.poll();
            sum += Q.poll();

            total += sum;

            Q.offer(sum);
        }
        System.out.println(total);
    }
}
728x90
반응형