김지팡의 저장소
728x90

 

🧑‍💻

백준의 13975번 파일 합치기 문제이다.

package variable.backzun.week02.day06;

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

public class 파일_합치기3_13975_테스트2번 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int cases = Integer.parseInt(br.readLine());

        for(int i = 0; i < cases; i++) {
            int num = Integer.parseInt(br.readLine());

            Queue<Long> pq = new PriorityQueue<>();
            String[] data = br.readLine().split(" ");

            for(int j = 0; j < num; j++) {
                pq.offer(Long.parseLong(data[j]));
            }

            long total = 0;
            while(pq.size() > 1) {
                long num1 = pq.poll();
                long num2 = pq.poll();
                long sum = num1 + num2;

                total += sum;
                pq.offer(sum);
            }
            System.out.println(total);
        }
    }
}
728x90
profile

김지팡의 저장소

@김지팡

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!