Very Easy Solution + Explanation ✅🔥|| C++ || Beats 100%🔥
\n\n# Intuition\nSo in this question we need to deal with a lot of things, first the smallest values, second the indices. When we see "k smallest" or "k largest
![leetcode img.png](https://assets.leetcode.com/users/images/c1270b34-53de-408b-a1b4-85fa17491b49_1710609658.3674772.png)\n\n# Intuition\nSo in this question we need to deal with a lot of things, first the smallest values, second the indices. When we see "k smallest" or "k largest" we tend to think of using heap. How can we solve this problem using heaps??\n\n# Approach\n1) Keep track of visited and not visited indices using set.\n2) Initialize a min heap and store all the values and their indices, as heap automatically sorts both of them. ie; if we insert [{1,3},{1,1},{2,2},{1,0}], the order in the heap is : [{1,0},{1,1},{1,3},{2,2}].\n3) Iterate throught the queries and insert the index into the visited set and erase it from unvisited.\n4) Now iterate the heap for k times as we need to mark k smallest elements as visited.\n5) If the top value is already visited, then we can not include it in k smallest elements, hence remove it from the heap and DO NOT decrement the value k.\n6) Else insert the top.second ie the index into the visited set and remove it from the unvisited set and pop the top out with decrementing k.\n7) Now as the question said, the summation result might be more than the range of integer, hence declare a long long sum and calculate the summation of elements which their indices in unvisited set.\n8) Push it into the answer vector and return it.\n\n# Complexity\n- Time complexity:\n$$O(n*n)$$\n- Space complexity:\n$$O(n + n)$$\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> unmarkedSumArray(vector<int>& nums, vector<vector<int>>& queries) {\n    set<int> vis, unvis;\n    for(int i = 0; i<nums.size(); i++) unvis.insert(i);\n    \n    priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>> > pq; // value,ind\n    for(int i = 0; i<nums.size(); i++)\n    {\n        pq.push({nums[i],i});\n    }\n    \n    vector<long long> ans;\n    for(auto i: queries)\n    {\n        int ind = i[0];\n        int k = i[1];\n        vis.insert(ind);\n        unvis.erase(ind);\n        while(pq.size() and k)\n        {\n            if(vis.find(pq.top().second) != vis.end())\n            {\n                pq.pop();\n            }\n            else\n            {\n                vis.insert(pq.top().second);\n                unvis.erase(pq.top().second);\n                pq.pop();\n                k--;\n            }\n        }\n        long long sum = 0;\n        for(auto j: unvis)\n        {\n            sum += nums[j];\n        }\n        ans.push_back(sum);\n    }\n    return ans;\n        \n}\n};\n```
['Heap (Priority Queue)', 'C++']