Simple JavaScript solution
Code\n\n/**\n * @param {number[]} nums\n * @param {number[]} changeIndices\n * @return {number}\n */\nvar earliestSecondToMarkIndices = function(nums, changeInd
# Code\n```\n/**\n * @param {number[]} nums\n * @param {number[]} changeIndices\n * @return {number}\n */\nvar earliestSecondToMarkIndices = function(nums, changeIndices) {\n    let result = -1\n    const freq = {},\n          numsLength = nums.length,\n          idxs = Array(numsLength).fill(0).map((_, i) => i + 1)\n    for (let i = 0; i < changeIndices.length; i++) {\n        if (freq[changeIndices[i]]) {\n            freq[changeIndices[i]].push(i)\n        }\n        else freq[changeIndices[i]] = [i]\n    }\n    const f = idxs.map(e => freq[e]).filter(e => e === undefined)\n    if (f.length > 0) return -1\n    while(changeIndices.length > 0) {\n        const last = idxs.map(e => [e, freq[e][freq[e].length - 1]]).sort((a, b) => a[1] - b[1])\n        let jobLeft = 0,\n            cando = true\n        for (let i = 0; i < last.length; i++) {\n            const [idx, rmAt] = last[i]\n            const jobRequire = nums[idx - 1]\n            jobLeft += i === 0 ? rmAt : last[i][1] - last[i - 1][1] - 1\n            jobLeft -= jobRequire\n            if (jobLeft < 0) {\n                cando = false\n                break\n            }\n        }\n        if (!cando) return result\n        result = changeIndices.length\n        const toRmv = changeIndices.pop()\n        if (freq[toRmv].length === 1) return result\n        else freq[toRmv].pop()\n    }\n    \n    \n};\n```
['JavaScript']