#include #include #include #include using namespace std; int main() { int N; cin >> N; vector p(N); map mp1, mp2; for (int i = 0; i < N; i++) { cin >> p[i]; mp1[p[i]] = i; mp2[i] = p[i]; } vector q; while (mp1.size() > 0) { if (mp1.size() == 2) { q.push_back(mp2.begin()->second); q.push_back(mp2.rbegin()->second); break; } auto [last_idx, last_value] = *mp2.rbegin(); auto fi = mp1.begin(); if (fi->second == last_idx) { fi++; } auto fi_itr = mp2.find(fi->second); auto se_itr = next(fi_itr); int fi_idx = fi_itr->first; int se_idx = se_itr->first; int fi_val = fi_itr->second; int se_val = se_itr->second; q.push_back(fi_val); q.push_back(se_val); mp2.erase(fi_idx); mp2.erase(se_idx); mp1.erase(fi_val); mp1.erase(se_val); } for (int i = 0; i < N; i++) { cout << q[i] << (i + 1 == N? '\n': ' '); } return 0; }