-
Notifications
You must be signed in to change notification settings - Fork 0
/
118_C.cpp
105 lines (95 loc) · 1.4 KB
/
118_C.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
int countS(string& s, int t, int k)
{
int count = 0;
for(int i=0;i<(int)s.length();i++)
{
if((t+'0') == s[i])
count++;
}
return (k-count);
}
pair<int,string> solve(string&s, int t, int k)
{
vector< vector<int> >p(10);
vector< vector<int> >n(10);
int req = countS(s,t,k);
// cout<<req<<" "<<t<<endl;
if(req<=0)
{
return make_pair(0,s);
}
for(int i=0;i<(int)s.length();i++)
{
int diff = s[i]-'0' - t;
if(diff==0)
continue;
if(diff>=0)
p[diff].push_back(i);
else
n[abs(diff)].push_back(i);
}
string m = s;
int cost = 0;
for(int i=0;i<10;i++)
{
if(req<=0)
break;
if((int)p[i].size()>0)
{
for(int j=0;j<p[i].size();j++)
{
if(req<=0)
break;
m[p[i][j]] = t+'0';
cost+=i;
req--;
}
}
if((int)n[i].size()>0)
{
for(int j=(int)n[i].size()-1;j>=0;j--)
{
if(req<=0)
break;
m[n[i][j]] = t + '0';
cost+=i;
req--;
}
}
}
return make_pair(cost,m);
}
int main()
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
int minm = INT_MAX;
string ret;
for(int i=0;i<10;i++)
{
pair<int,string> a = solve(s,i,k);
if(a.first<minm)
{
ret = a.second;
minm = a.first;
}
else if(a.first == minm)
{
if(a.second<ret)
{
ret = a.second;
}
}
}
cout<<minm<<endl<<ret<<endl;
return 0;
}