比赛 20090927练习赛 评测结果 AAAAAAAAAA
题目名称 筷子 最终得分 100
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-09-27 21:51:10
显示代码纯文本
program t4;
var
  a:array[0..100] of longint;
  n,m,i,k,j,t,ans:longint;
  f:array[0..100,0..53] of longint;
begin
  assign(input,'chop.in');
  assign(output,'chop.out');
  reset(input);
  rewrite(output);
  readln(n,m);
  m:=m+3;
  ans:=maxlongint;
  if n<m*2
    then writeln(-1)
    else
    begin
      for i:=1 to n do read(a[i]);
        for i:=1 to n-1 do
        begin
          j:=i;
          for k:=i+1 to n do
            if a[k]<a[j]
              then j:=k;
          t:=a[i];
          a[i]:=a[j];
          a[j]:=t;
        end;
      fillchar(f,sizeof(f),100);
      for i:=0 to n do
        f[i,0]:=0;
      for i:=1 to n do
      begin
        for j:=1 to m do
        begin
          f[i,j]:=f[i-1,j];
          for k:=1 to i-1 do
          begin
            t:=f[k-1,j-1]+sqr(a[i]-a[k]);
            if t<f[i,j] then f[i,j]:=t;
          end;
        end;
      end;
      writeln(f[n,m]);
    end;
  close(input);
  close(output);
end.