Arrange Values to Maximize Range Requests. Rearrange the values of nums before answering all inclusive, zero-based ranges in requests. Each request contributes the sum of the values in its range.
### Problem
Rearrange the values of `nums` before answering all inclusive, zero-based ranges in `requests`. Each request contributes the sum of the values in its range. Return the largest possible combined sum, reduced modulo `10^9 + 7`.
```cpp
class Solution {
public:
int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests);
};
```
### Examples
**Example 1:**
```text
nums = [2,4,6], requests = [[0,1],[1,2]]
Output: 18
Explanation: Put 6 in the middle; its contribution is doubled, giving 2 + 12 + 4.
```
**Example 2:**
```text
nums = [0,5,8], requests = [[0,0],[0,0]]
Output: 16
Explanation: Put 8 at position 0 for both requests.
```
### Constraints
- `n == nums.length` and `1 <= n <= 10^5`.
- `0 <= nums[i] <= 10^5`.
- There are `1..10^5` requests, each with two endpoints satisfying `0 <= start <= end < n`.
### Clarifying Questions
Choose one common permutation for all requests. The result is the optimum sum, rather than the permutation that achieves it.
```hint Inspect the structure
How does one chosen rearrangement affect repeated or overlapping requests?
```
### Reasoning checkpoints
- Evaluate one common rearrangement across all requests, including repeated and overlapping ranges.
- Prove that no other rearrangement has a larger combined sum, and apply the modulus without changing the optimization.
### Optional extensions
How would signed values affect the same rearrangement argument?
Overview: Arrange Values to Maximize Range Requests. Rearrange the values of nums before answering all inclusive, zero-based ranges in requests. Each request contributes the sum of the values in its range.
Rearrange the values of nums before answering all inclusive, zero-based ranges in requests. Each request contributes the sum of the values in its range. Return the largest possible combined sum, reduced modulo 10^9 + 7.
class Solution {
public:
int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests);
};
Examples
Example 1:
nums = [2,4,6], requests = [[0,1],[1,2]]
Output: 18
Explanation: Put 6 in the middle; its contribution is doubled, giving 2 + 12 + 4.
Example 2:
nums = [0,5,8], requests = [[0,0],[0,0]]
Output: 16
Explanation: Put 8 at position 0 for both requests.
Constraints
n == nums.length
and
1 <= n <= 10^5
.
0 <= nums[i] <= 10^5
.
There are
1..10^5
requests, each with two endpoints satisfying
0 <= start <= end < n
.
Clarifying Questions Guidance
Choose one common permutation for all requests. The result is the optimum sum, rather than the permutation that achieves it.
Reasoning checkpoints
Evaluate one common rearrangement across all requests, including repeated and overlapping ranges.
Prove that no other rearrangement has a larger combined sum, and apply the modulus without changing the optimization.
Optional extensions
How would signed values affect the same rearrangement argument?