// @EXPECTED_RESULTS@: TIME_LIMIT_EXCEEDED, WRONG_ANSWER import kotlin.math.ceil fun main() { val (_, f) = readln().split(" ").map { it.toDouble() } val values = readln().split(" ").map { it.toDouble() } if (values.size == 1) { return println(findBestFps(f, values.first())) } val div = 1_000_000.0 var best = Double.MAX_VALUE for (i in div.toLong() .. (f.toLong()*div.toLong() )) { val current = values.calulateBest(i/div) if(current< best) { best = current } } println(best) } fun findBestFps(end: Double, values: Double, start: Double = 1.0): Double { var step = (end - start) / 2 var current = start + step if (current < start) current = start var best = values.calulateBest(current) step /= 2 while (step > 2 * EPSILON) { val left = values.calulateBest(current - step) val right = values.calulateBest(current + step) best = best.coerceAtMost(left).coerceAtMost(right) when (best) { left -> current -= step right -> current += step } // println("$step: $best ( $current)") step /= 2 } return best } const val SPEED = 1000 const val EPSILON = 1e-18 fun List.calulateBest(currentFramerate: Double) = sumOf { it.calulateBest(currentFramerate) } fun Double.calulateBest(currentFramerate: Double): Double { val timeNeeded = this / SPEED val frameDuration = 1 / currentFramerate val test = ceil(timeNeeded / frameDuration) if (test * frameDuration - timeNeeded <= EPSILON) { return timeNeeded } else { return (test) * frameDuration } }