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 = sorted(x)
if m<=n:
    print(0)
else:
    dif = []
    for i in range(m-1):
        dif.append(x[i+1]-x[i])
    dif = sorted(dif)
    print(sum(dif[:m-n]))

Solution (Python):
def calculate(n,m,arr):

    if n >= m:
        print(0)
        return

    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    drr = drr[:(m-n)]
    print(sum(drr))



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

Solution (Python):
def calculate(n,m,arr):
    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    total = 0
    for i in range(m-n):
        total += drr[i]
    print(total)



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

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

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

dis=[x[i+1]-x[i] for i in range(m-1)]
dis.sort()
print(sum(dis[:m-n]))

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









x.sort()
dx=[]
if n>=m:
    print(0)
else:

    for i in range(len(x)-1):
        dx.append(x[i+1]-x[i])
    dx.sort()
    dxsum=sum(dx[0:m-n])
    print(dxsum)

Solution (Python):
import itertools
import math
import sys

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

Solution (Python):

def pin(type=int):
    return map(type,input().split())

"""
入力Pを何個かの塊にわかる
塊の中では1個の駒による操作が行われる、
1回にとなりの1ますに歯科移動できない
すなわちそのなかでの最大と最小の差の絶対値

"""


N,M=pin()
P=sorted(list(pin()))

PP=[abs(P[j+1]-P[j]) for j in range(M-1)]

Q=sorted(PP)

if N>=M:
    print(0)
else:
    for i in range(N-1):
        Q.pop()
    print(sum(Q))

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

Solution (Python):
N, K = map(int, input().split())
L = list(map(int, input().split()))
L.sort()
s = len(L)
L_dif = [[i,L[i+1]-L[i]] for i in range(s-1)]
L_dmax = L_dif.copy()
L_dmax.sort(key=lambda tup: tup[1:], reverse=True)
size = L[K-1]-L[0]
piv = min(K,N)
rem_dif = [L_dmax[i][1] for i in range(piv-1)]
ans = size - sum(rem_dif)
print(ans)

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] = abs(X[i+1] - X[i])

diff.sort()
if N >= M:
    print(0)
else:
    diff = diff[0:M-N]
    print(sum(diff))

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

ans = 0

if not l:
    pass
else:
    ans = sum(l[n-1:])

print(ans)

Solution (Python):
N_koma, M_visit = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
diff = []

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

diff.sort()

if (N_koma >= M_visit):
    print(0)
    exit()

answer = sum(diff[: M_visit - N_koma])

print(answer)

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

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

X.sort()

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

distance = []

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

distance.sort()

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

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

else:
  del x[m:]
  x.sort()
  d = []

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

  d.sort()

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

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

ans=0

if N>=M:
    print(ans)
else:
    Y=Y[::-1]
    ans=sum(Y[N-1:])
    print(ans)

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

Y = sorted(y,reverse = True)

ans = sum(Y)
for i in range(n-1):
  ans = ans - Y[i]
print(ans)

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

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

x.sort()

dis = []

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

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

dis.sort(reverse=True)

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

    diff = sorted([b - a for a, b in zip(X[:-1], X[1:])], reverse=True)
    print(sum(diff[N-1:]))
    
if __name__ == '__main__':
    main()

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

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

x.sort()
d=[abs(x[i]-x[i+1]) for i in range(m-1)]
d.sort(reverse=True)
print(abs(x[m-1]-x[0])-sum([d[i] for i in range(n-1)]))

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

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

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

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

for i in range(len(dis)):
    if i < m - n:
        ans += dis[i]
    else:
        break
print(ans)

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

x = sorted(x)
diffs = []
for i in range(m - 1):
    diffs.append(x[i + 1] - x[i] - 1)
diffs = sorted(diffs, reverse=True)
print(x[-1] - x[0] + 1 - sum(diffs[: n - 1]) - n)

Solution (Python):
N, M = map(int, input().split())
Xlist = sorted(list(map(int, input().split())))
sbn = [abs(Xlist[i] - Xlist[i + 1]) for i in range(M - 1)]
sbn.sort(reverse=True)
print(sum(sbn[N - 1:]))

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

Solution (Python):
f = lambda:map(int,input().split())
n,m = f()
l = sorted(f())
d = sorted(l[i]-l[i-1] for i in range(1,m))
print(sum(d[:max(0,m-n)]))

Solution (Python):
n,m = map(int,input().split())
X = sorted(map(int,input().split()))
D = []

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

print(sum(sorted(D)[::-1][n-1:]))

Solution (Python):
import sys

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

li.sort()
x = []

if N == 1:
  print(abs(li[-1] - li[0]))
  sys.exit()
elif N >= M:
  print(0)
  sys.exit()

for index, i in enumerate(li):
  if index+1 > len(li) - 1:
    break

  x.append(abs(li[index] - li[index+1]))

x.sort(reverse=True)

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


print(sum(x))

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

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

Solution (Python):
n, m = map(int, input().split())
x = sorted(map(int, input().split()))
if n >= m:
  print(0)
else:
  d = [x[i + 1] - x[i] for i in range(m - 1)]
  s = sorted(d, reverse=True)[:n - 1]
  i = [0] + [d.index(si) + 1 for si in s] + [0]
  ans = 0
  for k in range(n):
    ans += x[i[k + 1] - 1] - x[i[k]]
  print(ans)

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

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

L = sorted([x[i+1]-x[i] for i in range(m-1)], reverse=True)
print(sum(L[n-1:]))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
M,N=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
l=[None]*(N-1)
for i in range(N-1):
    l[i]=X[i+1]-X[i]
l.sort()
if N>M:
    print(sum(l[:N-M]))
else:
    print(0)

Solution (Python):
import heapq

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

    for _ in range(n-1):
        heapq.heappop(diff)
    print(-sum(diff))

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

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

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

l=sorted(l)
print(sum(l)-sum(l[-n+1:]) if n!=1 else sum(l))

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
diff=[0]*(M-1)

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

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

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

X = sorted(X)

d = [X[i + 1] - X[i] for i in range(len(X) - 1)]
d = sorted(d)

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

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

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

dis = [x[i+1] - x[i] for i in range(m-1)]
dis = sorted(dis, reverse=True)

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

Solution (Python):
def solve(n, m, X):
    if n >= m:
        return 0
    X.sort()
    distances = sorted(X[i + 1] - X[i] for i in range(0, m - 1))
    return sum(distances[:m - n])

_n, _m = map(int, input().split())
_X = list(map(int, input().split()))
print(solve(_n, _m, _X))

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

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N, M = MAP()
    X = LIST()

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

    X = sorted(X)

    H = []
    heapify(H)
    for i in range(len(X)-1):
        heappush(H, (X[i+1] - X[i])*(-1))

    for i in range(N-1): heappop(H)

    print(sum(H)*(-1))


if __name__ == '__main__':
    main()

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

Solution (Python):
import numpy as np

N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)
 
ans = 0
if K-N >= 0:
  ans = sum(X[:K-N])
print(ans)

Solution (Python):
import numpy as np

N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)

ans = 0
if K-N >= 0:
  ans = sum(X[:K-N])
print(ans)

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

if n >= m:
	print(0)
else:
	sorted_x = sorted(x)
 
	diff_x = []
	diff = 0
	result = 0
 
	for i in range(m-1):
		diff = sorted_x[i+1] - sorted_x[i]
		result += diff
		diff_x.append(diff)

	sorted_diff = sorted(diff_x, reverse=True)[:n-1]
 
	for i in range(n-1):
		result -= sorted_diff[i]
	 
	print(result)

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

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

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

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

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

Solution (Python):

import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N,M = cin.mapInt()
    X = cin.mapInt()
    S = [0] * (M - 1)
    X.sort()
    for i in range(M - 1):
        S[i] = X[i + 1] - X[i]
    ans = sum(S)
    S.sort(reverse = True)
    for i in range(min(N - 1,M - 1)):
        ans -= S[i]
    print(ans)
    return

if __name__ == "__main__":
    main()

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()

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

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

Solution (Python):


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

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

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

ans = positions[-1] - positions[0]
distances = [abs(positions[i] - positions[i-1]) for i in range(1, m)]
distances = sorted(distances)[::-1]

print(ans - sum(distances[:n-1]))

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

diff = []

if n >= m:
    ans = 0
else:

    for i in range(m-1):
     diff.append(l[i+1]-l[i])

    diff = sorted(diff,reverse=True)

    for i in range(n-1):
        diff.pop(0)
    
    ans = sum(diff)
    

print(ans)

Solution (Python):
n,m =map(int, input().split( ))
if n>=m:
    print(0)
    exit()


x = list(map(int, input().split( )))
x.sort()
y=[]

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

tmp = 0
for i in range(n-1):
    tmp += y[-i-1]
print(x[-1]-x[0]-tmp)

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

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

xsort = sorted(x)

if n >= m:
    print("0")
else:
    ans = xsort[m - 1] - xsort[0]
    masum = [0] * m
    for i in range(m - 1):
        masum[i] = xsort[i + 1] - xsort[i]
    sum = sorted(masum)
    for i in range(n - 1):
        ans -= sum[len(sum) - 1 - i]
    print(ans)

Solution (Python):
import sys

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

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

X.sort()

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

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

Solution (Python):
n, m = map(int, input().split())
if n >= m:
    print(0)
    exit()

a = list(map(int, input().split()))
a.sort()
mark = []
d = []
for i in range(m-1):
    d.append(a[i+1] - a[i])
d.sort()
ans = sum(d)

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

print(ans)

Solution (Python):
from heapq import heappush, heappop


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

X.sort()

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

q1 = []

for i in range(M - 1):
    sec = X[i + 1] - X[i]
    heappush(q1, -sec)

ans = 0
for _ in range(N - 1):
    ans -= heappop(q1)

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

Solution (Python):
from heapq import heappush, heappop


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

X.sort()

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

q1 = []

for i in range(M - 1):
    sec = X[i + 1] - X[i]
    heappush(q1, (-sec, i + 1))

q2 = []
for n in range(N - 1):
    sec, idx = heappop(q1)
    heappush(q2, idx)

heappush(q2, M)
cur = 0
ans = 0
for n in range(N):
    idx = heappop(q2)
    ans += X[idx - 1] - X[cur]
    cur = idx

print(ans)

Solution (Python):
def main():
    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(reverse=True)
    print(X[-1] - X[0] - sum(dist[:N - 1]))


if __name__ == '__main__':
    main()

Solution (Python):

import math
import heapq
import bisect
import numpy as np


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

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

print(ans)

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

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

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

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

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

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

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

ans = 0
if N < M:
    Xm = sorted(Xm)
    Ym = [0] * (M-1)

    for i in range(1, M):
        Ym[i-1] = abs(Xm[i] - Xm[i-1])

    Ym = sorted(Ym, reverse=True)
    for i in range(N-1):
        Ym[i] = 0

    ans = sum(Ym)

print(ans)

Solution (Python):
n, m = map(int, input().split())
x = sorted(set(map(int, input().split())))
t = []
for i, j in zip(x, x[1:]):
    t += [abs(i - j)]
t = sorted(t, reverse=True)
print(sum(t[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()
nas = lambda: stdin.readline().split()

n, m = na()
x = na()

x.sort()
d = [x[i] - x[i - 1] for i in range(1, m)]
d.sort()
if n >= m:
    print(0)
else:
    print(sum(d[:m - n]))

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

Solution (Python):
N, M = [int(n) for n in input().split()]
X = sorted([int(n) for n in input().split()])
if N>=M:
    print(0)
else:
    dx = []
    for j in range(M-1):
        dx.append(X[j+1]-X[j])
    ans = sorted(dx, reverse=True)
    print(sum(ans[N-1:]))

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

if M <= N:
  print(0)
  exit(0)
  
X.sort()
intervals = []
for i in range(M-1):
  intervals.append(X[i+1] - X[i])
  
intervals.sort()
R = max(X) - min(X)
for _ in range(N-1):
  R -= intervals.pop()
  
print(R)

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

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

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

Solution (Python):
import sys

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

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

    X = sorted(X)

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

    kukan = sorted(kukan, reverse=True)

    jogai = 0
    for j in range(N-1):
        jogai += kukan[j]

    print(X[M-1] - X[0] - jogai)

main()

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

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

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

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

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

    l=sorted(l,reverse=True)
    ans=sum(l)-sum(l[:n-1])
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N, M = LI()
    X = LI()
    X = sorted(X)
    diff = []
    for i in range(M-1):
        d = X[i+1] - X[i]
        diff.append(d)
    diff = sorted(diff)
    while N - 1> 0 and len(diff) > 0:
        diff.pop()
        N -= 1
    ans = sum(diff)
    print(ans)
    '''
    X = sorted(X)
    min_dist = [0] * M
    if N == 1:
        return X[-1] - X[0]
    print(X)
    for i in range(1, M-1):
        min_dist[i] = min(abs(X[i] - X[i-1]), abs(X[i+1] - X[i]))
        print(min_dist)
    min_dist = sorted(min_dist)[::-1]
    print(min_dist)
    for i in range(N-2):
        if len(min_dist) > 0:
            min_dist.pop(-1)
    print(sum(min_dist))
    '''
main()

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

sm = x[-1] - x[0]
acc = 0
if n >= m:
    print(0)
    exit()
print(sm - sum(diff[:n-1]))

Solution (Python):
import sys

n, m, *x = map(int, sys.stdin.read().split())

def main():
    x.sort()
    s = x[-1] - x[0]
    d = [0] + sorted([x[i+1] - x[i] for i in range(m-1)], reverse=True)
    for i in range(m-1):
        d[i+1] += d[i]
    
    return s - d[min(n, m)-1]

if __name__ == '__main__':
    ans = main()
    print(ans)

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

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

    ans = 0
    for i in range(m-n):
        ans += dis[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()
    diff = [X[i] - X[i-1] for i in range(1,M)]
    diff.sort()
    print(sum(diff[:M-N]))

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

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

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

intervals.sort()
result=x[len(x)-1]-x[0]
for i in range(n-1):
    result-=intervals.pop()
print(result)

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

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

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

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

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

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

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


def main():
    N, M = [int(x) for x in input().split()]
    X = [int(x) for x in input().split()]
    X.sort()
    Y = []
    for i in range(M - 1):
        Y.append(X[i + 1] - X[i])
    Y.sort()

    i = 0
    while Y and i < N - 1:
        i += 1
        Y.pop()
    print(sum(Y))










if __name__ == '__main__':
    main()

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

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [xn - x for xn, x in zip(X[1:], X[:-1])] 
    
    
    x_diff.sort(reverse=True)
    ans = sum(x_diff[N-1:])

print(ans)

Solution (Python):
import numpy as np
import sys

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

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

x_diff = np.diff(x)
x_diff_sort = sorted(x_diff)[::-1]

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


print(ans)

Solution (Python):
n,m=map(int,input().split())
x=[int(i) for i in input().split()]
if n>=m:
  print(0)
else:
  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())
A=list(map(int,input().split()))
B=sorted(A)
if N>=M:
  print(0)
else:
  a=M-N
  C=[0]*(M-1)
  for i in range(M-1):
    C[i]=B[i+1]-B[i]
  D=sorted(C)
  ans=sum(D[:a])
  print(ans)

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

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()
if n >= m:
    print(0)
else:
    ans = 0
    for i in range(m - n):
        ans += y[i]
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,M=map(int,input().split())
    X=list(map(int,input().split()))
    X.sort()
    dif=[0]*(M-1)
    for i in range(M-1):
        dif[i]=X[i+1]-X[i]
    dif.sort(reverse=True)
    ans=sum(dif[N-1:])
    print(ans)

if __name__ == '__main__':
    main()