https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805070971912192?type=7&page=0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
typedef vector<long long> VI;
#define int long long
#define INF 0x3f3f3f3f
#define endl '\n'
#define N 200010
const int mod = 1e9 + 7;
ll ksm(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1LL) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x) {
return -x & x;
}
int p[N], si[N];
int find(int x) {
if (x == p[x])return p[x];
p[x] = find(p[x]);
return p[x];
}
//size[find(b)] += size[find(a)];
//p[find(a)] = find(b);
VI res;
int n;
VI a(N);
void dfs1(int x, int y) { //先序遍历 左最小
if (x > y)return ;
int l = x + 1;
int r = y;
while (l <= y && a[l] < a[x])l ++;
while (r > x && a[r] >= a[x]) r --; //这里r > x 不加= 因为x为根节点
if (l - r != 1)return ;
dfs1(x + 1, r);
dfs1(l, y);
res.push_back(a[x]);
}
void dfs2(int x, int y) {
if (x > y)return ;
int l = x + 1;
int r = y;
while (l <= y && a[l] >= a[x])l ++;
while (r > x && a[r] < a[x]) r --;
if (l - r != 1)return ;
dfs2(x + 1, r);
dfs2(l, y);
res.push_back(a[x]);
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
dfs1(1, n);
if (res.size() != n) {
res.clear();
dfs2(1, n);
}
if (res.size() == n) {
cout << "YES" << endl;
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " "[i == res.size() - 1];
}
} else {
cout << "NO" << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T --)
solve();
return 0;
}