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

Solution (Python):
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)

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

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

if N>=2:
    print(sum(dis[:-N+1]))
else: print(sum(dis))

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

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

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

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

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

N, M = map(int, input().split())
if N >= M:
    print(0)
else:
    X = [int(_) for _ in input().split()]
    X = sorted(X)
    diff = [0]*(M-1)

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

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

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

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

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

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

Solution (Python):
import sys

N,M=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=[x[i+1]-x[i] for i in range(M-1)]
a.sort()

if N>=M:
    print(0)
    sys.exit()
    
print(sum(a[:M-N]))

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

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

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

Solution (Python):


import sys
input = sys.stdin.readline

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

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

print(sum(X_dif))

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

if N >= M:
    print(0)
else:
    X = sorted(X)
    X_diff = [X[i] - X[i-1] for i in range(1, len(X))]
    X_diff = sorted(X_diff, reverse=True)
    print(sum(X_diff[N-1:]))

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

Solution (Python):
import numpy as np
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X = np.sort(X)

diff = np.diff(np.sort(X), n=1)
print(np.sum(np.sort(diff)[::-1][N-1:]))

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, m = na()
li = na()
ls = []
list.sort(li)
for i in range(len(li) - 1):
    ls.append(li[i + 1] - li[i])
ls = sorted(ls, reverse=True)
print(sum(ls[n-1:]))

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-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 input()
def pf(s): return print(s, flush=True)

N, M = LI()
X = LI()
X = sorted(X)
X_dist = []
for i in range(M-1):
    X_dist.append(abs(X[i] - X[i+1]))

X_dist = sorted(X_dist, reverse=True)
print(sum(X_dist[N-1:]))

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

if N >= M:
    print(0)
else:
    list = list()
    last = 0
    flg = True
    for x in X:
        if flg == True:
            last = x
            flg = False
            continue

        list.append(x - last)
        last = x

    list.sort()

    for i in range(N-1):
        list.pop(-1)
    print(sum(list))

Solution (Python):
N,M = [int(i) for i in input().split()]
if N >= M:
    print(0)
else:
    koma = sorted(list(map(int, input().split())))
    kaisa = []
    for i in range(M-1):
        kaisa.append(koma[i+1] - koma[i])
    kaisa.sort()
    ans = 0
    for i in range(M-N):
        ans += kaisa[i]
    print(ans)

Solution (Python):




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

x.sort()

aida = [0] * (m-1)
for i in range(1,m):
    aida[i-1] = x[i] - x[i-1]

aida.sort()
aida.reverse()

del aida[:n-1]

print(sum(aida))

Solution (Python):
def main():
    N, M = [int(n) for n in input().split()]
    X = [int(n) for n in input().split()]
    X.sort()
    diffs = [X[i+1]-X[i] for i in range(M-1)] 
    diffs.sort(reverse=True)
    
    cut = sum(diffs[:N-1])
    
    print(X[-1] - X[0] - cut)

if __name__ == "__main__":
    main()

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

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


"""
N個のコマ：N-1個の閉経路が存在する
各座標の距離を求める(M-1個)
M：座標の数　N:駒の数
移動しなくても通っている座標の数: N
M - N
"""

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

if N >= M:
    print(0)
else:
    A = sorted(list(map(int, input().split())))
    B = sorted(A[i]-A[i-1] for i in range(1, len(A)))
    print(sum(B[0:M-N]))

Solution (Python):
n,m = map(int,input().split())
x = list(map(int, input().split()))
x_new = sorted(x)
intervals = [0]*(m-1)
for i in range(m-1):
  intervals[i] = abs(x_new[i+1] - x_new[i])
c = sorted(intervals, reverse=True)
s = 0
if n < m:
    for j in range(n-1):
        s += c[j]
    min = sum(intervals) - s
    print(min)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split(" "))
destination = list(map(int, input().split(" ")))
dis = []
ans = 0
destination.sort()
for i in range(M-1):
    dis.append(destination[i+1] - destination[i])
dis.sort()
for j in range(M-N):
    ans += dis[j]
print(ans)

Solution (Python):
NM = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
X.sort()
if(NM[1] <= NM[0]):
    print(0)
else:
    list1 = []
    for i in range(NM[1] - 1):
      list1.append(X[i + 1] - X[i])
    list1.sort()
    while(NM[0] > 1):
        del list1[-1]
        NM[0] -= 1
    print(sum(list1))

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

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

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

dist = sorted(dist, key=lambda t: t[0], reverse=True)[:N-1]
dist.append((0, 0))
dist = sorted(dist, key=lambda t: t[1])
dist.append((0, 0))

ans = 0
for i in range(len(dist)-1):
    ans += X[dist[i+1][1]-1] - X[dist[i][1]]

print(ans)

Solution (Python):
N,M = map(int,input().split())
l = list(map(int,input().split()))
if M <= N:
    print('0')
else:
    l.sort()
    li = []
    for i in range(M-1):
        difference  = abs(l[i]-l[i+1])
        li.append(difference)
    li.sort()
    li.reverse()
    dif = 0
    for i in range(N-1):
        dif += li[i]
    print(l[M-1]-l[0]-dif)

Solution (Python):


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

X.sort()


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

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

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

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

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

if n >= m:
    print(0)
else:
    di = sorted([xi[i+1]-xi[i] for i in range(m-1)])
    print(sum(di[:m-n]))

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

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

X.sort() 


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

D.sort() 

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

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

diff = [b-a for a,b in zip(list,list[1::])]
diff.sort(reverse=True)
print(sum(diff[N-1:]) if M-N >0 else 0)

Solution (Python):
N, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    if len(distance) > 0:
        del distance[-1]
    else:
        print(0)
        quit()
print(sum(distance))

Solution (Python):
N, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    try:
        del distance[-1]
    except:
        print(0)
        quit()
print(sum(distance))

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

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

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
w=[[] for i in range(m-1)]
for i in range(m-1):
    w[i]=[x[i],x[i+1]-x[i]]
w=sorted(w,key=lambda y:y[1])
w.reverse()
skip=[]
if m!=1:
    for i in range(n-1):
        skip.append(w[i])
        if i==m-2:
            break
            
    skip=sorted(skip,key=lambda y:y[0])
ans=[]
k=x[0]
for i in skip:
    ans.append(i[0]-k)
    k=i[0]+i[1]
ans.append(x[m-1]-k)
print(sum(ans))

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

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

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

if n >= m:
    print(0)
else:
    print(X[-1] - X[0] - sum(X_diff[:n-1]))

Solution (Python):
N, M = map(int,input().split())
X = list(map(int,input().split()))
if N < len(X):
    D =[]
    S = 0
    X.sort()
    for i in range(M-1):
        D.append(X[i+1]-X[i])
    E =sorted(D,reverse = True)
    S = sum(E)
    for j in range(N-1):
        S -= E[j]
    print(S)
else:
    print("0")

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


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


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

Solution (Python):

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

if N < M:
	dist = []
	for i in range(M-1):
		dist.append(abs(X[i+1] - X[i]))
	dist.sort()
	ans = sum(dist[:M-N])
else:
	ans = 0

print(ans)

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

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

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

del L[:N-1]
print(sum(L))

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

if(N >= M):
  cost = 0
  print(cost)
else:
 X = input().split()
 for i in range(M):
  X[i] = int(X[i]) 
 X.sort() 

 L = []
 for i in range(M):
  if(i != 0): 
    L.append(X[i]-X[i-1])
 L.sort()

 
 

 tmp = 0
 for i in range(N-1):
  tmp += L.pop(-1)
 cost2 =  (X[M-1]-X[0]) - tmp
 print(cost2)

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

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

list.sort()

dif = [b-a for a,b in zip(list,list[1:])]
dif.sort()
print(sum(dif[:M-N]) if M-N > 0 else 0)

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

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

a = sorted(a)
diff = []
for i in range(1, m):
    diff += [abs(a[i] - a[i-1])]

diff = sorted(diff, reverse=True)

ans = a[-1] - a[0]
for i in range(n-1):
    ans -= diff[i]

print(ans)

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

Solution (Python):
n,m = map(int, input().split())
xx = sorted(list(map(int, input().split())))
if n>m:
    print(0)
else:
    distance =[0]*(m-1)
    for i in range(m-1):
        distance[i] = abs(xx[i+1] - xx[i])
    distance = sorted(distance)
    print(sum(distance[0:len(distance)-n+1]))

Solution (Python):

def func(n, m, x):
    x.sort()
    dis = []
    for i in range(1, len(x)):
        dis.append(x[i] - x[i-1])
    dis.sort()
    n = min(n, m)
    
    print(sum(dis[:m-n]))

    
"""
func(3, 5, [1, 2, 10, 12, 14])
func(3, 7, [-10, -3, 0, 9, -100, 2, 17])
func(100, 1, [-100])
"""
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
func(n, m, x)

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

if N>=M:
    print(0)
else: 
    dist = [b-a for a, b  in zip(X, X[1:])]
    dist.sort()
    if (N-1)==0:
        print(sum(dist[:M-N]))
    else:
        print(sum(dist[:-(N-1)]))

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

if N>=M:
    print(0)
else: 
    dist = [b-a for a, b  in zip(X, X[1:])]
    dist.sort()
    print(sum(dist[:M-N]))

Solution (Python):
import numpy as np
N, M = map(int,input().split())
target_list = np.array([int(i) for i in input().split()])
if N > M:
    print(0)
else:
    target_list = np.sort(target_list)
    diff_list = target_list[1:] - target_list[:-1]
    diff_list = np.sort(diff_list)
    if N > 1:
        diff_list = diff_list[0:-(N-1)]
    print(np.sum(diff_list))

Solution (Python):

import sys


def solve(N: int, M: int, X: "List[int]"):
    X.sort()
    intervals = []
    for i in range(1, M):
        intervals.append(X[i] - X[i - 1])
    intervals.sort(reverse=True)
    ret = X[-1] - X[0] - sum(intervals[:N - 1])
    print(ret)
    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())
l=list(map(int, input().split()))

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

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

d.sort()
print(sum(d[:m-n]))

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

Solution (Python):

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

if N >= M:
	print(0)
	exit()
X.sort()
dis = [b - a for a, b in zip(X, X[1:])]
dis.sort()

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

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
if N >= M :
    print(0)
    exit()
X.sort()
flag = [b-a for a,b in zip(X,X[1:])]
flag.sort()
print(sum(flag[:M-N:]))

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

Solution (Python):
pieces_num, markpoints_num = map(int, input().split())
markpoints = sorted(map(int, input().split()))

distances = []
for markpoints_i in range(1, markpoints_num):
    distances.append(markpoints[markpoints_i] - markpoints[markpoints_i - 1])
distances.sort()

print(sum(distances[:1 - pieces_num] if pieces_num != 1 else distances))

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

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

if N >= M:
  print(0)
else:
  X_list = [int(x) for x in input().split()]
  X_list.sort()

  L_list = []
  for i in range(1, M):
    L_list.append(X_list[i] - X_list[i-1])
  L_list.sort(reverse=True)
  print(sum(L_list[N-1:]))

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

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

y = sorted(x)

l = [0] * (m-1)

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

ll = sorted(l)

s = 0

for i in range(0,m-n):
    s = s + ll[i]

print(s)

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

ans = 0
if N >= M:
	pass
else:
	X.sort()
	
	Y = [0]*(M-1)
	for i in range(M-1):
		Y[i] = X[i+1] - X[i]
	Y.sort(reverse=True)
	
	L = 0
	for i in range(N-1):
		L += Y[i]
	ans = X[M-1] - X[0] - L
print(ans)

Solution (Python):
import re
import numpy as np

N, M = [int(item) for item in re.split(r"\s", input())]
X = [int(item) for item in re.split(r"\s", input())]

if N >= M:
    print(0)
elif N == 1:
    s_X = np.array(sorted(X))
    print(s_X[-1] - s_X[0])
else:
    splitter = N - 1
    s_X = np.array(sorted(X))

    diffs = np.sort(np.diff(s_X))[::-1] - 1
    diff_sum = diffs[:splitter].sum()
    print(s_X[-1] - s_X[0] + 1 - N - diff_sum)

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

X.sort()
dis = []
for i in range(len(X)-1):
    dis.append(X[i+1] - X[i])
ans=0
dis.sort()
for i in range(len(dis)-N+1):
    ans += dis[i]

print(ans)

Solution (Python):
import numpy as np

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

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

argsrt = np.argsort(dX)[::-1][:N-1]
idxes = sorted(argsrt)

def calc(l):
    if len(l) == 1:
        return 0

    return l[len(l) - 1] - l[0]

s = 0
last_idx = 0
for i in idxes:
    s += calc(X[last_idx:i+1])
    last_idx = i + 1

s += calc(X[last_idx:])

print(s)

Solution (Python):


if __name__ == "__main__":
    n,m = map(int, input().split())
    l = list(map(int, input().split()))

    if n >= m:
        print("0")
    else:
        d = (m-1) * [0]

        l = sorted(l)
        for i in range(1,m):
            d[i-1] = l[i] - l[i-1]
        d = sorted(d, key=lambda x: -x)

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

        print(l[-1] - l[0] - ans)

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

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

if N >= M:
    print(0)
else:
    L=[]
    for i in range(M-1):
        L.append(li[i+1]-li[i])
    L = sorted(L, reverse=True)
    x=0
    for j in range(N-1):
        x+=L[j]
    answer = li[-1]-li[0]-x
    print(answer)

Solution (Python):



def solve(n, m, x):

    if m <= n:
        return 0

    x.sort()
    total = x[-1] - x[0]

    if n == 1:
        return total

    px = x[0]
    y = []
    for xi in x[1:]:
        y.append(xi - px)
        px = xi

    y.sort(reverse = True)
    r = sum(y[:n - 1])

    return total - r

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

    print(solve(n, m, x))


if __name__ == '__main__':
    main()

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

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

x.sort()

d = [x[i] - x[i-1] for i in range(1,m)]

d.sort()

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

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

Solution (Python):
import sys

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

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

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

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

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

def A():
    t,x = LI()
    print(t/x)


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



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


def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])


if __name__ == "__main__":
    C()

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

def A():
    t,x = LI()
    print(t/x)


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



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


def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])


if __name__ == "__main__":
    C()

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

Solution (Python):
N,M = map(int,input().split())
X = sorted(list(map(int,(input().split()))))
dif_X = sorted([X[i+1]-x for i,x in enumerate(X[:-1])])
print(sum(dif_X[:max(0,M-N)]))

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

X.sort()

dif_X = [X[i+1]-x for i,x in enumerate(X[:-1])]
dif_X.sort()
print(sum(dif_X[:max(0,M-N)]))

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

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

if N > M:
    ans = 0
else:
    distance = [abs(a[i+1] - a[i]) for i in range(M-1)]
    
    distance.sort()
    
    del distance[M-N:]
    ans = 0 
    for j in distance:
        ans += j
print(ans)

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

X.sort()

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

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

print(ans)

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

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

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

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

x = sorted(x)
for i in range(1, m):
  x[i-1] = x[i] - x[i-1]
x[-1] = 10**6

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

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

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

X.sort()

Y = []

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

Y.sort()

if M-N <= 0:
    ans = 0
else:
    ans = sum(Y[:M-N])

print(ans)

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