🤖
ABC 362 C - Sum = 0 のメモ
仮に2つ目の条件がなければ、全部
この場合、各
import java.io.PrintWriter
fun main() {
val n = readln().toInt()
val lr = List(n) {
val (l, r) = readln().split(" ").map { it.toLong() }
l to r
}
val result = solve(lr)
if(result.isEmpty()) {
println("No")
return
}
println("Yes")
val out = PrintWriter(System.out)
for(i in result.indices) {
out.print(result[i])
if(i == n - 1) {
out.println()
} else {
out.print(' ')
}
}
out.flush()
}
fun solve(lr: List<Pair<Long, Long>>): LongArray {
val result = lr.map { it.first }.toLongArray()
var current = result.sum()
if(current > 0) {
return LongArray(0)
}
val rests = lr.map { it.second - it.first }
for(i in lr.indices) {
val (l, r) = lr[i]
val rest = rests[i]
current += rest
if(current == 0L) {
result[i] = r
return result
}
if(current > 0) {
result[i] = r - current
return result
}
result[i] = r
}
return LongArray(0)
}
Discussion