Different Approach using Binary Search and Set....
Complexity\n- Time complexity: O(max(n, m)logmax(n , m))\n# Code\n\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int
# Complexity\n- Time complexity: O(max(n, m)logmax(n , m))\n# Code\n```\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        // Initialize the search range\n        int low = 1, high = changeIndices.size();\n        long long sum = 0;\n        int n = nums.size();\n        // Calculate the sum of all numbers in nums\n        for(int i = 0; i < nums.size(); i++){\n            sum += nums[i];\n        }\n        int fin = -1;\n        // Binary search loop\n        while(low <= high){\n            int mid = low + (high - low) / 2;\n            long long temp = 0;\n            set<int> st; \n            int store = mid;\n            mid--;\n            long long req = 0;\n            // Iterate over changeIndices to mark indices\n            while(mid >= 0){\n                // If the index hasn\'t been marked yet\n                if(st.find(changeIndices[mid] - 1) == st.end()){\n                    st.insert(changeIndices[mid] - 1);\n                    req += nums[changeIndices[mid] - 1]; // Increment the marking requirement\n                }\n                else{\n                    // If the index has been marked, decrement the marking requirement if it\'s greater than 0\n                    if(req > 0){\n                        req--;\n                    } \n                }\n                mid--;\n            }\n            // If all indices have not been marked, search in the higher half\n            if(st.size() != n){\n                low = store + 1;\n            }\n            else{\n                // If all indices have been marked and marking requirement is 0, update fin and search in the lower half\n                if(req == 0){\n                    fin = store;\n                    high = store - 1;\n                }\n                // If all indices have been marked but marking requirement is not 0, search in the higher half\n                else{\n                    low = store + 1;\n                }\n            }\n        }\n        // Return the result\n        return fin;\n    }\n};\n\n```
['Binary Search', 'Ordered Set', 'C++']