Using Min Heap, easy to understand
Intuition\nQuestion has clearly stated that we have to pick the k smallest values from the array. So we will be using min heap for it.\n\n# Code\n\nclass Soluti
# Intuition\nQuestion has clearly stated that we have to pick the k smallest values from the array. So we will be using min heap for it.\n\n# Code\n```\nclass Solution {\npublic:\n    typedef long long ll;\n    vector<long long> unmarkedSumArray(vector<int>& nums, vector<vector<int>>& queries) {\n        ll sum = 0;\n\n        // take the sum of all elements\n        for(auto &i : nums) sum += i;\n        vector<ll> ans;\n\n        // for checking whether the element is visited ot not\n        vector<bool> check(nums.size(), false);\n        \n        // min heap since we have to take minimum two elements\n        priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> h;\n        \n        for(int i = 0; i < nums.size(); i++){\n            h.push({nums[i], i});\n        }\n\n        int cnt = nums.size();\n        \n        for(int i = 0; i < queries.size(); i++){\n            int idx = queries[i][0];\n            int k = queries[i][1];\n            \n            // if not visited\n            if(!check[idx]){\n                sum -= nums[idx];\n                check[idx] = true;\n                cnt--;\n            }\n\n\n            if(cnt < k && cnt > 0){\n                // less than k elements left so take all of them and mark all\n                for(int i = 0; i < nums.size(); i++){\n                    if(!check[i]){\n                        sum -= nums[i];\n                        check[i] = true;\n                        cnt--;\n                    }\n                }\n            }\n            else{    \n                // take top 2 elements and if already visited then simply pop it\n                // no need to consider it\n                while(k > 0 && h.size() > 0){\n                    int ind = h.top().second;\n                    if(!check[ind])\n                    {\n                        sum -= h.top().first;\n                        check[ind] = true;\n                        cnt--;\n                        k--;\n                    }\n                    h.pop();\n                }\n            }\n            // push it into ans\n            ans.push_back(sum);\n        }\n        return ans;\n        \n    }\n};\n```
['C++']