scala solution
scala []\nobject Solution {\n  def minOperations(nums: Array[Int], queries: Array[Int]): List[Long] = {\n    val sorted = nums.to(Vector).map(_.toLong).sorted\n
```scala []\nobject Solution {\n  def minOperations(nums: Array[Int], queries: Array[Int]): List[Long] = {\n    val sorted = nums.to(Vector).map(_.toLong).sorted\n    val pfx = sorted.iterator.drop(1).scanLeft((sorted.head,0L,1)){case ((last,sum,cnt), num) =>\n      (num,sum+(num-last).abs*cnt,cnt+1)\n    }.to(Vector)\n    val sfx = sorted.reverseIterator.drop(1).scanLeft((sorted.last,0L,1)){case ((last,sum,cnt), num) =>\n      (num,sum+(num-last).abs*cnt,cnt+1)\n    }.to(Vector).reverse\n    queries.toList.map(_.toLong).map{q =>\n      val (y0,y1) = (\n        pfx.search((q,Long.MinValue,Int.MinValue)).insertionPoint,\n        sfx.search((q,Long.MaxValue,Int.MaxValue)).insertionPoint)\n      if(y0!=y1) pfx(y0)._2 + sfx(y1-1)._2\n      else if(y0==sfx.size) pfx.last._2 + (q-pfx.last._1).abs*pfx.last._3\n      else if(y0==0) sfx.head._2 + (sfx.head._1-q).abs*sfx.head._3\n      else pfx(y0-1)._2 + (q-pfx(y0-1)._1)*pfx(y0-1)._3 + sfx(y1)._2 +(sfx(y1)._1-q)*sfx(y1)._3\n    }\n  }\n}\n```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'Scala']