Rust, Python. (n + m) log n. Detailed explanation with math
Intuition\nLets look how to address this for one query and the value is $v$.\n\n- If the numbers are below $v$. Then the results are $v - a_1 + v - a_2 + ... v 
# Intuition\nLets look how to address this for one query and the value is $v$.\n\n- If the numbers are below $v$. Then the results are $v - a_1 + v - a_2 + ... v - a_n = v \\cdot n -  \\sum_{i=1}^{n} a_i $\n- If the numbers are above $v$. With similar logic, results are $\\sum_{i=1}^{n} a_i - v \\cdot n$\n\nSo we need to find how many numbers are below and above the $v$ and prefix sums to be able to quickly find sums. To be do both of those efficiently you need to sort array of numbers.\n\nAfter the sorting you will have:\n\n$a_1, a_2, ..., a_k$ **v** $a_{k + 1}, ..., a_n$\n\nThe result is:\n\n$v \\cdot k - \\sum_{i=1}^{k} a_i + s - \\sum_{i=1}^{k} a_i - v\\cdot n + v \\cdot k = s + 2 vk -vn - 2 \\sum_{i=1}^{k} a_i = s + v (2k - n) - 2 \\sum_{i=1}^{k} a_i$\n\nThe value of `s` is a full sum: `prefix[-1]`. The value of sum is `prefix[k]`. \n\n# Complexity\n- Time complexity: $O((n + m) \\log n)$\n- Space complexity: $O(n)$\n\n\n```Rust []\nimpl Solution {\n  pub fn min_operations(mut nums: Vec<i32>, queries: Vec<i32>) -> Vec<i64> {\n    nums.sort_unstable();\n    let n = nums.len();\n\n    let mut prefix: Vec<i64> = vec![0; n + 1];\n    for i in 0 .. n {\n      prefix[i + 1] = prefix[i] + nums[i] as i64;\n    }\n\n    let mut res: Vec<i64> = vec![0; queries.len()];\n    for i in 0 .. queries.len() {\n      let k = match nums.binary_search(&queries[i]) {\n        Err(k) => k,\n        Ok(k)  => k,\n      };\n      res[i] = prefix[n] + (queries[i] as i64) * (2 * k - n) as i64 - 2 * prefix[k];\n    }\n\n    return res;\n  }\n}\n```\n```python []\nfrom bisect import bisect_right\n\nclass Solution:\n  def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n    nums.sort()\n\n    n = len(nums)\n    prefix = [0] * (n + 1)\n    for i, v in enumerate(nums):\n      prefix[i + 1] = prefix[i] + v\n\n    res, s = [0] * len(queries), prefix[-1]\n    for i, v in enumerate(queries):\n      k = bisect_right(nums, v)\n      res[i] = s + v * (2 * k - n) - 2 * prefix[k]\n    \n    return res\n```\n\n\nQuestion to rust pro (this is my first weeks, so not so fluent). Is there a way to combine two arms in match (err/ok)?\n
['Math', 'Python', 'Rust']