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 = list(map(int,(input().split())))
X.sort()
if M > N:
    dist = [X[i+1]-X[i] for i in range(M - 1)]
    dist.sort(reverse = True)
    del dist[0:N-1]
    print(sum(dist))
else:
 print(0)

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

Solution (Python):
import math
import sys
from heapq import heappush, heappop


def solution(arr, moves):
    if len(arr) <= moves:
        return 0

    arr.sort()
    if moves == 1:
        return abs(arr[0] - arr[-1])

    heapq_max = []
    
    

    for i in range(0, len(arr) - 1):
        heappush(heapq_max, (-abs(arr[i] - arr[i + 1] ), i , i + 1))

    placed = [0]
    for _ in range(moves - 1):
        val = heappop(heapq_max)
        placed.append(val[2])

    placed.sort()
    counter = 0
    for i in range(len(placed) - 1):
        if (abs(placed[i] -  placed[i + 1]) > 1):
            counter += abs(arr[placed[i]] - arr[placed[i + 1] - 1])
    if placed[-1] != len(arr) - 1:
        counter += abs(arr[placed[-1]] - arr[-1])
    return counter

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

for i,line in enumerate(sys.stdin):
    if i == 0:
       M, N = line.split(" ")
    if i == 1:
        A = [int(i) for i in line.split(" ")]
        print(solution(A, int(M)))

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

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

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

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

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

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

X.sort() 

if N>=M:
  print(0)

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

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

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

Solution (Python):

import sys
N, M = map(int, input().split())
if N >= M:
    print(0)
    sys.exit()
li = list(map(int, input().split()))
li.sort()
diff_list = []
for i in range(M - 1):
    diff_list.append(li[i + 1] - li[i])

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

print(ans)

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

Solution (Python):
s1=[int(i) for i in input().split()]
N=s1[0]
M=s1[1]
s2=[int(i) for i in input().split()]
a=sorted(s2)


sabun=[]
for i in range(len(a)-1):
  sabun.append(a[i+1]-a[i])

s_sabun=sorted(sabun)

trysum=0
for i in range(M-N):
  trysum=trysum+s_sabun[i]

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

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

def main():
    if N >= M:
        print(0)
        return
    XList.sort()
    dX = [XList[i+1]-XList[i] for i in range(len(XList)-1)]
    dX.sort(reverse=True)
    ans = XList[-1]-XList[0]-sum(dX[:N-1])
    print(ans)

if __name__ == '__main__':
    main()

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

d = []

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

d.sort(reverse=True)

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

Solution (Python):



def main():
    N = list(map(int, (input().strip().split(' '))))
    M = N[1]
    N = N[0]
    X = list(map(lambda xi: int(xi), input().strip().split(' ')))
    X = sorted(X)

    if False and N >= M: 
        min_steps = 0
    else:
        L = list()
        i = 0
        while i+1 < len(X):
            L.append(X[i+1]-X[i])
            i += 1
        L = sorted(L, reverse=True)
        min_steps = X[-1] - X[0] - sum(L[0:N-1])

    print(min_steps)

if __name__ == "__main__":
    main()

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

if m <= n:
    print(0)
else:
    points.sort()
    dx = [ abs(points[i]-points[i+1]) for i in range(0,len(points)-1) ]
    dx.sort()
    dx.reverse()
    print(sum(dx[n-1:]))

Solution (Python):
s = input().split()
n, m = map(int, s)
if n >= m:
    print(0)
    exit(0)
x = input().split()
x = [int(i) for i in x]
x.sort()

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

dist = dist[:m-n]
print(sum(dist))

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

if n < m:
    x.sort()
    d = [x[i] - x[i - 1] for i in range(1, len(x))]
    d.sort()

    sum = 0
    for i in range(0, m - n):
        sum += d[i]
    print(sum)
else:
    print(0)

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

Solution (Python):
N, M = map(int, input().split())
x = list(map(int, input().split()))
x = sorted(x)
y = [xb - xa for xa, xb in zip(x, x[1:])]
y = sorted(y, reverse=True)
print(sum(y) - sum(y[:N - 1]))

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

Xs.sort()
L=[]
for i in range(M-1):
  L.append(Xs[i+1]-Xs[i])
  
L.sort()
result = sum(L[:1-N]) if N>1 else sum(L)

print(result)

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

X = sorted(X)
Sub = []

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

Sub = sorted(Sub, reverse = True )

answer = X[-1] - X[0]

for x in Sub[:N-1] :
    answer -= x

print(answer)

Solution (Python):

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

aida = []
for i in range(len(x)-1):
    a = x[i]
    b = x[i + 1]
    sa = b - a
    aida.append(sa)

if n == 1:
    print(x[-1]-x[0])
else:
    sum_ = sum(sorted(aida)[:-n+1])
    print(sum_)

Solution (Python):

N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    X.sort()
    new_list = []
    for j in range(1, M):
        new_list.append(abs(X[j] - X[j-1]))
    new_list.sort()
    new_list.reverse()
    
    for i in range(M-N):
        new_list.pop()
    ans=(max(X)-min(X))-sum(new_list)
    
    print(ans)

Solution (Python):
def game(n, m, x_list):
  if n >= m:
    return 0
  
  x_list.sort()
  interval = []
  for i in range(m - 1):
    interval.append(x_list[i + 1] - x_list[i])
  interval.sort(reverse = True)
  sum_interval = 0
  for i in range(n - 1):
    sum_interval += interval[i]
  return x_list[m - 1] - x_list[0] - sum_interval

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

print(game(n, m, x))

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

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

if(N>=M):
    print(0)
else:
    X_list.sort()
    L_list=list()
    
    for i in range(M-1):
        L_list.append(abs(X_list[i+1]-X_list[i]))
    
    L_list.sort()
    L_list.reverse()
    
    for i in range(M-N):
        L_list.pop()
        
    ans=(max(X_list)-min(X_list))-sum(L_list)
    
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

def main():
	n, m = map(int, input().split())
	x=list(map(int, input().split()))
	x.sort()
	a = []
	s = 0
	for i in range(1,m):
		a += [x[i]-x[i-1]]
		s += a[-1]
	a.sort()
	a=a[::-1]
	print(0 if n >= m else s - sum(a[0:n-1]))

main()

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

Solution (Python):
def find_min_moves_num(N, X):
        X.sort()
        D = []
        for i in range(len(X) - 1):
                D.append(X[i + 1] - X[i])
        D.sort()
        min_moves = 0
        for i in range(len(D) - (N - 1)):
                min_moves += D[i]
        return min_moves


N, _ = [int(v) for v in input().split()]
X = [int(v) for v in input().split()]
print(find_min_moves_num(N, X))

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

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

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

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

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

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

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

X.sort()
Xdif = []

for i in range(M-1):
    if i != M-1: Xdif.append( X[i+1]-X[i] )
Xdif.sort(reverse=True)

if N >= M: print(0)
else:
    ans = max(X) - min(X) 
    for i in range(N-1):
        ans -= Xdif[i]
    print(ans)

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

Solution (Python):
import numpy as np
n,m = map(int,input().split())
if m-n<=0:
  print(0)
  exit()

data = [int(i) for i in input().split()]
data.sort()
data1 = data.copy()
data1.insert(0,0)
data1= np.array(data1[0:-1])
data = np.sort((np.array(data)-data1)[1:])
print(data[:m-n].sum())

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

if n >= m:
    print(0)
else:
    for i in range(len(locations) - 1):
        distance.append(locations[i+1] - locations[i])
    
    distance = sorted(distance)

    if n != 1:
        for i in range(n-1):
            distance.pop()
    
    print(sum(distance))

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

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


print(sum(Y))

Solution (Python):
N, M= map(int, input().split())
zahyo = []
for i in map(int,input().split()):
    zahyo += [i]
sum=0


def merge(p,q):
    result=[]
    i,j=0,0
    
    while i<len(p) and j<len(q):
        
        
        if p[i] <= q[j]:
            result.append(p[i])
            i+=1
        else:
            result.append(q[j])
            j+=1

    
    
    
    if i<len(p):
        result.extend(p[i:])
    if j<len(q):
        result.extend(q[j:])
    
    return result


def mergesort(a):
    
    if len(a)==1:
        return a
    
    p=mergesort(a[:len(a)//2])
    
    q=mergesort(a[len(a)//2:])
    
    return merge(p,q)

if M==1:
    print(0)
else:
    zahyo = mergesort(zahyo)
    gap = []
    for i in range(M-1):
        gap += [zahyo[i+1]-zahyo[i]] 
    gap = mergesort(gap)
    if N >= M:
        print(0)
    else:
        for i in gap[:M-N]:
            sum += i
        print(sum)

Solution (Python):

(N,M) = list(map(int,input().split()))
xlis = list(map(int,input().split()))
if N >= M:
    print("0")
else:
    xlis.sort()
    dist_lis = []
    for i in range(1,len(xlis)):
        dist_lis.append(xlis[i] - xlis[i-1])
    dist_lis.sort()

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

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

l.sort()
d = l[len(l)-1] - l[0]
distance = []
sum = 0

for j in range(len(l)-1):
    distance.append( l[j+1] - l[j] )

distance.sort(reverse=True)

if n >= m:
    print("0")
else:
    for i in range(n-1):
        sum += distance[i]
    print(d-sum)

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


if N > M:
    N = M

if N == M:
    print("0")

else:
    for i in range(len(xList)-1):
        distanse = xList[i+1] - xList[i]
        distanse_list.append(distanse)
 
    distanse_list.sort()
    print(sum(distanse_list[0:int(M - N)]))

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

X.sort()

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

for i in range(N - 1):
  if T == []:
    break
  del T[-1]
  
Ans = 0
for i in T:
  Ans += i

print(Ans)

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

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

distanse_list = []
if N > M:
    N = M

if N == M:
    print("0")
else:
    for i in range(len(xList)-1):
        distanse = xList[i+1] - xList[i]
        distanse_list.append(distanse)

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

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

x = sorted(x)
diff = np.asarray([x[i + 1] - x[i] for i in range(len(x) - 1)])
argsort = diff.argsort()[::-1][:n - 1]

start = 0
sum_length = 0
for split_idx in argsort:
    sum_length += x[split_idx] - x[start]
    start = split_idx + 1
sum_length += x[-1] - x[start]

print(sum_length)

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
s = sorted([x[i+1]-x[i] for i in range(m-1)])

if(n>m):
    print("0")
else:
    print(sum(s[:m-n]))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, 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)
    print(X[M-1]-X[0]-sum(L[:N-1]))

Solution (Python):
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

if M != 1:
    for i in range(0,M-1):
        tousa.append(abs(Xls[i+1] - Xls[i]))




tousa.sort()


if N<M:
    for i in range(0,N-1):
        tousa.pop()
else:
    print(0)
    exit()
ret = 0

for i in tousa:
    ret = i + ret

print(ret)

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()
L = []
for i in range(m-1):
  L.append(X[i+1] - X[i])

L.sort(reverse=True)


print((X[m-1]-X[0]) - (sum(L[0:n-1])))

Solution (Python):
import numpy as np

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

if N >= M:
    c = 0
else:
    X = np.array(sorted(X))
    dist = np.sort(X[1:] - X[:-1])
    c = np.sum(dist[:M-N])

print(c)

Solution (Python):



import copy

n, m = map(int, input().split())
ary = [int(s) for s in input().split()]

if n < m :
    ary.sort()
    sa = []
    
    for i in range(0, m - 1):
        sa.append(ary[i + 1] - ary[i])
    
    
    
    sa.sort()
    
    for i in range(0, n - 1):
        sa.pop(-1)
    
    print(sum(sa))
else :
    print(0)

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

Solution (Python):
n, m = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()
b = [a[i+1] - a[i] for i in range(len(a) - 1)]
b.sort()
if n == 1:
  print(sum(b))
elif len(b) <= n-1:
  print(0)
else:
  print(sum(b[0:-n+1]))

Solution (Python):


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

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

xs.sort()

ls = [xs[i] - xs[i - 1] for i in range(1, len(xs))]
ls.sort()

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

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

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

ds.sort()
to = m - n

if to > 0:
    print(sum(ds[0:to]))
else:
    print(0)

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

    x.sort()

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

    L=[0]*(m-1)

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

    L.sort(reverse=True)

    ans=sum(L[n-1:])


    return ans
print(stream())

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

    x.sort()

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

    L=[0]*(m-1)

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

    L.sort(reverse=True)

    ans=sum(L[n-1:])


    return ans
print(stream())

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

Solution (Python):
def calc_sum(width, wall):
    for i in wall:
        width -= Xs[i] - Xs[i-1]
    return width

N, M = map(int, input().split())
Xs = sorted([int(i) for i in input().split()])
diff_ls = [Xs[i+1] - Xs[i] for i in range(M-1)]
max_diff = sorted(diff_ls, reverse=True)[:N-1]
print(Xs[-1] - Xs[0] - sum(max_diff))

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

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

diff.sort(reverse=True)

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

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

Solution (Python):
from sys import stderr, setrecursionlimit, exit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

n, m = getInts()
x = sorted(getInts())

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

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

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

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

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


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

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

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

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

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

x_list = sorted(x_list, reverse = True)
diff_list = [0] * (M - 1)

ans = 0
if M > N:
    for i in range(M-1):
        diff_list[i] = x_list[i] - x_list[i+1]

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

Solution (Python):



def main():
    s1 = input()
    s2 = input()
    ret = solve(s1, s2)
    print(ret)


def solve(s1, s2):
    N, M = list(map(int, s1.split()))
    X = list(map(int, s2.split()))
    if (N >= M):
        return 0
    X.sort()
    l = []
    for i in range(M-1):
        l.append(X[i+1]-X[i])
    l.sort()
    ct = 0
    for j in range(M-N):
        ct += l[j]
    return ct


main()

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

x.sort()

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

sub_array.sort()

for i in range(n-1):
    if (len(sub_array) == 0):
        break
    sub_array.pop(-1)

print (sum(sub_array))

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

Solution (Python):

import sys

N, M = [int(x) for x in input().split()]
nums = list(set([int(x) for x in input().split()]))

if not nums:
    print(0)
    sys.exit()

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

nums.sort()

rank = [(abs(nums[i] - nums[i-1]), (i-1, i)) for i in range(1, len(nums))]
rank = sorted(rank, reverse=True)[:N-1]

pos = [0] + [pos[1] for dis, pos in rank]
pos.sort()
ans = 0
for i in range(len(pos)):
    if i == len(pos)-1:
        ans += abs(nums[pos[i]] - nums[-1])
    else:
        ans += abs(nums[pos[i]] - nums[pos[i+1]-1])

print(ans)

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

if(n>=m):
  print('0')
  exit(0)
l.sort()
 
gap = [l[i+1]-l[i] for i in range(m-1)]
 
gap.sort()

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

Solution (Python):
list1 = list(map(int, input().split()))
N = list1[0]
M = list1[1]
list2 = list(map(int, input().split()))
if list1[0] - list1[1] >= 0:
    print(0)
    exit(0)
list2 = sorted(list2, reverse=True)

list3 = []
for i in range(len(list2) - 1):
    list3.append(list2[i] - list2[i + 1])
list3 = sorted(list3, reverse=True)

sum = 0
for i in range(N - 1):
    sum = sum + list3.pop(0)
ans = list2[0] - list2[-1] - sum
print(ans)

Solution (Python):
N, M = [int(x) for x in input().split(' ')]
XP = [int(x) for x in input().split(' ')]
XP.sort()
D = [0] * (len(XP)-1)
for x in range(len(XP)-1):
    d = XP[x+1] - XP[x]
    D[x] = d
D.sort()
s = 0
for i in range(len(D) - N + 1):
    s += D[i]
print(s)

Solution (Python):
import math
N,num = map(int, input().split())
list = [int(i) for i in input().split()]
if N >= num:
    print(0)
else:
    
    list.sort()
    list2 = []
    for i in range(0,num-1):
        list2.append(abs(list[i+1] - list[i]))
    list2.sort()
    print(sum(list2[:num-N]))

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

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

Solution (Python):
n,m = map(int, input().split())
if n >= m:
  print(0)
else:
  ls = list(map(int, input().split()))
  ls.sort()
  

  difls= []
  for h in range(len(ls)-1):
    difls.append(ls[h+1]-ls[h])
  difls.sort(reverse=True)
  

  lsum=0
  for i in range(n-1):
    lsum += difls[i]
  print(sum(difls)-lsum)

Solution (Python):
n,m=map(int,input().strip().split())
xs=[i for i in map(int,input().strip().split())]
xs.sort()
if n>=m:
    print(0)
else:
    ls=[xs[i+1]-xs[i] for i in range(len(xs)-1)]
    ls.sort()
    ans=xs[-1]-xs[0]
    for i in range(n-1):
        d=ls.pop()
        ans-=d
    print(ans)

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

sum =0
for i in range(0,len(diff)-n+1) :
    sum += diff[i]

print (sum)

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

Solution (Python):
n, m = [int(i) for i in input().split()]
xs = [int(i) for i in input().split()]
xs.sort()
step = 0

if n < m:
    l = sorted([b - a for a, b in zip(xs, xs[1:])])
    step = sum(l[:1-n]) if n > 1 else sum(l)

print(step)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)



def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

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

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

    t = sum(sorted(space, reverse=True)[:N - 1])
    print(length - t)


if __name__ == '__main__':
    main()

Solution (Python):

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

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

Xs.sort()
XsDist = [b - a for a, b in zip(Xs, Xs[1:])]

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

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

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

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

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

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

list_x.sort()
list_d=[]

if m<=n:
    print(0)
else:
    i=0
    while i<=m-2:
        list_d.append(list_x[i+1]-list_x[i])
        i+=1
    list_d.sort()
    print(sum(list_d[0:m-n:1]))

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

if n > m:
    print(0)
    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(" "))
num = input().split(" ")
ans = 0
for i, x in enumerate(num):
    t = int(x)
    num[i] = t
num.sort()
kyori = []
for i in range(M-1):
    x = num[i+1]-num[i]
    kyori.append(x)
kyori.sort()
l = len(kyori)-(N-1)
for i in range(l):
    ans +=kyori[i]
print(ans)

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

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

if N > M:
    print("0")
else:
    print(sum(difX[:M - N]))

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

    A.sort()

    if N >= M:
        print(0)   
        return

    D = M - N

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

    B.sort()

    print(sum(B[:D]))

main()

Solution (Python):
import sys
n = map(int, raw_input().split())
x = map(int, raw_input().split())
if n[0]>n[1]:
    print(0)
else:
    x.sort()
    ma = max(x)
    mi = min(x)
    z=[]
    for y in range(len(x)-1):
        z.append(x[y+1]-x[y])
    z.sort()
    z.reverse()
    z=z[:n[0]-1]
    print(ma-mi-sum(z))

Solution (Python):
NM = [int(x) for x in input().split()]
N = NM[0]
M = NM[1]
X = [int(x) for x in input().split()]
X.sort()

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

Solution (Python):
L = input()
X = input()
L = L.split()
X = X.split()
for i, val in enumerate(L):
    L[i] = int(val)
for i, val in enumerate(X):
    X[i] = int(val)
times = 0
N = L[0]
M = L[1]
if N >= M:
    times = 0
    print(times)
else:
    
    X.sort()
    dis = []
    for i in range(M-1): 
        distance = X[i+1]-X[i]
        dis.append(distance)
    dis.sort()
    times = 0
    for i in range(M-N):
        times += dis[i]
    print(times)