package Yukicoder /** * Created by hichikawa on 2015/11/12. */ fun main(args: Array) { fun readLineLongArray(): List { val str = readLine() as String val arrStr = str.split(" ") val ret = arrStr.map { it.toLong() } return ret } fun readLineLong(): Long { val str = readLine() as String return str.toLong() } fun readLineInt(): Int { val str = readLine() as String return str.toInt() } fun readLineIntArray() : List { val str = readLine() as String val arrStr = str.split(" ") val ret = arrStr.map { it.toInt() } return ret } fun readLineDoubleArray(): List { val str = readLine() as String val arrStr = str.split(" ") val ret = arrStr.map { it.toDouble() } return ret } fun readLineDouble() : Double { val str = readLine() as String return str.toDouble() } val input = readLineIntArray() val num = input[0] val iceArray = (readLine() as String).toCharArray().map { it.toString().toInt() } var sum = 0 for (item in iceArray) { sum += item } sum -= num var needIce = input[1] var buyIce = 0 var moreIce = 0 for (i in 0..Int.MAX_VALUE) { if (moreIce == 0) { buyIce++ } else { moreIce-- } moreIce += iceArray[i%num] needIce-- if (needIce <= 0) break if (i >= num) { if (sum >= 0) { break } else { if (((needIce / num) - 2) > 0) { // println(((needIce / num) - 2)) buyIce += ((needIce / num) - 2) * (-sum) needIce -= ((needIce / num) - 2) * num } } } // println("\t" + needIce) if (needIce <= 0) break } println(buyIce) // println(sum) }