complete 11057.kt 15835.kt 20411.kt 25345.kt 28242.kt 32628.kt

This commit is contained in:
2025-07-29 06:36:46 +09:00
parent 2e957b221e
commit b4b22e9af7
6 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import kotlin.math.max
import kotlin.math.min
class Bag(items: List<Long>) {
val cumulativeWeight: List<Long>
init {
var x = 0L
cumulativeWeight = listOf(0L) + items.map {
x += it
x
}
}
fun weight(to: Int): Long {
return cumulativeWeight[to]
}
}
class BagManager(val n: Int, val bag1: Bag, val bag2: Bag) {
fun smallestBig(k: Int): Long {
return (max(0, k - n)..min(n, k)).map { i -> i to (k - i) }.minOf { (i, j) ->
val w1 = bag1.weight(n - i)
val w2 = bag2.weight(n - j)
max(w1, w2)
}
}
}
fun main() = with(System.`in`.bufferedReader()) {
val (n, k) = this.readLine()!!.split(' ').map { it.toInt() }
val bag1 = Bag(ArrayDeque<Long>(this.readLine()!!.split(' ').map { it.toLong() }))
val bag2 = Bag(ArrayDeque<Long>(this.readLine()!!.split(' ').map { it.toLong() }))
val manager = BagManager(n, bag1, bag2)
println(manager.smallestBig(k))
}