记录编号 340177 评测结果 AAAAAAAAAA
题目名称 饭堂 最终得分 100
用户昵称 Gravatar凤雏之死 是否通过 通过
代码语言 Pascal 运行时间 0.014 s
提交时间 2016-11-06 12:34:45 内存使用 0.21 MiB
显示代码纯文本
var
a:array[1..10000] of longint;
t:array[-9..18] of longint;
c:array[0..9,0..9] of longint;
ch:char;
n,k,i,j,sum,min,cha,m,c1,c2:longint;

begin
assign(input,'fancy.in');
reset(input);
assign(output,'fancy.out');
rewrite(output);
readln(n,k);
for i:=1 to n do
 begin
 read(ch);
 j:=ord(ch)-ord('0');
 a[i]:=j;
 inc(t[j]);
 end;

for i:=0 to 9 do
 for j:=0 to 9 do
 if j=0 then c[i,0]:=t[i] else
 c[i,j]:=t[i-j]+t[i+j];

min:=maxlongint;
for i:=0 to 9 do
 begin
 j:=0; sum:=0; m:=0;
 while (j<=9)and(k>sum+c[i,j]) do
  begin
  sum:=sum+c[i,j];
  m:=m+j*c[i,j];
  inc(j);
  end;
 if not(j=10) then
 m:=m+j*(k-sum);
 if m<min then begin cha:=i; min:=m; c1:=j; c2:=k-sum; end;
 end;

for i:=1 to n do
 if abs(cha-a[i])<c1 then a[i]:=cha else
 if (c2>0)and(a[i]-cha=c1) then begin c2:=c2-1; a[i]:=cha; end;
if c2>0 then
 begin
 for i:=n downto 1 do
  if (cha-a[i]=c1)and(c2>0) then begin c2:=c2-1; a[i]:=cha; end;
 end;

writeln(min);
for i:=1 to n do
write(a[i]);
close(input);
close(output);
end.