코딩 문제 풀이/백준
[백준] 자바 문제 풀이 1715 : 골드4
코딩하는 랄로
2023. 9. 22. 17:16
반응형
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();
}
}
반응형