Under construction

Still being built. Things may change

K-mins

Time limit2sMemory limit1024 MB

Summary
Given an array of distinct integers, find the sum of the K-th smallest value over every subarray. Subarrays shorter than K contribute 0.
Level

Medium6 of 10

Topics
Divide and conquer, Sorting, Array
Solved
No attempts yet

Problem

A sequence A=[A1,A2,…,AN]A=[A_1, A_2, \dots, A_N] of length NN is given. For any two integers ii and jj with 1≤i<j≤N1 \leq i < j \leq N, Ai≠AjA_i \neq A_j holds.

For two integers ll and rr with 1≤l≤r≤N1 \leq l \leq r \leq N, define f(l,r)f(l,r) as follows.

f(l,r)f(l,r) is the KK-th smallest value among Al,Al+1,…,ArA_l, A_{l+1}, \dots, A_r.

If the length of the interval, r−l+1r-l+1, is less than KK, then f(l,r)=0f(l,r)=0.

Compute ∑l=1N∑r=lNf(l,r)\sum_{l=1}^{N}\sum_{r=l}^{N}f(l,r).

Input

The first line contains the number of elements NN and the integer KK, separated by a space. (1≤N≤100 0001 \leq N \leq 100\,000; 1≤K≤101 \leq K \leq 10)

The second line contains the NN integers A1,A2,…,ANA_1, A_2, \dots, A_N, separated by spaces. (−109≤Ai≤109-10^9 \leq A_i \leq 10^9)

Output

Print the value of ∑l=1N∑r=lNf(l,r)\sum_{l=1}^{N}\sum_{r=l}^{N}f(l,r).

Examples1

  1. Example 1

    Input
    5 2
    2 1 5 3 4
    
    Expected output
    32