/*
TASK: Stone Arranging 2
LANG: C++
AUTHOR: Yowaine
*/
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
#define all(x) x.begin() , x.end()
#define endl "\n"
#define colr first
#define idx second
unordered_map <int,int> mp;
stack <PII> st; // {color , idx};
using ll = long long;
const int N = 200200;
int color , n;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cout.tie(0);
cin >> n;
// st.push({0 , 1});
for (int i = 1; i <= n ; i++){
cin >> color;
mp[color]++;
int id = i;
while(!st.empty() && mp[color] >= 2){
PII now = st.top();
st.pop();
mp[now.colr]--;
if (now.colr == color){
id = max(i , now.idx);
break;
}
}
st.push({color , id});
}
stack <PII> ans;
while(!st.empty()){
ans.push(st.top());
st.pop();
}
int id = 1;
while (!ans.empty()){
PII now = ans.top(); ans.pop();
for (int i = id ; i <= now.idx ; i++) cout << now.colr << "\n";
id = now.idx + 1;
}
return 0;
}