PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000491. Tổng một đoạn trong dãy tổng đoạn con đã sắp xếp (Range Sum of Sorted Subarray Sums)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng một đoạn trong dãy tổng đoạn con đã sắp xếp (Range Sum of Sorted Subarray Sums)

Range Sum of Sorted Subarray Sums

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An array AA contains nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n. A nonempty subarray is determined by two positions i,ji,j satisfying 1≤i≤j≤n1\le i\le j\le n, and contains every entry from position ii through position jj. Its sum is ∑t=ijat\sum_{t=i}^{j}a_t.

Form a sequence BB containing the sums of all n(n+1)/2n(n+1)/2 nonempty subarrays and arrange it in nondecreasing order. When distinct subarrays have equal sums, each still contributes its own entry to BB.

Given two positions l,rl,r in BB, numbered from 11, compute Bl+Bl+1+⋯+BrB_l+B_{l+1}+\cdots+B_r modulo 109+710^9+7.

Input

  • The first line contains three integers n,l,rn,l,r: the original array length and the first and last requested positions in the sorted sequence.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

Print one integer: the nonnegative remainder of the requested sum upon division by 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 1≤n≤501\le n\le 50; 1≤ai≤1001\le a_i\le100 for every 1≤i≤n1\le i\le n; 1≤l≤r≤n(n+1)/21\le l\le r\le n(n+1)/2.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤ai≤1001\le a_i\le100 for every 1≤i≤n1\le i\le n; 1≤l≤r≤min⁡(1000,n(n+1)/2)1\le l\le r\le \min(1000,n(n+1)/2).
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 1≤ai≤1001\le a_i\le100 for every 1≤i≤n1\le i\le n; 1≤l≤r≤n(n+1)/21\le l\le r\le n(n+1)/2.

Examples

Input 1

4 1 5
1 2 3 4

Output 1

13

Explanation 1

For n=4n=4, subarrays starting at position 11 have sums 1,3,6,101,3,6,10; at position 22: 2,5,92,5,9; at position 33: 3,73,7; at position 44: 44. The sorted sequence is 1,2,3,3,4,5,6,7,9,101,2,3,3,4,5,6,7,9,10. The first five entries add to 1+2+3+3+4=131+2+3+3+4=13, below the modulus.

Input 2

4 3 4
1 2 3 4

Output 2

6

Explanation 2

The sorted sums are again 1,2,3,3,4,5,6,7,9,101,2,3,3,4,5,6,7,9,10. Positions 3,43,4 both contain 33: the subarray [1,2][1,2] and the singleton 33 give equal sums. Including both occurrences gives 66.

Input 3

4 1 10
1 2 3 4

Output 3

50

Explanation 3

All ten entries 1,2,3,3,4,5,6,7,9,101,2,3,3,4,5,6,7,9,10 are requested. The first five sum to 1313; the last five sum to 5+6+7+9+10=375+6+7+9+10=37. The result is 13+37=5013+37=50.