-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11286.java
29 lines (25 loc) · 923 Bytes
/
11286.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
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.io.BufferedReader;
public class Main {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> {
int abs1 = Math.abs(o1);
int abs2 = Math.abs(o2);
if(abs1 == abs2) return o1 > o2 ? 1 : -1;
return abs1 - abs2;
});
for(int i = 0 ; i < n; i++){
int val = Integer.parseInt(br.readLine());
if(val == 0){
if(queue.isEmpty()) System.out.println("0");
else System.out.println(queue.poll());
}else{
queue.add(val);
}
}
}
}