#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]; vector ans(n); multiset L, R(a.begin(), a.end()), st; rep(i,n) { R.erase(R.lower_bound(a[i])); if(L.count(a[i]) && !R.count(a[i])) st.erase(st.lower_bound(a[i])); ans[i] = a[i]; if(!st.empty()) ans[i] = max(ans[i], *st.rbegin()); if(!L.count(a[i]) && R.count(a[i])) st.insert(a[i]); L.insert(a[i]); } rep(i,n) cout << ans[i] << " \n"[i == n - 1]; }