99클럽 코테 스터디 24일차 TIL - 비리 국회의원 다솜

수삼·2024년 11월 20일
0

코딩테스트

목록 보기
29/44

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int cnt = Integer.parseInt(br.readLine()) - 1;
        int dasom = Integer.parseInt(br.readLine());
        int rst = 0;

        PriorityQueue pq = new PriorityQueue(Collections.reverseOrder());

        for (int i = 0; i < cnt; i++) {
            pq.offer(Integer.parseInt(br.readLine()));
        }
        while(!pq.isEmpty() && dasom <= (int) pq.peek()) {
            pq.offer((int) pq.poll() - 1);
            dasom++;
            rst++;
        }

        System.out.println(rst);
    }
}

1. 다솜의 표는 따로 저장해서 나중에 비교한다.

2. 큐를 만든다.

3. 최대값과 비교해서 다솜의 표가 더 작으면 while을 실행한다.

4. 가장 큰 수에서 하나씩 뺀다.

예외 처리를 잘하자.

0개의 댓글