C++, prefix sum
\n# Code\n\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(), nums.end());\n
\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(), nums.end());\n        vector<long long> sums(nums.size(), 0);\n        sums[0] = nums[0];\n        for(int i = 1; i < nums.size(); i++) sums[i] = nums[i] + sums[i-1];\n        vector<long long> ans(queries.size(), 0);\n        for(int i = 0; i < queries.size(); i++){\n            auto it = upper_bound(nums.begin(), nums.end(), queries[i]);\n            long long index = (int)(it - nums.begin());\n            long long v1 = queries[i] * index;\n            long long v2 = queries[i] * (nums.size()-index);\n            long long res1 = 0;\n            if(index != 0) res1 = v1 - sums[index - 1];\n            long long res2 = 0;\n            if(index == 0) res2 = sums[sums.size()-1] - v2;\n            else if(index != nums.size()) res2 = sums[sums.size()-1] - sums[index - 1] - v2;\n            ans[i] = res1 + res2;\n        }\n        return ans;\n    }\n};\n```
['C++']