-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppd_phonenum.py
47 lines (43 loc) · 1.18 KB
/
ppd_phonenum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding:utf-8 -*-
from collections import Counter
line = list(map(int, input().split()))
N = line[0]
K = line[1]
number_input = input()
d = Counter(list(map(int, number_input)))
res = float("inf")
ans = "X"
for i in range(10):
tmp = number_input
cost = 0
dist = 1
need = K - d[i]
while need > 0:
if i + dist <= 9:
if d[i + dist] < need:
need -= d[i + dist]
cost += dist * d[i + dist]
tmp = tmp.replace(str(i+dist), str(i))
else:
cost += dist * need
tmp = tmp.replace(str(i+dist), str(i), need)
break
if i - dist >= 0:
if d[i - dist] < need:
need -= d[i - dist]
cost += dist * d[i - dist]
tmp = tmp.replace(str(i-dist), str(i))
else:
tmp_s = tmp[::-1]
tmp_s = tmp_s.replace(str(i-dist), str(i), need)
cost += need * dist
tmp = tmp_s[::-1]
break
dist += 1
if cost < res:
res = cost
ans = tmp
elif cost == res and tmp < ans:
ans = tmp
print(res)
print(ans)