比赛 10101115 评测结果 AAAAAAAAAA
题目名称 矩形分割 最终得分 100
用户昵称 gragon 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-15 11:16:10
显示代码纯文本
program ex;
type
c=array[0..2002] of longint;
var
n,m,i,j:longint;
a,b:c;
f:array[0..2002,0..2002] of longint;

procedure init;
begin
  assign(input,'cut.in');
  assign(output,'cut.out');
  reset(input);
  rewrite(output);
  readln(n,m);
  for i:=1 to n-1 do read(a[i]); readln;
  for i:=1 to m-1 do read(b[i]); readln;
  close(input);
end;

function min(x,y:longint):longint;
begin
  if x<y then min:=x else min:=y;
end;

procedure sort(p,q:longint; var a:c);
var
l,r,o,x:longint;
begin
  l:=p; r:=q;
  o:=a[l+random(r-l+1)];
  repeat
  while a[l]>o do inc(l);
  while o>a[r] do dec(r);
  if l<=r then
  begin
    x:=a[l];
    a[l]:=a[r];
    a[r]:=x;
    inc(l); dec(r);
  end;
  until l>r;
  if p<r then sort(p,r,a);
  if l<q then sort(l,q,a);
end;

begin
  init;
  randomize;
  sort(1,n-1,a);
  sort(1,m-1,b);
  for i:=1 to n-1 do f[i,0]:=f[i-1,0]+a[i];
  for i:=1 to m-1 do f[0,i]:=f[0,i-1]+b[i];
  for i:=1 to n-1 do
  for j:=1 to m-1 do
  f[i,j]:=min(f[i-1,j]+a[i]*(j+1),f[i,j-1]+b[j]*(i+1));
  writeln(f[n-1,m-1]);
  close(output);
end.