recursive function qsort(x) result(y) integer,intent(in) ::x(:) integer,allocatable::y(:) integer::pivot,total total = size(x) if (total <=1) then y = x else pivot = x(total/2) y = [qsort(pack(x, x .lt. pivot)), & pack(x, x .eq. pivot), & qsort(pack(x, x .gt. pivot))] endif end function qsort program main implicit none interface recursive function qsort(x) result(y) integer,intent(in) ::x(:) integer,allocatable::y(:) end function qsort end interface integer::NW,NB integer,allocatable::W(:),B(:) integer,allocatable::WS(:),BS(:) integer::i,j,top,itrw,itrb,maxw,maxb,height read *,NW allocate(W(NW),WS(NW)) read *,W read *,NB allocate(B(NB),BS(NB)) read *,B WS = qsort(W) BS = qsort(B) top = BS(NB) height = 1 do do itrw=NW, 1,-1 if(WS(itrw).lt.top) then top = WS(itrw) height = 1 + height exit end if end do if(itrw.eq.0.) exit do itrb=NB, 1,-1 if(BS(itrb).lt.top) then top = BS(itrb) height = 1 + height exit end if end do if(itrb.eq.0.) exit end do maxw = height top = WS(NW) height = 1 do do itrb=NB, 1,-1 if(BS(itrb).lt.top) then top = BS(itrb) height = 1 + height exit end if end do if(itrb.eq.0.) exit do itrw=NW, 1,-1 if(WS(itrw).lt.top) then top = WS(itrw) height = 1 + height exit end if end do if(itrw.eq.0.) exit end do maxb = height print '(i0)', MAX(maxw,maxb) end program main