https://www.acmicpc.net/problem/1927
#include <iostream>
#include <vector>
#include <queue>
#pragma warning(disable: 4996)
using namespace std;
const int MAX = 100000;
int n;
int ar[MAX];
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
scanf("%d", &n);
int c;
priority_queue<int, vector<int>, greater<int>> q;
for(int i=0;i<n;i++){
scanf("%d", &c);
if(c == 0){
if(q.size() == 0)
printf("0\n");
else{
printf("%d\n", q.top());
q.pop();
}
}else{
q.push(c);
}
}
return 0;
}