fun main(args: Array) { val stageCount = readLine()!!.toInt() (1..stageCount).forEach { stageList[it] = TreeNode(it) } (1..stageCount).forEach { val (l, s) = readLine()!!.split(" ").map { it.toInt() } stageList[it]!!.value = l * 10 stageList[it]!!.parent = stageList[s]!! stageList[s]!!.items.add(stageList[it]!!) } var ans = 0 (1..stageCount).forEach { var sublist = stageList.filter { it.value.parent == null && !it.value.clearFlag } if(sublist.isEmpty()) { sublist = stageList.filter { !it.value.clearFlag } } val cur = sublist.minBy { it.value.value }!!.value ans += cur.value cur.clearFlag = true cur.parent?.let { it.items.remove(cur) } cur.parent = null cur.items.forEach{ it.value = it.value / 2 it.parent = null } cur.items.clear() } println(ans.toDouble()/10) } val stageList = mutableMapOf() class TreeNode(val id:Int) { var parent:TreeNode? = null var clearFlag = false var items = mutableListOf() var value = 0 }