Heap for (n, i), Set for marked indices
image.png\n\n\n# Code\n\nclass Solution:\n    def unmarkedSumArray(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n        sum_nums = sum(nums)\
image.png\n![image.png](https://assets.leetcode.com/users/images/6614cb74-56e2-4e71-87e9-22b1fec03a44_1713361215.9399302.png)\n\n# Code\n```\nclass Solution:\n    def unmarkedSumArray(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n        sum_nums = sum(nums)\n        sum_mark = 0\n        set_mark = set()\n        ans = [0] * len(queries)\n        h = []\n        for i, n in enumerate(nums):\n            heappush(h,(n, i))\n\n        for ans_i, (idx, k) in enumerate(queries):\n            if not h:\n                break\n            if idx not in set_mark:\n                set_mark.add(idx)\n                sum_mark += nums[idx]\n            for _ in range(k):\n                if not h:\n                    break\n                val, j = heappop(h)\n                while j in set_mark and h:\n                    val, j = heappop(h)\n                if j not in set_mark:\n                    sum_mark += val\n                    set_mark.add(j)\n            ans[ans_i] = sum_nums - sum_mark\n\n        return ans\n```
['Python3']