본문 바로가기
알고리즘 & 자료구조/백준

백준 1715

by 신재권 2022. 7. 1.
package baekjoon.그리디;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main1715 {

   public static void main(String[] args) throws Exception {

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int N = Integer.parseInt(br.readLine());
      PriorityQueue<Integer> q = new PriorityQueue<>();
      for (int i = 0; i < N; i++) {
         q.add(Integer.parseInt(br.readLine()));
      }
      if (N == 1) {
         System.out.println(0);
         return;
      }
      int ans = 0;
      while (true) {
         int a = q.poll() + q.poll();
         ans += a;
         q.add(a);
         if (q.size() == 1) {
            break;
         }
      }

      System.out.println(ans);

   }

}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 1541  (0) 2022.07.04
백준 10817  (0) 2022.07.02
백준 1946  (0) 2022.06.30
백준 11052  (0) 2022.06.29
백준 2470  (0) 2022.06.29