program main
implicit None
	integer(8)::n,m,i
	integer(8),allocatable::x(:),y(:)
	
	read*,n,m
	
	allocate(x(m),y(m-1))
	
	read*,x
	
	if(n >= m)then
		print "(i0)",0
		stop
	endif
	
	call h1(x,m)
	
	do i = 1,m-1
		y(i) = x(i+1)-x(i)
	enddo
	
	call h1(y,m-1)
	
	if (n > 1)then
		print "(i0)",sum(y)-sum(y(m-n+1:m-1))
		else
		print "(i0)",sum(y)
	endif
end program main

subroutine h1(a,n)
implicit None
	integer(8)::a(n),x
	integer(8)::n,i,ie,i0,i1,i2
	
	do i = n/2,1,-1
		x = a(i);ie = n + 1; i0 = i; i1 = i0 * 2
		do while(i1 < ie)
			i2 = i1 + 1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie)
		a(ie) = a(1)
		a(1) = x
		i0 =1;i1 =2
		do while(i1 <ie)
			i2 = i1 +1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h1
