| 比赛 | 20101119 | 评测结果 | AAAATAAAAA | 
    | 题目名称 | 求和 | 最终得分 | 90 | 
    | 用户昵称 | Oo湼鞶oO | 运行时间 | 0.000 s | 
    | 代码语言 | Pascal | 内存使用 | 0.00 MiB | 
    | 提交时间 | 2010-11-19 11:07:12 | 
显示代码纯文本
program suma(input,output);
type
  t_list=array[1..100000]of longint;
var
  i,j1,j2,a,b,n,k,p,ans,t:longint;
  sum:t_list;
procedure qsort(var a:t_list; const max:longint);
  procedure sort(l,r: longint);
    var
      i,j,x,y: longint;
    begin
      i:=l;
      j:=r;
      x:=a[(l+r) div 2];
      repeat
        while a[i]<x do
          inc(i);
        while x<a[j] do
          dec(j);
        if not(i>j) then
        begin
          y:=a[i];
          a[i]:=a[j];
          a[j]:=y;
          inc(i);
          dec(j);
        end;
      until i>j;
      if l<j then
        sort(l,j);
      if i<r then
        sort(i,r);
    end;
  begin
    sort(1,max);
  end;
begin
  assign(input,'suma.in');
  reset(input);
  assign(output,'suma.out');
  rewrite(output);
  readln(n,k,p);
  for i:=1 to n do
  begin
    readln(a);
    sum[i]:=(sum[i-1]+a)mod p;
  end;
  qsort(sum,n);
  ans:=maxlongint;
  for i:=1 to n do
  begin
    j1:=i-1;
    while ((sum[i]-sum[j1])mod p<k)and(j1>0) do
      dec(j1);
    j2:=i+1;
    while ((sum[j2]-sum[i])mod p<k)and(j2<=n) do
      inc(j2);
    if j1>0 then
    begin
      a:=(sum[i]-sum[j1])mod p;
      if a<ans then
        ans:=a;
    end;
    if j2<n then
    begin
      a:=(sum[j2]-sum[i])mod p;
      if a<ans then
        ans:=a;
    end;
  end;
  writeln(ans);
  close(input);
  close(output);
end.