/** * Seems pretty straight forward, a just do it problem. The initial problem seemed more scary than it is. * * One thing that is not entirly clear is if all steps(exepct the last one) is needed by an other step. * * My output for example 3 is reversed, but that seems valid since both orderings should are. Consider specifying this. */ fun main() { val input = System.`in`.bufferedReader() val cases = input.readLine().toInt() val map = (0 until cases).associate { val (name, lines) = input.readLine().split(" ") val steps = (0 until lines.toInt()) .map { readln() } .map { it.toReceptStap() }.associateBy { it.name } val noobTime = steps.values.sumOf { it.time } val profTime = steps.findProfesionalTime(steps.keys.last()) name to (noobTime.toDouble() / profTime) } println(map.entries.sortedBy { it.value }.joinToString(separator = "\n") { it.key }) } fun Map.findProfesionalTime(stepName: String): Long { val currentStep = get(stepName)!! return currentStep.time + if (currentStep.dependency == 0) { 0 } else currentStep.dependentOn.maxOf { findProfesionalTime(it) } } fun String.toReceptStap(): ReceptStap { val split = split(" ") val (name, time, dependency) = split return ReceptStap(name, time.toLong(), dependency.toInt(), split.drop(3)) } data class ReceptStap( val name: String, val time: Long, val dependency: Int, val dependentOn: List )