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(DD(i)*DD(j).ge.0) then minimum = MIN(minimum, MAX(ABS(DD(i)),ABS(DD(j)))) else minimum = MIN(minimum, ABS(DD(i))+ABS(DD(j)) + MIN(ABS(DD(i)),ABS(DD(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