https://www.acmicpc.net/problem/1655
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static void input() {
Reader scanner = new Reader();
int N = scanner.nextInt();
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for(int idx = 0; idx < N; idx++) {
if(maxHeap.size() == 0) maxHeap.offer(scanner.nextInt());
else if(maxHeap.size() == minHeap.size()) maxHeap.offer(scanner.nextInt());
else minHeap.offer(scanner.nextInt());
if(maxHeap.size() != 0 && minHeap.size() != 0)
solution(minHeap, maxHeap);
else
sb.append(maxHeap.peek()).append('\n');
}
}
static void solution(PriorityQueue<Integer> minHeap, PriorityQueue<Integer> maxHeap) {
if(minHeap.peek() < maxHeap.peek()) {
int maxCentral = maxHeap.poll(), minCentral = minHeap.poll();
minHeap.offer(maxCentral);
maxHeap.offer(minCentral);
}
sb.append(maxHeap.peek()).append('\n');
}
public static void main(String[] args) {
input();
System.out.println(sb);
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}