#include using namespace std; int main() { int n; cin >> n; vector p(n); map> mp; for (auto &e : p) { cin >> e; mp[e] = {-1, -1}; } for (int i = 0; i < n; i++) { if (i - 1 >= 0) { mp[p[i]].first = p[i - 1]; } if (i + 1 < n) { mp[p[i]].second = p[i + 1]; } } vector ans; for (int i = 1; i <= n; i++) { const auto [prv, nxt] = mp[i]; if (nxt == -1) { continue; } ans.push_back(i); ans.push_back(nxt); auto &[nprv, nnxt] = mp[nxt]; if (prv != -1) { mp[prv].second = nnxt; } if (nnxt != -1) { mp[nnxt].second = prv; } nprv = -1; nnxt = -1; } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " \n"[i == n - 1]; } return 0; }