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

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

Solution (Python):
N, M = map(int,input().split())
X = list(map(int,input().split()))
if N >= M:
    print(0)
    exit()

X.sort()

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

L.sort(reverse=True)

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

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

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

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
MOD = 10**9+7

n,m = MAP()
x = MAP()
x.sort()

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

ll = []
for i in range(m-1):
    ll.append(x[i+1]-x[i])
ll.sort()
print(sum(ll[0:max(0, m - n)]))

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

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

Solution (Python):
import numpy as np

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

X_sorted = np.sort(X)[::-1]
X_diff = X_sorted[:-1] - X_sorted[1:]
X_diff_sorted = np.sort(X_diff)[::-1]

print(X_sorted[0] - X_sorted[-1] - X_diff_sorted[:N-1].sum())

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

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

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

Solution (Python):
from sys import stdin
import numpy as np

N,M = map(int,stdin.readline().strip().split())
X = np.array(list(map(int,stdin.readline().strip().split())))
X.sort()
dX = np.diff(X,n=1)
dX.sort()

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

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

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

xl = sorted(xl)

aidal = []
before_x = xl[0]
for idx,x in enumerate(xl):
    aida = x - before_x
    aidal.append((aida,idx))
    before_x = x
    
aidal = sorted(aidal,reverse=True)
aidal.pop()
ans = 0
before_x = xl[0]
new = sorted([i[1] for i in sorted(aidal[:n-1])])
for idx in new:
    ans += xl[idx-1] - before_x
    before_x = xl[idx]
    
ans += xl[-1] - before_x
print(ans)

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

ans = 0
if n < m:
    xli.sort()
    wli = list()
    for i in range(len(xli)-1):
        wli.append(xli[i+1]-xli[i])
    sortWli = list(reversed(sorted(wli)))
    su = 0
    for i in range(n-1):
        su += sortWli[i]
    ans = xli[-1] - xli[0] - su
print(ans)

Solution (Python):

import sys










def main():
    n, m, *x = (int(i) for i in sys.stdin.read().split())
    x.sort()
    if n >= m:
        print(0)
        sys.exit()

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

    
    
    
    dist.sort()
    ans = sum(dist[:(m-1)-(n-1)])

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
l=[int(i) for i in input().split()]
if n>=m:
    print(0)
    exit()
l.sort() 
diff=[l[i]-l[i-1] for i in range(1,m)]
diff.sort() 
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()
X_dis = []
for m in range(M-1):
    X_dis.append(X[m+1]-X[m])
X_dis.sort()
ans = 0
if N < M:
    for i in range(M-N):
        ans += X_dis[i]
print(ans)

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

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 = [0] * (M - 1)
for i in range(M-1):
    L[i] = X[i+1] - X[i]

L.sort()

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

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 = [0] * (M - 1)
for i in range(M-1):
    L[i] = X[i+1] - X[i]

L.sort()

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

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

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

cost.sort()
ans = 0
for i in range(m-n):
    ans += cost[i]
print(ans)

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

if M <= N:
    print(0)

else:
    X.sort()
    diffs = [X[i+1] - X[i] for i in range(M-1)]
    diffs.sort(reverse=True)
    n = N-1
    ans = sum(diffs[n:])
    print(ans)

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

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

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

Solution (Python):

import sys


def solve(N: int, M: int, X: "List[int]"):
    if N >= M:
        print(0)
    else:
        X_s = sorted(X)
        b = []
        for i in range(0, M-1):
            b.append(X_s[i+1] - X_s[i])
        b_s = sorted(b, reverse=True)
        print(X_s[-1] - X_s[0] - sum(b_s[:N-1]))
    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())
X = list(map(int,input().split()))
if N >= M:
    ans = 0
else:
    X.sort()
    Sa = [0]*(M-1)
    for i in range(M-1):
        Sa[i] = X[i+1] - X[i]
    Sa.sort()
    ans = sum(Sa[:M-N])
print(ans)

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

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

x.sort()
l = []
for i in range(len(x)):
    if i == 0:
        continue
    l.append(x[i] -  x[i-1])
l.sort(reverse=True)
sum = sum(l)
for i in range(n-1):
    sum -= l[i]

print(sum)

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

Solution (Python):
from sys import stdin
import heapq

n,m = [int(x) for x in stdin.readline().rstrip().split()]
li = list(map(int,stdin.readline().rstrip().split()))

li.sort()
sa = []

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

for i in range(1,m):
    sa.append(li[i]-li[i-1])

sa.sort()

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

print(sum(sa))

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

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()
print(sum(XX))

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

X.sort()

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

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

A=sorted(A)
B=[0]*(M-1)

for i in range(len(A)-1):
    B[i]=A[i+1]-A[i]

B=sorted(B)
if(N>=M):
    print("0")
    exit()
else:
    print(sum(B[0:M-N]))

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

locs.sort()
 
difLocs = []
first = locs[0]
for bot,top in zip(locs, locs[1:]):
  if top == first:
    break
  difLocs.append(top-bot)
 
difLocs.sort()
if (m-n) < 0:
  print(0)
else:
  newLocs = difLocs[0:m-n]
  print(sum(newLocs))

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

locs.sort()
 
difLocs = []
first = locs[0]
for bot,top in zip(locs, locs[1:]):
  if top == first:
    break
  difLocs.append(top-bot)
 
difLocs.sort()
if (m-n) < 0:
  print(0)
else:
  newLocs = difLocs[0:m-n]
  print(sum(newLocs))

Solution (Python):
n, m = [int(x) for x in input().split()]
locs = [int(x) for x in input().split()]
locs.sort()
 
difLocs = []
first = locs[0]
for bot,top in zip(locs, locs[1:]):
  if top == first:
    break
  difLocs.append(top-bot)
 
difLocs.sort()
if (m-n) < 0:
  print(0)
else:
  newLocs = difLocs[0:m-n]
  print(sum(newLocs))

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

    
    if N >= M:
        print(0)
        return

    Xs.sort()
    distance_list = []
    for i in range(M):
        if i == M-1:
            break
        distance = abs(Xs[i] - Xs[i+1])
        distance_list.append(distance)
    distance_list.sort(reverse=True)

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

    print(sum(distance_list))


if __name__ == "__main__":
    main()

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

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

gap.sort()
print(sum(gap if N == 1 else gap[:-1 * (N - 1)]))

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

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

if N >= M:
    print(0)
else:
    Diff = []
    for i in range(len(Data)-1):
        Diff.append(Data[i+1]-Data[i])
    Diff.sort()
    ans = 0
    for i in range((M-1)-(N-1)):
        ans += Diff[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()
    L = [abs(X[i]-X[i-1]) for i in range(1,M)]
    L.sort(reverse=True)
    print(X[-1]-X[0]-sum(L[:N-1]))

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

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

dis_X = sorted(dis_X, reverse=True)
ans = 0
for i in range(N-1, len(dis_X)):
    ans += dis_X[i]

print(ans)

Solution (Python):
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums2.sort()
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3.sort()
nums3 = nums3[:len(nums3)-n+1]
if(n>=m):
  print(0)
else:
  print(sum(nums3))

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

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

diff.sort()

indexes = [x[1] for x in diff[-1:-min(n,m):-1]]

final_index = [0]
for i in indexes:
	final_index.append(i)
	final_index.append(i-1)

final_index.append(len(coords)-1)
final_index.sort()

total = 0
for i in range(1,len(final_index),2):
	total += coords[final_index[i]]-coords[final_index[i-1]]

print(total)

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

for i in range(M - 1):
	Y.append(X[i + 1] - X[i])
Y = sorted(Y, reverse = True)

print(max(X) - min(X) - sum(Y[0 : N - 1]))

Solution (Python):
if __name__ == '__main__':
    N, M = map(int, input().split())
    X = sorted(list(map(int, input().split())))
    if len(X) <= N:
        print(0)
        exit()

    diff = sorted([X[idx + 1] - x for idx, x in enumerate(X[:-1])], reverse=True)
    print(sum(diff[N - 1:]))

Solution (Python):
import sys
n,m=map(int,input().split())
if m<=n:
  print(0)
  sys.exit()
x=sorted(list(map(int,input().split())))
l=[0]*(m-1)
for i in range(m-1):
  l[i]=x[i+1]-x[i]
l.sort()
s=0
for i in range(n-1):
  s+=l[m-2-i]
print(x[m-1]-x[0]-s)

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
maxdi = 0
if M-N <= 0:
    print(0)
    exit()
dis = []
for i in range(M-1):
    dis.append(X[i+1]-X[i])
alldi = max(X)-min(X)
dis.sort(reverse=True)
partdi = sum(dis[:N-1])
print(alldi-partdi)

Solution (Python):

def main():
    N, M, *X = map(int, open(0).read().split())
    X = sorted(set(X))
    dist = [j - i for i, j in zip(X, X[1:])]
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, M, *X = map(int, open(0).read().split())
    X = sorted(set(X))
    dist = []
    for i, j in zip(X, X[1:]):
        dist.append(j - i)
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

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

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

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

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

Solution (Python):
import heapq
N , M = map(int,input().split())
X = list(map(int,input().split()))
X.sort(reverse=True)
diff = []
heapq.heapify(diff)
for i in range(len(X) - 1):
    heapq.heappush(diff , -1 * (X[i] - X[i + 1]))
for _ in range(N - 1):
    if not diff:
        print("0")
        exit()
    else:
        heapq.heappop(diff)

print(-1 * sum(diff))

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

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

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

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

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

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

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

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

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

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

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

x.sort()
y = x[1::]
dlt = [i-j for (i,j) in zip(y,x)]
dlt.sort()
if n==1:
  print(sum(dlt))
else:
  print(sum(dlt[0:-1*n+1]))

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

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

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

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

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

Solution (Python):
import sys

input = sys.stdin.readlines()
N, M = map(int, input[0].strip().split(' '))
Xs = list(map(int, input[1].strip().split(' ')))
s = sorted(Xs)
gaps = [s[i+1]-s[i] for i in range(len(s)-1)]
sgaps = sorted(gaps)
if N>=M:
    print(0)
else:
    print(sum(sgaps[:(M-N)]))

Solution (Python):


import heapq

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

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

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

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

heapq.heapify(l)

for _ in range(N-1):
    heapq.heappop(l)

print(-sum(l))

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

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

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

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

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

x.sort()

if n>=m:
    print(0)
    exit()
sa=[]
for i in range(m-1):
    sa.append(x[i+1]-x[i])
sa.sort(reverse=True)
ans=x[-1]-x[0]
for i in range(n-1):
    ans-=sa[i]
print(ans)

Solution (Python):
from operator import itemgetter

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

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




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


dist_between_destination.sort(reverse = True)

print((X[-1] - X[0]) - sum(dist_between_destination[:N-1]))

Solution (Python):
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
X = [int(x) for x in stdin.readline().rstrip().split()]
X.sort()
if N >= M:
    print('0')
else:
    r = []
    for i in range(M - 1):
        r.append(X[i + 1]-X[i])
    r.sort()
    if N != 1:
        print(sum(r[:-N+1]))
    else:
        print(sum(r))

Solution (Python):
import sys
N,M=map(int,input().split())
L=[i for i in map(int,input().split())]
D=[]
if N >= M or M==1 :
    print(0)
    sys.exit()

L.sort()
for i in range(0,M-1) :
    D.append(abs(L[i]-L[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())))

dis=[]

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

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


dis.sort()

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
        exit()
    minimum = x[0]
    maximum = x[-1]
    if n == 1:
        print(maximum-minimum)
        exit()
    arr = []
    for i in range(len(x)-1):
        arr.append(abs(x[i+1]-x[i]))

    arr.sort()

    print(maximum-minimum-sum(arr[-(n-1):]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
        exit()
    minimum = x[0]
    maximum = x[-1]
    if n == 1:
        print(maximum-minimum)
        exit()
    arr = []
    for i in range(len(x)-1):
        arr.append(abs(x[i+1]-x[i]))

    arr.sort()

    print(maximum-minimum-sum(arr[-(n-1):]))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)

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


if __name__ == '__main__':
    main()

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

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

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

Y.sort(reverse=False)

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

else:

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

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

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

s = sorted(X)
dist = [s[n]-s[n-1] for n in range(1,len(s))]
dist = sorted(dist)[:M-N]
if N >= M:
  print(0)
else:
  res = 0
  for n in range(len(dist)):
    res += dist[n]
  print(res)

Solution (Python):

n,m = [int(i) for i in input().split()]

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

a.sort()

list = [i for i in range(len(a) - 1)]

for nn,value in enumerate(a):
    if nn == 0:
        diff = value
    else:
        list[nn-1] = value - diff
        diff = value

list.sort(reverse=True)
count = min(n-1, len(list))
for dl in range(count):
    list[dl] = 0

print(sum(list))

Solution (Python):
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *

from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

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

Solution (Python):
import numpy as np

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

distances = sorted(X[1:] - X[:-1])
print(sum(distances) if N == 1 else sum(distances[:-N+1]) )

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

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

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

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

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

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

print(ans)

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

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

distance = []

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

distance.sort()
if n >= m:
    print(0)
else:
    for i in range(n - 1):
        distance.pop()

    print(sum(distance))

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

t=0
if n<m:
  x.sort()
  s=list()
  for i in range(m-1):
    s.append(x[i+1]-x[i])
  s.sort()

  for i in range(m-n):
    t += s[i]

print(t)

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

if n >= m:
  print(0)
elif n == 1:
  Diffs.sort()
  print(sum(Diffs))
else:
  Diffs.sort()
  print(sum(Diffs[:-(n-1)]))

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

li=[]


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

print(max(x)-min(x)-sum(li[:(n-1)]))

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

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

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

if N >= M:
    print(0)
else:
    X.sort()
    spot = [0]*M
    diff = [[0, 0]]*(M-1)
    for i in range(M-1):
        diff[i] = [X[i+1]-X[i], i]
    diff.sort(key=lambda x: x[0])
    if N == 1:
        print(X[-1]-X[0])

    else:
        ans = X[-1] - X[0]
        for i in range(N-1):
            tmp = diff.pop()
            ans -= tmp[0]
        print(ans)

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