Hard deck/reindexing d1

033 : 카드 정렬하기

서버관리자 페페 2023. 6. 13. 15:46

 

 

pq > 5 4 3 2 1 

offer > (자동정렬) > remove

 

 

 

 

 

 

-

 

 

 

while 내부 local pointer

- (pq.size != 1) 로 표현해도 되고

- (pq.size() > 1)로 표혀해도 됨

- 중요한거는 2가 빠지고 1이 추가되므로 1씩 줄어든다

 

plate는 이제까지의 횟수가 모두 누적된 것이므로,

새로 넣는 offer는 plate가 아닌 d1+d2를 넣어야 함

 

 

 

 

-

 

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

public class Main {
    public static void main(String[] args) {

        // ISC
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        // D2F
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int i = 1; i <= N; i++) {
            int p = sc.nextInt();
            pq.add(p);
        }

        // OP
        int plate = 0;
        while (pq.size() > 1) {
            int d1 = pq.remove();
            int d2 = pq.remove();
            plate += (d1 + d2);
            pq.offer(d1 + d2);
        }

        // OEC
        System.out.println(plate);
    }
}