记录编号 13156 评测结果 AAAAAAAAAA
题目名称 筷子 最终得分 100
用户昵称 Gravatarybh 是否通过 通过
代码语言 Pascal 运行时间 0.018 s
提交时间 2009-09-28 21:14:18 内存使用 0.13 MiB
显示代码纯文本
program KuaiZi;
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.