declare var require: any; let lines: string[] = require("fs").readFileSync("/dev/stdin", "utf8").split("\n"); let p = lines[1].split(' ').map(Number); const getMinIndex = (arr: number[]) => arr.reduce((acc, cur, i) => acc.min > i ? {min: i, i} : acc, {min: Infinity, i: -1}); const q = []; while(p.length > 0) { const mins = getMinIndex(p.slice(0, -1)); q.push(p[mins.i], p[mins.i+1]); p = p.slice(0, mins.i).concat(p.slice(mins.i+2)); } console.log(q.join(' '))