AtCoder Regular Contest 163
翻译中…
Problem Statement
翻译中…
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).
翻译中…
- Choose an integer
$i$such that$1 \le i \le N$, and increase or decrease$A_i$by$1$.
翻译中…
Your 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.
翻译中…
Constraints
翻译中…
$3 \le N \le 2 \times 10^5$$1 \le M \le N-2$$1 \le A_i \le 10^9$
翻译中…
Input
翻译中…
The input is given from Standard Input in the following format:
翻译中…
N M
A_1 A_2 \dots A_N
翻译中…
Output
翻译中…
Print the minimum number of operations required.
翻译中…
样例
Input #1
3 1
2 3 5
Output #1
2
You 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.
- Choose $i=3$, and decrease $A_i$ by $1$.
- Choose $i=2$, and increase $A_i$ by $1$.
Since it is impossible to achieve the goal with less than $2$ operation, the answer is $2$.
Input #2
5 2
1 4 2 3 5
Output #2
0
You may already have achieved the goal from the start.
Input #3
8 5
15 59 64 96 31 17 88 9
Output #3
35
题目信息
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。