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=map(int,input().split())
*x,=map(int,input().split())
x.sort()

a=sorted([(x[i+1]-x[i],i) for i in range(m-1)])[::-1]
s=0
v=0
for i,j in sorted(a[:n-1],key=lambda x:x[1]):
    v+=x[j]-x[s]
    s=j+1
if s<m:
    v+=x[-1]-x[s]
print(v)

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

Solution (Python):

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



X.sort()


if N >= M:
    ans = 0
    
else:
    Y = []
    i = 1
    for i in range(M-1):
        sa = X[i+1]-X[i]
        Y.append(sa)
    Y.sort()

    sa = X[M-1]-X[0]

    
    ans = sa
    
    for i in range(N-1):
        ans = ans-Y[M-i-2]
        

    
print(ans)

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

Solution (Python):
n, m = [int(i) for i in input().strip().split(" ")]
if n >= m:
    result = 0
else:
    xs = sorted([int(i) for i in input().strip().split(" ")])
    ls = []
    for i in range(m-1):
        ls.append(xs[i+1] - xs[i])
    ls = sorted(ls)
    result = sum(ls[:m - n])

print(result)

Solution (Python):
N, M = input().split()
X = input().split()
deff = []
ans = 0

for i in range(len(X)):
    X[i] = int(X[i])

if int(N) >= int(M):
    print(0)
else:
    X.sort()

    for i in range(len(X)):
        if i == 0:
            pass
        else:
            deff.append(X[i] - X[i-1])

    deff.sort()

    for i in range(int(M) - int(N)):
        ans += deff[i]

    print(ans)

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[:M-N]))

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

Solution (Python):
NM = list(map(int,input().split()))
N,M = NM[0], NM[1]
li = list(map(int,input().split()))

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

Solution (Python):

from pprint import pprint as pp
N, M = map(int, input().split())
Xm = list(map(int, input().split()))

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


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

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

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


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

Solution (Python):
n, m = map(int, raw_input().strip().split())
lis = map(int, raw_input().strip().split())
lis = sorted(lis)
lol = []
for i in range(1, m):
    lol.append(lis[i] - lis[i-1])
lol = sorted(lol)
ans = lis[-1] - lis[0]
idx = len(lol) - 1
if idx >= 0:
    for i in xrange(n-1):
        ans -= lol[idx]
        idx-=1
        if idx == -1:
            break
print ans

Solution (Python):
NM = list(map(int,input().split()))
N,M = NM[0], NM[1]
li = list(map(int,input().split()))

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

Solution (Python):
import sys
import numpy as np

N, M = map( int, input().split() )
X_list = np.array( sorted( list( map( int, input().split() ) ) ) )

diff=np.diff(X_list, n=1)

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

sorted_diff=np.sort(diff)

cnt=np.sum(sorted_diff[:M-N])

print(cnt)

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

if(N>M):
    N=M
if(M==1):
    print(0)
    exit()
for i in range(0,len(x)-1):
    leng.append([x[i+1]-x[i],i])
leng=sorted(leng)

n=N
max=len(leng)-1
sets=list()
while n>1:
    n-=1
    sets.append(leng[max][1]+1)
    max-=1

for i in x:
    if(x.index(i) not in sets):
        sets.append(x.index(i))
        break
sets=sorted(sets) 
ans=0
for i in range(0,len(sets)-1):
    ans+=x[sets[i+1]-1]-x[sets[i]]

ans+=x[-1]-x[sets[-1]]

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
k=[]
for i in range(1,m):
    dif=l[i]-l[i-1]
    k.append(dif)
k.sort()
ans=0
for i in range(m-n):
    ans+=k[i]
print(ans)

Solution (Python):
N, M = list(map(int, input().rstrip().split()))
X = list(map(int, input().rstrip().split()))
if N >= M:
    cnt = 0;
elif N == 1:
    cnt = max(X) - min(X)
else:
    X.sort()
    D = []
    for i in range(1, len(X)):
        D.append(X[i] - X[i - 1])
    D.sort()
    del D[-(N - 1):]
    cnt = sum(D)
print(cnt)

Solution (Python):
import numpy as np

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

l = [int(i) for i in input().split()]
l.sort()
if n == 1 or m == 1:
    print(l[-1]-l[0])
elif n > m:
    print(0)
else:
    d = [0 for i in range(m - 1)]
    for i in range(m-1):
        d[i] = l[i+1] - l[i]
    d = np.array(d)
    dl = d.argsort()[::-1]
    count = 0
    for i in range(n):
        if i == 0:
            count += l[dl[i]] - l[0]
            index = dl[i]
        elif i == n - 1:
            count += l[-1] - l[index+1]
        else:
            count += l[dl[i]] - l[index+1]
            index = dl[i]
    print(count)

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

    if (M < N):
        print(0)
        return
    X.sort()
    dx = []
    for i in range(M-1):
        dx.append(abs(X[i] - X[i+1]))
    dx.sort()
    
    res = 0
    for i in range(M-N):
        res += dx[i]
    print(res)

main()

Solution (Python):
nm = list(map(int, input().split()))
lis = list(map(int, input().split()))

lis.sort()

sum = 0
diff = []

if nm[1] - nm[0] <= 0:
    print(0)
else:
    for i in range (nm[1] - 1):
        diff.append(lis[i + 1] - lis[i])

    diff.sort()

    if nm[0] != 1:
        for j in range (nm[0] - 2):
            diff.pop(-1)

    for k in range (nm[1] - nm [0]):
        sum = sum + diff[k]

    print(sum)

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

if n >= m:
    print(0)
else:
    xs.sort()
    diffs = [xs[i] - xs[i-1] for i in range(1, m)]
    if n == 1:
        ans = sum(diffs)
    else:
        indxs = list(np.array(diffs).argsort()[::-1][:n-1])
        indxs.sort()
        ans = sum(diffs[0:indxs[0]])
        if indxs[-1]+1 < len(diffs):
            ans += sum(diffs[indxs[-1]+1:])
        for i in range(len(indxs)-1):
            ans += sum(diffs[indxs[i]+1:indxs[i+1]])
    print(ans)

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

if(M <= N):
  print(0)
  
else :  
  for i in range(len(X)-1):
    sub = abs(X[i+1] - X[i])
    Y.append(sub)
  Y.sort()
  Y.reverse()
  xid = 0
  for i in range(N-1):
    Z.append(Y[i])

  ans = sum(Y)-sum(Z)
  if ans < 0:
    ans = 0
  print(ans)

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

X.sort()

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

print(sum(Y[0: max(M - N, 0)]))

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

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

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


def main():
    if N > M:
        print(0)
        return

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

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

if __name__ == '__main__':
    main()

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

Solution (Python):
import numpy as np
import heapq

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

if n >= m:
    print(0)
else:
    npx = np.array(x)
    li = list(np.diff(npx, n = 1))

    print(sum(heapq.nsmallest(len(li)-n+1, li)))

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

for i in range(M-1):
    diflist.append(numlist[i + 1] - numlist[i])
diflist.sort()
diflist.reverse()

delnum = N - 1
del diflist[:delnum]
sumnum = sum(diflist)

if M <= N:
    print("0")
else:
    print(sumnum)

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

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

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

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

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

X.sort()

sa = []
for i in range(0, M-1):
    ima_sa = X[i+1] - X[i]
    sa.append(ima_sa)

sa.sort()

for i in range(0, N-1):
    sa.pop()

kotae = sum(sa)
print(kotae)

Solution (Python):

import sys



def solve(lines):
    N,M = map(int, lines.pop(0).split(' '))
    X = list(map(int, lines.pop(0).split(' ')))
    if N >= M:
        print(0)
        sys.exit(0)
    X.sort()
    span = []
    for i in range(M-1):
        span.append( X[i+1] - X[i] )
    span.sort()
    for i in range(N-1):
        span.pop(-1)
    print(sum(span))





lines = [x.strip() for x in sys.stdin.readlines()]




solve(lines)

Solution (Python):

 
n, m = map(int, input().split())
l = sorted(list(map(int, input().split())))
divcount = n - 1
 
l1 = l[1:]
l2 = l[:m - 1]
divl = []
 
for i in range(m - 1):
    divl.append(l1[i] - l2[i])
 
divl.sort(reverse = True)
 
if n == 1:
    print(max(l) - min(l))
else:
    if m > n:
        for i in range(divcount):
            del divl[0]
    
        print (sum(divl))
    
    else:
        print(0)

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

Solution (Python):
import numpy as np
 
N,M=[int(i) for i in input().split()]
x=input().split()
y=[int(i) for i in x]
y.sort()
if(N==1):
    print(max(y)-min(y))

    exit()
if(M==1):
    print("0")
    exit()
if(N>=M):
    print("0")
    exit()
diff=[]
for i in range(M-1):
    diff.append(y[i+1]-y[i])
sub=[]
array=np.array(diff)
index=np.argsort(array)
c=index[-1::-1]
for i in range(N-1):
    sub.append(c[i])
ans=0
for i in range(N):
    if(i==0):
        ans+=y[sub[i]]-y[0]
    elif(i==N-1):
        ans+=y[M-1]-y[sub[i-1]+1]
    else:
        ans+=y[sub[i]]-y[sub[i-1]+1]
print(ans)

Solution (Python):


N, M = map(int, input().split())
X = [int(v) for v in input().split()]
lx = len(X)



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

    X.sort()    
    coma = [0] * N
    
    diff = [x2-x1 for x2, x1 in zip(X[1:], X[:-1])]
    diff.sort() 
    coma[0] = sum(diff)

    
    for i in range(1, N):
        coma[i] = coma[i-1] - diff[-1]
        del diff[-1]

    print(coma[-1])

Solution (Python):
import numpy as np

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

if N >= M:
    print(0)
else:
    X_m = np.array(sorted(list(map(lambda x: int(x), input().split()))))
    X_m_sub = np.array(X_m[1:]-X_m[:-1])
    
    pos_option = X_m_sub.argsort()
    
    for n in range(N-1):
        X_m_sub[pos_option[-(n+1)]] = 0
    
    print(X_m_sub.sum())

Solution (Python):
import math

def solve(N,M,Xs):
    if N >= M:
        return 0

    Ds = [abs(Xs[i]-Xs[i+1]) for i in range(len(Xs)-1)] 

    Ds.sort()
    
    

    
    ntake = N-1

    return sum(Ds[0:len(Ds)-ntake])

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


if __name__ == '__main__':
    main()

Solution (Python):
N, M = map(int, input().split())
num_lis = sorted(list(map(int, input().split())))
num_dif = []
ans = 0

if N > M:
  print(0)
elif M == 1:
  print(0)
else:
  for n in range(M):
    if n == M-1:
      break
    n_dif = num_lis[n+1] - num_lis[n]
    n_dif = abs(n_dif)
    num_dif.append(n_dif)
  sorted_num_dif = sorted(num_dif)
  if N == 1:
    ans = sum(sorted_num_dif)
  else:
    for i in sorted_num_dif[:-(N-1)]:
      ans += i

  print(ans)

Solution (Python):
a=input()
b=input()
c=a.split()
n=int(c[0])
d=b.split()
d=[int(i) for i in d]
d.sort(reverse=True)
count=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n-1):
    ccc=len(ll)
    if ccc>=1:
        del ll[-1]
print(sum(ll))

Solution (Python):
import numpy as np

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

X.sort()

dif_X = X[1:] - X[:-1]
dif_X.sort()

if M - N <= 0:
  print(0)
else:
  print(sum(dif_X[:M-N]))

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

index = range(len(X)-1)

diff = [sorted_x[n+1] - sorted_x[n] for n in index]
sorted_diff = sorted(diff,reverse=True)

sub_diff = sorted_diff[N-1:]

print(sum(sub_diff))

Solution (Python):
num, points_num = list(map(int, input().split(' ')))
points = list(map(int, input().split(' ')))

if num >= points_num:
    print("0")
else:
    points.sort()
    points = list(map(lambda x: [x], points))
    index = 0
    step = 1
    while num < len(points):
        
        if index + 1 >= len(points):
            step = step + 1
            index = 0

        if points[index + 1][0] - points[index][-1] <= step:
            concat_arr = points.pop(index + 1)
            points[index].extend(concat_arr)
        else:
            index = index + 1
    
    move_num = sum(list(map(lambda x: x[-1] - x[0], points)))
    print(move_num)

Solution (Python):


import copy
import numpy as np
import random


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

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

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

dif_list = sorted(dif_list)
answer = 0
for i in range((m-1)-(n-1)):
    answer += dif_list[i]

print(answer)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect

setrecursionlimit(10**7)

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N, M = reads()
X = reads()
X.sort()
diff = [X[i] - X[i-1] for i in range(1, M)]
diff.sort()
i = max(M-N, 0)
print(sum(diff[:i]))

Solution (Python):
import numpy as np

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

if M <= N:
    print(0)
elif N == 1:
    print(X_list[-1] - X_list[0])
else:
    diff = np.diff(X_list)
    ans = max(X_list) - min(X_list) - np.sum((-np.sort(-diff))[:N-1])
    print(ans)

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

if n>=m:
    print(0)
elif n==1:
    ans=max(x)-min(x)
    print(ans)
else:
    delta=[0]*(m-1)
    for i in range(m-1):
        delta[i]=x[i+1]-x[i]
    delta_s=sorted(delta)
    ans=sum(delta_s[:-n+1])
    print(ans)

Solution (Python):
N, M = list(map(int, input().split()))
X_list = sorted(list(map(int, input().split())))
offset_list =[]
for x in range(len(X_list)):
    if x==0:
        continue
    offset = abs(X_list[x] - X_list[x-1])
    offset_list.append(offset)
offset_list.sort(reverse=True)
del offset_list[:N-1]
move_count = 0
for o in offset_list:
    move_count += o
print(move_count)

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


if N >= M or M == 1:
	ans = 0
elif N == 1:
	X.sort()
	ans = X[-1] -X[0]
else:
	X.sort()
	D = []
	for i in range(M-1):
		d = X[i+1] - X[i]
		D.append([i,d])
	

	D.sort(key = lambda x:(x[1]),reverse =True)
	
	sikiri = []
	for i in range(N-1):
		sikiri.append(D[i][0])
	sikiri.sort()
	ans = X[sikiri[0]] - X[0]
	for i in range(N-1):
		l = sikiri[i]+1
		if i == N-2:
			r = M-1
		else:
			r = sikiri[i+1]
		ans+= X[r]-X[l]
print(ans)

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

    if n >= m:
        print(0)
        return


    x.sort()
    x1 = x[1:]
    x2 = x[:-1]
    xs = [_x1 - _x2 for _x1, _x2 in zip(x1, x2)]


    argxs = sorted(enumerate(xs), key=lambda y: y[1])

    a = []
    for i in range(1, n):
        a.append(argxs[-i][0])
    
    tmp = 0
    ans = 0

    for _a in a:
        ans += x[_a] - x[tmp]
        tmp = _a + 1
    ans += x[-1] - x[tmp]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

n,m=input().split()
N = int(n)
M = int(m)
X = input().split()

X = [int(x) for x in X]

X.sort()
if M == 1:
    print(0)
    sys.exit()
elif M < 1:
    sys.exit()

minus = [X[i]-X[i-1] for i in range(1,M)]


minus.sort()

sum_num = 0
if N < M:
    for i in range(M-N):
        sum_num += minus[i]
    print(sum_num)
elif N >= M:
    print(0)

Solution (Python):
n,m = map(int, input().split())
num = list(map(int, input().split()))
num_new = sorted(num)
gap = []

for i in range(m-1):
	gap.append(num_new[i+1] - num_new[i])

gap_new = sorted(gap, reverse=True)
del gap_new[0:n-1]
count = 0

for i in gap_new:
	count += i

print(count)

Solution (Python):

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

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

X_diff = sorted(X_diff)

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

print(ans)

Solution (Python):


import numpy as np

INPUT = [int(n) for n in input().split()]
N, M = INPUT[0], INPUT[1]
X = [int(x) for x in input().split()]
X.sort()
Diff = []

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

Diff.sort()

for i in range(N-1):
    if Diff == []:
        break
    Diff.pop(-1)

print(int(np.sum(Diff)))

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

Solution (Python):
line1 = list(map(lambda x: int(x), input().split()))
N = line1[0]
M = line1[1]
X = list(map(lambda x: int(x), input().split()))

if N >= M or M == 1:
  print(0)
else:
  X.sort(key=lambda x:x, reverse=False)

  diff = list(map(lambda x: x[0]-x[1], zip(X[1:],X[:-1])))
  diff.sort(key=lambda s:s, reverse=False)

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

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

X.sort()
if N == 1:
    print(X[-1]-X[0])

X.append(0)
A = []
for x, y in zip(X[:-1], X[1:]):
    A.append(y-x)
B = A[:-1]
C = sorted(B)

if N == 2:
    print(sum(C[:-1]))
elif N > 2:
    print(sum(C[:-N+1]))

Solution (Python):
if __name__ == '__main__':
    n, m = map(int, input().split())
    xs = sorted(int(s) for s in input().split())
    ds = [y - x for x, y in zip(xs[:-1], xs[1:])]
    ds.sort(reverse=True)
    print(sum(ds[n - 1:]))

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

X.sort()

result = 0
temp = []
if M <= N:
    print(0)
else:
    for i in range(M-1):
        temp.append(-(X[i] - X[i+1]))
    temp.sort()
    for i in range(M-N):
        result += temp[i]
    print(result)

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

q.sort()

data = []

for a in range(1,len(q)):
  data.append( q[a] - q[a-1] )

data.sort()

for a in range( n-1 ):
 try:
  data.pop()
 except:
  pass

ans = 0

for a in range( len(data) ):
  ans += data[a]

print ans

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

Solution (Python):
Ns,Ms = input().split()
N=int(Ns)
M=int(Ms)
Ls = input().split()
ans=0
L=[]
Lx=[]
for i in range(M):
    L.append(int(Ls[i]))
L.sort()
for i in range(M-1):
    Lx.append(L[i+1]-L[i])
Lx.sort()
if N>=M:
    print(ans)
else:
    for i in range(M-N):
        ans=ans+Lx[i]
    print(ans)

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

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

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

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

Xs = sorted(X)

diff = []
for i in range(M - 1):
    tmp = Xs[i + 1] - Xs[i]
    diff.append(tmp)

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

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

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

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

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

A.sort()
if len(A) > N-1:
    for i in range(N-1): 
        A.pop()
    print(sum(A))
else:
    print(0)

Solution (Python):
import sys

lines = sys.stdin.readlines()
N, M = map(int, lines[0].split())
X = [int(x) for x in lines[1].split()]

X.sort()

diff = []
for i in range(0, len(X) - 1):
	diff.append(abs(X[i] - X[i+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()

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

if M > N:

    difX = difX[N-1:]

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

    print(total)
else:
    print(0)

Solution (Python):
import numpy as np

N,M = map(int,input().split())
Xs = sorted([int(i) for i in input().split()])

if N >= M:
    print(0)
elif N == 1:
    print(Xs[-1]-Xs[0])
else:
    margin = [Xs[i+1]-Xs[i] for i in range(len(Xs)-1)]
    margin = [(i,x) for i,x in enumerate(margin)]
    point = [x[0] for x in sorted(margin,key=lambda x:x[1],reverse=True)[:N-1]]
    move = 0
    move += Xs[point[0]] - Xs[0]
    for i in range(1,len(point)):
        move += Xs[point[i]] - Xs[point[i-1]+1]
    move += Xs[-1] - Xs[point[-1]+1]
    print(move)

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

if(m-n<=0):
	print(0)
	exit()



y=np.array(x)
dy = y[1:]-y[:-1]

dy.sort()
dy=dy[::-1]

print(sum(dy[n-1:]))

"""
mi = min(dy)
ma = max(dy)
dy = np.append(dy,[ma+1])
print(dy)
for dif in range(mi,ma+1):
	if(len(dy[dy>dif]) <= n):
		lis = np.where(dy>dif)[0]
		print(dif)
		print(lis)
		s=sum(dy[0:lis[0]])
		print(s)
		for i in range(len(lis)-1):
			idx = lis[i]+1
			idx2 = lis[i+1]
			s += (sum(dy[idx:idx2]))
		break
print(s)




"""



"""
dp[i][j]	jまでの部分配列をi回分割した時の和の最小値

dp[0][j] = sum(dy[:j])
dp[i][j](i>=j) = NAN
dp[i][j](i=j-1) = 0
dp[i][j](i<j) = 
"""

Solution (Python):
n,m=(int(i) for i in input().split())
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-1-(n-1)):
  ans+=y[i]
  
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n_dices, n_points = map(int, input().split())
points = sorted(list(map(int, input().split())))
if n_dices >= n_points:
    print(0)
    sys.exit()
interval_list = [points[i + 1] - points[i] for i in range(n_points - 1)]
interval_list.sort(reverse = True)
print(sum(interval_list[n_dices - 1:]))

Solution (Python):


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

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



D = []

R = 0

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




if D !=[]:
	D = sorted(D)
	D = D[::-1]
	D = D[N-1:]


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


print(R)

Solution (Python):
n,m = map(int,input().split())
list_x = list(map(int,input().split()))
list_sort = sorted(list_x)
if n >= m:
  print(0)
else:
  a = m - n
  list_A = []
  for i in range(m - 1):
    t = list_sort[i+1] - list_sort[i]
    list_A.append(t)
  list_B = sorted(list_A)
  s = 0
  for j in range(a):
    s += list_B[j]
  print(s)

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

if n >= m:
    print(0)
else:
    x.sort()
    y = [x[i+1]-x[i] for i in range(m-1)]
    z = sorted(y, reverse=True)
    a = []
    cnt = 0
    for i in range(n-1):
        dif = z.pop(0)
        idx = y.index(dif)
        a.append(idx)
    a.sort()
    x0 = x[0]
    for ai in a:
        cnt += x[ai] - x0
        x0 = x[ai+1]
    cnt += x[-1] - x0
    print(cnt)

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

lx.sort()
dx = []
s = 0

for i in range(len(lx)-1):
  dx.append(lx[i+1]-lx[i])

if n == 1:
  print(lx[m-1]-lx[0])
elif m <= n:
  print(0)
else:
  dx.sort()
  for i in range(len(dx)-n+1):
    s = s + dx[i]
  print(s)

Solution (Python):

"""
Created on Sun Feb  3 20:43:36 2019

@author: Owner
"""


"""
Created on Sun Jan 27 20:47:58 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
    
all(nstr.count(c) for c in '753')

ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))


"""

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

X = [0]*M
X[:] = map(int, input().split())

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

X.sort()

if N == 1:
    print(X[M-1]-X[0])
    sys.exit()
else:
    pass

dX = [0]*(M-1)
for m in range(M-1):
    dX[m] = X[m+1] - X[m]
dX = np.array(dX)
dX_rank = dX.argsort()[::-1]

X_div = dX_rank[0:N-1]
X_div.sort()
result =0
cnt = 0
last_idx = 0


for nx in X_div:
    if cnt == 0:
        result = result + X[nx]-X[0]
        last_idx = nx
        cnt =+ 1         
    else:
        result = result + X[nx] - X[last_idx+1]
        last_idx = nx
    

result = result + X[-1] - X[last_idx+1]

print(result)

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

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = map(int, input().split())
x = list(map(int, input().split()))
if n >= m:
    print(0)
    sys.exit()

x2 = sorted(x)
y = []
for i in range(1, m):
    heapq.heappush(y, (-(abs(x2[i] - x2[i - 1]))))
for i in range(n - 1):
    heapq.heappop(y)
print(-(sum(y)))

Solution (Python):
import numpy as np

in_num = list(map(int, input().split()))
in_num2 = list(map(int, input().split()))
n = in_num[0]
m = in_num[1]
x = np.asarray(in_num2)

x = np.sort(x)
xsub = x[1:] - x[:-1] - 1

xsub = np.sort(xsub)[::-1]
xsum = np.max(x) - np.min(x)
xsub = np.sum(xsub[:n-1])
result = xsum + 1 - xsub - n
if result < 0:
    result = 0
print(result)