Problem:
Score : 800 points

Problem Statement

Let N be a positive even number.

We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N).
Snuke is constructing another permutation of (1, 2, ..., N), q, following the procedure below.

First, let q be an empty sequence.
Then, perform the following operation until p becomes empty:

Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.

When p becomes empty, q will be a permutation of (1, 2, ..., N).

Find the lexicographically smallest permutation that can be obtained as q.

Constraints

N is an even number.

2 ≤ N ≤ 2 × 10^5

p is a permutation of (1, 2, ..., N).

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print the lexicographically smallest permutation, with spaces in between.

Sample Input 1

4
3 2 4 1

Sample Output 1

3 1 2 4

The solution above is obtained as follows:

p

q

(3, 2, 4, 1)

()

↓

↓

(3, 1)

(2, 4)

↓

↓

()

(3, 1, 2, 4)

Sample Input 2

2
1 2

Sample Output 2

1 2

Sample Input 3

8
4 6 3 2 8 5 7 1

Sample Output 3

3 1 2 7 4 6 8 5

The solution above is obtained as follows:

p

q

(4, 6, 3, 2, 8, 5, 7, 1)

()

↓

↓

(4, 6, 3, 2, 7, 1)

(8, 5)

↓

↓

(3, 2, 7, 1)

(4, 6, 8, 5)

↓

↓

(3, 1)

(2, 7, 4, 6, 8, 5)

↓

↓

()

(3, 1, 2, 7, 4, 6, 8, 5)

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {
	static int[] p, q;
	static int qI;
	static RmqSparseTable[] rmq = new RmqSparseTable[2];
	static PriorityQueue<Node> queue = new PriorityQueue<Node>();
	
	public static void main(String[] args) {
		StdIn in = new StdIn();
		int n=in.nextInt();
		p = in.readIntArray(n);
		int[] p0 = new int[n/2], p1 = new int[n/2];
		q = new int[n];
		for(int i=0; i<n; ++i)
			if(i%2==0)
				p0[i/2]=p[i];
			else
				p1[i/2]=p[i];
		rmq[0] = new RmqSparseTable(p0);
		rmq[1] = new RmqSparseTable(p1);
		
		genPerm(0, n-1, null);
		List<Node> nodes = new ArrayList<Node>(n/2);
		while(!queue.isEmpty()) {
			Node node = queue.poll();
			nodes.add(node);
			if(node.parent==null)
				continue;
			++node.parent.childrenReady;
			if(node.parent.childrenReady==node.parent.children)
				queue.add(node.parent);
		}
		StringBuilder output = new StringBuilder();
		for(int i=n/2-1; i>=0; --i)
			output.append(nodes.get(i).a).append(' ').append(nodes.get(i).b).append(' ');
		System.out.println(output);
	}
	
	static void genPerm(int s, int e, Node parent) {
		int modS=s%2, modE=e%2;
		int i1=2*rmq[modS].minPos(s/2, (e-1)/2)+modS;
		int i2=2*rmq[modE].minPos((i1+1)/2, e/2)+modE;
		if(i1+1==i2) {
			queue.add(new Node(p[i1], p[i2], parent));
			if(parent!=null)
				++parent.children;
		} else {
			Node node = new Node(p[i1], p[i2], parent);
			if(parent!=null)
				++parent.children;
			genPerm(i1+1, i2-1, node);
		}
		if(i1!=s) {
			genPerm(s, i1-1, parent);
		}
		if(i2!=e) {
			genPerm(i2+1, e, parent);
		}
	}
	
	static class Node implements Comparable<Node> {
		Node parent;
		int a, b, children, childrenReady;
		Node(int a, int b, Node parent) {
			this.a=a;
			this.b=b;
			this.parent=parent;
		}
		public int compareTo(Node node) {
			return -(a-node.a);
		}
	}
	
	static class RmqSparseTable {
		int[] logTable;
		int[][] rmq;
		int[] a;
		RmqSparseTable(int[] a) {
			this.a = a;
			int n = a.length;
			logTable = new int[n + 1];
			for (int i = 2; i <= n; i++)
				logTable[i] = logTable[i >> 1] + 1;
			rmq = new int[logTable[n] + 1][n];
			for (int i = 0; i < n; ++i)
				rmq[0][i] = i;
			for (int k = 1; (1 << k) < n; ++k) {
				for (int i = 0; i + (1 << k) <= n; i++) {
					int x = rmq[k - 1][i];
					int y = rmq[k - 1][i + (1 << k - 1)];
					rmq[k][i] = a[x] <= a[y] ? x : y;
				}
			}
		}
		int minPos(int i, int j) {
			int k = logTable[j - i];
			int x = rmq[k][i];
			int y = rmq[k][j - (1 << k) + 1];
			return a[x] <= a[y] ? x : y;
		}
	}
	
	interface Input {
		public String next();
		public String nextLine();
		public int nextInt();
		public long nextLong();
		public double nextDouble();
	}
	static class StdIn implements Input {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public StdIn() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		
		public StdIn(String filename) {
			try{
				din = new DataInputStream(new FileInputStream(filename));
			} catch(Exception e) {
				throw new RuntimeException();
			}
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		
		public String next() {
			int c;
			while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
			StringBuilder s = new StringBuilder();
			while (c != -1)
			{
				if (c == ' ' || c == '\n'||c=='\r')
					break;
				s.append((char)c);
				c=read();
			}
			return s.toString();
		}

		public String nextLine() {
			int c;
			while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
			StringBuilder s = new StringBuilder();
			while (c != -1)
			{
				if (c == '\n'||c=='\r')
					break;
				s.append((char)c);
				c = read();
			}
			return s.toString();
		}

		public int nextInt() {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
			{
				ret = ret * 10 + c - '0';
			}  while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}
		
		public int[] readIntArray(int n) {
			int[] ar = new int[n];
			for(int i=0; i<n; ++i)
				ar[i]=nextInt();
			return ar;
		}

		public long nextLong() {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}
		
		public long[] readLongArray(int n) {
			long[] ar = new long[n];
			for(int i=0; i<n; ++i)
				ar[i]=nextLong();
			return ar;
		}

		public double nextDouble() {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');

			if (c == '.')
			{
				while ((c = read()) >= '0' && c <= '9')
				{
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() {
			try{
				if (bufferPointer == bytesRead)
					fillBuffer();
				return buffer[bufferPointer++];
			} catch(IOException e) {
				throw new RuntimeException();
			}
		}

		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}
	}
}