program pasky;
uses Crt;
var a : array [1..1000] of integer;
    b : array [1..1000] of integer;
    c : array [1..1000] of integer;
    i,j,k,M,N,p : integer;
    vymena : boolean;

begin
  Clrscr;
  randomize;
  write('Zadaj pocet prvkov prveho pola: ');
  readln(M);
  write('Zadaj pocet prvkov druheho pola: ');
  readln(N);
  for i:=1 to M do a[i]:=random(100);
  for i:=1 to N do b[i]:=random(100);
  a[M+1]:=999;
  b[N+1]:=999;
  
  repeat
    vymena:=false;
    for i:=1 to M-1 do
      if a[i] > a[i+1] then
        begin
          p:=a[i]; a[i]:=a[i+1]; a[i+1]:=p;
          vymena:=true;
        end;
  until vymena=false;
  writeln;
  writeln('Prve pole: ');
  for i:=1 to M do write(a[i],' ');
  
  repeat
    vymena:=false;
    for i:=1 to N-1 do
      if b[i] > b[i+1] then
        begin
          p:=b[i]; b[i]:=b[i+1]; b[i+1]:=p;
          vymena:=true;
        end;
  until vymena=false;
  writeln; writeln;
  writeln('Druhe pole je: ');
  for i:=1 to N do write(b[i],' ');
  
  i:=1; j:=1; k:=1;
  repeat
    if a[i]<=b[j] then begin c[k]:=a[i]; i:=i+1; k:=k+1; end
                  else begin c[k]:=b[j]; j:=j+1; k:=k+1; end;
  until (a[i]=999) AND (b[j]=999);

  writeln; writeln;
  writeln('Usporiadane pole je: ');
  for i:=1 to M+N do
    write(c[i],' ');
  readln;
end.
