Prefix Sum
To make all elements equal, we need to increase elements smaller than query q, and decrease larger ones.\n    \nWe sort our array first, so we can find index i 
To make all elements equal, we need to increase elements smaller than query `q`, and decrease larger ones.\n    \nWe sort our array first, so we can find index `i` of first element larger than `q`.\n    \nThen, we use the prefix sum array to get:\n- sum of smaller elements `ps[i]`.\n- sum of larger elements `ps[-1] - ps[i]`.\n    \nThe number of operations is:\n- `q * i - ps[i]` for smaller elements.\n- `ps[-1] - ps[i] - q * (n - i)` for larger elements.\n    \n**Python 3**\n```python\nclass Solution:\n    def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n        nums.sort()\n        ps, n = [0] + list(accumulate(nums)), len(nums)\n        splits = [(q, bisect_left(nums, q)) for q in queries]\n        return [q * i - ps[i]  + (ps[-1] - ps[i]) - q * (n - i) for q, i in splits] \n```\n**C++**\n```cpp\nvector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n    vector<long long> ps{0}, res;\n    sort(begin(nums), end(nums));\n    for (int n : nums)\n        ps.push_back(ps.back() + n);\n    for (long long q : queries) {\n        int i = upper_bound(begin(nums), end(nums), q) - begin(nums);\n        res.push_back(q * i - ps[i] + (ps.back() - ps[i]) - q * (nums.size() - i));\n    }\n    return res;\n}\n```
['C', 'Python3']