Binary Saerch
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// epqx\n// long long\nclass Solution {\npublic:\n    \n    bool chk(long long mid , long long sum , vector<int> nums,vector<int> ind)\n    {\n        long long cnt = nums.size();\n        map<long long,long long> m;\n        for(long long i = 0 ; i < mid ; i++)\n        {\n            m[ind[i]] = i;\n        }\n        long long tots = 0;\n        for(long long i = 0 ; i < mid ; i++)\n        {\n            if(m[ind[i]]==i)\n            {\n                long long rk = nums[ind[i]-1];\n                if(tots >= rk)\n                {\n                    tots -= rk;\n                    nums[ind[i]-1]=0;\n                    cnt--;\n                }\n                else\n                {\n                    return false;\n                }\n            }\n            else\n            {\n                tots++;\n            }\n        }\n        return cnt <= 0;\n    }\n    \n    long long earliestSecondToMarkIndices(vector<int>& nums, vector<int>& ind) {\n        long long n = nums.size();\n        long long m = ind.size();\n        \n        long long sum = 0;\n        for(auto i : nums)\n        {\n            sum += i;\n        }\n        long long l = sum;\n        long long r = m;\n        long long ans = -1;\n        while(l<=r)\n        {\n            long long mid = l + (r-l)/2;\n            if(chk(mid,sum,nums,ind))\n            {\n                ans = mid;\n                r = mid-1;\n            }\n            else\n            {\n                l = mid+1;\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']