CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int, input().split(" ")))
XList = list(map(int,input().split()))

XList = sorted(XList)
deltaList = [0]

for i in range(M-1) :
    deltaList.append(XList[i+1] - XList[i])
deltaList = sorted(deltaList, reverse=True)


for i in range(N-1) :
    deltaList[i] = 0
    if i == len(deltaList)-1 :
        break
print((sum(deltaList)))

# Optimized solution
n,m = list(map(int, input().split(" ")))
xList = [int(i) for i in input().split(" ")]
xList = sorted(xList)
dList = [0]
for i in range(len(xList) -1) :
    dList.append(xList[i+1] - xList[i])
dList = sorted(dList, reverse=True)    

for i in range(n-1) :
    dList[i] = 0
    if(i == len(dList) -1) :
        break
print((sum(dList)))
# Original solution
N,M = list(map(int,input().split()))
Xs = list(map(int,input().split()))
Xs.sort()
diffs = []
for i in range(1,M):
    diffs.append(Xs[i]-Xs[i-1])

if len(diffs) != 0:
    for i in range(N-1):
        diffs[diffs.index(max(diffs))] = 0

print((sum(diffs)))

# Optimized solution
N,M = list(map(int,input().split()))
Xs = list(map(int,input().split()))
Xs.sort()
diffs = []
for i in range(1,M):
    diffs.append(Xs[i]-Xs[i-1])
if len(diffs) > 0 and M > N:
    diffs.sort()
    print((sum(diffs[:M-N])))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
x=list(map(int,input().split()))
x.sort()
y=[]
for i in range(1,len(x)):
    y.append(x[i]-x[i-1])

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



# Optimized solution
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
diff = []

for i in range(m-1):
	diff.append(x[i+1]-x[i])
fin_diff=sorted(diff, reverse=True)

print((sum(fin_diff[n-1:])))
# Original solution
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
dist = [j - i for i, j in zip(x, x[1:])]
dist.sort(reverse=1)
print((sum(dist[n-1:])))

# Optimized solution
n, m = list(map(int, input().split()))
a = sorted(list(map(int, input().split())), reverse=True)
l = sorted([a[i-1] - a[i] for i in range(1, m)], reverse=True)
print((sum(l[n-1:])))
# Original solution
N, M = list(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:])))



# Optimized solution
N, M = list(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)))
# Original solution
N, M = list(map(int, input().split()))#駒の数N,地点の数M
 
xList = [int(i) for i in input().split()]
xList.sort()#地点座標xList
 
distanse_list = []

#駒の数が座標の数以上のとき動かす必要がない
if N > M:
    N = M

if N == M:
    print("0")
#
else:
    for i in range(len(xList)-1):
        distanse = xList[i+1] - xList[i]
        distanse_list.append(distanse)
 
    distanse_list.sort()
    print((sum(distanse_list[0:int(M - N)])))

# Optimized solution
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()
# Original solution
n,m = list(map(int,input().split()))
X = list(map(int,input().split()))
X.sort()
if m <= n:
  print((0))
else:
  Dif_X = []
  for i in range(m-1):
    Dif_X += [X[i+1]-X[i]]
  count = 0  
  while count < n-1:
    Dif_X[Dif_X.index(max(Dif_X))] = 0
    count += 1
  print((sum(Dif_X)))

# Optimized solution
n,m = list(map(int,input().split()))
X = list(map(int,input().split()))
X.sort()
if m <= n:
  print((0))
else:
  Dif_X = []
  for i in range(m-1):
    Dif_X += [X[i+1]-X[i]]
  Sum_Dif_X = sum(Dif_X)  
  Sorted_Dif_X = sorted(Dif_X, reverse = True)
  count = 0  
  while count < n-1:
    Sum_Dif_X -= Sorted_Dif_X[count]
    count += 1
  print(Sum_Dif_X)
# Original solution
n,m = list(map(int, input().split()))
x = sorted(list(map(int, input().split())))
l = [0] * (m-1)
ans = 0
if n < m:
  for i in range(m-1):
    l[i] = x[i+1] - x[i]
  for _ in range(m-n):
    ans += min(l)
    l.pop(l.index(min(l)))
print(ans)

# Optimized solution
n,m = list(map(int, input().split()))
x = sorted(list(map(int, input().split())))
l = [0] * (m-1)
if n >= m:
  print((0))
  exit()
else:
  for i in range(m-1):
    l[i] = x[i+1] - x[i]
  print((sum(sorted(l)[:m-n])))
# Original solution
import sys
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X.sort()

pos = [[0] * 2 for i in range(N)]
total = 0

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

for i in range(N):
    pos[i][0] = i
    pos[i][1] = abs(X[i+1] - X[i])
    total = total + pos[i][1]

pos = sorted(pos, key=lambda x:(x[1]))

for j in range(N, M-1):
    dim = abs(X[j+1]-X[j])
    total = total + dim
    if pos[0][1] < dim:
        pos[0][0] = j
        pos[0][1] = dim
        pos = sorted(pos, key=lambda x:(x[1]))


for i in range(1, N):
    total = total - pos[i][1]

print(total)


# Optimized solution
import sys
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X.sort()

L = []

for i in range(1, M):
    L.append(X[i]-X[i-1])

L = sorted(L, reverse=True)

print((X[M-1]-X[0]-sum(L[:N-1])))
# Original solution
# ABC117C - Streamline
def main():
    n, m = list(map(int, input().split()))
    lst = sorted(set(map(int, input().split())))
    dis = []
    for i, j in zip(lst, lst[1:]):
        dis += [j - i]
    print((sum(sorted(dis, reverse=True)[n - 1 :])))


if __name__ == "__main__":
    main()


# Optimized solution
# ABC117C - Streamline
def main():
    N, M, *X = list(map(int, open(0).read().split()))
    X = sorted(set(X))
    dist = []
    for i, j in zip(X, X[1:]):
        dist.append(j - i)
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
# ABC117C - Streamline
def main():
    N, M, *X = list(map(int, open(0).read().split()))
    X = sorted(set(X))
    dist = []
    for i, j in zip(X, X[1:]):
        dist.append(j - i)
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ABC117C - Streamline
def main():
    N, M, *X = list(map(int, open(0).read().split()))
    X = sorted(set(X))
    dist = [j - i for i, j in zip(X, X[1:])]
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()

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

if M > N:
    
    for i in range(N-1):
        difX.pop(difX.index(max(difX)))

    total = 0
    for x in difX:
        total+= x

    print(total)
else:
    print((0))

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

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

if M > N:

    difX = difX[N-1:]

    total = 0
    for x in difX:
        total+= x

    print(total)
else:
    print((0))
# Original solution
def _solve(current, n, idx, x):
    if n >= len(x) - idx:
        return 0

    if n == 0:
        return x[-1] - current


    if current is None:
        return solve(x[idx], n - 1, idx + 1, x)

    a = x[idx] - current + solve(x[idx], n, idx + 1, x)
    b = solve(x[idx], n - 1, idx + 1, x)
    return min(a, b)

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

    dp = [[0 for _ in range(n + 1)] for _ in range(m)]
    dp[m-1][0] = x[-1] - x[-2]
    for i in range(1, n + 1):
        dp[m-1][i] = 0

    for idx in reversed(list(range(m - 1))):
        for i in range(n + 1):
            if i >= m - idx + 1:
                dp[idx][i] = 0
            elif i == 0:
                dp[idx][i] = x[-1] - x[idx-1]
            elif idx == 0:
                dp[idx][i] = dp[idx+1][i-1]
            else:
                a = x[idx] - x[idx - 1] + dp[idx+1][i]
                b = dp[idx+1][i-1]
                dp[idx][i] = min(a, b)

    return dp[0][n]

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

    print((solve(n, m, x)))

main()


# Optimized solution
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 = list(map(int, input().split()))
    x = [int(s) for s in input().split()]
    x.sort()

    print((solve(n, m, x)))

main()
# Original solution
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
x_diff = []
for i in range(1,m):
  x_diff.append(x[i]-x[i-1])
x_diff.sort()
if n >= m:
  print((0))
else:
  for j in range(1,n):
    x_diff.remove(x_diff[-1])
  print((sum(x_diff)))

# Optimized solution
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
x_diff = [x[i]-x[i-1] for i in range(1,m)]
x_diff.sort(reverse = True)
if n >= m:
  print((0))
else:
  del x_diff[:n-1]
  print((sum(x_diff)))
# Original solution
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

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

X.sort()
hoge = [0]*(M-1)
for i in range(1, M):
    hoge[i-1] = X[i] - X[i-1]

hoge = list(reversed(sorted(hoge)))

ans = sum(hoge)
if N >= M:
    ans = 0
else:
    for i in range(N-1):
        ans -= hoge[i]
print(ans)


# Optimized solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))

X = list(sorted(X))
Y = [0]*(M-1)
for i in range(M-1):
    Y[i] = X[i+1] - X[i]

Y = list(reversed(sorted(Y)))
ans = sum(Y) - sum(Y[:N-1])

print(ans)
# Original solution
def nyu():
    N, M = list(map(int, input().split()))
    X = list(map(int,input().split()))
    X.sort()
    return N,M,X


def kansu(N,M,X):

    x_abs = [X[m+1] -X[m] for m in range(M-1)]    
#    print(x_abs)
    cnt_n = N
    
    while cnt_n-1 > 0:
        x_abs[x_abs.index(max(x_abs))]  = 0
        cnt_n -= 1

    koma_max = 0
    for m in range(M-1):
        koma_max += x_abs[m]

    print(koma_max)


N,M,X = nyu()
#print(X)

if M == 1:
    print((0))
else:
    kansu(N,M,X)

#print(S)



# Optimized solution
def nyu():
    N, M = list(map(int, input().split()))
    X = list(map(int,input().split()))
    X.sort()
    return N,M,X


def kansu(N,M,X):

    x_abs = [X[m+1] -X[m] for m in range(M-1)]    
#    print(x_abs)

    
    x_abs.sort()

    koma_max = 0
    for m in range(M-N):
        koma_max += x_abs[m]

    print(koma_max)


N,M,X = nyu()
#print(X)

if M == 1:
    print((0))
else:
    kansu(N,M,X)

#print(S)
# Original solution
n,m=list(map(int,input().split()))
x=sorted(list(map(int,input().split())));#print(x)

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

# Optimized solution
def mi():return list(map(int,input().split()))
n,m=mi()
x=sorted(list(mi()))
d=sorted([x[i+1]-x[i] for i in range(m-1)])
print((sum(d[:max(m-n,0)])))
# Original solution
n, m = list(map(int, input().split()))
L = sorted(map(int, input().split()))
d = [0] * (m - 1)
for i in range(1, m):
    d[i - 1] = abs(L[i] - L[i - 1])
d = sorted(d, reverse=True)[(n - 1):]
print((sum(d)))


# Optimized solution
n, m = list(map(int, input().split()))
L = sorted(map(int, input().split()))
d = [L[i + 1] - L[i] for i in range(m - 1)]
d.sort(reverse=True)
print((sum(d[(n - 1):])))
# Original solution
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x = sorted(x)
if m<=n:
    print((0))
else:
    dif = []
    for i in range(m-1):
        dif.append(x[i+1]-x[i])
    dif = sorted(dif)
    print((sum(dif[:m-n])))

# Optimized solution
n,m = list(map(int,input().split()))
x = sorted(list(map(int,input().split())))
dif = []
for i in range(m-1):
    dif.append(x[i+1]-x[i])
dif.sort()
print((sum(dif[:max(m-n,0)])))
# Original solution
### C

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

X = list(map(int , sorted(list(map(int , input().split())))))

#print("X = " + str(X))

D = []

R = 0

if M-1 > 0 :
	for i in range(M-1):
		D.append(int(X[i+1]) - int(X[i]))


#print("D = " + str(D))

for i in range(N-1):
	if D !=[]:
		MAXD = max(D)
		D.remove(MAXD)
#		print(str(D) + "minus " + str(i+1) + "roots.")

if D != []:
	for d in D:
		R += d


print(R)



# Optimized solution
### C

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

X = list(map(int , sorted(list(map(int , input().split())))))

#print("X = " + str(X))

D = []

R = 0

if M-1 > 0 :
	for i in range(M-1):
		D.append(int(X[i+1]) - int(X[i]))


#print("D = " + str(D))

if D !=[]:
	D = sorted(D)
	D = D[::-1]
	D = D[N-1:]
#print("D = " + str(D))

if D != []:
	for d in D:
		R += d


print(R)
# Original solution
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:]))
  for _ in range(N-1):
    ds.remove(max(ds))

  print((sum(ds)))


# Optimized solution
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])))
# Original solution
N,M = list(map(int,input().split()))
X=[int(i) for i in input().split()]
X.sort()

Y=list()
for i in range(M)[1:]:
  Y.append(X[i]-X[i-1])

Y.sort()

if N==1:
  print((sum(Y)))
else:
  print((sum(Y[:-(N-1)])))

# Optimized solution
N,M = list(map(int,input().split()))
X=[int(i) for i in input().split()]
X.sort()

if N==1:
  print((X[M-1]-X[0]))
else:
  Y=list()
  [Y.append(X[i]-X[i-1]) for i in range(M)[1:]]
  Y.sort()
  print((sum(Y[:-(N-1)])))
# Original solution
N,M = list(map(int,input().split()))
X=[int(i) for i in input().split()]
X.sort()

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


# Optimized solution
N,M = list(map(int,input().split()))
X=sorted([int(i) for i in input().split()])

if N==1:
  print((X[M-1]-X[0]))
elif N>=M:
  print((0))
else:
  Y=list()
  [Y.append(i-j) for i,j in zip(X[1:],X)] 
  Y.sort(reverse=True)
  print((sum(Y[N-1:])))
# Original solution
N,M = list(map(int,input().split()))
X=sorted([int(i) for i in input().split()])

if N==1:
  print((X[M-1]-X[0]))
elif N>=M:
  print((0))
else:
  Y=list()
  [Y.append(i-j) for i,j in zip(X[1:],X)] 
  Y.sort(reverse=True)
  print((sum(Y[N-1:])))


# Optimized solution
N,M = list(map(int,input().split()))
X=sorted([int(i) for i in input().split()])
if N==1:
  print((X[M-1]-X[0]))
elif N>=M:
  print((0))
else:
  print((sum(sorted([i-j for i,j in zip(X[1:],X)],reverse=True)[N-1:])))
# Original solution
N,M=list(map(int,input().split()))
X=list(map(int,input().split()))

X.sort()
diff=[]
for i in range(M-1):
    diff.append(X[i+1]-X[i])
gap=[]
for i in range(N-1):
    for j in range(M-1):
        x=diff.index(max(diff))
        gap.append(x)
        diff[x]=0
        break
gap.append(M-1)
gap=list(set(gap))
gap.sort()
offset=0
cnt=0
for i in gap:
    cnt+=X[i]-X[offset]
    offset=i+1
print(cnt)

# Optimized solution
N,M=list(map(int,input().split()))
X=list(map(int,input().split()))

X.sort()
diff=[]
for i in range(M-1):
    diff.append(X[i+1]-X[i])
diff.sort()
to=M-N
if to<=0:
    ans=0
else:
    ans=sum(diff[:to])
print(ans)
# Original solution
n, m = list(map(int, input().split()))
l = list(map(int, input().split()))

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

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

d.sort()

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




# Optimized solution
n, m = list(map(int, input().split()))
X = list(map(int, input().split()))

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

X.sort()

if n == 1:
    print((X[-1]-X[0]))
    exit()

Y = []
for i in range(m-1):
    Y.append(X[i+1]-X[i])
#print(Y)
Y.sort()
ans = sum(Y[0:-(n-1)])
print(ans)
# Original solution
n,m = list(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])
if n==1:
    print((x[-1]-x[0]))
elif n>=len(x):
    print((0))
else:
    for i in range(n-1):
        d.remove(max(d))
    print((sum(d)))

# Optimized solution
n,m = list(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))
# Original solution
N, M = list(map(int, input().split()))
X = list(sorted(map(int, input().split())))
if N >= M:
  print('0')
else:
  d = []
  for i in range(M - 1):
    d.append(X[i + 1] - X[i])
  for j in range(N - 1):
    d.remove(max(d))
  print((sum(d)))

# Optimized solution
N, M = list(map(int, input().split()))
X = list(sorted(map(int, input().split())))
if N >= M:
  print('0')
else:
  d = []
  ans = X[-1] - X[0]
  for i in range(M - 1):
    d.append(X[i + 1] - X[i])
  ans -= sum(list(sorted(d, reverse=True))[0:N - 1])
  print(ans)
# Original solution
n, m = list(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)))

# Optimized solution
n, m = list(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])))
# Original solution
#!/usr/bin/env python3
#ABC117 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
x = LI()
x.sort()
if n >= m:
    print((0))
    quit()
y = [abs(x[i] - x[i+1]) for i in range(m-1)]
y.sort()
ans = sum(y[:m-n])
print(ans)


# Optimized solution
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
x = LI()
x.sort()
lst = []
for i in range(m-1):
    lst.append(x[i+1]-x[i])
lst.sort(reverse=1)
if n >= m:
    print((0))
    quit()
print((sum(lst[n-1:])))
# Original solution
#x=int(input())
n,m=list(map(int,input().split()))
xl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
ans=0
if n>=m:
    pass
else:
    cum=[]
    xl.sort()
    ans=xl[m-1]-xl[0]
    for i in range(m-1):
        cum.append(xl[i+1]-xl[i])
    cum.sort(reverse=True)
    for i in range(n-1):
        ans-=cum[i]

print(ans)

# Optimized solution
#n = int(input())
n, m = list(map(int, input().split()))
xl = list(map(int, input().split()))
#al=[list(input()) for i in range(n)]
xl.sort()
difl = []

for i in range(1, m):
    difl.append(xl[i]-xl[i-1])

difl.sort(reverse=True)

if n >= m:
    ans = 0
else:
    ans = xl[-1]-xl[0]-sum(difl[:n-1])
print(ans)
# Original solution
# -*- coding: utf-8 -*-


def main():
    n, m = list(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()


# Optimized solution
# -*- coding: utf-8 -*-


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

    for i, j in zip(x, x[1:]):
        diff.append(abs(i - j))

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


if __name__ == '__main__':
    main()
# Original solution
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)

# Optimized solution
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)
# Original solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [X[i + 1] - X[i] for i in range(len(X) - 1)]
    for _ in range(N - 1):
        x_diff.remove(max(x_diff))
    ans = sum(x_diff)

print(ans)


# Optimized solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [xn - x for xn, x in zip(X[1:], X[:-1])] #[X[i + 1] - X[i] for i in range(len(X) - 1)]
    # for _ in range(N - 1):
    #     x_diff.remove(max(x_diff))
    x_diff.sort(reverse=True)
    ans = sum(x_diff[N-1:])

print(ans)
# Original solution
M, N = list(map(int, input().split()))

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

Y = []

for i in range(N - 1):
    Y.append((i, X[i + 1] - X[i]))
Y.sort(key=lambda y: y[1], reverse=True)
Y = Y[:M - 1]
Y.sort(key=lambda y: y[0])

p = 0
c = 0
for i, y in Y:
    c += X[i] - X[p]
    p = i + 1
c += X[-1] - X[p]

print(c)


# Optimized solution
M, N = list(map(int, input().split()))

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

Y = []

for i in range(N - 1):
    Y.append(X[i + 1] - X[i])

Y.sort()

if M < N:
    c = X[-1] - X[0] - sum(Y[N - M:])
else:
    c = 0

print(c)
# Original solution
N,M = list(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])))

# Optimized solution
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N,M = list(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])))
# Original solution
n,m = list(map(int,input().split()))
X = list(map(int,input().split()))
X.sort()
num = []
for i in range(1,m):
    num.append(X[i]-X[i-1])
num.sort(reverse = True)
print((sum(num[n-1:])))

# Optimized solution
n,m = list(map(int,input().split()))
X = list(map(int,input().split()))
X.sort()
p = []
for i in range(1,m):
    p.append(X[i]-X[i-1])
p.sort(reverse=True)
#print(p)
print((sum(p[n-1:])))