Advertisement
paranid5

5 10.11

Nov 10th, 2021
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.91 KB | None | 0 0
  1. private infix fun Boolean.arrow(other: Boolean) = !(this && !other)
  2.  
  3. private val invalid = 0 to 1e9.toInt() // max / min
  4.  
  5. private fun dfs(
  6.     tab: List<List<Int>>,
  7.     n: Int = tab.size - 1,
  8.     m: Int = tab.first().size - 1,
  9.     dp: List<MutableList<Pair<Int, Int>>> = List(tab.size) { MutableList(tab.first().size) { invalid } }.also {
  10.         it[0][0] = tab[0][0] to tab[0][0]
  11.     }
  12. ): Pair<Int, Int> {
  13.     if (n < 0 || m < 0)
  14.         return invalid
  15.  
  16.     if (dp[n][m] != invalid)
  17.         return dp[n][m]
  18.  
  19.     dp[n][m] = run {
  20.         val f = dfs(tab, n - 1, m, dp)
  21.         val s = dfs(tab, n, m - 1, dp)
  22.         maxOf(f.first, s.first) to minOf(f.second, s.second)
  23.     }.let { (f, s) -> tab[n][m].let { f + it to s + it } }
  24.  
  25.     return dp[n][m]
  26. }
  27.  
  28. fun main() = dfs((0..9).map { readLine()!!.trim().split("\t").map { it.toInt() / 8 } })
  29.     .let { (f, s) -> println("MAX: ${f * 8}; MIN: ${s * 8}") }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement