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 numpy as np
def inpl(): return list(map(int,input().split()))
N, M = inpl()
X = np.sort(np.array(inpl(),dtype=np.int))

if N >= M:
  print(0)
  exit()
else:
  D = np.sort(X[1:] - X[:-1])
  print(D[:M-N].sum())

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

Solution (Python):
N, M = map(int, input().split())
if N >= M:
  print(0)
  exit()

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

X = sorted(X)
L = [ X[i] - X[i-1] for i in range(1, M)]
L = sorted(L)



print(sum(L[:M-N]))

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:]))[-n::-1]))

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

print(result)

Solution (Python):
N, M = map(int,input().split())
X = sorted(list(map(int,input().split())))
tmp = sorted([X[i+1]-X[i] for i in range(M-1)])
ans = sum(tmp[:M-N])
if(M <= N):
  ans = 0
print(ans)

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

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

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

if N >= M:
    ans = int(0)
else:
    ans = sum(dls[0:M-N])

print(ans)

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
diff = []
 
X.sort()
 
if (N < M): 
    for i in range(M-1):
        dif = X[i+1] - X[i]
        diff.append(dif)
    
    diff.sort()
    
    for i in range(M-N):
        ans += diff[i]
    
    
   
    
    
print(ans)

Solution (Python):
n,m=map(int,input().split())
line=list(map(int,input().split()))
if n >= m:
  print(0)
else:
  line.sort()
  stock=0
  distance=[]
  for i in line:
    distance.append(i-stock)
    stock=i
  distance.pop(0)
  distance.sort()
  ans=0
  for i in range(m-n):
    ans+= distance[i]
  print(ans)

Solution (Python):



N,M = [int(x) for x in input().split()]
xls = [int(x) for x in input().split()]
xls.sort()
dls = [xls[i+1]-xls[i] for i in range(M-1)]
if N >= M:
    ans = 0
else:
    dls.sort()
    ans = sum(dls[0:M-N])

print(ans)

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

Solution (Python):
n, m = (int(i) for i in input().split())  
x = sorted([int(i) for i in input().split()])

if m > 1:
    x_diff = []
    for i in range(m-1):
        x_diff.append(x[i+1] - x[i])
else:
    x_diff = [0]

if n > 1:
    x_diff = sorted(x_diff)[:-(n-1)]

print(sum(x_diff))

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

Solution (Python):
def ans():
  N, M = map(int,input().split())
  X = sorted(list(map(int,input().split())))
  tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
  if(M <= N):
    print(0)
  else:
    print(sum(tmp[:M-N]))
ans()

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


if N >= M:
    print(0)
else:
    Di = []
    for i in range(M - 1):
        d = Xi[i + 1] - Xi[i]
        Di.append([i, d])
    Di.sort(key=lambda x: x[1], reverse=True)

    put_idx = [0]
    for i in range(N - 1):
        put_idx.append(Di[i][0] + 1)
    put_idx.append(M)

    total = 0
    for i in range(N):
        si = put_idx[i]
        ei = put_idx[i + 1] - 1
        s = Xi[si]
        e = Xi[ei]
        total += e - s
    print(total)

Solution (Python):
n, m = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()
blist = []

if n >= m :
  print(0)
elif n == 1:
  print(alist[m-1] - alist[0])
else:
  for i in range(0,m-1):
    blist.append(alist[i+1] - alist[i])
  blist.sort()
  d = -1 * (n-1)
  c = sum(blist[d:])
  b1 = alist[m-1] - alist[0]
  print(b1 - c)

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

if m <= n:
	print(0)
elif n == 1:
	print(abs(x[-1]-x[0]))
else:
	diff = [0]*(m-1)
	for it in range(m-1):
		diff[it] = x[it+1]-x[it]
	diff = sorted(diff)
	diff = diff[:m-n]
	
	print(sum(diff))

Solution (Python):
import numpy as np
N,M=list(map(int,input().split()))
X=np.array(list(map(int,input().split())))
if N>=M:
  print(0)
else:
	sX=np.sort(X)
	dsX=np.diff(sX)
	sdsX=np.sort(dsX)
	print(np.sum(sdsX[:M-N]))

Solution (Python):
import pdb
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
d=[]
if n<m:
    for i in range(0,len(x)-1):
        d.append(x[i+1]-x[i])
    d.sort(reverse=True)
    for i in range(n-1):
        d.pop(0)
    ans=sum(d)
    print(ans)
else:
    print(0)

Solution (Python):
N , M  = map(int,input().split())
Xm = [ x for x  in  map(int, input().split()) ]
Xm.sort()
lst = []
for i in range(M -1):
    lst.append(abs(Xm[i] - Xm[i+1]))
               
if(N >= M ):
    print(0)
else:
    lst.sort(reverse=True)
    print(sum(lst[N-1:]))

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

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

Solution (Python):
def streamline(N: int, M: int, X: list) -> int:
    
    
    
    
    _X = sorted(X)
    dist = [_X[i+1] - _X[i] for i in range(M-1)]
    max_dist = sorted(dist, reverse=True)[:N-1]
    return sum(dist) - sum(max_dist)


if __name__ == "__main__":
    N, M = [int(s) for s in input().split()]
    X = [int(s) for s in input().split()]

    ans = streamline(N, M, X)
    print(ans)

Solution (Python):
N, M = map(int, input().split())
x = list(map(int, input().split()))
x = list(sorted(x))
y = [xb - xa for xa, xb in zip(x, x[1:])]
y = list(sorted(y, reverse=True))
print(sum(y) - sum(y[:N - 1]))

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
def search():
  if N>=M:
    return 0
  X.sort()
  cost=[X[i+1]-X[i] for i in range(M-1)]
  ans_tmp=0
  cost.sort()
  for i in range(M-N):
    ans_tmp+=cost[i]
  return ans_tmp

print(search())

Solution (Python):
def main():
    N, M = map(int, input().split())
    *X, = map(int, input().split())

    ans = f(N, M, X)
    print(ans)


def f(N, M, X):
    """
    1 <= N, M <= 10^5
    |Xi| <= 10^5
    Xi != Xj

    入力例1より初期位置をマスにしてもいいので、0
    N < M なら1回以上動くコマが存在する
    N=1 なら
        X をsortして最小値から最大値まで動かす
    M=N+1 なら
        1動かす
        最後のXに1番近いコマを動かす
        1番近いかを調べるには、X同士の比較なのでN^2: TLE
        外側から内側に移動したほうがよい
            内側から外側に行ってから再度内側に行く場合、損する
            好きな整数座標なので-10^5-1でもよい
        Xをsortしたときの前との差分をとると、どこの間隔が長いか分かる
            その中で短い部分を移動
    """
    if N >= M:
        return 0

    X = sorted(X)
    diff = [abs(X[i+1] - X[i]) for i in range(M-1)]

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

    return ans


if __name__ == '__main__':
    main()

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

X.sort()

D = [X[i+1] - X[i] for i in range(M - 1)]

D.sort()

print(0 if M <= N else sum(D[:M-N]))

Solution (Python):
import numpy as np

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

X.sort()

D = np.diff(X)

D.sort()

print(0 if M <= N else np.sum(D[:M-N]))

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

X.sort()

D = [X[i+1] - X[i] for i in range(M - 1)]

D.sort()

print(0 if M <= N else sum(D[:M-N]))

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

X.sort()

D = [X[i+1] - X[i] for i in range(M - 1)]

D.sort()

print(0 if M <= N else sum(D[:M-N]))

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

X.sort()

D = [X[i+1] - X[i] for i in range(M - 1)]

D.sort()

print(0 if M <= N else sum(D[:M-N]))

Solution (Python):
import numpy as np

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


X.sort()


D = np.diff(X)


D = np.sort(D)[::-1]


print(X[-1] - X[0] - np.sum(D[:N - 1]))

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

Solution (Python):
N, M = map(int, input().split())
lst = sorted([int(x) for x in input().split()])
lst2 = []
for x in range(1, len(lst)):
    lst2.append(lst[x] - lst[x - 1])

lst2.sort()
if N >= M:
    print(0)
else:
    print(sum(lst2[:M-N]))

Solution (Python):
"""
配列をソート
差を取って、最も大きいところから切っていく
K個塊になるまで切っていく
塊の端っこから端っこまでを足していく
"""
import numpy as np

N, M  = map(int,input().split())
A = input().split()
A = np.array([int(a) for a in A])

def count(A):
    A_sorted = np.sort(A)
    delta = np.abs(A_sorted[:-1] - A_sorted[1:])
    delta_ind = delta.argsort()[::-1]
    delta_ind_sort = np.sort(delta_ind[:N-1]) 
    if len(delta_ind_sort) == 0:
        return 0 
    N_all = np.abs(A_sorted[0] - A_sorted[delta_ind_sort[0]]) 
    for i in range(len(delta_ind_sort)-1):
        
        N_all += np.abs(A_sorted[delta_ind_sort[i]+1] - A_sorted[delta_ind_sort[i+1]])
    
    N_all += np.abs(A_sorted[-1]-A_sorted[delta_ind_sort[-1]+1])
    return N_all


if N == 1:
    print(A.max()-A.min())
else:
    print(count(A))

Solution (Python):

def solve():
    
    
    N, M = [int(x) for x in input().split(" ")]
    X = [int(x) for x in input().split(" ")]

    if M == 1:
        print(0)
        return

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

    ans = 0
    for i in range(0, len(distance)-(N-1)):
        ans += distance[i]
    
    print(ans)



if __name__ == "__main__":
    solve()

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

X.sort()
dif = [ X[i+1] - X[i] for i in range(M-1) ]
dif.sort()
add = max(0,M - N)
print(sum(dif[:add]))

Solution (Python):
N,M=map(int,input().split())
X=sorted(map(int,input().split()))
diff=[0]*(M-1)
for i in range(M-1):
    diff[i]=X[i+1]-X[i]
print(sum(sorted(diff)[::-1][N-1:]))

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

if n >= m:
    print(0)

else:
    arr.sort()

    diff = [arr[i+1] - arr[i] for i in range(m-1)]
    diff.sort()

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

Solution (Python):
n, m = map(int, input().split())
line = [int(part) for part in input().split()]

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

line.sort()

sub_line = []
for num in range(len(line)-1):
    sub_line.append(line[num+1]-line[num])

sub_line.sort()

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

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

x = sorted(x)
mid = [x[i+1]-x[i] for i in range(m-1)]
mid = sorted(mid)
ans = sum(mid) if n == 1 else sum(mid[:-(n-1)])
print(ans)

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

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

X_arr.sort()

D_arr = [X_arr[x+1]-X_arr[x] for x in range(len(X_arr)-1)]
D_arr.sort()

D_all = 0
for D in D_arr[:(M-N)]:
    D_all += D

print(D_all)

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

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

X.sort()
Y = []
for i in range(len(X)-1):
    Y.append(X[i+1]-X[i])
Y.sort(reverse=True)

print(X[-1]-X[0]-sum(Y[:n-1]))

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

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


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

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

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

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

X.sort()
Y = []
for i in range(len(X)-1):
    Y.append(X[i+1]-X[i])
Y.sort(reverse=True)

print(X[-1]-X[0]-sum(Y[:n-1]))

Solution (Python):
n,m,*a=map(int,open(0).read().split());a.sort();print(abs(sum(sorted([a[s]-a[s+1]for s in range(m-1)])[n-1:])))

Solution (Python):
N,M=map(int,input().split(" "))
A=list(sorted(map(int,input().split(" "))))
L=sorted([A[s]-A[s+1] for s in range(M-1)])
print(abs(sum(L[N-1:])))

Solution (Python):
n,m=map(int,input().split())
l=[int(i) for i in input().split()]
l.sort()
ans=[0]
for i in range(m-1):
    ans.append(l[i+1]-l[i])
ans.sort()
for i in range(n-1):
    if len(ans)==1: break
    ans.pop()
print(sum(ans))

Solution (Python):
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
if N >= M:
    print(0)
    quit()
X_dis = []
for i in range(1, M):
    X_dis.append(X[i] - X[i-1])


X_dis = sorted(X_dis)
ans = sum(X_dis[:M-N])
print(ans)

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

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

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

move = 0

if n >= m:
  print(move)
elif n==1:
  move = max(x) - min(x)
  print(move)
else:
  x_diff = [None]*(m-1)
  for i in range(m-1):
    x_diff[i] = [i+1, x[i+1] - x[i]]
  x_diff = sorted(x_diff, key=lambda x:-x[1])
  sep = sorted(x_diff[:n-1])
  for j in range(n):
    if j==0:
      move += max(x[0:sep[j][0]])-min(x[0:sep[j][0]])
    elif j==n-1:
      move += max(x[sep[j-1][0]:])-min(x[sep[j-1][0]:])
    else:
      move += max(x[sep[j-1][0]:sep[j][0]])-min(x[sep[j-1][0]:sep[j][0]])
  print(move)

Solution (Python):
def solve(n, m, x):
    if m == 1:
        return 0
    x = list(sorted(x))
    d = [x[i+1] - x[i] for i in range(m-1)]
    d = list(sorted(d))
    k = max(0, len(d) - (n-1))
    return sum(d[:k])

n, m = map(int, input().split())
x = map(int, input().split())
print(solve(n, m, x))

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

if N >= M:
	print(0)
else:
	Xl = []
	for i in range(M - 1):
		Xl.append(X[i + 1] - X[i])
	Xl = sorted(Xl)
	ans = 0
	for i in range(M - N):
		ans += Xl[i]

	print(ans)

Solution (Python):
N, M = [int(x) for x in input().split()]
x = sorted([int(y) for y in input().split()])
sub = [x[i]-x[i-1] for i in range(1, M)]
if N >= M:
    print(0)
elif N == 1:
    print(sum(sub))
else:
    print(sum(sorted(sub)[:-(N-1)]))

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

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

Solution (Python):
N,M = map(int, input().split())
X = list(map(int,input().split()))
X.sort()
def solve():
    ans = 0
    if(N>=M):
        return print(ans)
    else:
        sub = []
        for i in range(M-1):
            sub_next = abs(X[i+1]-X[i])
            sub.append(sub_next)
        sub.sort()
        return print(sum(sub[0:M-N]))
solve()

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

Solution (Python):
def main():
    N, M = map(int, input().split())
    x = list(map(int, input().split()))
    
    if N >= M:
      print(0)
    else:
      x.sort()
      l_list = [x[i+1] - x[i] for i in range(M - 1)]
      l_list.sort(reverse=True)
      print(sum(l_list[N - 1:]))


if __name__ == '__main__':
    main()

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

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

if n >= m:
  	print(0)
else:
  for i in range(m):
    x[i] = int(x[i])
  x.sort()
  ans = x[-1] - x[0]
  
  b = []
  for i in range(m-1):
  	b.append(x[i+1]-x[i])
  b.sort()
  
  for i in range(1,n):
    
    ans -= b[-i]
  print(ans)

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



for i in range(1,M):
    X_intvl[i-1] = X[i] - X[i-1]



X_intvl.sort(reverse=True)



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

Solution (Python):

def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()

if(N >= M):
    print(0)
else:
  new_list = sorted(X)
  diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
  new_dif_list = sorted(diff_list, reverse = True)

  if(N  >= 2):
    for i in range(N-1):
        new_dif_list.pop(0)

  print(sum(new_dif_list))

Solution (Python):

def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()
new_list = sorted(X)
diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
new_dif_list = sorted(diff_list, reverse = True)

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

else:
  if(N  >= 2):
      for i in range(N-1):
        new_dif_list.pop(0)

  print(sum(new_dif_list))

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
def search():
  if N>=M:
    return 0
  X.sort()
  cost=[X[i+1]-X[i] for i in range(M-1)]
  ans_tmp=X[-1]-X[0]
  cost.sort(reverse=True)
  for i in range(N-1):
    ans_tmp -= cost[i]
  return ans_tmp

print(search())

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
x = sorted(x)
a=[]
if n>=m:
    print(0)
else:
    for i in range(len(x)-1):
        a.append(x[i+1]-x[i])
    a = sorted(a)
    ans = 0
    for i in range(m-n):
        ans+=a[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)
    L = []
    for i in range(0, M-1):
        L.append(X[i+1] - X[i])
    L = sorted(L, reverse = True)
    S = 0
    for i in range(N-1):
        S += L[i]
    res = X[M-1] - X[0] - S
    print(res)

Solution (Python):
n, m = map(int, input().split())
xlist = list(sorted(map(int, input().split())))
dxlist = sorted([nx - px for px, nx in zip(xlist, xlist[1:])], key=lambda x: -x)
if n >= m:
  print(0)
else:
  print(xlist[-1] - xlist[0] - sum(dxlist[:n-1]))

Solution (Python):
N, M = list(map(int ,input().split()))
X = list(map(int, input().split()))
dis = []
if N >= M:
    print(0)
else:
    X.sort()
    for i in range(M-1):
        dis.append(abs(X[i+1] - X[i]))
    dis.sort(reverse=True)
    ans = max(X) - min(X) - sum(dis[:N-1])
    print(ans)

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

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

print(max(X) - min(X) - sum(d[:N-1]))

Solution (Python):
n,m = map(int, input().split())
if m <= n:
  print(0)
  exit()
xx = list(map(int, input().split()))
xx.sort()
sub_x = [0]*(m-1)
for i, (front, end) in enumerate(zip(xx[:-1],xx[1:])):
  sub_x[i] = end - front
sub_x.sort()
print(sum(sub_x[:m-n]))

Solution (Python):
N, M = map(int, input().split(" "))
X = list(map(int, input().split(" ")))
X.sort()
total = X[-1]-X[0]
dif = [X[i+1] - X[i] for i in range(len(X)-1)]
dif.sort()
Y = [0 for i in range(N-1)]
for i in range(N-1):
  try:
    Y[i] = dif.pop()
  except:
    Y[i]=0
print(total-sum(Y))

Solution (Python):
num, num_of_coordinates = map(int, input().split(" "))
if num >= num_of_coordinates:
  print ("0")
else:
  coordinates = list(map(int, input().split(" ")))
  total = 0
  abs_distance = []
  coordinates.sort()
  for i in range(0, len(coordinates)-1):
    dist = abs(coordinates[i] - coordinates[i+1])
    total += dist
    abs_distance.append(dist)  

  
  abs_distance.sort()
  abs_distance.reverse()
  for i in range(num-1):
    total -= abs_distance[i]

  print(total)

Solution (Python):
nm_ent = list(map(int, input().split()))
n = nm_ent[0]
m = nm_ent[1]

x = list(map(int, input().split()))
x.sort()
if n >= m:
    print(0)
else:
    xl = []
    for i in range(0, m-1):
        xl.append(x[i+1] - x[i])
    xl.sort(reverse = True)
    del xl[:n-1]
    print(sum(xl))

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

dists = [abs(i - j) for i, j in zip(xs[1:], xs)]
dists.sort()
ans = 0 if n >= m else sum(dists[:m - n])

print(ans)

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

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

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

x.sort()

y = []
for i in range(0, m - 1):
    y.append(x[i + 1] - x[i])

y.sort()

s = 0
if m > n:
    s = sum(y[0:m - n])
print(s)

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


bet = [] 
for i in range(1,M):
  bet.append(x[i]-x[i-1])

if N == 1:
  print(max(x)-min(x))
elif N>=M:
  print(0)
else:
  bet = sorted(bet)
  print(sum(bet[:M-N]))

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

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

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

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

Solution (Python):
import numpy as np
import heapq

n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

x = sorted(x)
x = np.diff(x)


if n >= m:
    print(0)

elif n == 1:
    print(sum(x))

else:
    print(sum(heapq.nsmallest(len(x)-n+1, x)))

Solution (Python):
N_M = list(map(int,input().split()))
A = list(map(int,input().split()))

A.sort()

diff = []
for i in range(1,N_M[1]):
    diff.append(A[i] - A[i-1])

diff.sort()
print(sum(diff[::-1][N_M[0]-1:]))

Solution (Python):
import numpy as np


def solve(string):
    n, m, *x = map(int, string.split())
    x = sorted(np.diff(sorted(x)), reverse=True)
    return str(sum(x[n - 1:]))


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Solution (Python):
import numpy as np


def solve(string):
    n, m, *x = map(int, string.split())
    x = sorted(np.diff(sorted(x)))
    return str(sum(x)) if n == 1 else str(sum(x[:-n + 1]))


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Solution (Python):
def run(N, M, X):
    if N >= M:
        return 0
    X = sorted(X)
    diff = []
    for i in range(M-1):
        diff.append(X[i+1] - X[i])
    diff = sorted(diff)
    ret = X[-1] - X[0]
    for i in range(N-1):
        ret -= diff[-i-1]
    return ret


def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    print(run(N, M, X))


if __name__ == '__main__':
    main()

Solution (Python):






import numpy as np





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





if N >= M:
    print(0)
else:
    sorted_x = np.sort(X)
    dist = sorted_x[1:] - sorted_x[:-1]
    ans = np.sort(dist)[::-1][N-1:].sum()
    print(ans)

Solution (Python):
n, m = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
x.sort()

def play(n, m, x):
    if n >= m:
        return 0

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

    
    
    
    b = []
    for i, j in enumerate(a):
        b.append((j, i))
    b.sort(reverse=True)

    index = []
    for i in range(n):
        if i == 0:
            index.append(len(x) - 1)
        else:
            index.append(b[i-1][1])

    index.sort()
    move = []
    for i in range(len(index)):
        if i == 0 and index[i] == 0:
            move.append(0)
        elif i == 0:
            move.append(x[index[i]] - x[0])
        else:
            move.append(x[index[i]] - x[index[i-1] + 1])

    
    
    
    
    
    return sum(move)

print(play(n, m, x))

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

X.sort()
diffX = list(map(lambda x,y:x-y, X[1:], X[:-1]))
diffX.sort(reverse = True)

if N-1 >= len(diffX):
	print(0)
else:
	print(sum(diffX[N-1:]))

Solution (Python):
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

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

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

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

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

Solution (Python):
def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    if M <= N:
        print(0)
        return
    X.sort()
    diff = [0] * (M-1)
    for i in range(M-1):
        diff[i] = X[i+1] - X[i]
    diff.sort()
    print(sum(diff[:M-N]))


if __name__ == "__main__":
    main()

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

Solution (Python):
import numpy as np

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

li.sort()

dist = 0
_li = np.zeros(m)

for i in range(len(li)-1):
    _li[i] = (li[i+1] - li[i])

ans = sorted(_li, reverse=True)

print(int(sum(ans[(n-1):])))

Solution (Python):
import numpy as np
N, M = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
X = sorted(X)
ans = np.zeros(M)
for i in range(M-1):
    ans[i] = X[i+1] - X[i]

ans = sorted(ans, reverse=True)

print(int(sum(ans[N-1:])))

Solution (Python):
N,M = list(map(int,input().split()))
x=list(map(int,input().split()))
l=[]
s=0

if N >= M:
  print(0)
  quit()
else:
  x.sort()
  for i in range(1,M):
    l.append(x[i]-x[i-1])
  l.sort(reverse=True)
  for j in range(N-1):
    s += l[j]
  
  print(x[M-1]-x[0]-s)

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

X.sort()

d = [X[i]-X[i-1] for i in range(1,M)]
d.sort()


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

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

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

Solution (Python):


n,m=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
l=[a[i+1]-a[i] for i in xrange(m-1)]
l.sort(reverse=True)
print sum(l[n-1:])

Solution (Python):
n, m = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()

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

B.sort(reverse = True)

print(sum(B[(n - 1):]))

Solution (Python):
def get_int(): return int(input())
def get_float(): return float(input())
def get_line(): return input().split()
def get_lines(v): return [get_line() for _ in range(v)]
def get_int_line(): return list(map(int, get_line()))
def get_int_lines(v): return [get_int_line() for _ in range(v)]
def get_float_line(): return list(map(float, get_line()))
def get_float_lines(v): return [get_float_line() for _ in range(v)]

N, M = get_int_line()
X = get_int_line()

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

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

sub = 0
for i in range(N - 1):
  sub += x_dist[-(i + 1)]

print(sum(x_dist) - sub)

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

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x=sorted(x)
sub=[]
for i in range(len(x)-1):
	sub.append(x[i+1]-x[i])
sub=sorted(sub)
if n==1:
	print(sum(sub))
else:
	print(sum(sub[:-(n-1)]))

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

L.sort()
dist = [0 for _ in range(M-1)]
for i in range(M-1):
    dist[i] = L[i+1] - L[i]
    
dist.sort(reverse=True)
res = sum(dist[N-1:])
print(res)

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

l.sort(reverse=True)


d = list(map(lambda x: l[x] - l[x + 1], range(m - 1)))
d.sort(reverse=True)



x = l[0]-l[-1]-sum(d[:n-1])


print(x)