This page is still under construction.

Parts of this page are still being built. What you see may change.

Measuring the Median

Time limit1sMemory limit256 MB

Summary
Compute the sum of the medians of every contiguous window of length K over N temperature readings.
Level

Medium6 of 10

Topics
Heap, Sliding window
Solved
No attempts yet

Problem

Meteorology mainly uses the median as its representative value. The hint defines the median.

A machine reads the temperature once per second, and it needs software. The machine has one small digital display, and every second that display shows the median of the temperatures measured during the last KK seconds.

The software is tested on a computer first, before it is loaded onto the machine.

Given the temperatures measured over NN seconds, write a program that computes the sum of the medians shown on the display. In other words, given NN numbers, compute the sum of the medians of the N−K+1N-K+1 contiguous subsequences of length KK.

Input

The first line contains NN and KK. (1≤N≤2500001 \le N \le 250000, 1≤K≤50001 \le K \le 5000, K≤NK \le N)

Each of the next NN lines contains one measured temperature, given in order. A temperature is an integer between 00 and 6553565535, inclusive.

Output

Print the sum of the medians of all N−K+1N-K+1 contiguous subsequences of length KK.

Hint

The median of KK numbers is the ⌊(K+1)/2⌋\lfloor (K+1)/2 \rfloor-th smallest of them. Counting starts at 1, and the added 1 makes the index land exactly in the middle when KK is odd.

For example, the median of (1,2,6,5,4,3)(1, 2, 6, 5, 4, 3) is 33, and the median of (11,13,12,14,15)(11, 13, 12, 14, 15) is 1313.

Examples1

  1. Example 1

    Input
    10 3
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    Expected output
    60