CF118C.Fancy Number

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

A car number in Berland consists of exactly nn digits. A number is called beautiful if it has at least kk equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of nn 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.

输入格式

The first line contains two space-separated integers nn and kk ( 2<=n<=104,2<=k<=n2<=n<=10^{4},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 nn digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits.

输出格式

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.

输入输出样例

  • 输入#1

    6 5
    898196
    

    输出#1

    4
    888188
    
  • 输入#2

    3 2
    533
    

    输出#2

    0
    533
    
  • 输入#3

    10 6
    0001112223
    

    输出#3

    3
    0000002223
    

说明/提示

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

The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string xx is lexicographically smaller than the string yy , if there exists such ii ( 1<=i<=n1<=i<=n ), that x_{i}<y_{i} , and for any jj ( 1<=j<i ) xj=yjx_{j}=y_{j} . The strings compared in this problem will always have the length nn .

首页