import kotlin.system.measureTimeMillis fun main(args:Array) { val columnCount = readLine()!!.toInt() val map = readLine()!!.split(" ").map { it.toInt()}.toMutableList() val total = map.sum() var fixColumnCount = 1 var subTotal = 1 var fixMap = mutableListOf(1) while (subTotal + (fixColumnCount + 2) <= total) { fixColumnCount += 2 subTotal += fixColumnCount fixMap = fixMap.map { it + 1 }.toMutableList() fixMap.add(0, 1) fixMap.add(1) } map.addAll((1..fixColumnCount - 1).map { 0 }) var pool = 0 var count = 0 for(i in (0 until fixMap.size)) { val num1 = fixMap[i] var num2 = map[i] var diff = num1 - num2 if(Math.abs(diff) > Math.abs(pool) && diff * pool < 0) { count += Math.abs(diff) - Math.abs(pool) } else if(diff * pool >= 0) { count += Math.abs(diff) } pool += diff } println(count) }