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

Solution (Python):
import sys

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

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

lst.sort()


sa = []

for i in range(M-1):
    sa += [abs(lst[i] - lst[i+1])]

sa.sort(reverse = True)


ans = -(lst[0] - lst[M-1])

if not sa ==[]:
    if N <= M:
        for j in range(N-1):
            ans -= sa[j]
    else:
        ans = 0
print (ans)

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


if M == 1:
  print(0)
  exit()

interval = [0] * (M - 1)
for i in range(M-1):
  interval[i] = X[i+1] - X[i]
  
interval.sort()
ans = 0
for i in range(len(interval) - (N-1)):
  ans += interval[i]
  
print(ans)

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

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

skip = N - 1
print(sum(diffs[skip:]))

Solution (Python):

N,M = list(map(int, input().split()))
A = list(map(int, input().split()))
'''
まず一つで考えてみよう
並び替えて、順々に行くのが最適。
方向性を決めることはできるだろうか。
次に二つの場合、どうするか


'''
if len(A) <= N:
    print(0)
    exit(0)

A.sort()

result =[]

for i,a in enumerate(A):
    if i == 0:
        continue
    result.append(A[i]-A[i-1])

result.sort(reverse=True)

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

Solution (Python):

import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, M: int, X: "List[int]"):
    X = sorted(X)
    d = sorted([e - b for b, e in zip(X, X[1:])], reverse=True)
    return X[-1] - X[0] - sum(d[:N-1])


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    X = [ int(next(tokens)) for _ in range(M) ]  
    result = solve(N, M, X)
    print(result)

if __name__ == '__main__':
    main()

Solution (Python):
import math
N,M = list(map(int,input().split(" ")))
X = list(map(int,input().split(" ")))
X.sort()
diffs = []

if N >= M:
    print(0)
else:
    for i,x in enumerate(X[1:]):
        diff = abs(x-X[i])
        diffs.append(diff)
    diffs.sort()
    

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

Solution (Python):


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

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

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

if __name__ == "__main__":
    solve()

Solution (Python):
n, m = 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)

Solution (Python):
N, M = 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:]))

Solution (Python):
N, M = 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)

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

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

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

X = sorted(X)
Y = [X[i + 1] - X[i] for i in range(M - 1)]
Y = sorted(Y, reverse=True)

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

Solution (Python):
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
if n>=m:
    print(0)
else:
    l=[]
    r=[]
    for i in range(m-1):
        l.append(x[i+1]-x[i])
    ll=sorted(l,reverse=True)
    for i in range(n-1):
        r.append(ll.pop(0))
    print(x[m-1]-x[0]-(sum(r)))

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

diff = []

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

Solution (Python):
c,n=map(int,input().split())
arr=list(map(int,input().split()))
if c>=n:
  print(0)
else:
  arr=sorted(arr)
  diff=[]
  for i in range(n-1):
    diff.append(abs(arr[i]-arr[i+1]))
  diff=sorted(diff)
  for i in range(c-1):
    diff.pop()
  print(sum(diff))

Solution (Python):
N, M = 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))

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

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

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

Solution (Python):

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

Solution (Python):
r=input().split()
n=int(r[0])
m=int(r[1])

if n<m:
    x=sorted([int(i) for i in input().split()])[::-1]
    xd=sorted([x[i]-x[i+1] for i in range(m-1)])
    if n==1:
        ans=x[0]-x[-1]
    else:
        ans=sum(xd[:-(n-1)])
else:
    ans=0
print(ans)

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

x.sort()

y = []

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

y.sort()

print(sum(y[:-n+1]) if n>1 else sum(y))

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

x.sort()

y = []

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

y.sort()

print(sum(y[:-n+1]) if n>1 else sum(y))

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline

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

if n >= m:
    print(0)
    sys.exit()
    
l.sort()
le = len(l) - 1
nl = [0] * le
for i in range(0, len(l) - 1):
    nl[i] = l[i + 1] - l[i]
    
nl.sort()
print(sum(nl[:le - n + 1]))

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

Solution (Python):
import numpy as np

NM = list(map(int, input().split()))
X = list(map(int, input().split()))
N = NM[0]
M = NM[1]


if N >= M :
    ans = 0
    print(ans)
    
else:
    Xs = sorted(X)

    
    length = []
    for i in range(len(Xs)-1):
        length.append(abs(Xs[i+1]-Xs[i]))

    length_n = sorted(length)
    
    del length_n[M-N:]
    

    idousum = sum(length_n)

    ans = idousum
    print(ans)

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

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

X.sort()
X.reverse()

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

Y.sort()
Y.reverse()

Z=0
for k in range(N-1,M-1):
    Z=Z+Y[k]
print(Z)

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

x.sort()
x_d = list()

if n > m:
    print(0)
else:
    if m == 1:
        print(0)
    else:


        x_d = list(np.diff(x))


            

            
        x_d.sort(reverse=True)

        sumd = sum(x_d[n-1:])
        print(sumd)

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

X_list = list(np.diff(X))

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

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

Solution (Python):
n, _  = map(int, input().split())
x = sorted([ int(x) for x in input().split() ])
L = sorted([ x[i+1] - x[i] for i in range(len(x)-1) ], reverse=True )
print(x[-1]-x[0]-sum(L[:n-1]))

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

Solution (Python):
nm = input().split()
n = int(nm[0])
m = int(nm[1])
xlist = [int(x) for x in input().split()]

xlist.sort()

sublist = []
prev_x = None
for x in xlist:
    if prev_x is not None:
        sublist.append(x - prev_x)
    prev_x = x

sublist.sort()

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

print(ans)

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

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

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

    
    x_list.sort()

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

    
    diff_list.sort(reverse=True)

    return sum(diff_list[n-1:])


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

    ans = solve(n, m, x_list)
    print(ans)


main()

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

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

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

ans = X[-1]-X[0]

o.sort(reverse=True)
for i in range(N-1):
    ans -= o[i]

print(ans)

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

X.sort()

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

l.sort(reverse=True)

if len(l) <= N - 1:
    print(0)
else:
    print(sum(l[N-1:]))

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

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

darr.sort(reverse=True)
ans = sum(darr[min(N-1, len(darr)):])
print(ans)

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

Solution (Python):
M, N = 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)

Solution (Python):
M, N = 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)

Solution (Python):
from collections import deque

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

if N < M : 

    X = sorted(list(map(int, input().split())))
    
    move = []
    
    for i in range(len(X)-1) : 
        move.append(X[i+1] -X[i])
        
    

    move = sorted(move)    

    
    
    sum = 0
    
    for i in range(M-N) : 
        sum += move[i]

else : 
    sum = 0
    
print(sum)

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

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

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

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

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

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

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

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

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

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

Solution (Python):
n, m = map(int, input().split())
xs = list(map(int, input().split()))
xs.sort()
habas = []
for i in range(m - 1):
  habas.append(xs[i+1] - xs[i])
habas.sort()

if n >= m:
  print(0)
else:
  print(sum(habas[:m-n]))

Solution (Python):
n,m = 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(sum(p[n-1:]))

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

X=sorted(X)

total=abs(X[-1]-X[0])


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

print(total - sum(gap[:n-1]))

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

X.sort()

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

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

length.sort(key=lambda i:i[0], reverse=True)

cand = []
cand.append(0)
for i in range(N-1):
    cand.append(length[i][1]+1)
cand.append(len(X))

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

ans = 0
for i in anslist:
    ans += i 

print(ans)

Solution (Python):
B = [0]

N,M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
for i in range(1,M):
    x = X[i]-X[i-1]
    B.append(x)
B = sorted(B)
B = B[::-1]
print(X[-1]-X[0]-sum(B[:N-1]))

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

Solution (Python):
from numpy import*
n,m,*x=map(int,open(0).read().split())
x=sorted(x)
x=[x[i+1]-x[i]for i in range(m-1)]
print(int(sum(sorted(x)[::-1][n-1:])))

Solution (Python):
import sys
from sys import stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
if N >= M:
    print(0)
    sys.exit()
X = [int(_) for _ in stdin.readline().rstrip().split()]
X.sort()
diffs = [X[i+1]-X[i] for i in range(M-1)]
diffs.sort()
print(sum(diffs[:M-N]))

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

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

Xs_sorted = sorted(Xs)

dists = [Xs_sorted[i+1]-Xs_sorted[i] for i in range(len(Xs_sorted)-1)]
dists_sorted = sorted(dists)

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

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

Solution (Python):
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]



def main(N, M, Xm):
    
    if N >= M:
        print(0)
        return
    
    Xm.sort()
    
    if N == 1:
        d = Xm[M-1] - Xm[0]
        print(d)
        return
    
    
    Dm = [Xm[i] - Xm[i-1] for i in range(1, M)]
    
    Dm.sort()
    
    
    
    
    
    
    d = 0
    for i in range(M-N):
        d += Dm[i]
    print(d)

N, M = i2nn()
Xm = i2nn()
main(N, M, Xm)

"""
C - Streamline
https://atcoder.jp/contests/abc117/tasks/abc117_c

2 5
10 12 1 2 14
    5

3 7
-10 -3 0 9 -100 2 17
    19

100 1
-100000
    0
"""

Solution (Python):
import numpy as np
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
a = np.array(a)

diff = a[:-1] - a[1:]
diff.sort()

if n == 1:
    print(sum(diff))
else:
    print(sum(diff[:-n + 1]))

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

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

Y = sorted([n- X[i-1] for i, n in enumerate(X) if i != 0], reverse=True)

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

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

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

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

Solution (Python):
import sys
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
div = [0]*(M-1)
for i in range(M-1):
    div[i] = abs(X[i+1] - X[i])
div = sorted(div,reverse=True)
ans = X[-1] - X[0]
if N >= M:
    print(0)
    sys.exit(0)
ans -= sum(div[:N-1])
print(ans)

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



  
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))

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

X.sort()

maxlist = [0] * (M-1)
cnt = 0
ans = 0

for i in range(1,M):
    maxlist[i-1]= X[i] - X[i-1]
    ans += maxlist[i-1]
    
maxlist.sort()
maxlist.reverse()

minus = 0
for i in range(min(N,M)-1):
    minus += maxlist[i]
    
print(ans-minus)

Solution (Python):
n, m = map(int, (input().split()))
x = sorted(list(map(int, input().split())))
y = sorted([i - x[n - 1]  for n ,i in enumerate(x) if n != 0], reverse=True)
print(sum(y[n - 1:]))

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

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

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

if M <= N:
    ans = 0
else:
    points = X[:]
    points.sort()

    diff_p = []
    for i in range(1, len(points)):
        diff_p.append(points[i] - points[i-1])

    diff_p.sort()
    

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

Solution (Python):

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

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

score = max(x) - min(x)

x.sort()

dist = []

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

dist.sort()



if n >= m:
    score = 0
else:
    for i in range(1,n):
        reduce = dist[-i]
        score -= reduce
print(score)

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

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

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

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

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

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

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

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

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

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

X_lst = sorted(Xs)
dX = sorted([X_lst[i+1] - X_lst[i] for i in range(len(X_lst)-1)])
if N >= M:
  print(0)
elif N == 1:
  print(sum(dX))
else:
  print(sum(dX[:-(N-1)]))

Solution (Python):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
if N >= M:
    print(0)
else:
    tmp = []
    for i in range(M - 1):
        tmp.append(A[i+1] - A[i])
    tmp.sort(reverse=True)
    interval = 0
    for i in range(N - 1):
        if i + 1 == M - 1:
            break
        interval += tmp[i]
    print(sum(tmp) - interval)

Solution (Python):
N, M = map(int, input().split())
zahyo = list(map(int, input().split()))
zahyo.sort()
sa = [0 for _ in range(M-1)]

for i in range(M-1):
    sa[i] = zahyo[i+1] - zahyo[i]

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

print(sum(sa))

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

Solution (Python):

import sys


def solve(N: int, M: int, X: "List[int]"):
    if N >= M:
        print(0)
        return
    X.sort()
    dist = []
    for i in range(1, len(X)):
        dist.append(abs(X[i] - X[i - 1]))
    distIndices = [(dist[i], i) for i in range(len(dist))]
    distIndices.sort(reverse=True)
    splitIndex = []
    for i in range(N-1):
        splitIndex.append(distIndices[i][1])
    splitIndex.sort()
    ranges = []
    begin = 0
    for next in splitIndex:
        ranges.append(X[begin:next+1])
        begin = next+1
    ranges.append(X[begin:])
    cost = 0
    for R in ranges:
        if len(R) == 1:
            continue
        cost += R[-1] - R[0]
    print(cost)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    X = [int(next(tokens)) for _ in range(M)]  
    solve(N, M, X)


if __name__ == '__main__':
    main()

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

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

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

print(sum(dif))

Solution (Python):
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g


n,m = map(int,input().split())
arr = list(map(int,input().split()))
arr.sort()
temp = []
for i in range(m-1):
    temp.append(arr[i+1]-arr[i])
temp.sort()
temp[::-1]
ans = 0

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

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

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

path_cnt = sorted([X[i + 1] - X[i] for i in range(M - 1)])
if N == 1:
    print(sum(path_cnt))
else:
    print(sum(path_cnt[:-N + 1]))

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

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


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

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

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

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


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

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

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

x.sort()

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

ans = sum(distance[n-1:])
print(ans)

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

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

btw = []
for i in range(M):
  if i == M - 1:
    break
  else:
    btw.append(X[i+1] - X[i])
btw = sorted(btw, reverse=True)

ans =0
for e in btw[N-1:]:
  ans += e
 
print(ans)

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

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

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

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

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

for i in reversed(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)

Solution (Python):
import math
N, M = input().split()
N=int(N)
M=int(M)
values = [int(i) for i in input().split()]
values.sort()
app = []
for i in range(M-1):
    app.append(values[i+1]-values[i])
app.sort()
ans = 0
for i in range(M-N):
    ans += app[i]
print(ans)

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

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

X.sort()



L = [0] * (M - 1)

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



L.sort(reverse=True)

result = X[-1] - X[0] - sum(L[:N - 1])

print(result)

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

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

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

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

if n >= m:
    print("0")
else:
    x.sort()

    l = [x[i+1] - x[i] for i in range(m - 1)]

    l.sort(reverse=True)

    ans = x[m-1] - x[0]
    for i in range(n-1):
        ans -= l[i]

    print(ans)

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

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

X.sort()

dX=[]

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

dX.sort()

out=0

for i in range(M-N):
    out+=dX[i]

print(out)

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

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

x.sort()

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

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

Solution (Python):
n, m = 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)

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

Solution (Python):
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
dif = [None] * (M-1)
for i in range(M-1):
    dif[i] = X[i+1] - X[i]
    
dif = sorted(dif)
res = 0
for i in range(M-N):
    res += dif[i]
print(res)

Solution (Python):
n, m = list(map(int, input().split()))
x_li = list(map(int, input().split()))
x_li.sort()
dist = []
for i in range(m-1):
  dist.append(abs(x_li[i] - x_li[i + 1]))
dist.sort()
ans = 0
for i in range(m - n):
  ans += dist[i]
print(ans)

Solution (Python):
def main():
	n, m = map(int, input().split())
	l = list(map(int, input().split()))
	if m <= n:
		print(0)
		return
	l.sort()
	dif = [l[i+1]-l[i] for i in range(m-1)]
	dif.sort()
	result = 0
	for i in range(m-n):
		result += dif[i]
	print(result)

if __name__ == '__main__':
    main()