#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef vector<int>::iterator itr;
bool isbst(itr beg,itr end){
if(end-beg<=2)return true;
itr mid=find_if(beg+1,end,[&](int x)->bool{return x>=*beg;});
if(any_of(mid,end,[&](int x)->bool{return x<*beg;}))
return false;
return isbst(beg+1,mid)&&isbst(mid,end);
}
bool ismbst(itr beg,itr end){
if(end-beg<=2)return true;
itr mid=find_if(beg+1,end,[&](int x)->bool{return x<*beg;});
if(any_of(mid,end,[&](int x)->bool{return x>=*beg;}))
return false;
return ismbst(beg+1,mid)&&ismbst(mid,end);
}
void pre2post_bst(itr beg,itr end){
if(end-beg<=1)return;
itr mid=find_if(beg+1,end,[&](int x)->bool{return x>=*beg;});
pre2post_bst(beg+1,mid);
pre2post_bst(mid,end);
rotate(beg,beg+1,end);
}
void pre2post_mbst(itr beg,itr end){
if(end-beg<=1)return;
itr mid=find_if(beg+1,end,[&](int x)->bool{return x<*beg;});
pre2post_mbst(beg+1,mid);
pre2post_mbst(mid,end);
rotate(beg,beg+1,end);
}
int main(){
int n;cin>>n;
vector<int>val(n),ret,val2;
for(auto &x:val)cin>>x;val2=val;
if(isbst(val.begin(),val.end())){
pre2post_bst(val.begin(),val.end());
ret=val;
}
val=val2;//但pre2post_bst可能改变val,这两个判断必须使用相同的val
if(ismbst(val.begin(),val.end())){
pre2post_mbst(val.begin(),val.end());
ret=val;
}
if(ret.size()){
bool first=true;
cout<<"YES"<<endl;
for(auto x:ret){
if(first)first=false;
else cout<<' ';
cout<<x;}
}else cout<<"NO"<<endl;
return 0;
}
先序(根,左子树,右子树)变为后序(左子树,右子树,根)