#ds604. 第k大查询

第k大查询

K-th Largest Query

Problem Description

Given a permutation a1,a2,a3,…,ana_1, a_2, a_3, \dots, a_n of 1∼n1 \sim n, consider all intervals [l,r][l, r] satisfying r−l+1≥kr - l + 1 \ge k. For each such interval, take the kk-th largest element among the numbers inside it, and output the sum of all these kk-th largest elements.

Input Format

The first line contains two integers n,kn, k.

The next line contains nn integers a1,a2,a3,…,ana_1, a_2, a_3, \dots, a_n.

Output Format

Output one line containing a single integer: the answer.

Sample Input

5 2
1 2 3 4 5

Sample Output

30

Sample Explanation

The intervals [1,5],[2,5],[3,5],[4,5][1,5], [2,5], [3,5], [4,5] have 44 as their 22nd largest; the intervals [1,4],[2,4],[3,4][1,4], [2,4], [3,4] have 33 as their 22nd largest; the intervals [1,3],[2,3][1,3], [2,3] have 22 as their 22nd largest; the interval [1,2][1,2] has 11 as its 22nd largest.

Constraints

There are 1010 test cases in total.

  • Test points 1,21, 2: 1≤n≤1001 \le n \le 100.
  • Test points 3,43, 4: 1≤n≤1031 \le n \le 10^3.
  • Test points 5,65, 6: k=1k = 1.
  • Test points 7,87, 8: n≤5×104n \le 5 \times 10^4.
  • For 100%100\% of the data: 1≤n≤5×105, 1≤k≤501 \le n \le 5 \times 10^5,\ 1 \le k \le 50.

Hint

See the files attached to this problem.