반응형
BOJ 1715 풀이 코드
import java.io.*;
import java.util.PriorityQueue;
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 n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i < n; i++) {
pq.add(Integer.parseInt(br.readLine()));
}
int total = 0;
if(n != 1){
while (pq.size() > 2) {
int num = pq.poll() + pq.poll();
total += num;
pq.add(num);
}
total = total + pq.poll() + pq.poll();
}
bw.write(total + "\n");
bw.flush();
bw.close();
br.close();
}
}
반응형
'코딩 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 자바 문제 풀이 2109 : 골드3 (0) | 2023.09.23 |
---|---|
[백준] 자바 문제 풀이 17822 : 골드2 (0) | 2023.09.22 |
[백준] 자바 문제 풀이 12837 : 골드1 (0) | 2023.09.22 |
[백준] 자바 문제 풀이 9935 : 골드4 (0) | 2023.09.22 |
[백준] 자바 문제 풀이 13334 : 골드2 (0) | 2023.09.21 |