본문 바로가기

Algorithm/백준

백준 1715 카드 정렬하기

package practice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

public class Boj1715 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int result = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int i =0 ; i< n ; i ++){
            pq.add(Integer.parseInt(br.readLine()));
        }
        while(pq.size() > 1){
            int sum = pq.poll() + pq.poll();
            pq.add(sum);
            result += sum;
        }

        System.out.println(result);
    }
}

'Algorithm > 백준' 카테고리의 다른 글

백준 11286 절댓값 힙  (0) 2024.07.25
백준 2075 N번째 큰 수  (0) 2024.07.25
백준 29160 나의 FIFA 팀 가치는?  (0) 2024.07.25
백준 13904 과제  (0) 2024.07.25
백준 2841 외계인의 기타 연주  (0) 2024.07.25