CF1328F.Make k Equal
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given the array a consisting of n elements and the integer k≤n .
You want to obtain at least k equal elements in the array a . In one move, you can make one of the following two operations:
- Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of a is mn then you choose such index i that ai=mn and set ai:=ai+1 );
- take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of a is mx then you choose such index i that ai=mx and set ai:=ai−1 ).
Your task is to calculate the minimum number of moves required to obtain at least k equal elements in the array.
输入格式
The first line of the input contains two integers n and k ( 1≤k≤n≤2⋅105 ) — the number of elements in a and the required number of equal elements.
The second line of the input contains n integers a1,a2,…,an ( 1≤ai≤109 ), where ai is the i -th element of a .
输出格式
Print one integer — the minimum number of moves required to obtain at least k equal elements in the array.
输入输出样例
输入#1
6 5 1 2 2 4 2 3
输出#1
3
输入#2
7 5 3 3 2 1 1 1 3
输出#2
4