import java.util.* fun main(arr:Array) { val zairyoCount = readLine()!!.toInt() val seizouCount = readLine()!!.toInt() val need = Array(zairyoCount + 1, {0}) val seizou = mutableMapOf>() (1..seizouCount).forEach { val (zairyo, value, dekiru) = readLine()!!.split(" ").map { it.toInt() } if(!seizou.containsKey(dekiru)) { seizou[dekiru] = mutableMapOf() } seizou[dekiru]!![zairyo] = value } val queue = LinkedList() queue.push(Task(zairyoCount, 1)) while (queue.isNotEmpty()) { val task = queue.pop() seizou[task.target]?.also { it.forEach { queue.push(Task(it.key, it.value * task.value)) } } ?: run { need[task.target] = need[task.target] + task.value } } need.drop(1).dropLast(1).forEach { println(it) } } data class Task(val target:Int, val value:Int)