We are given an integer array nums and another list called requests.
Each request is a pair [l, r], meaning that we must consider the sum:
You are allowed to rearrange (permute) the elements of nums in any order.
Your goal is to place larger numbers in positions that appear more frequently across requests, so that the total contribution of all requested ranges becomes as large as possible.
The result may become very large, so return it modulo 1,000,000,007 (10^9 + 7).
Input: nums = [5, 1, 3, 2] requests = [[0,2], [1,3]]
Output: 19
Explanation:
An optimal permutation is [5,3,2,1]. Request [0,2] → 5 + 3 + 2 = 10 Request [1,3] → 3 + 2 + 1 = 6 Total = 10 + 6 = 16 A better arrangement gives total = 26.
Input: nums = [4,7,2] requests = [[1,1]]
Output: 7
Explanation:
Only index 1 is counted, so we place the largest number at index 1.
Input: nums = [8,1,6,2,9] requests = [[0,4], [2,4], [3,3]]
Output: 58
Explanation:
Index 3 appears more often, so we place one of the largest values there.
Accepted:
Submission: