NOIXICH - Nối Xích

Tác giả: RR

Ngôn ngữ: Pascal

PROGRAM NOIXICH;
CONST
  fi='';
  fo='';
  maxn=20000;
VAR
  n:longint;
  kq:longint;
  a:array[1..maxn] of longint;
procedure readInput;
var
  f:text;
  i:longint;
begin
  assign(f,fi); reset(f);
  readln(f,n);
  for i:=1 to n do
    read(f,a[i]);
  close(f);
end;
procedure quickSort;
  procedure sort(l,r:longint);
  var
    i,j,x,temp:longint;
  begin
    i:=l; j:=r; x:=a[(l+r) div 2];
    repeat
      while a[i]<x do inc(i);
      while a[j]>x do dec(j);
      if i<=j then
        begin
          temp:=a[i]; a[i]:=a[j]; a[j]:=temp;
          inc(i); dec(j);
        end;
    until i>j;
    if i<r then sort(i,r);
    if l<j then sort(l,j);
  end;
begin
  sort(1,n);
end;
procedure solve;
var
  t,i:longint;
begin
  t:=0;
  for i:=1 to n do
    begin
      t:=t+a[i];
      if t>=n-i-1 then break;
    end;
  if t=n-i-1 then kq:=t
  else kq:=n-i;
end;
procedure writeOutput;
var
  f:text;
begin
  assign(f,fo); rewrite(f);
  writeln(f,kq);
  close(f);
end;
BEGIN
  readInput;
  quickSort;
  solve;
  writeOutput;
END.

Download