ap
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```\nclass Solution {\npublic:\nint l(int mid,vector<int>&n,vector<int>&c)\n{\n    int cnt=n.size();\n    vector<int> vis(n.size(),0);\n    long long int sum=0;\n    for(int i=mid-1;i>=0;i--)\n    {\n        if(!vis[c[i]-1])\n        {\n           vis[c[i]-1]=1;\n           cnt--;\n           sum+=n[c[i]-1]; \n        }else\n        {\n            if(sum>0)sum--;\n        }\n    }\n\n    return sum==0&&cnt==0;\n}\nint ans=-1;\nvoid k(int i,int j,vector<int>&n,vector<int>&c)\n{\n    if(i>j)return;\n\n    int mid=(i+j)/2;\n    if(l(mid,n,c))\n    {\n        ans=mid;\n        k(i,mid-1,n,c);\n    }else\n    {\n        k(mid+1,j,n,c);\n    }\n}\n    int earliestSecondToMarkIndices(vector<int>& n, vector<int>& c) {\n        k(0,c.size(),n,c);\n        return ans;\n    }\n};\n```
['C++']