比赛 20101118 评测结果 AWAWWAWWWW
题目名称 分班 最终得分 30
用户昵称 reamb 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-18 09:16:03
显示代码纯文本
program fenban;
var
  n,i,sum,l,r,q,p,t,j:longint;
  a:array[1..50]of longint;
begin
  assign (input,'groupa.in');
  reset (input);
  assign (output,'groupa.out');
  rewrite (output);
    readln (n);
    for i:=1 to n do
    begin
      read (a[i]);
      sum:=sum+a[i]
    end;
    readln;
    readln (l,r);
    p:=l*n;
    q:=r*n;
    t:=0;
    if (sum>=p)and(sum<=q) then
    begin
      for i:=1 to n do
      begin
        if a[i]<l then
        begin
          j:=0;
          repeat
            j:=j+1;
            if a[j]>l then
            begin
              if a[j]-l>l-a[i] then
              begin
                t:=t+l-a[i];
                a[j]:=a[j]-l+a[i];
                a[i]:=l
              end
              else
              begin
                t:=t+a[j]-l;
                a[i]:=a[i]+a[j]-l;
                a[j]:=l
              end
            end
          until a[i]=l
        end;
        if a[i]>r then
        begin
          j:=0;
          repeat
            j:=j+1;
            if a[j]<r then
            begin
              if r-a[j]>a[i]-r then
              begin
                t:=t+a[i]-r;
                a[j]:=a[j]+a[i]-r;
                a[i]:=r
              end
              else
              begin
                t:=t+r-a[j];
                a[i]:=a[i]-r+a[j];
                a[j]:=r
              end
            end
          until a[i]=r
        end
      end;
      writeln (t)
    end
    else
      writeln ('-1');
  close (input);
  close (output)
end.