반응형
BOJ 13975 : 파일합치기3
import java.util.PriorityQueue;
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_case = Integer.parseInt(br.readLine());
PriorityQueue<Long> pq;
StringTokenizer str;
for (int i = 0; i < test_case; i++) {
int size = Integer.parseInt(br.readLine());
str = new StringTokenizer(br.readLine());
pq = new PriorityQueue<>();
for (int k = 0; k < size; k++) {
pq.add(Long.parseLong(str.nextToken()));
}
long cost = 0;
while (pq.size() > 2) {
long new_num = pq.poll() + pq.poll();
pq.add(new_num);
cost += new_num;
}
cost += (pq.poll() + pq.poll());
bw.write(Long.toString(cost) + '\n');
bw.flush();
pq.clear();
}
bw.close();
br.close();
}
}
반응형
'코딩 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 자바 문제 풀이 5430 : 골드5 (0) | 2023.10.08 |
---|---|
[백준] 자바 문제 풀이 1351 : 골드5 (0) | 2023.10.08 |
[백준] 자바 문제 풀이 10026 : 골드5 (1) | 2023.10.06 |
[백준] 자바 문제 풀이 2014 : 골드1 (0) | 2023.10.06 |
[백준] 자바 문제 풀이 1976 : 골드4 (0) | 2023.10.06 |