a few solutions
Game Plan\n\nGreedily "consume" the smallest values first, ie. use a min heap q to store each index and value, i,x of the input array A, tracking ith indexes se
# Game Plan\n\nGreedily "consume" the smallest values first, ie. use a min heap `q` to store each index and value, `i`,`x` of the input array `A`, tracking `i`<sup>th</sup> indexes `seen`, while decrementing the total `T` (where `T` is initially set to the accumulated sum of `A`).\n\nNote: we lazily "discard" `seen` values from the min heap `q`\n\n# Code\n\n*Python3*\n```\nclass Solution:\n    def unmarkedSumArray(self, A: List[int], queries: List[List[int]]) -> List[int]:\n        ans, T, seen = [], sum(A), set()\n        q = [(x, i) for i, x in enumerate(A)]; heapify(q)\n        for i, cnt in queries:\n            if i not in seen:\n                T -= A[i]; seen.add(i)\n            for _ in range(cnt):\n                while len(q) and q[0][1] in seen:  # lazily discard seen values\n                    heappop(q)\n                if len(q):\n                    x, i = heappop(q)\n                    if i not in seen:\n                        T -= x; seen.add(i)\n            ans.append(T)\n        return ans\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using LL = long long;\n    using VL = vector<LL>;\n    using VI = vector<int>;\n    using VVI = vector<VI>;\n    using Pair = pair<int, int>;\n    struct Cmp { size_t operator()(const Pair& a, const Pair& b) { return a.first == b.first ? b.second < a.second : b.first < a.first; } };\n    using Queue = priority_queue<Pair, vector<Pair>, Cmp>;\n    using Set = unordered_set<int>;\n    VL unmarkedSumArray(VI& A, VVI& queries, Queue q = {}, Set seen = {}, VL ans = {}) {\n        auto t = accumulate(A.begin(), A.end(), 0LL);\n        for (auto i{ 0 }; i < A.size(); ++i)\n            q.push({ A[i], i });\n        for (auto& pair: queries) {\n            auto [i, cnt] = tie(pair[0], pair[1]);\n            if (seen.insert(i).second)\n                t -= A[i];\n            while (q.size() && cnt) {\n                auto [_, i] = q.top(); q.pop();\n                if (seen.insert(i).second)\n                    t -= A[i], --cnt;\n            }\n            ans.push_back(t);\n        }\n        return ans;\n    }\n};\n```
['Python3']