{"raw_statement":[{"iden":"problem statement","content":"You are given an integer sequence of length $N$: $A=(A_1,A_2,\\dots,A_N)$. You can perform the following operation any number of times (possibly zero).\n\n*   Choose an integer $i$ such that $1 \\le i \\le N$, and increase or decrease $A_i$ by $1$.\n\nYour goal is to make at least $M$ integers $i(3 \\le i \\le N)$ satisfy $A_1 \\le A_i \\le A_2$. Find the minimum number of operations required to achieve this goal."},{"iden":"constraints","content":"*   $3 \\le N \\le 2 \\times 10^5$\n*   $1 \\le M \\le N-2$\n*   $1 \\le A_i \\le 10^9$"},{"iden":"input","content":"The input is given from Standard Input in the following format:\n\n$N$ $M$\n$A_1$ $A_2$ $\\dots$ $A_N$"},{"iden":"sample input 1","content":"3 1\n2 3 5"},{"iden":"sample output 1","content":"2\n\nYou can make not less than one integer $i(3 \\le i \\le N)$ satisfy $A_1 \\le A_i \\le A_2$ by performing the operation as follows.\n\n*   Choose $i=3$, and decrease $A_i$ by $1$.\n*   Choose $i=2$, and increase $A_i$ by $1$.\n\nSince it is impossible to achieve the goal with less than $2$ operation, the answer is $2$."},{"iden":"sample input 2","content":"5 2\n1 4 2 3 5"},{"iden":"sample output 2","content":"0\n\nYou may already have achieved the goal from the start."},{"iden":"sample input 3","content":"8 5\n15 59 64 96 31 17 88 9"},{"iden":"sample output 3","content":"35"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}