//Siga Siga?! YES!
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<    
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int inf = 1e18,N = 3e3+1,MOD =  998244353;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<int, int, hash<int>> hmp;
const int BUF_SZ=1<<16;
char buf[BUF_SZ];
int pos;
int len;
inline char next_char() {
    if (pos == len) {
        pos = 0;
        len = (int)fread(buf, 1, BUF_SZ, stdin);
    }
    return buf[pos++];
}
 
inline int read_int() {
    int x;
    char ch=next_char();
    while(!isdigit(ch))ch=next_char();
    x = ch - '0';
    while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
    return x;
}
struct inpout{
    static const int sz=1<<15;
    char inp[sz];
    int curc=0,len=0;
    inline char gc(){
        if(curc==len){
            len=(int)fread(inp,1,sz,stdin);
            curc=0;
            if(!len)return -1;
        }
        return inp[curc++];
    }
    inline void readstr(char*c,int ll){
        for(int i=0;i<ll;i++){
            c[i]=gc();
        }
        gc();
    }
    inline int readint(){
        char c=gc();
        bool isneg=(c=='-');
        int ans=0; 
        if(!isneg)ans=c-'0';
        c=gc();
        while('0'<=c&&c<='9'){
            ans=10*ans+c-'0';
            c=gc();
        }
        if(isneg)return -ans;
        return ans;
    }
    inline int readuint(){
        char c=gc();
        int ans=c-'0'; 
        cerr<<"begin is "<<c<<' '<<ans<<' ';
        while('0'<=c&&c<='9'){
            ans=10*ans+c-'0';
            c=gc();
            cerr<<"is "<<c<<' '<<ans<<' ';
        }
        cerr<<'\n';
        cerr<<ans<<'\n';
        return ans;
    }
    char out[sz];
    int curind=0;
    inline void flush(){
        fwrite(out,1,curind,stdout);
        curind=0;
    }
    inline void putchar(char c){
        out[curind++]=c;
        if(curind==sz)flush();
    }
    char temp[10];
    inline void putint(int i){
        if(i<0){
            putchar('-');
            i=-i;
        }
        int lll=0;
        while(i){
            temp[lll++]=(i%10)+'0';
            i/=10;
        }
        while(lll){
            putchar(temp[--lll]);
        }
    }
    inline void putuint(int i){
        int lll=0;
        while(i){
            temp[lll++]=(i%10)+'0';
            i/=10;
        }
        while(lll){
            putchar(temp[--lll]);
        }
    }
}io;
void solve() {  
    int n;
    n = read_int();
    vi a(n+1);
    for (int i=1;i<=n;i++) {
        a[i] = read_int();
    }
    hmp mp;
    vi pq;
    for (int i=1;i<=n;i++) {
        while (!pq.empty() && mp.find(a[i])!=mp.end() && pq.back() > mp[a[i]]) {
            mp.erase(a[pq.back()]);
            pq.pop_back();
        }
        mp[a[i]] = i;
        pq.push_back(i);
    }
    vi ans(n+1,0);
    int ptr = n;
    while (!pq.empty()) {
        while (pq.back() <= ptr) ans[ptr--] = a[pq.back()];
        pq.pop_back();
    }
    for (int i=1;i<=n;i++) {
        cout << (ans[i]) << '\n';
    }
}
int32_t main() { 
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Dodi 
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int t = 1;
    //cin >> t;
    while (t --> 0) solve();
}
Compilation message (stderr)
Main.cpp:14:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   14 | const int inf = 1e18,N = 3e3+1,MOD =  998244353;
      |                 ^~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |