Ordered Dictionary: Beats 100% time
\n# Intuition\n Describe your first thoughts on how to solve this problem. \nSince the problem requires that we keep track of the information for every num in n
![image.png](https://assets.leetcode.com/users/images/3b16fc65-c2cc-426f-8a86-5a6da90dbfe2_1710608870.0252728.png)\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince the problem requires that we keep track of the information for every num in nums (position, mark, value) i decied to keep position, value as dictionary and mark as a list. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort a dictionary by the values and then by keys; such that keys are the positional indexes of the nums, and the values are the nums themselves.\n\nWhenever we need to mark a num we delete the key val pair with key=marked[key]. after that we remove the smallest items in the ordered dict and mark the respective indexes as true.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(max( len(queries)*max(queries[:, 1]), len(nums)*log(len(nums)))$$\nSorting is $$O(n*log(n))$$ and the rest of the algorithm is $$O(max( len(queries)*max(queries[:, 1])$$ where max(queries[:, 1]) = max(k) from the problem explanation\n \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def unmarkedSumArray(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n        ans = [0] * len(queries)\n        marked = [False] * len(nums)\n        num_dict = {}\n        for i, n in enumerate(nums):\n            num_dict[i] = n\n        num_dict = dict(sorted(num_dict.items(), key= lambda it: (it[1], it[0]), reverse=True))\n        # print(num_dict)\n        for i, l in enumerate(queries):\n            idx, k = l[0], l[1]\n            if not marked[idx]:\n                marked[idx] = True\n                del num_dict[idx]\n            for _ in range(k):\n                if not num_dict:\n                    break\n                index, val = num_dict.popitem()\n                # print(index, val)\n                marked[index] = True\n            if num_dict:\n\n                ans[i] = sum(num_dict.values())\n        return ans\n            \n```
['Python3']