min/max heap.
IntuitionApproachComplexity

Time complexity:
O(n+mlogm)


Space complexity:
O(m)

Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
O(n+mlogm)

- Space complexity:
O(m)

# Code
```cpp []
class Solution {
public:
    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {
        int n = nums.size(), m = queries.size(), ans = 0, j = 0;
        priority_queue<int> candidates;
        priority_queue<int, vector<int>, greater<>> chosen;
        sort(queries.begin(), queries.end());
        for (int i = 0; i < n; i++) {
            while (j < m && queries[j][0] == i) {
                candidates.push(queries[j][1]);
                j++;
            }
            nums[i] -= chosen.size();
            while (nums[i] > 0 && !candidates.empty() &&
                   candidates.top() >= i) {
                chosen.push(candidates.top());
                candidates.pop();
                ans++;
                nums[i]--;
            }
            if (nums[i] > 0)
                return -1;
            while (!chosen.empty() && chosen.top() <= i) {
                chosen.pop();
            }
        }
        return m - ans;
    }
};
```
['C++']