C++ Binary search
\nclass Solution {\n    bool isPossible(int maxSeconds,const vector<int>& nums,const vector<int>& changeIndices)\n    {\n        vector<int> lastIndex(nums.size
```\nclass Solution {\n    bool isPossible(int maxSeconds,const vector<int>& nums,const vector<int>& changeIndices)\n    {\n        vector<int> lastIndex(nums.size(),-1),seq(nums.size());\n        for (int i=0;i<nums.size();i++) seq[i]=i;\n        for(int i=0;i<maxSeconds;i++){\n            lastIndex[changeIndices[i]-1]=i;\n        }\n        \n        for (int i=0;i<nums.size();i++) if (nums[i]==-1) return false;\n        \n        sort(seq.begin(),seq.end(),[&lastIndex](int f, int s){return lastIndex[f]<lastIndex[s];});\n        \n        int used=0;\n        for(int i=0;i<nums.size();i++){\n            if (used+nums[seq[i]]<=lastIndex[seq[i]]){\n                used+=nums[seq[i]]+1;\n            }\n            else return false;\n        }\n        return true;\n    }\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        int sz = nums.size();\n        int l=0,m=0,h=changeIndices.size();\n        int ans=-1;\n        while(l<=h){\n            m=(l+h)/2;\n            if (isPossible(m,nums,changeIndices)){\n                h=m-1;\n                ans=m;\n            }\n            else{\n                l=m+1;\n            }\n        }\n        return ans;\n        \n    }\n};\n```
[]