Python3 O(N + MlogN) Solution (no bisect tricks)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n# Complexity\n- Time complexity: O(N +MlogN)\n- Space complexity: O(N)\
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n# Complexity\n- Time complexity: O(N +MlogN)\n- Space complexity: O(N)\n\n# Code\n```\nclass Solution:\n    def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n        answer = []\n        nums.sort()\n        leftSums = [0] * len(nums)\n        rightSums = [0] * len(nums)\n\n        leftSums[0] = nums[0]\n        for i in range(1, len(nums)):\n            leftSums[i] = leftSums[i - 1] + nums[i]\n\n        rightSums[len(rightSums) - 1] = nums[-1]\n        for i in range(len(nums) - 2, -1, -1):\n            rightSums[i] = rightSums[i + 1] + nums[i]\n\n        for n in queries:\n            # Binary search to find i where nums[i] <= n\n            left = 0\n            right = len(nums) - 1\n            closest = -1\n            while left <= right:\n                mid = (left + right) // 2\n                if nums[mid] <= n:\n                    closest = mid\n                    left = mid + 1\n                else:\n                    right = mid - 1\n\n            smaller, larger = 0, 0\n            if closest >= 0:\n                smaller = abs(leftSums[closest] - (n * (closest + 1)))\n            if closest + 1 < len(nums):\n                larger = abs((len(nums) - closest - 1) * n - rightSums[closest + 1])\n\n            answer.append(smaller + larger)\n\n        return answer\n```
['Python3']