You are given an array of numbers Ai which contains positive as well as negative numbers . The cost of the array can be defined as C(X)
C(x)=|A1+T1|+|A2+T2|+........|An+Tn| , where T is the transfer array which contains N zeros initially.
You need to minimize this cost . You can transfer value from one array element to another if and only if the distance between them is at most K.
Also, transfer value can't be transferred further.
Say array contains 3,1,2 and K=1
if we transfer 3 from 1st element to 2nd , the array becomes
Original Value 3,1,2
Transferred value 3,3,0
C(x)=|33|+|1+3|+........|2+0|=4 which is minimum in this case

Input:


First-line contains N and K separated by space
Second-line denotes an array of size N


Output


Minimum value of C(X)


C(X)


Post a Comment

Previous Post Next Post