C++ | O(mlogm) complexity | mark from last
\n\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& ch) {\n        int n = nums.size();\n        int m = ch.size(
\n```\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& ch) {\n        int n = nums.size();\n        int m = ch.size();\n        \n        int res = INT_MAX;\n        int l=1, h=m;\n        while(l<=h){\n            int mid = l+(h-l)/2;\n            if(check(nums, ch, mid)){\n                res = min(res, mid);\n                h = mid-1;\n            }\n            else l = mid+1;\n        }\n\n        return res==INT_MAX ? -1:res;\n    }\n    bool check(vector<int> &nums, vector<int> &ch, int t){\n        long long extra = 0;\n        int n = nums.size();\n        vector<bool> v(n, false);\n\n        int tmp = 0;\n        for(int i=t-1; i>=0; i--){\n            int ind = ch[i]-1;\n            if(v[ind]) extra = max(extra-1, (long long)0);\n            else{\n                extra += nums[ind];\n                v[ind] = true;\n                n--;\n            }\n        }\n\n        return n==0 && extra == 0;\n    }\n};\n```
['C++']