#include<bits/stdc++.h>
using namespace std;
const int MAXN=7e5+10;
const int inf=1e8;
int n,a[MAXN];
set<int>T;
vector<int>v;
struct SuffixTree{
int link[MAXN],n,rem,tail,start[MAXN];
int len[MAXN],s[MAXN],now;
map<int,int>ch[MAXN];
SuffixTree(){tail=now=1;n=rem=0;len[0]=inf;}
inline int build(int a,int b){
link[++tail]=1;
start[tail]=a;len[tail]=b;
return tail;
}
void Extend(int x){
s[++n]=x;++rem;
for(int last=1;rem;){
while(rem>len[ch[now][s[n-rem+1]]])
rem-=len[now=ch[now][s[n-rem+1]]];
int &v=ch[now][s[n-rem+1]];
int c=s[start[v]+rem-1];
if(!v||x==c){
link[last]=now;last=now;
if(!v)v=build(n,inf);
else break;
}
else{
int u=build(start[v],rem-1);
ch[u][c]=v;ch[u][x]=build(n,inf);
start[v]+=rem-1;len[v]+=rem-1;
link[last]=v=u;last=u;
}
if(now==1)--rem;
else now=link[now];
}
}
}S;
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){
#define gc() getchar()
int s=0,w=1;
char ch=gc();
while(!isdigit(ch)){
if(ch=='-')w=-1;
ch=getchar();
}
while(isdigit(ch)){
s=s*10+ch-'0';
ch=gc();
}
return w==-1?-s:s;
}
void dfs(int u,int dep){
if(dep>=n)return;
for(set<int>::iterator it=T.begin();it!=T.end();++it){
int p=*it;
if(p==2147483647)break;
if(S.ch[u][p]){
int R=S.start[S.ch[u][p]]+S.len[S.ch[u][p]]-1;
int L=S.start[S.ch[u][p]],fg=0;
for(int j=L;j<=R&&S.s[j]!=2147483647;++j){
if(S.s[j]==2147483647){
fg=1;
break;
}
else v.push_back(S.s[j]);
}
if(fg)break;
dfs(S.ch[u][p],dep+S.len[S.ch[u][p]]);
break;
}
}
}
int main(){
n=read();
for(int i=1;i<=n;++i)a[i]=read(),T.insert(a[i]);
for(int _=1;_<3;++_)for(int i=1;i<=n;++i)S.Extend(a[i]);
S.Extend(2147483647);T.insert(2147483647);
dfs(1,0);
for(int i=0;i<n;++i)printf("%d ",v[i]);
return 0;
}
蒟蒻调了好久 终于把WA change to RuntimeError10pts……蒟蒻求大佬帮忙看看