[Java] ✅ 97% ✅ REFERENCE TRICK ✅ PRO SOLUTION ✅ FASTEST ✅ BEST
Approach\n1. Create a class Element that holds a boolean flag (marked) and the value of the elememnt of nums\n2. Create two arrays: indexedElements and sortedEl
# Approach\n1. Create a class Element that holds a boolean flag (marked) and the value of the elememnt of nums\n2. Create two arrays: indexedElements and sortedElements of type Element of size nums.length\n3. Traverse nums and populate these two arrays with the SAME referenced object idx[i] = srtd[i] = new Element(value);\n    - Also calculate the sum of all nums. The unmarkedSum will be the diff between arraySum and markedSum.\n4. Sort the sortedElemens by value in increasing order.\n5. For each query\n    - if indexElements[q[0]] is NOT marked, set it as marked and add the value to a markedSum\n    - use an index markedIndex to keep track of last index of  sortedElements. Move that index query[1] times, as long as sortedElements[markedIndex].marked =  false and add that value to a markedSum\n    - set the diff between arraySum and markedSum in the answer array.\n\n# Complexity\n- Time complexity:$$O(n * log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] queries) {\n        long arraySum = 0L;\n        Element[] indexedElements = new Element[nums.length];\n        Element[] sortedElements = new Element[nums.length];\n        for (int i = 0; i < nums.length; i++) {\n            indexedElements[i] = sortedElements[i] = new Element(nums[i]);\n            arraySum += nums[i];\n        }\n        Arrays.sort(sortedElements, Comparator.comparingInt(a -> a.value));\n        return calculateUnmarkedSumArray(nums, queries, arraySum, indexedElements, sortedElements);\n    }\n\n    private long[] calculateUnmarkedSumArray(int[] nums, int[][] queries, long arraySum, Element[] indexedElements, Element[] sortedElements) {\n        long[] unmarkedSums = new long[queries.length];\n        long markedSum = 0L;\n        int markingIndex = 0;\n        for (int i = 0; i < queries.length && markingIndex < nums.length; i++) {\n            if (!indexedElements[queries[i][0]].marked) {\n                markedSum += indexedElements[queries[i][0]].value;\n                indexedElements[queries[i][0]].marked = true;\n            }\n            for (int j = 0; j < queries[i][1] && markingIndex < nums.length; markingIndex++) {\n                if (!sortedElements[markingIndex].marked) {\n                    j++;\n                    sortedElements[markingIndex].marked = true;\n                    markedSum += sortedElements[markingIndex].value;\n                }\n            }\n            unmarkedSums[i] = arraySum - markedSum;\n        }\n        return unmarkedSums;\n    }\n\n    private static class Element {\n        boolean marked;\n        int value;\n        public Element(int value) {\n            this.value = value;\n        }\n    }\n}\n```
['Java']