Heap + HashMap solution in Java
Code\njava []\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] queries) {\n        int n = queries.length;\n        long[] answer = new
# Code\n```java []\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] queries) {\n        int n = queries.length;\n        long[] answer = new long[n];\n\n        long total = 0;\n        Set<Integer> markedIndices = new HashSet<>();\n        \n        PriorityQueue<Integer> pq = new PriorityQueue<>((i1, i2) -> {\n            if (nums[i1] != nums[i2]) {\n                return nums[i1] - nums[i2];\n            }\n            return i1 - i2;\n        });\n        for (int i = 0; i < nums.length; i++) {\n            total += nums[i];\n            pq.add(i);\n        }\n\n        for (int i = 0; i < queries.length; i++) {\n            if (pq.isEmpty()) {\n                break;\n            }\n            \n            int[] q = queries[i];\n            int index = q[0];\n            int k = q[1];\n\n            if (markedIndices.add(index)) {\n                total -= nums[index];\n            }\n\n            while (!pq.isEmpty() && k != 0) {\n                index = pq.remove();\n                if (markedIndices.add(index)) {\n                    k--;\n                    total -= nums[index];\n                }\n            }\n            \n            answer[i] = total;\n        }\n\n        return answer;\n    }\n}\n```
['Java']