PROGRAM ATCODER

implicit none
integer :: n, m, i, j, a, ans
integer,allocatable :: x(:), dx(:),dx1(:),bar(:)

read(*,*) n, m
allocate(x(m))
allocate(dx(m-1))
allocate(dx1(m-1))
allocate(bar(n-1))
read(*,*) (x(i), i=1,m)

if(n >= m) then
	ans = 0
	write(*,'(i0)') ans
    stop
end if

call qs(x,m) !昇順に並び替え

if(n == 1) then
	ans = x(m) - x(1)
    write(*,'(i0)') ans
    stop
end if

do i =1, m-1
	dx(i) = x(i+1) - x(i)
    dx1(i) = dx(i)
end do

do i = 1, n-1
	a = maxloc(dx1,1)
	bar(i) = a
    dx1(a) = 0
end do

call qs(bar,n-1)

ans = x(bar(1))-x(1) + x(m) - x(bar(n-1)+1)
do i = 1, n-2
	ans = ans + x(bar(i+1)) - x(bar(i)+1)
end do

write(*,'(i0)') ans

END PROGRAM ATCODER


recursive subroutine qs(x,n)
   integer,intent(in) :: n
   integer,intent(inout) :: x(n)
   integer :: p,i,imin,imax,it,is
   if(n == 1) return    
   p = x(1)   
   imin = 1   
   imax = n   
   do while(imin < imax)
      do while(x(imin) < p .and. imin < imax)
         imin = imin + 1
      enddo
      do while(p <= x(imax) .and. imin < imax)
         imax = imax - 1
      enddo
      if(imin < imax)then
         it = x(imax)
         x(imax) = x(imin)
         x(imin) = it
      endif
   enddo
   is = imax - 1
   if(is == 0) is = 1
   call qs(x(1:is),is)
   call qs(x(is + 1:n),n - is)
end subroutine qs