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

X.sort()

Y = []
for i in range(M-1):
  Y.append(abs(X[i]-X[i+1]))
  Y.sort()
  
res=0
for i in range(M-N):
  res=res+Y[i]

print(res)

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

X.sort()

Y = []
for i in range(M-1):
  Y.append(abs(X[i]-X[i+1]))
  
Y.sort()
  
res=0
for i in range(M-N):
  res=res+Y[i]

print(res)
# Original solution
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    n,m = list(map(int, stdin.readline().rstrip().split()))
    x = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if n>m:
        print((0))
        exit()
    spl = n-1
    sa = [0]*(m-1)
    x.sort()
    for i in range(m-1):
        sa[i] = x[i+1] - x[i]
    for i in range(spl):
        sa.remove(max(sa))
    print((sum(sa)))



main()


# Optimized solution
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    n,m = list(map(int, stdin.readline().rstrip().split()))
    x = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if n>m:
        print((0))
        exit()
    spl = n-1
    sa = [0]*(m-1)
    x.sort()
    for i in range(m-1):
        sa[i] = x[i+1] - x[i]
    sa.sort()
    i = m-n
    print((sum(sa[:i])))



main()
# Original solution
N, M = input().split()
N = int(N)
M = int(M)
X = list(map(int, input().split(" ")))

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

L = sorted(L)
if N >= M:
    print((0))
elif N == 1:
    print((sum(L)))
else:
    print((sum(L[:-N+1])))

# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    x = sorted(list(map(int, input().split())))

    sa = []
    for i in range(M-1):
        sa.append(x[i+1] - x[i])
    sa = sorted(sa, reverse=True)
    # print(sa)
    if N >= M:
        print((0))
    else:
        print((sum(sa[N-1:])))


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


Xs.sort()
costs=[abs(Xs[i]-Xs[i+1]) for i in range(M-1)]


for i in range(N-1):
    if not costs:
        print((0))
        exit(0)
    costs.remove(max(costs))




print((sum(costs)))

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


Xs.sort()
costs=[abs(Xs[i]-Xs[i+1]) for i in range(M-1)]
costs.sort(reverse=True)

for i in range(N-1):
    if not costs:
        print((0))
        exit(0)
    costs.pop(0)




print((sum(costs)))
# Original solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
L = []

for i in reversed(list(range(len(X)))):
    if i - 1 < 0:
        break
    if (X[i] >= 0 and X[i-1] >= 0) or (X[i] <= 0 and X[i-1] <= 0):
        L.append(abs(X[i] - X[i-1]))
    else:
        L.append(abs(X[i]) + abs(X[i-1]))

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

print(L_min)


# Optimized solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
L = [0] * (M-1)

for i in range(M-1):
    L[i] = abs(X[i+1] - X[i])

L.sort()

print((sum(L[:-(N-1)]) if N != 1 else sum(L)))
# Original solution
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
a = []
for i in range(m-1):
  a.append(x[i+1]-x[i])
if n >= m:
  print((0))
else:
  for i in range(n-1):
    a.remove(max(a))
  print((sum(a)))

# Optimized solution
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
a = []
ans = 0
for i in range(m-1):
  a.append(x[i+1]-x[i])
a.sort()
if n >= m:
  print((0))
else:
  for i in range(m-n):
    ans += a[i]
  print(ans)
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 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 eval(input())
def main():
    N, M = LI()
    X = LI()
    X = sorted(X)
    diff = []
    for i in range(M-1):
        d = X[i+1] - X[i]
        diff.append(d)
    diff = sorted(diff)
    while N - 1> 0 and len(diff) > 0:
        diff = diff[:-1]
        N -= 1
    ans = sum(diff)
    print(ans)
    '''
    X = sorted(X)
    min_dist = [0] * M
    if N == 1:
        return X[-1] - X[0]
    print(X)
    for i in range(1, M-1):
        min_dist[i] = min(abs(X[i] - X[i-1]), abs(X[i+1] - X[i]))
        print(min_dist)
    min_dist = sorted(min_dist)[::-1]
    print(min_dist)
    for i in range(N-2):
        if len(min_dist) > 0:
            min_dist.pop(-1)
    print(sum(min_dist))
    '''
main()



# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 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 eval(input())
def main():
    N, M = LI()
    X = LI()
    X = sorted(X)
    diff = []
    for i in range(M-1):
        d = X[i+1] - X[i]
        diff.append(d)
    diff = sorted(diff)
    while N - 1> 0 and len(diff) > 0:
        diff.pop()
        N -= 1
    ans = sum(diff)
    print(ans)
    '''
    X = sorted(X)
    min_dist = [0] * M
    if N == 1:
        return X[-1] - X[0]
    print(X)
    for i in range(1, M-1):
        min_dist[i] = min(abs(X[i] - X[i-1]), abs(X[i+1] - X[i]))
        print(min_dist)
    min_dist = sorted(min_dist)[::-1]
    print(min_dist)
    for i in range(N-2):
        if len(min_dist) > 0:
            min_dist.pop(-1)
    print(sum(min_dist))
    '''
main()
# Original solution
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
d = [0]*m
for i in range(1,m):
    d[i] = x[i]-x[i-1]

for i in range(n-1):
    if d == []:
        break
    else:
        d.remove(max(d))
ans = 0
for i in range(len(d)):
    ans += d[i]
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
d = [0]*m
for i in range(1,m):
    d[i] = x[i]-x[i-1]
d.sort()
if n < m:
    d = d[1:m-n+1]
else:
    d = [0]
ans = 0
for i in range(len(d)):
    ans += d[i]
print(ans)
# Original solution
N, M = list(map(int,input().split()))
X = [int(x) for x in input().split()]
X.sort(reverse=True)
valX = [X[x] - X[x + 1] for x in range(M - 1)]

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

if N == 1:
    print((max(X) - min(X)))
    exit()

for i in range(1,N):
    valX[valX.index(max(valX))] = 0

print((sum(valX)))

# Optimized solution
N, M = list(map(int,input().split()))
X = [int(x) for x in input().split()]
X.sort(reverse=True)
valX = [X[x] - X[x + 1] for x in range(M - 1)]
valX.sort(reverse=True)

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

if N == 1:
    print((max(X) - min(X)))
    exit()

for i in range(N - 1):
    valX[i] = 0

print((sum(valX)))
# Original solution
def main():
    n, m = list(map(int, input().split()))
    t = list(map(int, input().split()))
    if n >= m:
        return 0
    t.sort()
    x = [[t[i], t[i+1] - t[i]] for i in range(m-1)]
    x.append([t[m-1], 0])
    x.sort(key=lambda a: a[1], reverse=True)
    sum_dis = t[m-1] - t[0]
    skip_dis = sum([x[i][1] for i in range(n - 1)])
    return sum_dis - skip_dis


print((main()))

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

    if N >= M:
        return 0

    x = [j-i for i, j in zip(t, t[1:])]
    x.sort(reverse=True)
    return (t[-1] - t[0]) - sum([x[i] for i in range(N - 1)])


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

x.sort()
diffx = [x[i+1]-x[i] for i in range(M-1)]

if M == 1:
    print((0))
else:
    for i in range(N-1):
        idx = diffx.index(max(diffx))
        diffx[idx] = 0
    print((sum(diffx)))

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

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

if M == 1:
    print((0))
else:
    tmp = sum(diffx[:(N-1)])
    print((x[-1] - x[0] - tmp))
# Original solution
n, m = list(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))

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


# Optimized solution
N,M=list(map(int,input().split()));X=sorted([int(i) for i in input().split()]);print((sum(sorted([X[i+1]-X[i] for i in range(M-1)],reverse=True)[N-1:M-1])))
# Original solution
N, M = list(map(int, input().split()))
x = list(map(int, input().split()))
x = list(sorted(x))

y = [x[i] - x[i - 1] for i in range(1, M)]
y = list(sorted(y))

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


# Optimized solution
N, M = list(map(int, input().split()))
x = list(map(int, input().split()))
x = list(sorted(x))
d = [x1 - x0 for x1, x0 in zip(x[1:], x)]
d = list(sorted(d))

ans = 0
for i in range(M - N): # (M - 1) - (N - 1)
    ans += d[i]
print(ans)
# Original solution
N, M = list(map(int, input().split()))
x = list(map(int, input().split()))
x = sorted(x)
y = [xb - xa for xa, xb in zip(x, x[1:])]
y = sorted(y, reverse=True)
print((sum(y) - sum(y[:N - 1])))


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


# Optimized solution
N, M = list(map(int, input().split()))
X = sorted(map(int, input().split()))
dist = sorted([X[i+1] - X[i] for i in range(M-1)])[::-1]
print((sum(dist[N-1:])))
# Original solution
N,M=list(map(int,input().split()))

X=[int(_) for _ in input().split()]
X.sort()
if N>=M:
    print('0')
    exit(0)

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

Sum.sort()
for i in range(N-1):
    Sum.remove(Sum[-1])

print((sum(Sum)))


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

X=[int(_) for _ in input().split()]
X.sort()
if N>=M:
    print('0')
    exit(0)
Sum=[]
for i in range(M-1):
    Sum.append(abs(X[i+1]-X[i]))
Sum.sort()
for i in range(N-1):
    Sum.pop()
print((sum(Sum)))
# Original solution
#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
t,x = LI()
print(t/x)
"""

#B
"""
n= II()
a = LI()
m = max(a)
if m >= sum(a)-m:
    print("No")
else:
    print("Yes")
"""
#C
n,m = LI()
x = LI()
x.sort()
l = [x[i+1]-x[i] for i in range(m-1)]
l.sort()
l = l[::-1]
for i in range(n-1):
    if len(l) == 0:
        print((0))
        quit()
    l.pop(0)
print((sum(l)))
#D
"""
n,k = LI()
a = LI()
l = 0
b = [0 for i in range(n)]
for i in range(n):
    b[i] = list(bin(a[i]))[2:]
    l = max(l,len(b[i]))
s = [0 for i in range(l)]
for i in range(n):
    for j in range(l-len(b[i])):
        b[i].insert(0,"0")
for i in range(n):
    for j in range(l):
        s[j] += 1-int(b[i][j])
ke = 1
for i in range(l)[::-1]:
    if s[i] <= n//2:
        s[i] = [(n-s[i])*ke,0,float("inf")]
    else:
        s[i] = [(n-s[i])*ke,s[i]*ke,ke]
    ke *= 2
s.sort(key = lambda x:x[0])
d = 0
ans = 0
key = len(list(bin(k)))-2
key -= l
ke = 2**l
print(s)
for i in range(key):
    d += ke
    ans += n*ke
    ke *= 2
for i in range(l):
    r,q,p = s[i]
    if d+p <= k:
        d += p
        ans += q
    else:
        ans += r
print(ans)
"""
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
t,x = LI()
print(t/x)
"""

#B
"""
n= II()
a = LI()
m = max(a)
if m >= sum(a)-m:
    print("No")
else:
    print("Yes")
"""

#C
n,m = LI()
x = LI()
x.sort()
l = [x[i+1]-x[i] for i in range(m-1)]
l.sort()
l = l[::-1]
ans = sum(l[max(n-1,0):])
print(ans)
#D
"""
n,k = LI()
a = LI()
l = 0
b = [0 for i in range(n)]
for i in range(n):
    b[i] = list(bin(a[i]))[2:]
    l = max(l,len(b[i]))
s = [0 for i in range(l)]
for i in range(n):
    for j in range(l-len(b[i])):
        b[i].insert(0,"0")
for i in range(n):
    for j in range(l):
        s[j] += 1-int(b[i][j])
ke = 1
for i in range(l)[::-1]:
    if s[i] <= n//2:
        s[i] = [(n-s[i])*ke,0,float("inf")]
    else:
        s[i] = [(n-s[i])*ke,s[i]*ke,ke]
    ke *= 2
s.sort(key = lambda x:x[0])
d = 0
ans = 0
key = len(list(bin(k)))-2
key -= l
ke = 2**l
print(s)
for i in range(key):
    d += ke
    ans += n*ke
    ke *= 2
for i in range(l):
    r,q,p = s[i]
    if d+p <= k:
        d += p
        ans += q
    else:
        ans += r
print(ans)
"""
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T
# Original solution


import sys
import math

# -*- coding: utf-8 -*-

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

a=list(map(int, input().split()))
##まずソート
a.sort()

dist = []
##次のターゲットとの距離を出す
for i in range(m-1):
    dist.append((a[i+1]-a[i]))

##最後のターゲットは右隣がないので大きな値
dist.append(1000000)

dist.sort()
score = 0
##隣との距離が小さい順に駒に回らせる
while len(dist) > n :
    score += dist[0]
    dist = dist[1:]

print(score)




# Optimized solution


import sys
import math

# -*- coding: utf-8 -*-

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

a=list(map(int, input().split()))
##まずソート
a.sort()

dist = [None] * m
##次のターゲットとの距離を出す
for i in range(m-1):
    dist[i] = (a[i+1]-a[i])

##最後のターゲットは右隣がないので大きな値
dist[m-1] = 1000000

dist.sort()
score = 0
##隣との距離が小さい順に駒に回らせる
for i in range(m-n):
    score += dist[i]

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

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

for _ in range(N-1):
    try:
        X_subtraction.remove(max(X_subtraction))
    except:
        break

print((sum(X_subtraction)))

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

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

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

print((sum(X_subtraction)))
# Original solution
# coding: utf-8
# Your code here!
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:  
  for j in range(min(a[0] - 1,a[1])):
    for k in range(len(c)):
      if c[k] == max(c):
        c[k] = 0
        break
  print((sum(c)))



# Optimized solution
# coding: utf-8
# Your code here!
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)))
# Original solution
N,M=list(map(int,input().split()))
X=sorted(map(int,input().split()))
d=sorted(j-i for i,j in zip(X,X[1:]))[::-1]
print((sum(d[N-1:])))

# Optimized solution
N,M=list(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:])))
# Original solution
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N >= M:
    print((0))
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

for n in range(N-1):
    max_index = diff.index(max(diff))
    diff.pop(max_index)

print((sum(diff)))

# Optimized solution
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N >= M:
    print((0))
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

diff.sort(reverse=True)
print((sum(diff[N-1:])))
# Original solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
dist = [X[i+1] - X[i] for i in range(len(X)-1)]
if N>=M: print((0))
else:
  for i in range(N-1):
    dist.remove(max(dist))
  print((sum(dist)))

# Optimized solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
D = [X[i+1] - X[i] for i in range(len(X)-1)]
D.sort()
if N>=M: print((0))
else:
  newD = D[:len(D)-(N-1)]
  print((sum(newD)))
# Original solution
import sys

koma_num, goal_num = [int(a) for a in input().split(' ')]

if koma_num >= goal_num:
  print((0))
  sys.exit()

goals = [int(a) for a in input().split(' ')]
goals.sort()
dist_diff = [int(goals[i+1] - goals[i]) for i in range(len(goals)-1)]

for i in range(koma_num-1):
  max_dist = max(dist_diff)
  dist_diff.remove(max_dist)

print((sum(dist_diff)))

# Optimized solution
import sys

koma_num, goal_num = [int(a) for a in input().split(' ')]

if koma_num >= goal_num:
  print((0))
  sys.exit()

goals = [int(a) for a in input().split(' ')]
goals.sort()
dist_diff = [int(goals[i+1] - goals[i]) for i in range(len(goals)-1)]
dist_diff.sort()
ans_num = goal_num - koma_num
print((sum(dist_diff[:ans_num])))
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
dp=[[0 for i in range(m)]for i in range(m)]
s=[a[i]-a[i-1] if i!=0 else 0 for i in range(1,m) ]
s.sort()
if n!=1:
    print((sum(s[:-n+1])))
else:
    print((sum(s)))



# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
s=[a[i]-a[i-1] if i!=0 else 0 for i in range(1,m) ]
s.sort()
if n!=1:
    print((sum(s[:-n+1])))
else:
    print((sum(s)))
# Original solution
n, m = list(map(int, input().split()))
k = list(map(int, input().split()))
d = [0]*(m-1)
k.sort()
for i in range(m-1):
    d[i] = abs(k[i]-k[i+1])
d = sorted(d, reverse=True)
res = 0
for i in range(n-1):
    if i==m-1:
        break
    res += d[i]
print((sum(d)-res))


# Optimized solution
n, m = list(map(int, input().split()))
k = list(map(int, input().split()))
d = [0]*(m-1)
k.sort()
for i in range(m-1):
    d[i] = abs(k[i]-k[i+1])
d = sorted(d, reverse=True)
print((sum(d)-sum(d[:n-1])))
# Original solution
N,M = list(map(int,input().split()))
X = sorted(list(map(int,input().split())))
#X.sort()
B=[]

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

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



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

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

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

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

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

sorted_diff = sorted(diff)
sorted_diff.reverse()

ans = sum(sorted_diff)
for i in range(min([N,M])-1):
  ans -= sorted_diff[i]
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
d = []
for i in range(M-1):
  d.append(X[i+1]-X[i])
d.sort()
if M-N >= 0:
  print((sum(d[0:M-N])))
else:
  print((0))
# Original solution
import itertools
import math
import fractions
import functools
import copy
n, m = list(map(int, input().split()))
x  = list(map(int, input().split()))

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

sub.sort(reverse=True)
if n >= m:
    print((0))
    quit()
for i in range(n-1):
    sub[i] = 0
print((sum(sub)))

# Optimized solution
def main():
    # n = int(input())
    n, m = list(map(int, input().split()))
    x = list(map(int, input().split()))
    # s = input()
    # h = [int(input()) for _ in range(n)]

    x.sort()

    subs = []

    for i in range(1, m):
        subs.append(abs(x[i]-x[i-1]))

    subs.sort()
    if (m < n):
        print((0))
    else:
        print((sum(subs[0:m-n])))


if __name__ == '__main__':
    main()