Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private infix fun Boolean.arrow(other: Boolean) = !(this && !other)
- private val invalid = 0 to 1e9.toInt() // max / min
- private fun dfs(
- tab: List<List<Int>>,
- n: Int = tab.size - 1,
- m: Int,
- dp: List<MutableList<Pair<Int, Int>>> = List(tab.size) { MutableList(tab.first().size) { invalid } }.also { list ->
- (0..9).forEach { list[0][it] = tab[0][it] to tab[0][it] }
- }
- ): Pair<Int, Int> {
- if (n < 0 || m !in 0..9)
- return invalid
- if (dp[n][m] != invalid)
- return dp[n][m]
- dp[n][m] = run {
- val f = dfs(tab, n - 1, m - 1, dp)
- val s = dfs(tab, n - 1, m + 1, dp)
- val t = dfs(tab, n - 1, m, dp)
- arrayOf(f.first, s.first, t.first).maxOrNull()!! to arrayOf(f.second, s.second, t.second).minOrNull()!!
- }.let { (f, s) -> f + tab[n][m] to s + tab[n][m] }
- return dp[n][m]
- }
- fun main() {
- val list = (0..9).map { readLine()!!.trim().split("\t").map(String::toInt) }
- (0..9).map { dfs(list, m = it) }.flatMap { (f, s) -> listOf(f, s) }.let {
- println("MAX: ${it.maxOrNull()!!} MIN: ${it.minOrNull()!!}")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement