@ExperimentalStdlibApi fun main(args: Array) = p1021() @ExperimentalStdlibApi fun p1021() { val (n, m) = readLine()!!.split(' ').map { it.toInt() } val aList = readLine()!!.split(' ').map { it.toLong() } val s = readLine()!! val queue = ArrayDeque(aList) var left = 0 var right = aList.lastIndex for (c in s) { if (c == 'R') { if (right == aList.lastIndex && queue.size >= 2) { queue.addLast(queue.removeLast() + queue.removeLast()) } left = Math.min(left + 1, aList.lastIndex) right = Math.min(right + 1, aList.lastIndex) } else { if (left == 0 && queue.size >= 2) { queue.addFirst(queue.removeFirst() + queue.removeFirst()) } left = Math.max(left - 1, 0) right = Math.max(right - 1, 0) } } val answer = (List(left) { 0 } + queue + List(aList.lastIndex - right) { 0 }).joinToString(" ") println(answer) }