Java simple solution with explanation (binary search)
Intuition\n Describe your first thoughts on how to solve this problem. \nIn terms of approach, it is hard to imagine a greedy approach that would work. Dynamic 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn terms of approach, it is hard to imagine a greedy approach that would work. Dynamic programming might be possible but I didn\'t see any obvious way. That\'s for what didn\'t seem to work.\nOne greedy approach that seems to make sense though is to think in reverse time given an endtime. And then just adjust the endtime in a banry search fashion.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIf I want to end at time t, then the last action is going to be marking the corresponding index at that time t. If I am marking the index i at t, it mean I needed to do nums[i] decrements between 1 and t-1. This is what I called "debt", in the sense that since I am going backwards in time, marking the index at t forces me to pay back the debt nums[i] sometimes later.\nThe greedy approach is quite simple, going form t = time to t = 1, when the index can be marked (hasn\'t been marked before), then I do, that; otherwise, if debt is > 0 I do a decrement.\nAt the end of the feasibility test, the debt should be zero and all the indices need to be marked for the test to be successful.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nFeasible: O(m)\nTotal: O(m log(m))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)for the marked array in feasible().\n\n# Code\n```\nclass Solution {\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        int m = changeIndices.length;\n        int n = nums.length;\n        long sum = sum(nums);\n        if ((long) m < (long) n + sum) {\n            return -1;\n        }\n        int min = n + (int)sum;\n        int max = m;\n        if (!feasible(nums, changeIndices, max)) {\n            return -1;\n        }\n        return binarySearch(nums, changeIndices, min - 1, max);\n    }\n\n    private int binarySearch(int[] nums, int[] changeIndices, int below, int max) {\n        if (below + 1 >= max) {\n            return max;\n        }\n        int mid = (below + max) / 2;\n        if (feasible(nums, changeIndices, mid)) {\n            return binarySearch(nums, changeIndices, below, mid);\n        } else {\n            return binarySearch(nums, changeIndices, mid, max);\n        }\n    }\n\n    private boolean feasible(int[] nums, int[] changeIndices, int time) {\n        int n = nums.length;\n        boolean[] marked = new boolean[n];\n        long debt = 0;\n        int countMarked = 0;\n        for (int i = time-1; i >= 0; i--) {\n            int index = changeIndices[i] - 1;\n            if (!marked[index]) {\n                // If possible, mark\n                marked[index] = true;\n                countMarked++;\n                debt += nums[index];\n            } else if (debt > 0) {\n                // Otherwise pay down debt\n                debt--;\n            } else {\n                // nothing to do :(\n            }\n        }\n        return (countMarked == n) && (debt == 0);\n    }\n\n    private long sum(int[] nums) {\n        long sum = 0;\n        for (int num : nums) {\n            sum += num;\n        }\n        return sum;\n    }\n}\n```
['Java']