Java Solution || TreeMap
\n# Code\n\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] q) {\n        HashSet<Integer> hs=new HashSet<>(); // To store all visited 
\n# Code\n```\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] q) {\n        HashSet<Integer> hs=new HashSet<>(); // To store all visited index\n        long sum=0;\n        \n        int[] check=new int[nums.length]; // Store index as per smallest value\n        long[] ans=new long[q.length];\n        for(int e:nums)\n            sum+=e;\n        TreeMap<Integer,ArrayList<Integer>> tm=new TreeMap<>();\n        // TreeMap to get the nums value in sorted order as key and all index storing that key is stored in arraylist as values.\n        // For nums=[1,2,2,1,2,3,1]\n        // tm = (1 (0,3,6), 2 (2,4) , 3 (5)) --> (key, value) format\n        for(int i=0;i<nums.length;i++){\n            if(!tm.containsKey(nums[i])){\n                ArrayList<Integer> arr=new ArrayList<>();\n                arr.add(i);\n                tm.put(nums[i],arr);\n            }\n            else{\n                tm.get(nums[i]).add(i);\n            }\n        }\n        int ind=0;\n        for (Map.Entry<Integer,ArrayList<Integer>> e : tm.entrySet()){\n            ArrayList<Integer> a=e.getValue();\n            for(int h=0;h<a.size();h++){\n                check[ind]=a.get(h);\n                ind++;\n            }\n        }\n        int now=0;\n        for(int i=0;i<q.length;i++){\n            if(!hs.contains(q[i][0])){\n                sum-=nums[q[i][0]];\n                hs.add(q[i][0]);\n            }\n                \n            int count=q[i][1];\n            for(int j=0;j<count;j++){\n                if(now!=check.length){\n                    if(!hs.contains(check[now])){\n                        sum-=nums[check[now]];\n                        hs.add(check[now]);\n                    }\n                    else\n                        j--; // if it already contains then we need one more round of iteration\n                    now++;\n                }\n            }\n            ans[i]=sum;\n        }\n        return ans;\n    }\n}\n```
['Ordered Map', 'Java']