-
Notifications
You must be signed in to change notification settings - Fork 0
/
1417(PriorityQueue).java
44 lines (37 loc) · 1.15 KB
/
1417(PriorityQueue).java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.*;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int arr[] = new int[N];
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
int target = Integer.parseInt(br.readLine());
for(int i = 1; i < N; i++){
priorityQueue.add(Integer.parseInt(br.readLine()));
}
int count = 0;
while (N != 1){
int temp = priorityQueue.remove();
if(target < temp){
count++;
target++;
temp--;
priorityQueue.add(temp);
}else if(target == temp){
count+=1;
break;
}else{
break;
}
}
if(N == 1){
System.out.println(0);
}else{
System.out.println(count);
}
}
}