🐍Python Solution: Sort+PrefixSum+BinSearch
\n\n# Code\n\nclass Solution:\n    def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n        nums.sort()\n        prefix_sum = [nums[0
\n\n# Code\n```\nclass Solution:\n    def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n        nums.sort()\n        prefix_sum = [nums[0]]\n        ans = []\n        for i in range(1, len(nums)):\n            prefix_sum.append(prefix_sum[-1] + nums[i])\n        \n        for target in queries:\n            l = 0\n            r = len(nums) - 1\n            pos = -2 # -2 for not found case\n            #left bin search             \n            while l <= r:\n                mid = (l + r) // 2\n                if nums[mid] > target:\n                    r = mid - 1\n                elif nums[mid] < target:\n                    l = mid + 1\n                else:\n                    pos = mid\n                    r = mid - 1     \n\n            if pos == -2:\n                pos = min(l, r)\n            # pos == -1 leftmost case\n            if pos == -1:\n                ans.append(prefix_sum[-1] - (len(nums))*target)\n            else:\n                ans.append((pos+1)*target - prefix_sum[pos] + prefix_sum[-1] - prefix_sum[pos] - (len(nums) -1 - pos)*target)\n            \n        return ans\n```
['Python3']