CF1328F.Make k Equal

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given the array aa consisting of nn elements and the integer knk \le n .

You want to obtain at least kk equal elements in the array aa . 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 aa is mnmn then you choose such index ii that ai=mna_i = mn and set ai:=ai+1a_i := a_i + 1 );
  • take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of aa is mxmx then you choose such index ii that ai=mxa_i = mx and set ai:=ai1a_i := a_i - 1 ).

Your task is to calculate the minimum number of moves required to obtain at least kk equal elements in the array.

输入格式

The first line of the input contains two integers nn and kk ( 1kn21051 \le k \le n \le 2 \cdot 10^5 ) — the number of elements in aa and the required number of equal elements.

The second line of the input contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ai1091 \le a_i \le 10^9 ), where aia_i is the ii -th element of aa .

输出格式

Print one integer — the minimum number of moves required to obtain at least kk 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
首页