比赛 20101118 评测结果 RTRRRTRRRR
题目名称 分班 最终得分 0
用户昵称 itachi 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-18 11:27:07
显示代码纯文本
program pp1(input,output);
var
sum,t,ans:int64; i,n,j,l,r,head,tail:longint;
data:array[1..50] of longint;
 procedure Sort(l, r: longint);
var
  i, j, x, y :longint;
begin
  i := l; j := r; x := data[(l+r) DIV 2];
  repeat
    while data[i] > x do i := i + 1;
    while x > data[j] do j := j - 1;
    if i <= j then
    begin
      y := data[i]; data[i] := data[j]; data[j] := y;
      i := i + 1; 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,'groupa.in');
reset(input);
assign(output,'goupa.out');
rewrite(output);
readln(n);
sum:=0;
for i:= 1 to n do
 begin
 read(data[i]);
 inc(sum,data[i]);
 end;
 read(l,r);
close(input);
if (l*n >sum) or(r*n< sum )  then begin writeln(-1);  close(output); exit; end;
 ans:=0;
sort(1,n);
head:=1;tail:=n;
  while head< tail do
   begin
     if data[tail]< l then
      begin
        inc(ans,l-data[tail]);
        dec(data[head],l-data[tail]);
         data[tail]:=l;
         dec(tail);
       if data[head]<=r then inc(head);
      end;
     if data[head]> r then
       begin
        inc(ans,r-data[tail]) ;
        dec(data[head],r-data[tail]);
         inc(data[tail],r-data[tail]);
         dec(tail);
         if data[head]<=r then inc(head);
       end;

   end;
   if head=tail then
    begin
     if data[head]> r then inc(ans,data[head]-r);
     if data[head]<l then inc(ans,l-data[head]);
    end;
 writeln(ans);
 close(output);
end.