
{$R-,Q-}

program Post_Problem;

const
  name1     =      'post.in';
  name2     =      'post.out';
  maxV      =      310;
  maxP      =      31;

var
  V, P      :      integer;
  pos       :      array[1..maxV] of integer;
  Dy        :      array[0..maxV, 0..maxP] of longint;
  Fa        :      array[0..maxV, 0..maxP] of integer;

procedure init;
var inf  : text;
    i, j : integer;
begin
  assign(inf, name1);
  reset(inf);
  readln(inf, V, P);
  for i := 1 to V do read(inf, pos[i]);
  close(inf);

  fillchar(Dy, sizeof(Dy), 0);
  fillchar(Fa, sizeof(Fa), 0);
  for i := 1 to V do
    for j := 0 to P do
      Dy[i, j] := -1;
end;

function best_pos(t1, t2 : integer) : integer;
begin
  best_pos := t1 + (t2 - t1) div 2;
end;

function min(a, b : integer) : integer;
begin
  if a < b then min := a
    else min := b;
end;

procedure Dynamic;
var i, j, k, b, f, bb : integer;
    sum               : longint;
begin
  for i := 1 to V do
    for j := 1 to min(i, P) do
      if V - i >= P - j then
        begin
          sum := 0;
          b := best_pos(j, i);
          for k := j to i do
            inc(sum, abs(pos[k] - pos[b]));
          Dy[i, j] := sum;
          Fa[i, j] := j;

          f := j;
          for f := j+1 to i do
            begin
              dec(sum, abs(pos[f-1] - pos[b]));
              bb := best_pos(f, i);
              while b <> bb do
                begin
                  inc(sum, abs(pos[b+1] - pos[b]) *
                           (b - f + 1 - (i - b) ) );
                  inc(b);
                end;
              if (Dy[f-1, j-1] <> -1) and
                 (Dy[f-1, j-1] + sum < Dy[i, j]) then
                begin
                  Dy[i, j] := Dy[f-1, j-1] + sum;
                  Fa[i, j] := f;
                end;
            end;
        end;
end;

procedure Out;
var ans  : array[1..maxP + 1] of integer;
    i, j : integer;
    ouf  : text;
begin
  i := V; j := P;
  while i <> 0 do
    begin
      ans[j] := Fa[i, j];
      i := Fa[i, j]-1;
      dec(j);
    end;
  ans[P+1] := V+1;

  assign(ouf, name2);
  rewrite(ouf);
  writeln(ouf, Dy[V, P]);
  for i := 1 to P do
    begin
      write(ouf, pos[ best_pos(ans[i], ans[i+1]-1) ]);
      if i < P then write(ouf, ' ');
    end;
  writeln(ouf);
  close(ouf);
end;

begin
  init;
  Dynamic;
  out;
end.
