CodeEff/ECCO huggingface dataset:
# Original solution
N, M = list(map(int, input().split()))
if N >= M:
  print((0))
  exit()
  
X = list(map(int,input().split()))

X.sort()
dX,dX1 = [],[]
for i in range(M-1):
  dX.append(X[i+1]-X[i])
  dX1.append(X[i+1]-X[i])
seppoint = []

  
for i in range(N-1):
  dXmax = [j for j,v in enumerate(dX) if v == max(dX)]
  if len(dXmax) > 1: 
    sumdist0 = 2*10**5
    for k in dXmax:
      dXtmp = dX[:k] + dX[k+1:]
      sumdist1 = (sum(dXtmp))
      if sumdist0 > sumdist1:
        sumdist0 = sumdist1
        dX = dXtmp
        seppoint.append(k)
  else:
    dX = dX[:dXmax[0]] + dX[dXmax[0]+1:]
    seppoint.append(dXmax[0])
print((sum(dX)))
#seppoint.sort()
#print(seppoint)
#for i in seppoint:
# dX1[i] = -1
#print(dX1)
#anslist = []
#tmpans = 0

#for i in range(len(dX1)):
#  if dX1[i] == -1:
#    anslist.append(tmpans)
#    tmpans = 0
#  else:
 #   tmpans += dX1[i]
 #   if i == len(dX1)-1:
 #     anslist.append(tmpans)
 #    tmpans = 0
 # print(anslist)
    
#print(max(anslist))

# Optimized solution
N, M = list(map(int, input().split()))
#if N >= M:
#  print(0)
#  exit()
  
X = list(map(int,input().split()))

X.sort()
diff = []

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

diff.sort()

for i in range(N - 1):
  if len(diff) != 0:
    diff.pop()

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

X = sorted(X)
diff_dict = {}
for i in range(M-1):
    diff = X[i+1] - X[i]
    diff_dict[i] = diff
sorted_diff = sorted(list(diff_dict.items()), key=lambda x: x[1])[::-1][:N-1]
keys = {x[0] for x in sorted_diff}
tmp = []
count = 0
for i, x in enumerate(X):
    tmp.append(x)
    if i in keys:
        if len(tmp) > 1:
            count += (max(tmp) - min(tmp))
        tmp.clear()
if len(tmp) > 1:
    count += (max(tmp) - min(tmp))
print(count)


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

X = sorted(X)
diff = []
for i in range(M-1):
    diff.append(X[i+1] - X[i])
diff = sorted(diff)[::-1]
print((X[-1] - X[0] - sum(diff[:N-1])))
# Original solution
N,M= list(map(int, input().split()))
X= list(map(int, input().split()))
X.sort()
div=[]
if N>=M:
    print("0")
else:
    for i in range(M-1):
        div.append(X[i+1]-X[i])
    for i in range(N-1):
        div.pop(div.index(max(div)))
    print((sum(div)))

# Optimized solution
N,M= list(map(int, input().split()))
X= list(map(int, input().split()))
X.sort()
div=[]
if N>=M:
    print("0")
else:
    for i in range(M-1):
        div.append(X[i+1]-X[i])
    div.sort()
    print((sum(div[:M-N])))
# Original solution
n, m = list(map(int, input().split()))
x = sorted(list(map(int, input().split())))
if n < m:
    distance = []
    for i in range(m-1):
        distance.append(x[i + 1] - x[i])
    for _ in range(n - 1):
        distance.remove(max(distance))
    print((sum(distance)))
else:
    print((0))

# Optimized solution
n, m = list(map(int, input().split()))
x = sorted(list(map(int, input().split())))
if n < m:
    distance = []
    for i in range(m-1):
        distance.append(x[i + 1] - x[i])
    distance.sort()
    print((sum(distance[0:m-n])))
else:
    print((0))
# Original solution
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]

for i in range(n-1):
    max_num = max(x_diff)
    max_index = x_diff.index(max_num)
    x_diff[max_index] = 0

print((sum(x_diff)))

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

X =sorted(X)

dis = [X[i+1]-X[i] for i in range(len(X)-1)]
dis = sorted(dis)

ans = 0
for i in range(len(dis)-n+1):
    ans = ans + dis[i]
    
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
x = sorted(list(map(int, input().split())))
dis = sorted([x[i] - x[i-1] for i in range(1, M)])
if N < M:
    print((sum(dis[:M-N])))
else:
    print((0))
# Original solution
n, m = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
C = []
if m <= n:
	print((0))
	exit()
for i in range(1, m):
	C.append(abs(X[i] - X[i-1]))
for i in range(n-1):
	t = max(C)
	C[C.index(t)] = 0
print((sum(C)))

# Optimized solution
n, m = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
C = []
if m <= n:
	print((0))
	exit()
for i in range(1, m):
	C.append(abs(X[i] - X[i-1]))
C.sort()
for i in range(n-1):
	C[m-2-i] = 0
print((sum(C)))
# Original solution
N,M = list(map(int,input().split()))
Xs = list(map(int,input().split()))
Xs.sort()
Xdivs = []
for i in range(M-1):
    Xdivs.append(Xs[i+1]-Xs[i])
for _ in range(N-1):
    for i,x in enumerate(Xdivs):
        if Xdivs[i] == max(Xdivs):
            Xdivs[i] = 0
            break
print((sum(Xdivs)))

# Optimized solution
N,M = list(map(int,input().split()))
Xs = list(map(int,input().split()))
if len(Xs) <= N:
    print((0))
else:
    Xs.sort()
    Xdivs = []
    for i in range(M-1):
        Xdivs.append(Xs[i+1]-Xs[i])
    Xdivs.sort()
    print((sum(Xdivs[:M-N])))
# Original solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
t = sorted(X, reverse=True)
h = [0] * M
for i in range(1, M):
    h[i] = abs(t[i - 1] - t[i])

for i in range(N - 1):
    tmp = h.index(max(h))
    h[tmp] = 0
print((sum(h)))

# Optimized solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
t = sorted(X, reverse=True)
h = [0] * M
for i in range(1, M):
    h[i] = abs(t[i - 1] - t[i])

tmp = sorted(h, reverse=True)
print((sum(tmp[N - 1:])))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

A=sorted(A)
B=[0]*(M-1)

for i in range(len(A)-1):
    B[i]=A[i+1]-A[i]

for i in range(N-1):
    if(len(B)==0):
        print("0")
        exit()
    B.remove(max(B))

print((sum(B)))

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

A=sorted(A)
B=[0]*(M-1)

for i in range(len(A)-1):
    B[i]=A[i+1]-A[i]

B=sorted(B)
if(N>=M):
    print("0")
    exit()
else:
    print((sum(B[0:M-N])))
# Original solution
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print((0))
    elif N < M: 
        X.sort()
        diff = [[0,0] for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = [X[i+1] - X[i],i]
        diff.sort(reverse=True)
        diff = diff[:N-1]
        diff.sort(key=lambda p:p[1])

        ans = 0
        cnt = 0
        for i in range(N-1):
            ans += X[diff[i][1]] - X[cnt]
            cnt = diff[i][1] + 1
        ans += X[-1] - X[cnt]
        print(ans)
if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print((0))
    elif N < M: 
        X.sort()
        diff = [0 for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = X[i+1] - X[i]
        diff.sort(reverse=True)
        
        ans = X[-1] - X[0]
        for i in range(N-1):
            ans -= diff[i]
        print(ans)
if __name__ == "__main__":
    main()
# Original solution
import sys

# N: コマ数
# M: 地点数

N,M,*X = list(map(int, open(0).read().split()))

# コマ数が地点数以上の場合、各地点にコマを配置できる
# よって 0 回の移動で目的を達成できる
if N >= M:
    print((0))
    sys.exit()

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

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

# Optimized solution
import sys

# N: コマ数
# M: 地点数

N,M,*X = list(map(int, open(0).read().split()))

# コマ数が地点数以上の場合、各地点にコマを配置できる
# よって 0 回の移動で目的を達成できる
if N >= M:
    print((0))
    sys.exit()

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

print((X[-1] - X[0] - sum(sorted(ans)[::-1][:N-1])))
# Original solution
import sys

# N: コマ数
# M: 地点数

N,M,*X = list(map(int, open(0).read().split()))

# コマ数が地点数以上の場合、各地点にコマを配置できる
# よって 0 回の移動で目的を達成できる
if N >= M:
    print((0))
    sys.exit()

X.sort()

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

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

# Optimized solution
import sys

# N: コマ数
# M: 地点数

N,M,*X = list(map(int, open(0).read().split()))

# コマ数が地点数以上の場合、各地点にコマを配置できる
# よって 0 回の移動で目的を達成できる
if N >= M:
    print((0))
    sys.exit()

X.sort()
D = sorted([abs(X[i+1] - X[i]) for i in range(M-1)], reverse=True)
print((X[-1] - X[0] - sum(D[:N-1])))
# Original solution
import sys

# N: コマ数
# M: 地点数

N,M,*X = list(map(int, open(0).read().split()))

# コマ数が地点数以上の場合、各地点にコマを配置できる
# よって 0 回の移動で目的を達成できる
if N >= M:
    print((0))
    sys.exit()

X.sort()
D = sorted([abs(X[i+1] - X[i]) for i in range(M-1)], reverse=True)
print((X[-1] - X[0] - sum(D[:N-1])))

# Optimized solution
N,M,*X=list(map(int,open(0).read().split()))
X.sort()
print((0 if N>=M else sum(sorted(abs(X[i+1]-X[i])for i in range(M-1))[:M-N])))
# Original solution
n, m = list(map(int, input().split()))
X = sorted(list(map(int, input().split())))
Diffs = [0] + [X[i] - X[i-1] for i in range(1, m)]
for i in range(n-1):
  m = Diffs.index(max(Diffs))
  Diffs[m] = 0
print((sum(Diffs)))


# Optimized solution
n, m = list(map(int, input().split()))
X = sorted(list(map(int, input().split())))
Diffs = [0] + [X[i] - X[i-1] for i in range(1, m)]

if n >= m:
  print((0))
elif n == 1:
  Diffs.sort()
  print((sum(Diffs)))
else:
  Diffs.sort()
  print((sum(Diffs[:-(n-1)])))
# Original solution
# -*- coding: utf-8 -*-

N, M = list(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)


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

if n == 1:
    print((max(x) - min(x)))
elif n >= m:
    print((0))
else:

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

    for i in range(n - 1):
        dif.remove(max(dif))
    print((sum(dif)))


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

if n == 1:
    print((max(x) - min(x)))
elif n >= m:
    print((0))
else:
    dif = []
    for i in range(m - 1):
        dif.append(x[i+1] - x[i])
    dif = sorted(dif)
    dif = dif[:m-n]
    print((sum(dif)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

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

    diff.sort(reverse=True)
    print((sum(diff[N - 1 :])))


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

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

    dist.sort(reverse=True)

    if N >= M:
        print((0))
    else:
        # N - 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()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print((sum(y[:m-n]) if n < m else 0))


# Optimized solution
from sys import stdin
n, m, *x = list(map(int, stdin.read().split()))
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print((sum(y[:m - n]) if n < m else 0))
# Original solution
n,m = list(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]))
  for i in range(n-1):
    dx.remove(max(dx))
  print((sum(dx)))

# Optimized solution
n,m = list(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])))
# Original solution
N,M=list(map(int,input().split(" ")))
A=list(sorted(map(int,input().split(" "))))
L=[A[s]-A[s+1] for s in range(M-1)]
if M>N:
    print((abs(sum(sorted(L)[N-1:]))))
else:
    print((0))

# Optimized solution
N,M=list(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:]))))
# Original solution
n,m=list(map(int,input().split()))
X=sorted(list(map(int,input().split())))
init=[]
tmp=0
i=1

if m<n:
  n=m

while i<len(X):
  init.append([X[i]-X[i-1],i])
  i+=1
init.sort(reverse=True,key=lambda x:x[0])
init=list([x[1] for x in init])
ans=0
init=sorted(init[:n-1])
if len(init)==0:
  print((X[-1]-X[0]))
  exit()
tmp=0
for i in init:
  ans+=(X[i-1]-X[tmp])
  tmp=i
try:
  if i!=len(X)-1:
    ans+=X[-1]-X[i]
except IndexError:
  print((0))
  exit()
print(ans)


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


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

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

X.sort() # O(MlogM)

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

D.sort() # O(MlogM)

print((sum(D[:M-N]))) # O(M)
# Original solution
from itertools import accumulate

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

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

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

for i in range(n - 1):
    ans.remove(max(ans))
print((sum(ans)))


# Optimized solution
from itertools import accumulate

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

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

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

ans = sorted(ans, reverse=True)
print((sum(ans[n-1:])))
# Original solution
#C
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])
    k = 1
    while k < n:
        xl.remove(max(xl))
        k += 1
    print((sum(xl)))

# Optimized solution
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)))
# Original solution
#!/usr/bin/python
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
x = sorted(X)
i = 0
diff = []
while i + 1 < len(x):
    diff.append(x[i] - x[i + 1])
    i += 1
d = sorted(diff)[:N - 1]
i = 0
sum = 0
for _ in x:
    if i + 1 < len(x):
        if x[i] - x[i + 1] in d:
            d.remove(x[i] - x[i + 1])
        else:
            sum = sum + abs(x[i] - x[i + 1])
    i += 1
print(sum)

# Optimized solution
#!/usr/bin/python
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
x = sorted(X)
i = 0
diff = []
while i + 1 < len(x):
    diff.append(x[i] - x[i + 1])
    i += 1
d = sorted(diff)[N - 1:]
print((abs(sum((d)))))
# Original solution
import sys

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

x = list(map(int, input().split()))

gap = []


if( n >= m):
    print((0))
    sys.exit()
else:
    x = sorted(x)
    for i in range(m-1):
        gap.append(x[i+1] - x[i])
        gaps = sorted(gap)

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


# Optimized solution
import sys

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

x = list(map(int, input().split()))

gap = []


if( n >= m):
    print((0))
    sys.exit()
else:
    x = sorted(x)
    for i in range(m-1):
        gap.append(x[i+1] - x[i])
    gaps = sorted(gap)

print((sum(gaps[:m-n])))
# Original solution
N , M = list(map(int,input().split()))

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

X.sort()

# 隣接点の間の距離が長い順で分割する
# おけるコマの個数-1だけ辺を取り除ける

maxDist = X[-1] - X[0]

dist = [0] * (M-1)

# 間の距離を計算
for i in range(M-1) :
    dist[i] = X[i+1] - X[i]

dist.sort(reverse=True)

for i in range(min(N-1,M-1)) :
    maxDist -= dist[i]

print(maxDist)

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

D = [r - l for l, r in zip(X, X[1:])]
D.sort()

ans = sum(D)
while D and N > 1:
    N -= 1
    ans -= D.pop()
print(ans)
# Original solution
N, M = list(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)


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

# Optimized solution
N,M = list(map(int,(input().split())))
X = list(map(int,(input().split())))
if N >= M:
    print((0))
else:
    X.sort()
    gap = []
    for i in range(1,M):
        gap.append(X[i]-X[i-1])
    s = X[M-1] - X[0]
    gap.sort(reverse=True)
    print((s-sum(gap[0:N-1])))
# Original solution
N,M=list(map(int,input().split()))
X=[int(i) for i in input().split()]
X.sort()
count=[]
sum=0
if N<M:
  for i in range(M):
    if i != 0:
      count.append(X[i]-get)
      sum+=count[i-1]
    get=X[i]
  count.sort(reverse=True)
  if count != []:
    for i in range(N-1):
      sum-=count[i]
print(sum)

# Optimized solution
N,M=list(map(int,input().split()))
X=[int(i) for i in input().split()]
X.sort()
count=[]
sum=0
if N<M:
  for i in range(M):
    if i != 0:
      count.append(X[i]-get)
      sum+=count[i-1]
    get=X[i]
  count.sort(reverse=True)
  
  for i in range(N-1):
    sum-=count[i]
print(sum)
# Original solution
n,m = list(map(int,input().split()))

t = list(map(int,input().split()))

t.sort()
memo = []
for i in range(m-1):
    memo.append(t[i+1]-t[i])
memo.sort()
#print(memo)
ans = 0
for i in range(m-n):
    #print(i)
    ans += memo[i]
print(ans)

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

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

X.sort()

memo = []

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

memo.sort()
#print(memo)
ans = 0
for i in range(M-N):
    ans += memo[i]
print(ans)
# Original solution
N, M = list(map(int, input().split()))
x_list = list(map(int, input().split()))

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], x[0]))
cnt = [0]
for i, v in margin:
    if len(cnt) == N:
        break
    cnt.append(i + 1)

if len(cnt) == 1:
    p = -1
    for i, x in enumerate(x_list):
        if i == 0:
            p = x_list[0]
        else:
            ans += x_list[i] - p
            p = x_list[i]
    print(ans)
    exit()
start = 0
prev = x_list[0]
cnt.append(M)
cnt.sort()
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    if c != M:
        start = c
        prev = x_list[c]
print(ans)


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

margin = []
prev = x_list[0]
for i in range(1, M):
    margin.append(x_list[i] - prev)
    prev = x_list[i]

margin.sort(reverse=True)
print((x_list[-1] - x_list[0] - sum(margin[:N-1])))
# Original solution
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
if n >= m:
    print((0))
    exit()
x.sort()
dif = []
for i in range(m - 1):
    dif.append((x[i + 1] - x[i], i + 1))
dif.sort(key = lambda x: x[0], reverse = True)
split = set()
for i in range(n - 1):
    split.add(dif[i][1])
ans = 0
minn, maxx = x[0], x[0]
for i in range(m):
    if i in split:
        ans += maxx - minn
        minn = x[i]
    maxx = max(x[i], maxx)
ans += maxx - minn
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
if n >= m:
    print((0))
    exit()
x.sort()
dif = []
for i in range(m - 1):
    dif.append((x[i + 1] - x[i], i + 1))
dif.sort(key = lambda x: x[0], reverse = True)
split = set()
for i in range(n - 1):
    split.add(dif[i][1])
ans = x[-1] - x[0]
for i in split:
    ans -= x[i] - x[i - 1]
print(ans)
# Original solution
n,m=list(map(int,input().split()))
x=list(map(int,input().split()))
import sys
if n>m:
  print((0))
  sys.exit()

x.sort()
ans_list=[]
for i in range(1,m):
  ans_list.append(x[i]-x[i-1])
ans_list.sort(reverse=True)

print((max(x)-min(x)-sum(ans_list[:n-1])))




# Optimized solution
#1 コマは片方の方向に動かした方が効果的
#2 コマをはじめにおく位置は訪れるべき整数の方がよい
#3 M個の区間で覆うその長さの総和がコマの総移動距離になる
#4 M個の区間の最小値=M個の区間で覆われてない区間の最大値を最大にすればいい
#5 M個の区間で覆うということは覆われてない区間はmー１個ある
#6 よって区間の最大値を求めて上からm-1個選べばいい

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

if n>=m:
  print((0))
else:
  h=[]
  for i in range(1,m):
    h.append(x[i]-x[i-1])
  h.sort(reverse=True)
  print((x[m-1]-x[0]-sum(h[:n-1])))
# Original solution
N,M = list(map(int,input().split()))
X = list(map(int,input().split()))
x = []
ans = 0
X.sort()
if N >= M:
  print((0))
  exit()
for i in range(M-1):
  x.append(abs(X[i]-X[i+1]))
xx = sorted(x,reverse=True)
for j in range(N-1):
  xx[j] = 0
ans = sum(xx)
print(ans)

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

ans = 0

if N >= M:
  pass
else:
  X.sort()
  dist = []

  for i in range(1,M):
    dist.append(abs(X[i]-X[i-1]))
  dist = sorted(dist, reverse= True)

  ans = sum(dist[N-1:])

print(ans)
# Original solution
n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print((0))
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    for _ in range(n-1): dis.remove(max(dis))
    print((sum(dis)))

# Optimized solution
n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print((0))
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dis.sort()
    for _ in range(n-1): dis.pop(-1)
    print((sum(dis)))
# Original solution
N, M = list(map(int, input().split()))
X = sorted(list(map(int, input().split())))
if M <= N:
    print((0))
else:
    Y = []
    for i in range(1, len(X)):
        Y.append(X[i] - X[i-1])
    Y = sorted(Y)
    for i in range(N - 1):
        Y = Y[:-1]
    print((sum(Y)))


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