Easy Solution using multiset
idea is to use maximum r for an index and store not used l, r into a not_used multiset for further cases.\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int 
idea is to use maximum r for an index and store not used l, r into a not_used multiset for further cases.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        int n = nums.size();\n        vector<pair<int, int> > v;\n        for(auto it: queries) v.push_back({it[0], it[1]});\n        sort(v.begin(), v.end(),[](pair<int, int>& a, pair<int, int>& b){\n            if(a.first == b.first) return a.second > b.second;\n            else return a.first < b.first;\n        });\n\n        multiset<int> ms;\n        multiset<int> not_used;\n\n        int ind = 0;\n        for(int i = 0; i < n; i++){\n            while(!ms.empty() && *ms.begin() < i){\n                ms.erase(ms.begin());\n            }\n            nums[i] -= int(ms.size());\n            while(ind < v.size() && v[ind].first == i){\n                not_used.insert(v[ind].second);\n                ind++;\n            }\n\n            while(nums[i] > 0){\n                if(not_used.empty() || *not_used.rbegin() < i) return -1;\n                auto it = --not_used.end();\n                ms.insert(*it);\n                not_used.erase(it);\n                nums[i]--;\n            }\n\n        }\n\n        return not_used.size();\n    }\n};\n```
['C++']