#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 10;
int arr[maxN];
int ans[maxN];
vector<int> g[maxN];
int n;
int main() {
cin >> n;
vector<int> s;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
while (not s.empty() and arr[s.back()] >= arr[i]) {
s.pop_back();
}
if (not s.empty()) {
g[s.back()].push_back(i);
}
s.push_back(i);
}
s.clear();
for (int i = 1; i <= n; ++i) {
for (int v : g[i]) {
while (not s.empty() and arr[s.back()] >= arr[v]) {
s.pop_back();
}
if (not s.empty()) {
ans[v] = arr[s.back()];
}
s.push_back(v);
}
while (not s.empty() and arr[s.back()] >= arr[i]) {
s.pop_back();
}
s.push_back(i);
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " ";
} cout << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBtYXhOID0gMWU1ICsgMTA7CmludCBhcnJbbWF4Tl07CmludCBhbnNbbWF4Tl07CnZlY3RvcjxpbnQ+IGdbbWF4Tl07CmludCBuOwoKaW50IG1haW4oKSB7CgljaW4gPj4gbjsKCXZlY3RvcjxpbnQ+IHM7Cglmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKCQljaW4gPj4gYXJyW2ldOwoJCXdoaWxlIChub3Qgcy5lbXB0eSgpIGFuZCBhcnJbcy5iYWNrKCldID49IGFycltpXSkgewoJCQlzLnBvcF9iYWNrKCk7CgkJfQoJCWlmIChub3Qgcy5lbXB0eSgpKSB7CgkJCWdbcy5iYWNrKCldLnB1c2hfYmFjayhpKTsKCQl9CgkJcy5wdXNoX2JhY2soaSk7Cgl9CgkKCXMuY2xlYXIoKTsKCWZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkgewoJCWZvciAoaW50IHYgOiBnW2ldKSB7CgkJCXdoaWxlIChub3Qgcy5lbXB0eSgpIGFuZCBhcnJbcy5iYWNrKCldID49IGFyclt2XS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