比赛 10101115 评测结果 AWWWWWWWWW
题目名称 矩形分割 最终得分 10
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-15 09:09:05
显示代码纯文本
{矩形分割
 贪心算法
 Author: yangbohua
 Time: 2010-11-15 09:13}

program cut;
var
  a:array[1..4000] of longint;
  b:array[1..4000] of integer;
  n,m,i,x,y,ans:longint;

procedure sort(l,r:longint);
      var
         i,j,mid,temp:longint;
      begin
         i:=l;
         j:=r;
         mid:=a[(l+r) div 2];
         repeat
           while a[i]>mid do
            inc(i);
           while mid>a[j] do
            dec(j);
           if not(i>j) then
             begin
                temp:=a[i]; a[i]:=a[j]; a[j]:=temp;
                temp:=b[i]; b[i]:=b[j]; b[j]:=temp;
                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,'cut.in');
  reset(input);
  assign(output,'cut.out');
  rewrite(output);
  readln(n,m);
  for i:=1 to n+m-2 do
  begin
    readln(a[i]);
    if i<=n-1
      then b[i]:=1
      else b[i]:=2;
  end;
  sort(1,n+m-2);
  x:=1;
  y:=1;
  for i:=1 to n+m-2 do
  begin
    if b[i]=1 then
    begin
      ans:=ans+a[i]*y;
      x:=x+1;
    end
    else
    begin
      ans:=ans+a[i]*x;
      y:=y+1;
    end;
  end;
  writeln(ans);
  close(input);
  close(output)
end.