Codeforces Beta Round #89 (Div. 2) C. Fancy Number

来源:互联网 发布:网络论坛和网络社区 编辑:程序博客网 时间:2024/05/29 17:41
C. Fancy Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one.

Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one.

Input

The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. 

Output

On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one.

Sample test(s)
input
6 5898196
output
4888188
input
3 2533
output
0533
input
10 60001112223
output
30000002223
Note

In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188".

The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string xis lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < ixj = yj. The strings compared in this problem will always have the length n.



这道题明显是YY题,不过应该很好想到。

先把0-9每个数的个数统计出来,然后就是枚举0-9了

枚举的时候是枚举绝对值大小,把i从1到9枚举,设绝对值大小为t, 那么原串中要被换掉的数就为i+t和i-t,那么优先换谁好呢,如果换i+t,显然原串的字典序会变小,换i-t就会使字典序增大,所以要优先换掉i+t,而且,换i+t时要从左往右换,这样会使原串尽可能的变小,而换i-t时,就要从右向左换,这样,就会使原串的尽量少的变大。

代码就不写了,应该很好写