Easy c++ solution using binary search and prefix sum.
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can solve each query in log n time using binary search (upper_bound) and prefix sum 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can solve each query in log n time using binary search (upper_bound) and prefix sum in sorted array.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the array.\n2. Calculate prefix sum array of size n+1, and calcculate sum of all element along with that.\n3. Find upper bound for each query. check if query is greater then maximum element then push ans as n*query - total sum, if query is smaller then smallest element of array then push ans as total sum- n*query.\n4. If above two condition not satify then we will use upper bound to divide the array into two parts, in first part we will find sum of first part using index of upper bound and prefix sum and subtract first part sum with (ind)*query where ind is index of upper bound. and in second part subtract (n-ind)*query from sum of seccond part. Add both part and push into ans.\n5. return the ans\n\n# Complexity\n- Time complexity: O(N*LOGN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        vector<long long> ans,pre;\n        sort(nums.begin(),nums.end());\n        long long sum=0;\n        pre.push_back(0);\n        for(int i=0;i<nums.size();i++)\n        {\n            sum+=nums[i];\n            pre.push_back(sum);\n        }\n        int n=nums.size();\n        for(int i=0;i<queries.size();i++)\n        {\n           int c=queries[i];\n           auto b=upper_bound(nums.begin(),nums.end(),c);\n           if(c>=nums[n-1])\n           {\n              ans.push_back((long long)n*(long long)c  - sum);\n           }\n           else if(c<=nums[0])\n           {\n               ans.push_back(sum-(long long)n*(long long)c);\n           }\n           else\n           {\n               int ind=b-nums.begin();\n               long long  res=abs((sum-pre[ind])-(long long)(n-ind)*(long long)c);\n               res+=abs((long long)ind*(long long)c -pre[ind]);\n               ans.push_back(res);\n           }\n       }\n        return ans;\n    }\n};\n```
['C++']