Binary seach+greedy
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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @param {number[]} changeIndices\n * @return {number}\n */\nvar earliestSecondToMarkIndices = function (nums, changeIndices) {\n    const check = (nums, m, total) => {\n        const memo = new Array(n).fill(-1)\n        let needtodelete = 0, count = 0\n        for (let i = m - 1; i >= 0; i--) {\n            if (memo[changeIndices[i] - 1] === -1) {\n                needtodelete += nums[changeIndices[i] - 1]\n                memo[changeIndices[i] - 1] = nums[changeIndices[i] - 1]\n                count++\n                if (needtodelete + total - count > i) {\n                    return false\n                }\n            } else {\n                if (memo[changeIndices[i] - 1] >= 0) {\n                    if (needtodelete > 0)\n                        needtodelete--\n                }\n            }\n        }\n        if (count < total) {\n            return false\n        }\n        return true\n    }\n    let sum = 0\n    const n = nums.length\n    for (let i = 0; i < nums.length; i++) {\n        sum += nums[i]\n    }\n    let l = sum + n, r = changeIndices.length\n    if (l > r) {\n        return -1\n    }\n    while (l <= r) {\n        const mid = Math.floor(l + (r - l) / 2)\n        if (!check(nums, mid, n)) {\n            l = mid + 1\n        } else {\n            r = mid - 1\n        }\n    }\n    return ((l > changeIndices.length) || (l < 0) ? -1 : l)\n};\n```
['JavaScript']