#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(ll i = 0;i < n;i++) #define REPR(i, n) for(ll i = n;i >= 0;i--) #define FOR(i, m, n) for(ll i = m;i < n;i++) #define FORR(i, m, n) for(ll i = m;i >= n;i--) #define REPO(i, n) for(ll i = 1;i <= n;i++) #define ll long long #define INF (ll)1ll << 60 #define MINF (-1 * INF) #define ALL(n) n.begin(),n.end() #define MOD (ll)1000000007 #define P pair int n, nxt[210000], prv[210000], s[210000]; vector ans; int main(){ cin >> n; REP(i, n)cin >> s[i]; REP(i, n){ prv[s[i]] = (i == 0 ? -1 : s[i - 1]); nxt[s[i]] = (i == n - 1 ? -1 : s[i + 1]); } REPO(i, n){ if(nxt[i] != -1){ ans.push_back(i); ans.push_back(nxt[i]); if(prv[i] != -1) nxt[prv[i]] = nxt[nxt[i]]; if(nxt[nxt[i]] != -1) prv[nxt[nxt[i]]] = prv[i]; nxt[i] = nxt[nxt[i]] = -1; } } REP(i, n) { cout << ans[i] << " "; } cout << endl; }