ugly scala solution with mutable Array to avoid MLE
scala\nobject Solution {\n  val bMap = Array.fill(2,10_000)(42,42)\n  def matrixSumQueries(n: Int, queries: Array[Array[Int]]): Long =\n    def fill(i:Int=0 , j
```scala\nobject Solution {\n  val bMap = Array.fill(2,10_000)(42,42)\n  def matrixSumQueries(n: Int, queries: Array[Array[Int]]): Long =\n    def fill(i:Int=0 , j:Int=0): Unit =\n      if(i<2) if(j>=n) fill(i+1,0) else { bMap(i)(j) = (-1,0); fill(i, j + 1)}\n    def sum(i:Int=0 , j:Int=0, acc:Long=0L): Long =\n      if(i>=n) acc\n      else if(j>=n) sum(i+1,0,acc)\n      else sum(i,j+1, acc + (if(bMap(0)(i)._1 > bMap(1)(j)._1) bMap(0)(i)._2 else bMap(1)(j)._2))\n    fill()\n    queries.zipWithIndex.foreach{ case (a,i) => bMap(a(0))(a(1)) = (i,a(2))}\n    sum()\n\n  def matrixSumQueriesMLE(n: Int, queries: Array[Array[Int]]): Long =\n    val bMap = Array.fill(2,n)(-1,0)\n    queries.zipWithIndex.foreach{ case (a,i) => bMap(a(0))(a(1)) = (i,a(2))}\n    def sum(i:Int=0 , j:Int=0, acc:Long=0L): Long = {\n      lazy val y0 = bMap(0)(i)\n      lazy val y1 = bMap(1)(j)\n      if(i>=n) acc\n      else if(j>=n) sum(i+1,0,acc)\n      else sum(i,j+1, acc + (if(y0._1 > y1._1) y0._2 else y1._2))\n    }\n    sum()\n\n  def matrixSumQueriesMap(n: Int, queries: Array[Array[Int]]): Long =\n    lazy val aMap = queries.zipWithIndex.map{\n      case (a,i) => (a(0),a(1)) -> (i,a(2)) }.to(Map)\n      //.withDefaultValue(-1,0)\n    lazy val ans = (for(i <- 0 until n; j <- 0 until n) yield {\n      //List(aMap((0,i)), aMap((1,j))).max._2\n      if(aMap((0,i))._1 > aMap((1,j))._1) aMap((0,i))._2 else aMap((0,i))._1\n    }).foldLeft(0L)(_ + _)\n    def sum(i:Int=0 , j:Int=0, acc:Long=0L): Long = {\n      lazy val y0 = aMap.getOrElse((0,i), (-1,0))\n      lazy val y1 = aMap.getOrElse((1,j), (-1,0))\n      if(i>=n) acc\n      else if(j>=n) sum(i+1,0,acc)\n      else sum(i,j+1, acc + (if(y0._1 > y1._1) y0._2 else y1._2))\n      //else sum(i,j+1, acc + (if(aMap((0,i))._1 > aMap((1,j))._1) aMap((0,i))._2 else aMap((0,i))._1))\n      // else sum(i,j+1, acc + Seq(aMap((0,i)), aMap((1,j))).max._2)\n    }\n    sum()\n}\n```
['Scala']