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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()
X = sorted(LIST())

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

Solution (Python):
N,M= [int(s) for s in input().split()]
lst = [int(s) for s in input().split()]
lst.sort()
gap=[]
for i in range(1,M):
  gap.append(lst[i]-lst[i-1])
gap.sort()
ans=0
for i in range(M-N):
  ans+=gap[i]
print(ans)

Solution (Python):
import math;

def swap(a,b):
    return (b,a)

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

def main():
    n,m = map(int,input().split())
    X = [x for x in map(int,input().split())]
    if n>=m:
        print(0)
        return
    X.sort()
    X_dif = []
    for i in range(1,m):
        X_dif.append([abs(X[i]-X[i-1]),i])
    ans = 0
    X_dif.sort()
    X_dif.reverse()
    index = [0]
    for i in range(n-1):
        index.append(X_dif[i][1])
    for i in range(1,n):
        ans += X[index[i]-1] - X[index[i-1]]
    ans += X[-1]-X[index[-1]]
    print(ans)
    return

if __name__ == '__main__':
    main()

Solution (Python):
n, m, *x = map(int, open(0).read().split())
x.sort()
d = [i-j for i, j in zip(x[1:], x)]
d.sort()
print(sum(d[:m-n]) if n < m else 0)

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

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

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

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

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

Solution (Python):
import sys
N,M = map(int,input().split())
if N>M:
    print(0)
    sys.exit()
Xs = sorted(list(map(int,input().split())))
ranges = sorted([abs(Xs[i]-Xs[i-1]) for i in range(1,len(Xs))],reverse=True)
ans = Xs[M-1]-Xs[0]
for n in range(N-1):
    ans-=ranges[n]
print(ans)

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

Solution (Python):
N,M = map(int,input().split())
X = [int(x) for x in input().split()]
X.sort()
length = [x - X[i] for i,x in enumerate(X[1:])]
length.sort(reverse=True)
print(sum(length[N-1:]))

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

    if N >= M: return 0
    
    L = []
    for i in range(M-1):
        L.append(X[i+1] - X[i])
    L = sorted(L)
    return sum(L[:M-N])

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

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

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

X.sort(reverse=True)

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

diff.sort()

for _ in range(N-1):
    diff.pop()

print(sum(diff))

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

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

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

if N >= M:
  print(0);
else:
  diff = xList[M - 1] - xList[1];

  diffList = [];
  prevX = xList[0];
  for i in range(1, M):
      x = xList[i];
      diffList.append(x - prevX);
      prevX = x;
  diffList.sort();

  for i in range(N - 1):
      diffList.pop();

  length = len(diffList);
  result = 0;
  for i in range(length):
      result += diffList[i];

  print(result);

Solution (Python):
n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
Dis = [0]*(m-1)

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

for i in range(m-1):
    diff = abs(X[i]-X[i+1])
    Dis[i] = diff

Dis.sort()

for i in range(n-1):
    Dis.pop()

print(sum(Dis))

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

    print(sum(Xdsl))

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



N,M = II()
X = III()

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

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

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

Solution (Python):

import sys


def solve(N: int, M: int, X: "List[int]"):
    if(N >= M):
        print(0)
        exit()
    X.sort()
    L = [X[i+1] - X[i] for i in range(M-1)]
    L.sort(reverse=True)
    ans = sum([L[i] for i in range(N-1)])
    print(X[M-1]-X[0] - ans)

    return



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

if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()
if n>=m:
  print(0)
elif m!=1:

  d=[L[i+1]-L[i] for i in range(m-1)]
  d.sort()
  d.reverse()
  ans=L[-1]-L[0]


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

else:
  print(0)

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

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

Solution (Python):
import sys


INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)


def main(args):
    N, M = parse(args, int, int)
    X = tuple(map(int, next(args).split()))

    x = sorted(X)
    offset_x = [x[n] - x[n - 1] for n in range(1, M)]
    offset_x.sort(reverse=True)

    debug(offset_x)
    print(sum(offset_x[N - 1:]))

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

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

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

Solution (Python):


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

x = sorted(x)

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

dist = sorted(dist, reverse=True)

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

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

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


if __name__ == "__main__":
    print(solve())

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

if N >= M:
  print(0)
else:
  interval = [0] * (M-1)
  for i in range(M-1):
    interval[i] = Xs[i+1] - Xs[i]
  interval.sort(reverse = True)
  
  answer = Xs[M-1] - Xs[0]
  for i in range(N-1):
    answer -= interval[i]
  print(answer)

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

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

X.sort()

buff = []

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


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

for i in range(min(N-1, len(buff))):
    buff.pop(0)

print(sum(buff))

Solution (Python):

import bisect
import collections
import itertools


def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,m=getints()
xs=getints()
xs=list(sorted(xs))

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

spaces = list(sorted(spaces))

for i in range(n-1):
    if spaces: spaces.pop()

print(sum(spaces))

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

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

if n >= m:
    print(0)
else:
    x.sort()
    interval = [0]*(m-1)
    for i in range(m-1):
        interval[i] = x[i+1] - x[i]
    interval.sort(reverse = True)
    answer = x[m-1] - x[0]
    for i in range(min(m,n-1)):
        answer -= interval[i]
    print(answer)

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

if N >= M :
	print(0)
	exit()
	
darr = [Xs[i+1] - Xs[i]for i in range(M-1)]


heapq.heapify(darr)
res = 0

for _ in range(M - N):
	res += heapq.heappop(darr)
	
print(res)

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

Solution (Python):
import heapq
n, m = map(int, input().split())
if n >= m:
    print(0)
else:
    x = list(map(int, input().split()))
    x.sort()
    d = []
    heapq.heapify(d)
    for i in range(1, m):
        heapq.heappush(d, [-abs(x[i] - x[i-1]), i, i - 1])
    total = 0
    a = [0, m - 1]
    for i in range(n-1):
        p = heapq.heappop(d)
        a.append(p[1])
        a.append(p[2])
    a.sort()
    total = 0
    for i in range(1, len(a), 2):
        total += abs(x[a[i]] - x[a[i-1]])
    print(total)

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])
fin_diff=sorted(diff, reverse=True)

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

Solution (Python):
import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

if m == 1:
    print(0)
    sys.exit()


list.sort(x)


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


a.sort()


count = max(x) - min(x)
for i in range(n-1):
    if len(a) == 0:
        break
    a.pop()



print(sum(a))

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

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

ans = sum(xx)
print(ans)

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

if N >= M:
  print(0)
elif N == 1:
  ans = max(X) - min(X)
  print(ans)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X 

  DX = [] 
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] 
    DX.append(dx)
  
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) 
  I = [] 
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  
  
  
  move = X[I[0]] - 0
  
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  
  
  
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

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

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

  y_sorted = sorted(y, reverse=True)

  ans = 0
  for _y in y_sorted[n-1:]:
    ans += _y
  print(ans)

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

ans = 0

if N < M:
    XList = sorted(XList)
    XXList = [0] * (M-1)
    for i in range(M-1):
        XXList[i] = XList[i+1] - XList[i]
    XXList = sorted(XXList)
    for i in range(M-N):
        ans += XXList[i]

print(ans)

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

Solution (Python):


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

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

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

Solution (Python):
import math
import sys
import itertools
import queue
from fractions import gcd

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

mod = 1000000007

"""

a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 



    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())


    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  
"""



if __name__ == "__main__":
    N, M = map(int, sys.stdin.readline().split())
    if N >= M:
        print(0)
        exit()

    X = list(map(int, sys.stdin.readline().split()))
    X = sorted(X)
    length = max(X) - min(X)
    distance = []
    for i in range(len(X) - 1):
        distance.append(abs(X[i] - X[i + 1]))
    distance = sorted(distance, reverse=True)

    for i in range(N - 1):
        length -= distance[i]

    print(length)

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

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

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

X.sort()
Distance = [0] * (M-1)

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

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

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

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

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

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

diff = sorted([abs(x[i] - x[i-1]) for i in range(1, m)])
for i in range(n-1):
    diff.pop()

print(sum(diff))

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

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

xdelta.sort()

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

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

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

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd
from itertools import accumulate
from functools import reduce


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

mod = 1000000007

if __name__ == "__main__":
    n,m = map(int, sys.stdin.readline().split())
    x = list(map(int, sys.stdin.readline().split()))
    ans=0
    if n > m:
        print(0)
    else:
        l=[]
        x.sort()

        for i in range(m-1):
            l.append(x[i+1]-x[i])
        l.sort()
        for i in range(m-n):
            ans += l[i]
        print(ans)

Solution (Python):
def iread():
    return int(input())


def sread():
    return input()


def aread_int():
    tmp = input().split()
    ret = [int(i) for i in tmp]
    return ret


def aread_str():
    return input().split()

if __name__ == '__main__':
    n, m = map(int, input().split())
    
    x = aread_int()
    x = sorted(x)
    x_dist = [0] * (m - 1)
    for i in range(m - 1) :
        x_dist[i] += abs(x[i] - x[i + 1])
    
    if n > m :
        print(0)
        exit()
    
    x_dist = sorted(x_dist)
    idx = len(x_dist) - 1
    cost = sum(x_dist)
    for i in range(n - 1) :
        cost -= x_dist[idx]
        x_dist[idx] = 0
        idx -= 1

    print(cost)

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

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

if N >= M:
    res = 0
elif N == 1:
    res = max(x) - min(x)
else:
    res = sum(steps[:-N + 1])
print(res)

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

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

x = list(map(int, input().split()))
x = sorted(x)
diff = [x[i+1] - x[i] for i in range(0, m-1)]

sort_diff = sorted(diff, reverse=True)

sum = 0

if len(sort_diff) == 0:
    print(0)
    exit()

for i in range(n-1):
    sort_diff.pop(0)

for i in range(len(sort_diff)):
    sum += sort_diff[i]
print(sum)

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

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

x = sorted(x)
sub = [x[i+1] - x[i] for i in range(len(x) - 1)]
sub = sorted(sub)
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()

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

dist.sort()

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

print(ans)

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

if n == 1:
  print(xs[-1] - xs[0])
  exit()

diff = [(i, b-a) for i, (a, b) in enumerate(zip(xs, xs[1:]))]
diff.sort(key=lambda x: x[1])

start_indexes = [0] + list(map(lambda x:x[0] + 1, diff[-n+1:]))
start_indexes.sort()

steps = 0
for i_start, i_end in zip(start_indexes, start_indexes[1:] + [m]):
    steps += xs[i_end - 1] - xs[i_start]
print(steps)

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

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

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

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

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

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

    big_ds = 0
    for j in range(n-1):
        big_ds += l_d[j]

    print(sum(l_d)-big_ds)

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

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

for i in range(1,m):
    dst.append(lst[i]-lst[i-1])
dst.sort()
if n==1:
    ans = sum(dst)
else:
    ans = sum(dst[:-n+1])
print(ans)

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

if M <= N:
    ans = 0
elif N == 1:
    ans = max(nums) - min(nums)
else:
    nums.sort()
    diffNums = [nums[i+1] - nums[i] for i in range(M-1)]
    diffNums.sort()
    ans = sum(diffNums[:-(N-1)])

print(ans)

Solution (Python):
N,M = map(int,input().split())
X=sorted(map(int, input().split()))
if N==1:
  print(X[M-1]-X[0])
elif N>=M:
  print(0)
else:
  print(sum(sorted([i-j for i,j in zip(X[1:],X)],reverse=True)[N-1:]))

Solution (Python):
N,M = map(int,input().split())
X=sorted([int(i) for i in input().split()])
if N==1:
  print(X[M-1]-X[0])
elif N>=M:
  print(0)
else:
  print(sum(sorted([i-j for i,j in zip(X[1:],X)],reverse=True)[N-1:]))

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

if N==1:
  print(X[M-1]-X[0])
elif N>=M:
  print(0)
else:
  Y=list()
  [Y.append(i-j) for i,j in zip(X[1:],X)] 
  Y.sort(reverse=True)
  print(sum(Y[N-1:]))

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

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

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

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

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

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

Y.sort()

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

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

Y=list()
for i in range(M)[1:]:
  diff = X[i]-X[i-1]
  Y.append(diff)

Y=sorted(Y)

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

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

if M <= N:
    ans = 0
elif N == 1:
    ans = max(X) - min(X)
else:
    X.sort()
    List = []
    for i in range(M-1):
        List.append(X[i+1]-X[i])
    List.sort()
    ans = 0
    for j in range(M-N):
        ans += List[j]

print(ans)

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

ps=sorted(int(i) for i in input().split())

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

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

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

  if N>= 2:
    for i in range(N-1):
      ans -= subX[-i-1]

  print(ans)

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

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

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

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


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

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

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

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

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

sections = [abs(XXX[i] - XXX[i + 1]) for i in range(M) if i < M - 1]
sections.sort(reverse=True)
not_arrived = sum(sections[:N-1])

print((XXX[-1] - XXX[0]) - not_arrived)

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

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

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

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

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

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

Solution (Python):

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

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

x.sort()

x_dif = []

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

if m <= n:
    print(0)
else:
    print(sum(x_dif[:m-n]))

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

x.sort()
diffs = [0]
for i in range(len(x) - 1):
    diffs.append(abs(x[i] - x[i + 1]))
diffs.sort()
for i in range(n - 1):
    if len(diffs) <= 0:
        break
    diffs.pop()
print(sum(diffs))

Solution (Python):
N, M = map(int, input().split())
X = map(int, input().split())
X = sorted(X)
V = sorted(y - x for x, y in zip(X[:-1], X[1:]))
print(max(X)-min(X)-sum(V[max(0, len(V)-N+1):]))

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

x.sort()
diffs = [0]
for i in range(len(x) - 1):
    diffs.append(abs(x[i] - x[i + 1]))
diffs.sort()
for i in range(n - 1):
    if len(diffs) < 2:
        break
    diffs.pop()
print(sum(diffs))

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

l.sort()
if m-n>=0:
    ans=sum(l[0: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.sort()

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

y.sort(reverse=True)

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

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

Solution (Python):


import sys

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n=hoge[0]
m=hoge[1]

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

hoge.sort()

x=[]
sum=0
for i in range(0,m-1):
	x.append(hoge[i+1]-hoge[i])
	sum+=hoge[i+1]-hoge[i]
x.sort(reverse=True)



sc=0
for i in range(0,min(n-1,len(x))):
	sc+=x[i]

print(sum-sc)