C++ solution
Code\n\ntypedef long long ll;\n\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.be
# Code\n```\ntypedef long long ll;\n\nclass Solution {\npublic:\n    vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n        sort(nums.begin(), nums.end());\n\n        ll n = nums.size();\n        vector<ll> pref(n);\n        pref[0] = nums[0];\n        for(int i=1; i<n; i++)\n            pref[i] = pref[i - 1] + nums[i];\n\n        vector<ll> suff(n);\n        suff.back() = nums.back();\n        for(int i=n-2; i>=0; i--)\n            suff[i] = nums[i] + suff[i + 1];\n\n        vector<ll> res(queries.size());\n        for(ll i=0; i<queries.size(); i++) {\n            ll l = lower_bound(nums.begin(), nums.end(), queries[i]) - nums.begin();\n            if(l > 0)\n                res[i] = (queries[i] * l) - pref[l - 1];\n            ll h = upper_bound(nums.begin(), nums.end(), queries[i]) - nums.begin();\n            if(h < n)\n                res[i] += suff[h] - (queries[i] * (n - h));  \n        }\n\n        return res;\n    }\n};\n```
['C++']