public class Program { public static void Main() { int N = int.Parse(Console.ReadLine()!); var p_array = Console.ReadLine()!.Split('\x20').Select(int.Parse); var list = new LinkedList(); var nodes = new LinkedListNode?[N]; foreach (var n in p_array) { nodes[n - 1] = list.AddLast(n); } List q = new(N); for (int i = 0; i < N; i++) { var node0 = nodes[i]; if (node0 is not null) { var node1 = node0.Next; if (node1 == null) { continue; } nodes[node0.Value - 1] = null; q.Add(node0.Value); list.Remove(node0); nodes[node1.Value - 1] = null; q.Add(node1.Value); list.Remove(node1); } } foreach (var n in q) { Console.Write(n); Console.Write('\x20'); } Console.Write(Environment.NewLine); return; } }