Problem 827
Range Sum of Sorted Subarray Sums
Medium · 20 pts · Array, Sorting, Prefix Sum
Problem
Compute the sums of all N × (N + 1) / 2 non-empty blocks of consecutive elements and sort them in non-decreasing order, numbering them from 1. Print the sum of the sorted values from position LEFT to position RIGHT (inclusive), modulo 1000000007. With N up to 1000 there are about half a million block sums, so generating them all and sorting is fast enough. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
The first line contains N, LEFT and RIGHT. The second line contains the N integers.
Output
Print the sum of the sorted subarray sums from LEFT to RIGHT, modulo 1000000007.
Constraints
1 <= N <= 1000; 1 <= A[i] <= 100; 1 <= LEFT <= RIGHT <= N × (N + 1) / 2
Example input
4 1 5 1 2 3 4
Example output
13
Explanation
For 1 2 3 4 the block sums sorted are 1 2 3 3 4 5 6 7 9 10. Positions 1 to 5 hold 1, 2, 3, 3 and 4, which add up to 13.