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 = 0,
- m: Int = tab.size - 1,
- dp: List<MutableList<Pair<Int, Int>>> = List(tab.size) { MutableList(tab.first().size) { invalid } }.also {
- it[tab.size - 1][0] = tab[tab.size - 1][0] to tab[tab.size - 1][0]
- }
- ): Pair<Int, Int> {
- if (n >= tab.size || m < 0)
- return invalid
- if (dp[n][m] != invalid)
- return dp[n][m]
- dp[n][m] = run {
- val f = dfs(tab, n + 1, m, dp)
- val s = dfs(tab, n, m - 1, dp)
- val t = dfs(tab, n + 1, m - 1, dp)
- val c = tab[n][m]
- arrayOf(f.first + c, s.first + c, t.first + c * 2).maxOrNull()!! to
- arrayOf(f.second + c, s.second + c, t.second + c * 2).minOrNull()!!
- }
- return dp[n][m]
- }
- fun main() = dfs((0..11).map { readLine()!!.trim().split("\t").map(String::toInt)})
- .let { (f, s) -> println("MAX: $f; MIN: $s") }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement