-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path118C.rb
80 lines (80 loc) · 1.1 KB
/
118C.rb
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
k=gets.split[1].to_i
a=gets.split[0].bytes.to_a
n=a.length
cnt=Array.new(10,0)
n.times{|i|
a[i]-=48
cnt[a[i]]+=1
}
mincost=99999999
res=[]
10.times{|d|
cost=0
count=cnt[d]
1.upto(9){|i|
if count>=k then
break
end
if d+i<=9 then
if cnt[d+i]+count>=k then
cost+=i*(k-count)
count=k
else
count+=cnt[d+i]
cost+=cnt[d+i]*i
end
end
if d-i>=0 then
if cnt[d-i]+count>=k then
cost+=i*(k-count)
count=k
else
count+=cnt[d-i]
cost+=cnt[d-i]*i
end
end
}
if cost<mincost then
mincost=cost
res=[]
end
if cost==mincost then
ans=Array.new(n,0)
used=Array.new(n,0)
count=0
0.upto(9){|i|
tar=d+i
n.times{|j|
if count>=k then
break
end
if used[j]==0 && a[j]==tar then
ans[j]=d
count+=1
used[j]=true
end
}
tar=d-i
(n-1).downto(0){|j|
if count>=k then
break
end
if used[j]==0 && a[j]==tar then
ans[j]=d
count+=1
used[j]=true
end
}
}
n.times{|j|
if used[j]==0 then
ans[j]=a[j]
end
}
res.push(ans)
end
}
puts mincost
ans=res.min
ans.each{|i| print i}
puts