A8364 | Fancy Number
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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 $n$ and $k$ ( $2<=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 $n$ 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 $|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 $x$ is lexicographically smaller than the string $y$ , if there exists such $i$ ( $1<=i<=n$ ), that $x_{i}<y_{i}$ , and for any $j$ ( $1<=j<i$ ) $x_{j}=y_{j}$ . The strings compared in this problem will always have the length $n$ .
The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string $x$ is lexicographically smaller than the string $y$ , if there exists such $i$ ( $1<=i<=n$ ), that $x_{i}<y_{i}$ , and for any $j$ ( $1<=j<i$ ) $x_{j}=y_{j}$ . The strings compared in this problem will always have the length $n$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted