比赛 20101101 评测结果 AAAAAAAAAA
题目名称 奇怪的监狱 最终得分 100
用户昵称 Vow Ryan 运行时间 0.010 s
代码语言 Pascal 内存使用 0.21 MiB
提交时间 2012-11-05 11:05:09
显示代码纯文本
var
 a:array[0..110]of longint;
 f:array[0..110,0..110]of longint;
 i,j,k,l,m,n,p:longint;

function min(a,b:longint):longint;
 begin
  if a<b then exit(a) else exit(b);
 end;

begin
 assign(input,'prison.in');reset(input);
 assign(output,'prison.out');rewrite(output);
 readln(m,n);
 for i:=1 to n do read(a[i]);
 a[n+1]:=m+1;
 
 for i:=1 to n do
  for j:=1 to n-1 do
   if a[j]>a[j+1] then
    begin
     k:=a[j];a[j]:=a[j+1];a[j+1]:=k;
    end;
 
 for i:=1 to n do f[i,i]:=a[i+1]-a[i-1]-2;
 for p:=1 to n do
  for i:=1 to n-p do
   begin
    j:=i+p;
    f[i,j]:=maxlongint;
    for k:=i to j do
     f[i,j]:=min(f[i,j],f[i,k-1]+f[k+1,j]+a[j+1]-a[i-1]-2);
   end;

 
 writeln(f[1,n]);
 close(output);
end.