#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; using ll = long long; using ld = long double; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n; cin >> n; vector a(n); rep(i,n) cin >> a[i]; multiset L, R(a.begin(), a.end()); vector ans(n); rep(i,n) { R.erase(R.lower_bound(a[i])); int x = a[i]; if(!L.empty()) { auto itL = L.end(); itL--; if(R.count(*itL)) x = max(x, *itL); } if(!R.empty()) { auto itR = R.end(); itR--; if(L.count(*itR)) x = max(x, *itR); } ans[i] = (x == -1 ? a[i] : x); L.insert(a[i]); } rep(i,n) cout << ans[i] << " \n"[i == n - 1]; }