let lines: string[] = []; const reader = require('readline').createInterface({ input: process.stdin, output: process.stdout }); reader.on('line', function (line: string) { lines.push(line); if (lines.length === 2) { reader.close(); main(); } }); function main () { const n = +lines[0]; const nums = lines[1].split(' ').map((s,i) => [i+1, +s]).filter(e => e[1]); const res = result(n, nums); console.log(res); } function result (n: number, nums: number[][], res = 0): number { if (n === 0) return res; else { if (nums[nums.length-1][1] === 1) return result(n-1, nums.slice(0,nums.length-1), res * 10 + nums[nums.length-1][0]); else return result(n-1, [...nums.slice(0,nums.length-1), [nums[nums.length-1][0], nums[nums.length-1][1]-1]], res * 10 + nums[nums.length-1][0]); } }