program main implicit none integer::N,M,minimum,i,j integer,allocatable::D(:),DD(:) read *,N,M allocate(D(M),DD(M)) read *,D DD = qsort(D) minimum = 30000 do i=1,M-N+1 j = i+N-1 if(D(i)*D(j).ge.0) then minimum = MIN(minimum, MAX(ABS(D(i)),ABS(D(j)))) else minimum = MIN(minimum, ABS(D(i))+ABS(D(j)) + MIN(ABS(D(i)),ABS(D(j)))) end if end do print '(i0)',minimum contains 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 end program main