题目收集
题目列表题单
返回列表

Edit Set

CodeforcesP0045· 1000ms· 1024 MB· ZJU Summer 2026 Contest 4原题链接
贪心

创建于 2026/07/17 · 更新于 2026/07/17

题目描述

You were gifted a set containing many different integers. As you find large sets too intimidating, you want to make it smaller. In order to achieve this, you are allowed to apply the following operations to it:

  • choose one element from the set and add 1 to it
  • choose one element from the set and subtract 1 from it

Note that, as this is a set, when after an operation two elements would have the same value, only one of them stays in the set, decreasing its size by one.

输入格式

The first line contains two integers n,kn, kn,k (1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^51≤k≤n≤2⋅105), the initial size of the set and the size you want to reach.

The next line contains nnn distinct integers xix_ixi​ (1≤xi≤1091 \le x_i \le 10^91≤xi​≤109), the numbers in the initial set.

输出格式

Output the minimal number of operations required to reduce the size of the set to kkk.

样例输入 #1

5 4
8 4 6 1 10

样例输出 #1

2

样例输入 #2

4 2
1 8 3 9

样例输出 #2

3

样例输入 #3

4 1
11 4 12 7

样例输出 #3

8

样例输入 #4

3 3
1 5 4

样例输出 #4

0