728x90
카드 정렬하기
해설
구현
public class Main {
private static int N;
private static PriorityQueue<Integer> pQ = new PriorityQueue<>();
public static void main(String[] args) {
input();
solution();
}
private static void input() {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
for (int i = 0; i < N; i++) {
pQ.add(sc.nextInt());
}
}
private static void solution() {
int sum = 0;
while(pQ.size() > 1) { // 피연산자 2개 필요
int x = pQ.poll();
int y = pQ.poll();
sum += (x + y);
pQ.add(x + y); // 여기다가 sum 넣으면 안됌
}
System.out.println(sum);
}
}
728x90
'Algorithms > 문제 풀이' 카테고리의 다른 글
[BOJ 15686] 치킨 배달 (0) | 2022.01.03 |
---|---|
[BOJ 16234] 인구 이동 (0) | 2021.12.31 |
[BOJ 18352] 특정 거리의 도시 찾기 (0) | 2021.12.18 |
[BOJ 3190] 뱀 (0) | 2021.12.18 |
이것이 코딩 테스트다 : 모험가 길드 (0) | 2021.12.18 |