比赛 20101119 评测结果 WWATTTTTTE
题目名称 象棋比赛 最终得分 10
用户昵称 wo shi 刘畅 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 09:54:39
显示代码纯文本
const
  oo=maxlongint;

var
  n,k,i,j,l,min:longint;
  a:array[0..10000]of longint;
  f:array[0..3000,0..3000]of longint;

    procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

begin
  assign(input,'chess.in'); reset(input);
  assign(output,'chess.out'); rewrite(output);
  readln(n,k);
  for i:=1 to n do readln(a[i]);
  sort(1,n);
  for j:=1 to k do
   for i:=j+1 to n do
   begin
     f[i,j]:=oo;
     for l:=1 to i-1 do
     if f[l,j-1]+a[i]-a[l]<f[i,j] then
     f[i,j]:=f[l,j-1]+a[i]-a[l];
     if (i-1>j)and(f[i-1,j]<f[i,j]) then f[i,j]:=f[i-1,j];
   end;
  min:=oo;
  for i:=k+1 to n do
  if f[i,k]<min then min:=f[i,k];
  writeln(min);
  close(input);
  close(output);
end.