Problem:
 Score : 300 points    Problem Statement We will play a one-player game using a number line and N pieces.  First, we place each of these pieces at some integer coordinate.  Here, multiple pieces can be placed at the same coordinate.  Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:  Move : Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1 .  Note that the coordinates where we initially place the pieces are already regarded as visited.  Find the minimum number of moves required to achieve the objective.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq M \leq 10^5  -10^5 \leq X_i \leq 10^5  X_1, X_2, ..., X_M are all different.         Input Input is given from Standard Input in the following format:  N  M  X_1  X_2  ...  X_M       Output Find the minimum number of moves required to achieve the objective.        Sample Input 1 2 5
10 12 1 2 14      Sample Output 1 5  The objective can be achieved in five moves as follows, and this is the minimum number of moves required.   Initially, put the two pieces at coordinates 1 and 10 .  Move the piece at coordinate 1 to 2 .  Move the piece at coordinate 10 to 11 .  Move the piece at coordinate 11 to 12 .  Move the piece at coordinate 12 to 13 .  Move the piece at coordinate 13 to 14 .        Sample Input 2 3 7
-10 -3 0 9 -100 2 17      Sample Output 2 19       Sample Input 3 100 1
-100000      Sample Output 3 0

Solution (Python):











import sys
sys.setrecursionlimit(10000)

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time
	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def read_int(input=None):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [int(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret

def read_float(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [float(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
	
def read_str(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [x for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
		
def read_int(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return int(read)

def read_int_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [int(x) for x in read.split()]

def read_float(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return float(read)

def read_float_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [float(x) for x in read.split()]

def read_str(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return str(read)

def read_str_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [str(x) for x in read.split()]	

@mt
def slv(*args):
	X.sort()
	
	'''
	Y = []
	for i in range(M-1):
		Y.append(X[i+1] - X[i])
	'''
	Y = [X[i+1] - X[i] for i in range(M-1)]
	Y.sort(reverse=True)
	
	ans = X[-1] - X[0]
	for y in Y[:N-1]:
		ans -= y
	
	return ans

if __name__ == '__main__':
	t = '''3 7
-10 -3 0 9 -100 2 17'''.splitlines()
	
	N, M = read_int_n()
	X = read_int_n()
	
	print(slv(N,M,X))

Solution (Python):











import sys
sys.setrecursionlimit(10000)

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time
	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def read_int(input=None):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [int(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret

def read_float(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [float(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
	
def read_str(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [x for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
		
def read_int(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return int(read)

def read_int_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [int(x) for x in read.split()]

def read_float(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return float(read)

def read_float_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [float(x) for x in read.split()]

def read_str(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return str(read)

def read_str_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [str(x) for x in read.split()]	

@mt
def slv(*args):
	X.sort()
	
	Y = []
	for i in range(M-1):
		Y.append(X[i+1] - X[i])
	Y.sort(reverse=True)
	
	ans = X[-1] - X[0]
	for y in Y[:N-1]:
		ans -= y
	
	return ans

if __name__ == '__main__':
	t = '''3 7
-10 -3 0 9 -100 2 17'''.splitlines()
	
	N, M = read_int_n()
	X = read_int_n()
	
	print(slv(N,M,X))

Solution (Python):
N, M = [int(i) for i in input().split()]
XS = [int(i) for i in input().split()]

if M <= N:
  print(0)
else:
  xs = sorted(XS)
  ds = list(map(lambda a,b: b-a, xs, xs[1:]))
  print(sum(sorted(ds)[:M-N]))

Solution (Python):
n, m = list(map(int, input().split(' ')))
xs = list(map(int, input().split(' ')))

if n > m:
    print(0)
else:
    ranges = []
    prev_x = None
    for x in sorted(xs):
        if prev_x is not None:
            ranges.append(x - prev_x)
        prev_x = x

    ret = 0
    n_omit = n - 1
    omit = 0
    for r in sorted(ranges, reverse=True):
        if omit < n_omit:
            omit += 1
        else:
            ret += r
    print(ret)

Solution (Python):
n,m=input().split()
n=int(n)
m=int(m)
a = input().split()
for i in range(m):
  a[i]=int(a[i])
a.sort()
  
if n>=m :
  sum=0
else:
  b=[]
  sum=a[m-1]-a[0]
  for i in range(m-1):
    b+=[a[i+1]-a[i]]
  b.sort()
  b.reverse()
  for i in range(n-1):
    if b[i]>0:
      sum-=b[i]
    else:
      sum+=b[i]
print(sum)

Solution (Python):
import sys

N,M=map(int,input().split())
A=[int(x) for x in input().split()]
A.sort()
pos=[0 for x in range(M)]
dis=[]
for i in range(M-1):
  dis.append([abs(A[i]-A[i+1]),i+1])

if N>=M:
  print(0)
  sys.exit()
  
dis=sorted(dis,key=lambda x:-x[0])
for i in range(N-1):
  pos[dis[i][1]]=1
  
ans=0
cnt=A[0]
for i in range(M-1):
  if pos[i+1]==1:
    cnt=A[i+1]
  else:
    ans+=abs(cnt-A[i+1])
    cnt=A[i+1]
  
print(ans)

Solution (Python):
n, m = map(int, input().split())
x = [int(i) for i in input().split()]
x.sort()
if n>=m:
    print(0)
else:
    L = list()
    for i in range(m-1):
        L.append(x[i+1]-x[i])
    a=0
    L.sort()
    for i in range(n-1):
        L.pop(-1)
    for i in L:
        a = a + i
    print(a)

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
a=[0 for i in range(M-1)]
for i in range(M-1):
    a[i]=X[i+1]-X[i]
a.sort(reverse=True)
a=a[N-1:]
print(sum(a))

Solution (Python):
import numpy as np

N, M = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]

if N >= M:
    print(0)
    exit(0)

X = np.sort(X)
diff = []
for i in range(len(X) - 1):
    diff.append(X[i + 1] - X[i])

diff = np.sort(diff)
result = 0
for i in range(len(diff) - N + 1):
    result += diff[i]
print(result)

Solution (Python):

N,M = map(int,input().split())
X = [int(i) for i in input().split()]

X.sort()
if N>=M:
    print("0")
else:
    L = list()
    for i in range(M-1):
        L.append(X[i+1]-X[i])
    a=0 
    L.sort()
    for i in range(N-1):
        L.pop(-1)
    for i in L:
        a+=i
    print(a)

Solution (Python):
N,M=map(int,input().split())
pos=list(map(int,input().split()))

if N>=M:
	print(0)
else:

	pos=sorted(pos)
	dis=[]
	for i in range(1,M):
		dis.append(pos[i]-pos[i-1])

	dis=sorted(dis)
	ans=0
	for i in range(M-N):
		ans+=dis[i]
	print(ans)

Solution (Python):
n, m = map(int, input().split())
X = list(map(int, input().split()))

if(n >= m):
    print('0')
    exit()

X.sort()
diff = []
for i in range(1, len(X)):
    diff.append(X[i]-X[i-1])

diff.sort()

ans = 0
for i in range(m-n):
    ans += diff[i]

print(ans)

Solution (Python):
n,m = map(int,input().split())
li = list(map(int,input().split()))
li2 = []

li.sort()

for i in range(m-1):
    li2.append(li[i+1]-li[i])

li2.sort()
sum = 0
for i in range(m-n):
    sum += li2[i]

print(sum)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if n >= m:
    print(0)
    exit(0)

x.sort()

l = []

for i in range(m-1):
    l.append(x[i+1] - x[i])

l.sort()

print(sum(l[:m-n]))

Solution (Python):

N,M = [int(x) for x in input().split()]

x = [int(num) for num in input().split()]

x = sorted(x)

dis = [x[i+1]-x[i] for i in range(M-1)]

dis = sorted(dis)

dis_top = 0
for i in range(1,N):
    if((M-1)-i < 0):
        break;
    dis_top = dis_top + dis[(M-1)-i]

ans = sum(dis) - dis_top
print(ans)

Solution (Python):
import sys

n,m = map(int,input().split())

if n >= m :
  print("0")
  sys.exit()
xlist = [int(_) for _ in input().split()]
xlist.sort()
nlist = []
for i in range(m-1):
  nlist.append(xlist[i+1] - xlist[i])
nlist.sort()

print(sum(nlist[0:m-n]))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()

if n >= m:
    print(0)
else:
    diff = [x[i+1]-x[i] for i in range(m-1)]
    diff.sort()
    print(sum(diff[0:m-n]))

Solution (Python):
N,M = map(int,input().split())
if N>=M:
  print(0)
  exit()
X = list(map(int,input().split()))
X.sort()
diffX = []
for i in range(len(X)-1):
  diffX.append(X[i+1]-X[i])
diffX.sort()
totalWidth = X[len(X)-1]-X[0] 
for i in range(N-1):
  totalWidth -= diffX[len(X)-2-i]
print(totalWidth)

Solution (Python):



def main():
    n, m = map(int, input().split())
    xs = sorted(list(map(int, input().split())))

    if n >= m:
        print(0)
    else:
        ans = xs[-1] - xs[0]
        diff = [0 for _ in range(m - 1)]

        for i in range(m - 1):
            diff[i] = xs[i + 1] - xs[i]

        print(ans - sum(sorted(diff, reverse=True)[:n - 1]))


if __name__ == '__main__':
    main()

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()

if n >= m:
    print(0)
    exit()


sub = [0]*(m-1)
for i in range(m-1):
    sub[i] = x[i+1]-x[i]
sub.sort()

ass = 0
for i in range(m-n):
    ass += sub[i]
print(ass)

Solution (Python):

import sys

N, M = map(int, input().split())
X = map(int, input().split())

if N > M:
    print(0)
    sys.exit()
sorted_x = sorted(list(X))

sub_X = [abs(sorted_x[i+1] - sorted_x[i]) for i in range(0,M-1)]

sorted_sub_x = sorted(sub_X,reverse=True)

print( sorted_x[M-1] -sorted_x[0] - sum([ sorted_sub_x[i] for i in range(0,N-1)]) )

Solution (Python):
N, M = map(int, input().split())
X_list = list(map(int,input().split()))

if M == 1:
    print(0)
else:
    sorted_X_list = sorted(X_list)
    sabun_list = []

    for i in range(0,len(sorted_X_list)-1):
        sabun_list.append(sorted_X_list[i+1]-sorted_X_list[i])

    sabun_list.sort(reverse=True)
    del sabun_list[:N-1]

    print(sum(sabun_list))

Solution (Python):
n, m = map(int, input().split())
x_list = list(map(int, input().split()))

x_list.sort()
diff_list = [x_list[i] - x_list[i-1] for i in range(1, m)]
diff_list.sort()
if m-n < 0:
    nokori = 0
else:
    nokori = m - n
print(sum(diff_list[:nokori]))

Solution (Python):
N, M = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
X.sort()

diffList = [X[i+1] - X[i] for i in range(M - 1)]
diffList.sort()

ans = 0
if N - M < 0:
    ans += sum(diffList[:M - N])

print(ans)

Solution (Python):
n, m = map(int, input().split())
x = sorted(map(int, input().split()))

if n >= m:
    print(0)
else:
    d = sorted(abs(x[i] - x[i+1]) for i in range(m-1))
    print(sum(d[:m-n]))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()

if n >= m:
    print(0)
else:
    diff = [x[i+1]-x[i] for i in range(m-1)]
    diff.sort()
    for i in range(n-1):
        diff.pop()
    print(sum(diff))

Solution (Python):
n,m = map(int,input().split())
l = sorted(list(map(int,input().split())))
l_dis = []
for i in range(m-1):
        l_dis_i = l[i+1]-l[i]
        l_dis.append(l_dis_i)
l_dis = sorted(l_dis)
ans = 0
for i in range(m-n):
        ans += l_dis[i]
print(ans)

Solution (Python):
N,M = [int(x) for x in input().split()]
X= [int(x) for x in input().split()]

X.sort()

Y = []
x = X[0]
for i in range(1,M):
    Y.append(abs(x - X[i]))
    x = X[i]

Y.sort(reverse=True)
ans = max(X) - min(X) - sum(Y[0:N-1])

print(ans)

Solution (Python):
n, m = map(int, input().split())
x = sorted(map(int, input().split()))
d = sorted(abs(x[i]-x[i+1]) for i in range(m-1))
print(sum(d[:max(0, m-n)]))

Solution (Python):


N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()

if N >= M:
    print(0)
    exit()

diffX = [X[i + 1] - X[i] for i in range(len(X) - 1)]
diffX.sort()

ans = X[-1] - X[0]
for i in range(N - 1):
    ans -= diffX[-(i + 1)]

print(ans)

Solution (Python):




def solveProblem(chessman,checkPointCount,checkPointListOrg):
	if checkPointCount <= chessman:
		answer = 0
	else:
		checkPointList = sorted(checkPointListOrg)
		allLong = checkPointList[-1]-checkPointList[0]
		distanceList=list()
		checkPointRange= range(0,len(checkPointList)-1,+1)
		for i in checkPointRange:
			distanceList.append(checkPointList[i+1]-checkPointList[i])
		distanceList.sort(reverse=True)
		openLine = sum(distanceList[0:chessman-1])
		answer = allLong-openLine
	return answer

if __name__ == '__main__':
	N,M = map(int,(input().strip().split(' ')))
	Xi = list(map(int,input().strip().split(' ')))
	solution = solveProblem(N,M,Xi)
	print("{}".format(solution))

Solution (Python):
s=input().split()
n,m=int(s[0]),int(s[1])
a=input().split()
if n>=m:
    print(0)
else:
    for i in range(m):
        a[i]=int(a[i])
    a.sort()
    
    ans=a[-1]-a[0]
    b=[]
    for i in range(m-1):
        b.append(a[i+1]-a[i])
    b.sort()
    for i in range(1,n):
        ans-=b[-i]
    print(ans)

Solution (Python):
N, M = map(int, input().split())
X = sorted(map(int, input().split()))
D = sorted(abs(x - y) for x, y in zip(X, X[1:]))
if N > 1:
    print(sum(D[:-(N-1)]))
else:
    print(sum(D))

Solution (Python):
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))
a.sort(reverse=True)

if n >= m:
    print(0)
    exit()

b = []
for i in range(m-1):
    b.append(abs(a[i+1]-a[i]))

b.sort()
print(sum(b[:m-n]))

Solution (Python):
n, m = map(int, input().split())
des = sorted(list(map(int, input().split())))
dis = [0]*(m-1)
for i in range(m-1):
    dis[i] = des[i+1] - des[i]
dis.sort(reverse=True)
print(sum(dis[(n-1)::]))

Solution (Python):
try:
    N, M = map(int, input().split())
    Xi = [int(x) for x in input().split()]

    if N < 1 or N > 10**5:
        exit()
    if M < 1 or M > 10**5:
        exit()
    if M != len(Xi):
        exit()
    for i in Xi:
        if i < -10**5 or i > 10**5:
            exit()
    Xi = sorted(Xi)

    a = []
    for i in range(M-1):
        a.append(Xi[i+1]-Xi[i])

    a = sorted(a, reverse=True)
    s = 0
    
    for i in range(N-1):
        
        a.pop(0)

    for i in a:
        s += abs(i)
    print(s)
except ValueError:
    exit()
except IndexError:
    print(0)

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort(reverse=True)
a = [0 for i in range(M)]
for i in range(1,M):
    a[i-1] = X[i-1]-X[i]
a.sort(reverse=True)
print(sum(a[N-1:]))

Solution (Python):


a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.sort()

c = []
if len(b) != 1:
  for i in range(len(b) - 1):
    c.append(b[i+1] - b[i])

if len(b) == 1:
  print(0)
  
    
else:  
  c.sort()
  for j in range(min(a[0] - 1,a[1])):
    c[len(c)- 1 - j] = 0
  print(sum(c))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))

if N >= M:
    print(0)
else:
    X.sort()
    l = X[-1] - X[0]
    d = [X[i + 1] - X[i] for i in range(M - 1)]
    d.sort(reverse=True)
    result = l - sum(d[:N - 1])
    print(result)

Solution (Python):
def main():
  N, M = map(int, input().split())
  X = list(map(int, input().split()))
  
  X = sorted(X)
  X = sorted([X[i+1]-X[i] for i in range(M-1)], reverse=True)
  print(sum(X[N-1:]))


main()

Solution (Python):
import numpy as np
n,m = map(int,input().split())
x = np.sort(np.array(list(map(int,input().split()))))
d = np.sort(abs(x[:-1] - x[1:]))
print(0 if n >= m else sum(d[:m-n]))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
diff = []
X.sort()
for i in range(1, len(X)):
  diff.append(X[i] - X[i-1])
diff.sort(reverse=True)
print(sum(diff[N-1:]))

Solution (Python):
import numpy as np

n, m = [int(_) for _ in input().split()]
x = np.array(sorted((int(_) for _ in input().split())))
d = np.sort(x[1:] - x[:-1])[:-n:-1]

print(x[-1] - x[0] - np.sum(d))

Solution (Python):
nandm = input().split()
numlist = input().split()
N = int(nandm[0])
M = int(nandm[1])
if N >= M:
    print(0)
else:
    intlist = [int(num) for num in numlist]
    intlist.sort()
    subtract = [intlist[i + 1] - intlist[i] for i in range(len(intlist) - 1)]
    subtract.sort()
    print(sum(subtract[:(M - N)]))

Solution (Python):
def solve(n, m, x):
    if n >= m:
        return 0

    y = [x[i + 1] - x[i] for i in range(m - 1)]
    y.sort(reverse=True)

    return x[-1] - x[0] - sum(y[:n-1])

def main():
    n, m = map(int, input().split())
    x = [int(s) for s in input().split()]
    x.sort()

    print(solve(n, m, x))

main()

Solution (Python):
import numpy as np
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X = np.sort(X)

diff = np.diff(np.sort(X), n=1)
print(np.sum(np.sort(diff)[::-1][N-1:]))

Solution (Python):
n, m = (int(i) for i in input().split())
x = list(map(int, input().split()))
if(m <= n):
    print(0)
else:
    x.sort()
    out = list(map(lambda x,y: abs(x-y), x[:-1],x[1:]))
    out.sort()
    if(n==1):
        print(sum(out[0:]))
    else:
        print(sum(out[0:-(n-1)]))

Solution (Python):
import heapq

class Solver(object):
    def __init__(self):
        self._input()
        self.heap = []

    def solve(self):
        if self.n >= self.m:
            print(0)
            return

        self.x_s.sort()
        for i in range(len(self.x_s) - 1):
            heapq.heappush(self.heap, -abs(self.x_s[i+1] - self.x_s[i]))

        for i in range(self.n - 1):
            heapq.heappop(self.heap)

        print(-sum(self.heap))

    def _input(self):
        self.n, self.m = list(map(int, input().split(" ")))
        self.x_s = list(map(int, input().split(" ")))


if __name__ == "__main__":
    Solver().solve()

Solution (Python):
import numpy as np
N,M=map(int,input().split())
Xm = list(map(int,input().split()))
if N>=M:
  print(0)
else:
  Xm.sort()
  list1 = np.array(Xm[0:M-1])
  list2 = np.array(Xm[1:M])
  diff = (list2-list1).tolist()
  if N==1:
    print(sum(diff))
  else:
    diff.sort()
    while (N>1):
      N=N-1
      diff.pop()
    print(sum(diff))

Solution (Python):
import sys

def main(N, M, Xs):
    Xs.sort()

    if N >= M:
        return 0

    assert M == len(Xs)

    diffs = []
    
    for i in range(1, M):
        
        diffs.append((i, Xs[i] - Xs[i-1]))
    diffs.sort(key=lambda x: x[1], reverse=True)

    
    positions = [0] + [diff[0] for diff in diffs[:N-1]]

    
    moves = [ Xs[-1] - Xs[positions[-1]] ] 
    for i in range(1, N):
        moves.append(Xs[positions[i]-1] - Xs[positions[i-1]])

    return sum(moves)

line = sys.stdin.readline()
N,M = [int(x) for x in line.strip().split(' ')]

line = sys.stdin.readline()
Xs = [int(x) for x in line.strip().split(' ')]

print(main(N, M, Xs))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
Y = []
for i in range(M-1):
    Y.append(X[i+1] - X[i])
Y.sort(reverse=True)
print(sum(Y[N-1:]))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
sa = [X[i + 1] - X[i] for i in range(M - 1)]
sa.sort()
if N >= M:
    print(0)
else:
    print(sum(sa[:M - N]))

Solution (Python):
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
b=[0]*(m-1)
for i in range(0,m-1):
  b[i]=a[i+1]-a[i]
c=sorted(b)
s=a[m-1]-a[0]
if m>n:
  for i in range(0,n-1):
  	s=s-c[m-i-2]
else:
  s=0
print(s)

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))

if N >= M:
    print(0)
else:
    X.sort()
    D = []
    for i in range(M-1):
        D.append(X[i+1]-X[i])
    D.sort()
    print(sum(D[0:M-N]))

Solution (Python):
N, M = map(int, input().split())
lst_X = sorted(list(map(int, input().split())))

if N>=M:
    print(0)
else:
    lst_diff = sorted([lst_X[i+1]-lst_X[i] for i in range(M-1)])[:M-N]
    print(sum(lst_diff))

Solution (Python):
N, M = map(int, input().split())
lst_X = sorted(list(map(int, input().split())))

if N>=M:
    print(0)
else:
    lst_diff = []
    for i in range(M-1):
        diff = abs(lst_X[i] - lst_X[i+1])
        lst_diff.append(diff)
    lst_diff.sort(reverse=True)
    print(sum(lst_diff[N-1:]))

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
if n>=m:
  print(0)
else:
  x.sort()
  dx = []
  for i in range(m-1):
    dx.append(abs(x[i]-x[i+1]))
  dx.sort()
  print(sum(dx[:m-n]))

Solution (Python):
N, M = map(int,input().split())
if N >= M :
  print(0)
else:
  points = sorted(list(map(int,input().split())))
  length = sorted([points[i+1]-points[i] for i in range(M-1)])
  print(sum(length[0:M-N:1]))

Solution (Python):
import numpy as np
N,M=list(map(int,input().split()))
X=np.sort(np.array(list(map(int,input().split()))))

if N>=M:
    print(0)
else:
    L=np.sort(np.diff(X))
    ans=X[-1]-X[0]
    for i in range(1,N):
        ans-=L[-i]
    print(ans)

Solution (Python):

n,m = map(int,input().split())
xlist = list(map(int,input().split()))

xlist.sort()
ylist=[]

for i in range(1,m):
    ylist.append(xlist[i]-xlist[i-1])

ylist.sort()
a=0
for i in range(0,m-n):
    a+=ylist[i]
print(a)

Solution (Python):
N, M = map(int, input().split( ))
datas = list(map(int, input().split( )))
datas.sort()
Ms = M - 1

dest = list()
K = 0
i = 0
for i in range(Ms):
  K = datas[i+1] - datas[i]
  dest.append(K)

dest.sort()
L = 0
MN = M - N
for j in range(MN):
  L = L + dest[j]

print(L)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n >= m:
    print(0)
else:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:m-n]))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n < m:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:m-n]))
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
X    = list(map(int, input().split()))

X.sort()

P = [X[i+1] - X[i] for i in range(M-1)]
P.sort()

P = P[::-1]

print(sum(P[N-1:]))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n < m:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:(m-n)]))
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n < m:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:(m-n)]))
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n < m:
    x_distance = [x[i + 1] - x[i] for i in range(m - 1)]
    x_distance.sort()
    print(sum(x_distance[:(m-n)]))
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()

if n < m:
    x_distance = [x[i + 1] - x[i] for i in range(m - 1)]
    x_distance.sort()
    ans = 0
    for i in range (m - n):
        ans += x_distance[i]
    print(ans)
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()

if n < m:
    x_distance = [abs(x[i + 1] - x[i]) for i in range(m - 1)]
    x_distance.sort()
    ans = 0
    for i in range (m - n):
        ans += x_distance[i]
    print(ans)
else:
    print(0)

Solution (Python):
N,M = map(int,input().split())
X = sorted(list(map(int, input().split())))


XX = []
for i in range(M):
    if i == 0:

        continue
    else:
        XX.append((abs(X[i] - X[i-1]),i-1))

XX_sort = sorted(XX,reverse=True)

XX_sort_2 = XX_sort[N-1:]

if N >= M:
    ans = 0
elif N == 1:
    ans = abs(X[-1] - X[0])
else:
    ans = 0
    for k,v in XX_sort_2:
        ans += k

print(ans)

Solution (Python):
n,m=map(int,input().split())
buf=input().split()
a=[None for i in range(m)]
b=[None for i in range(m-1)]
for i in range(m):
    a[i]=int(buf[i])
a.sort()
if n==1:
    print(a[-1]-a[0])
else:
    for i in range(1,len(a)):
        b[i-1]=a[i]-a[i-1]
    b.sort()
    print(sum(b[:-n+1]))

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))

if n >= m:
    print(0)
else:
    x = sorted(x)
    x = [x[i+1]-x[i] for i in range(len(x)-1)]

    x = sorted(x, reverse=True)
    x = x[n-1:]
    ans = 0
    for i in x:
        ans+=i
    print(ans)

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))

if n >= m:
    print(0)
else:
    x = sorted(x)
    xx = [x[i+1]-x[i] for i in range(len(x)-1)]

    xx = sorted(xx, reverse=True)
    xx = xx[n-1:]
    ans = 0
    for i in xx:
        ans+=i
    print(ans)

Solution (Python):
N,M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]

X.sort()
D = []
for i in range(M-1):
    D.append(X[i+1]-X[i])

D.sort()
ans=0
for i in range(M-N):
    ans+=D[i]

print(ans)

Solution (Python):
N,M=map(int,raw_input().split())
X=map(int,raw_input().split())

X.sort()
G=[X[i+1]-X[i] for i in range(M-1)]
G.sort()
   
if M>N:
    Ans=sum(G[i] for i in range(M-N))
else:
    Ans=0
    
print Ans

Solution (Python):
import heapq

N, M = map(int, input().split())
XM = list(map(int, input().split()))

if N >= M:
    print(0)
    exit()

XM.sort()
diffs = [XM[i + 1] - XM[i] for i in range(M - 1)]
nmin_diffs = heapq.nlargest(N - 1, diffs)
nmin_diffs_ind = [diffs.index(nmin_diffs[i]) for i in range(N - 1)]
nmin_diffs_ind.sort()
nmin_diffs_ind.append(len(XM) - 1)

dist = XM[nmin_diffs_ind[0]] - XM[0]
for i in range(N - 1):
    dist += XM[nmin_diffs_ind[i + 1]] - XM[nmin_diffs_ind[i] + 1]

print(dist)

Solution (Python):
import sys
import numpy as np

def main(inp):
    N,M=map(int,next(inp).split())
    X=np.array(list(map(int,next(inp).split())))
    X=np.sort(X)
        



    X=np.sort(X)    



    
    if(N>M): return 0
    if(N==1):
        ans=X[len(X)-1]-X[0]
        return ans
    
    delta=np.empty([M-1],dtype=int)
    
    for i in range(M-1):
        delta[i]=X[i+1]-X[i]
    


    
    
    
    
    
    splpoint=np.argpartition(np.array(delta),-N+1)[-N+1:]

    
    delta=np.delete(delta,splpoint)


    
    ans=sum(delta)
        
    return ans

print(main(sys.stdin))

Solution (Python):
n,m = map(int,input().split(' '))
if n >= m:
    print(0)
else:
    X = list(map(int,input().split(' ')))
    X.sort()
    dis = []
    for i in range(m - 1):
        dis.append(X[i + 1] - X[i])
    dis.sort(reverse = True)
    div = 0
    total = X[m - 1] - X[0]

    for i in range(n - 1):
        div += dis[i]
    print(total - div)

Solution (Python):
n, m = tuple(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()

diff = [x[i + 1] - x[i] for i in range(len(x) - 1)]
diff.sort()

diff_sum = 0

for i in range(len(diff) - (n - 1)):
    diff_sum += diff[i]

print(diff_sum)

Solution (Python):
if __name__ == '__main__':
    N, M = map(int, input().split())
    X = sorted([int(i) for i in input().split()])
    diff = [None for i in range(M-1)]

    for i in range(len(diff)):
        diff[i] = X[i+1] - X[i]

    print(sum(sorted(diff, reverse=True)[N-1:]))

Solution (Python):
n, m = tuple(map(int, input().split()))
x = list(map(int, input().split()))
x = sorted(x)

diff = []

for i in range(len(x) - 1):
    diff.append(x[i + 1] - x[i])

diff = sorted(diff)

diff_sum = 0
diff_len = m - 1

for i in range(diff_len - (n - 1)):
    diff_sum += diff[i]

print(diff_sum)

Solution (Python):
n,m=map(int, input().split())
a=list(map(int, input().split()))
a.sort()
b=[a[i+1]-a[i] for i in range(m-1)]
b.sort(reverse=True)
print(sum(b[n-1:]))

Solution (Python):
n, m = map(int,input().split())
xs = list(map(int,input().split()))
ls=[]
before=1000000
for x in sorted(xs):
    if before != 1000000:
        ls.append(abs(x-before))
    before = x
ls2 = sorted(ls,reverse=True)
print(sum(ls2[n-1:]))

Solution (Python):
import sys
def main():
    n,m = map(int, input().split())

    if n >= m:
        print(0)
        sys.exit()

    x = sorted(list(map(int, input().split())))
    y = sorted([x[i]-x[i-1] for i in range(m-1,0,-1)])[::-1]
    s = sum(y)

    print(s-sum(y[:n-1]))
    
if __name__=="__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
if n>=m:print(0)
else:
  x.sort()
  l=[]
  for i in range(m-1):
    l.append(x[i+1]-x[i])
  l.sort()
  ct=0
  for j in range(m-n):
    ct+=l[j]
  print(ct)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    x = sorted(LI())
    t = sorted([x[i+1]-x[i] for i in range(m-1)])
    r = 0
    for i in range(m-n):
        r += t[i]

    return r



print(main())

Solution (Python):
n,m,*x=map(int,open(0).read().split());x.sort();print(sum(sorted(j-i for i,j in zip(x,x[1:]))[::-1][n-1:]))

Solution (Python):

from sys import stdin



s_in = lambda: stdin.readline()[:-1]  
d_in = lambda: int(stdin.readline())  
ds_in = lambda: list(map(int, stdin.readline().split()))  


N, M = ds_in()
X_list = ds_in()
if N >= M:
    print(0)
    exit()

X_list.sort()
diff = [X_list[i+1] - X_list[i] for i in range(M-1)]

diff.sort()
print(sum(diff[:M-N]))

Solution (Python):
N, M = map(int,input().split())
X = list(map(int, input().split()))
X.sort()
Xd = [0] * (M-1)
for i in range(M-1):
  Xd[i] = X[i+1] - X[i]

Xd.sort()

if N>=M:
  print(0)
else:
  print(sum(Xd[0:M-N]))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))









x.sort()








diff_list = []
for i in range(m-1):
    diff = x[i+1] - x[i]
    diff_list.append(diff)



diff_list.sort()



if n-1>0:
    print(sum(diff_list[:-(n-1)]))
else:
    print(sum(diff_list))

Solution (Python):
x,y=map(int,input().split())
if x>=y:
  print(0)
  exit()
a=list(map(int,input().split()))
a.sort()
l=a[0]
m=[]
for i in a[1:]:
  m.append(i-l)
  l=i
m.sort(reverse=True)
print(sum(m[x-1:]))