I found an interesting problem on https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/:
We have an array of integers, nums, and an array of requests where requests[i] = [starti, endi]. The ith request asks for the sum of nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi]. Both starti and endi are 0-indexed.
Return the maximum total sum of all requests among all permutations of nums.
Since the answer may be too large, return it modulo 109 + 7.
 
Example 1:
Input: nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
Output: 19
Explanation: One permutation of nums is [2,1,3,4,5] with the following result: 
requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
Total sum: 8 + 3 = 11.
A permutation with a higher total sum is [3,5,4,2,1] with the following result:
requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
requests[1] -> nums[0] + nums[1] = 3 + 5  = 8
Total sum: 11 + 8 = 19, which is the best that you can do.

Example 2:
Input: nums = [1,2,3,4,5,6], requests = [[0,1]]
Output: 11
Explanation: A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].
Example 3:
Input: nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
Output: 47
Explanation: A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].
 
Constraints:

n == nums.length
1 <= n <= 105
0 <= nums[i] <= 105
1 <= requests.length <= 105
requests[i].length == 2
0 <= starti <= endi < n

I tried it in Python, but could not do it. Can you solve it?
class Solution:
    def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
        \"\"\"
        Assume:
        1. list, list -> int
        2. only find sum, permutation is not unique
        3. nums is not sorted
        
        
        Algorithm:
        sort nums
        
        in requests, count number of coverage for each index. 
        counts = [c0, c1]
        put (c0, 0), (c1, 1) into maxHeap
        popout maxHeap, each index gets the assigned element from largest to smallest to form new nums
        
        calculate sum of requests in new nums
        
        Complexity:
        time: max(O(nlogn), O(mn))
        space: O(n)
        
        Follow up:
        1. use LinkedHashMap
        2. 
        
        \"\"\"
        if not nums:
            return -1 # rise IllegalInputArgumentException
        
        MOD = 10 ** 9 + 7
        result, n, m = 0, len(nums), len(requests)
        counts = [0 for _ in range(n)]
        max_heap = []
        
        # O(m*n)
        #for i in range(m):
        #    for j in range(requests[i][0], requests[i][1] + 1):
        #        counts[j] += 1
                
        # O(max(m, n)): use sweep line
        records = []
        for i in range(m):
            records.append([requests[i][0], 1])
            records.append([requests[i][1]+1, -1])
        for index, delta in sorted(records):
            if index < n:
                counts[index] += delta
        for i in range(n):
            if i != 0:
                counts[i] = counts[i] + counts[i-1]
        
        for i in range(n):
            heapq.heappush(max_heap, (-counts[i], i))
        
        # O(nlogn)
        nums.sort()
        new_nums, cur_max = [0 for _ in range(n)], n - 1
        while max_heap and cur_max >= 0:
            cur = heapq.heappop(max_heap)
            new_nums[cur[1]] = nums[cur_max]
            cur_max -= 1
        
        prefixsum = [0 for _ in range(n)]
        for i in range(n):
            if i == 0:
                prefixsum[i] =...