class UnionFind: def __init__(self, n): self.n = n self.par = [-1] * n self.group_ = n self.R = [i for i in range(n)] def find(self, x): if self.par[x] < 0: return x lst = [] while self.par[x] >= 0: lst.append(x) x = self.par[x] for y in lst: self.par[y] = x return x def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False if self.par[x] > self.par[y]: x, y = y, x self.par[x] += self.par[y] self.par[y] = x self.R[x] = max(self.R[x], self.R[y]) self.group_ -= 1 return True def size(self, x): return -self.par[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) @property def group(self): return self.group_ n = int(input()) P = list(map(int, input().split())) for i in range(n): P[i] -= 1 invP = [0] * n for i in range(n): invP[P[i]] = i Q = [] p = 0 UF = UnionFind(n) used = [False] * n while p < n: if used[p]: p += 1 continue x = invP[p] if UF.R[UF.find(x)] == n - 1: p += 1 continue y = UF.R[UF.find(x)] + 1 q = P[y] Q += [p + 1, q + 1] UF.unite(x, x - 1) UF.unite(y, y - 1) used[p] = True used[q] = True print(*Q)