function Main(input) { var data = input.split("\n").filter(s => s.length > 0) var n = data.shift() * 1 let r = '' function findMinIdx(input){ // console.log(input) const codes = input.map(s=>s.slice(0, 1)).map(s=>[].reduce.call(s, (sum ,c)=> sum += c.charCodeAt(0), 0)) const minCode = Math.min(...codes) const minIdxes = codes.map((c, i) => [c, i]).filter(a => a[0] == minCode).map(a => a[1]) if (minIdxes.length == 1) { return minIdxes.pop() } let next = input.filter((s, i) => s.charCodeAt(0) == minCode && s.length == 1) let nextInputIndex = [] if (nextInputIndex.length > 0){ return nextInputIndex.pop() } let nextInput = input.filter((s, i) => { let isNext = s.charCodeAt(0) == minCode if (isNext && s.length > 1){ nextInputIndex.push(i) } return isNext }).map(s => s.slice(1)).filter(s => s.length > 0) if (nextInput.length < 1) { return minIdxes.pop() } // console.log(nextInput) return nextInputIndex[findMinIdx(nextInput)] } var input = data while (input.length > 0) { const minIdx = findMinIdx(input) const str = input[minIdx] r += str[0] input[minIdx] = str.slice(1) input = input.filter(s => s.length > 0) } console.log(r) } // Don't edit this line! Main(require("fs").readFileSync("/dev/stdin", "utf8"));