[C++] Simple || Intuitive || Binary Search || Clear Code
\n# Code\n\nclass Solution {\npublic:\n    #define ll long long\n    #define F first\n    #define S second\n    \n    bool check(ll m,vector<int> &nums,vector<i
\n# Code\n```\nclass Solution {\npublic:\n    #define ll long long\n    #define F first\n    #define S second\n    \n    bool check(ll m,vector<int> &nums,vector<int>& changeIndices){\n        unordered_map<ll,ll> mp;\n        priority_queue<pair<ll,ll>> pq;\n        \n        for(int i=m-1;i>=0;i--){\n            int index = changeIndices[i] - 1;\n            \n            if(mp.find(index) == mp.end()){\n                mp[index] = 0;\n                pq.push({nums[index],index});\n            }\n            else{\n                while(!pq.empty() && pq.top().F == 0){\n                    pq.pop();\n                }\n                if(!pq.empty()){\n                    auto top = pq.top();\n                    pq.pop();\n                    \n                    top.F-=1;\n                    mp[top.S]++;\n                    pq.push(top);\n                }\n            }\n        }\n        \n        if(mp.size() != nums.size()) return false;\n        \n        for(int i=0;i<nums.size();i++){\n            if(mp[i] < nums[i]) return false;\n        }\n        return true;\n    }\n    \n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        ll n=nums.size();\n        ll m=changeIndices.size();\n        ll sum = 0;\n        for(auto &c: nums){\n            sum = sum + c;\n        if(sum + n >m) return -1;\n        }\n        \n        ll ans =  m+1 ;\n        \n        ll low = n, high = m;\n        \n        while(low <= high){\n            ll mid = low + (high - low)/2 ;\n            \n            if(check(mid,nums,changeIndices)){\n                ans = min(ans, mid);\n                high = mid -1;\n            }\n            else{\n                low = mid + 1;\n            }\n            \n        }\n        return ans>m ? -1: ans;\n    }\n};\n```
['C++']