比赛 20090927练习赛 评测结果 AAAAAAAAAA
题目名称 筷子 最终得分 100
用户昵称 lc 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-09-27 21:38:26
显示代码纯文本
program chop;

 const
   maxn = 110;
   maxk = 55;
   Inf =100000000;
 var
   n,k:         longint;
   A:           array[1..maxn] of longint;
   f:           array[0..maxn,0..maxk] of longint;

function min(a,b:longint):longint;

 begin
   if a <b then exit(a) else exit(b);
 end;


procedure init;
 var
   i,j,temp:           longint;
 begin
   readln(n,k);
   for i :=1 to n do read(A[i]);
   for i :=1 to n-1 do
       for j :=i+1 to n do
       if  A[i] >A[j] then begin
       temp :=A[i]; A[i] :=A[j]; A[j] :=temp;
       end;
 end;

procedure main;
 var
   i,j:         longint;

 begin
   if (k+3)*2 >n then begin writeln(-1); exit; end;
   inc(k,3);
   for j :=1 to k do
       for i :=1 to n do
       f[i][j] :=Inf;

   for j :=1 to k do
       for i :=j*2 to n do begin
       f[i][j] :=min(f[i][j],f[i-2][j-1]+ sqr(A[i-1]-A[i]) );
       if i >j*2 then f[i][j] :=min(f[i][j],f[i-1][j]);
       end;

   writeln(f[N][K]);
 end;

begin
  assign(input,'chop.in'); reset(input);
  assign(output,'chop.out');  rewrite(output);
  init;
  main;
  close(input); close(output);
end.