Binary search | beats 99% | O(m*n*log(n))
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(m*n*log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m+n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n\n    def earliestSecondToMarkIndices(self, nums: List[int], changeIndices: List[int]) -> int:\n        n, m = len(nums), len(changeIndices)\n        if len(set(changeIndices)) < n or sum(nums) + n > m:\n            return -1\n        indices = [list() for _ in range(n+1)]\n        for i, v in enumerate(changeIndices):\n            indices[v].append(i)\n\n        for end in range(sum(nums) + n - 1, m):\n            last_indices = []\n            for v in range(1, n+1):\n                idx = bisect.bisect_right(indices[v], end) - 1\n                if idx < 0:\n                    break\n                last_indices.append(indices[v][idx])\n            if len(last_indices) < n:\n                continue\n            last_indices.sort()\n            s = 0\n            for idx in last_indices:\n                if idx < s + nums[changeIndices[idx]-1]:\n                    break\n                s += nums[changeIndices[idx]-1] + 1\n            else:\n                return end + 1\n        return -1\n\n```
['Python3']