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, *X = map(int, open(0).read().split())
X = sorted(X)
if N >= M:
  print(0)
  exit(0)
D = []
for i in range(M-1):
  D.append(abs(X[i] - X[i+1]))
D = list(reversed(sorted(D)))
print(X[M-1] - X[0] - sum(D[:N-1]))

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

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

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

Solution (Python):
from operator import sub
N, M = map(int, input().split())
X = sorted(map(int, input().split()))
print(sum(sorted(map(sub, X[1:], X), reverse=True)[N-1:]))

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

a.sort()
a.reverse()

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

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

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

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

print(sum(D))

Solution (Python):


(koma_count, target_count) = [int(i) for i in input().rstrip().split(" ")]
target = sorted([int(i) for i in input().rstrip().split(" ")])
area = sorted([target[j] - target[j-1] for j in range(1, target_count)])
must = max(0, target_count-koma_count)

ans = 0
if must > 0:
    ans = sum(area[0:must])
print(ans)

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

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

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

print(sum(m))

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

X.sort()
Xx = sorted( [ X[i+1]-X[i] for i in range(m-1) ], reverse=True )
print(sum(Xx[n-1:]))

Solution (Python):
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
x.sort()
diff = [x[i] - x[i - 1] for i in range(1, m)]
diff.sort()
i = 0
while diff and i < n - 1:
    diff.pop()
    i += 1
print(sum(diff))

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

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

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

X.sort()
X = np.diff(X)
X.sort()
if n >= m:
    print(0)
else:
    print(X[:m-n].sum())

Solution (Python):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



N, M = list(map(int, sys.stdin.buffer.readline().split()))
X = list(map(int, sys.stdin.buffer.readline().split()))

X.sort()
diffs = np.abs(np.diff(X))
diffs.sort()
if N >= M:
    print(0)
else:
    ans = diffs.cumsum()[M - N - 1]
    print(ans)

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

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

length = X[m-1] - X[0]
L = []
for i in range(m-1):
	L.append(X[i+1] - X[i])
L.sort()

if n >= m:
	print(0)
else:
	print(length - sum(L[m-1-(n-1):m-1]))

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

d = [0] * (m - 1)
for i in range( m - 1):
    d[i] = x[i + 1] - x[i]
d_s = sorted(d, reverse=True)

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

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

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

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

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

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

x.sort()

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

num.sort()
if n > m:
  print(0)
else:
  print(sum(num[:m-n]))

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

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

if m <= n:
    ans = 0
else:
    x.sort()
    dx = [x[i]-x[i-1]-1 for i in range(1,m)]
    dx.sort(reverse=True)
    ans = x[m-1] - (x[0] - 1) - n - sum(dx[:n-1])

print(ans)

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

x.sort()
dx = [x[i]-x[i-1]-1 for i in range(1,m) if x[i]-x[i-1]-1>0]
dx.sort(reverse=True)

a = 0
if n-1 <= len(dx):
    for i in range(n-1):
        a += dx[i]
else:
    a = sum(dx)

if m <= n:
    ans = 0
else:
    ans = x[m-1] - (x[0] - 1) - n - a
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(0)

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

Solution (Python):
import heapq

def solve():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X_ = sorted(X)
    ret = X_[-1] - X_[0]
    diff = [X_[i]-X_[i+1] for i in range(M-1)]
    heapq.heapify(diff)

    while N > 1 and diff:
        ret += heapq.heappop(diff)
        N -= 1
        
    print(ret)
    
solve()

Solution (Python):
def solve():
    N, M, *X = map(int, open(0).read().split())
    
    X.sort()
    dx = [0 for _ in range(M - 1)]
    for i in range(1, M):
        dx[i - 1] = X[i] - X[i - 1]
    
    dx.sort()
    ans = 0
    for i in range(M - N):
        ans += dx[i]
    print(ans)


if __name__ == '__main__':
    solve()

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

Solution (Python):
import heapq

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

X.sort()
D = heapq.nlargest(N - 1, (X[i+1] - X[i] for i in range(M-1)))
print(X[-1] - X[0] - sum(D))

Solution (Python):
from heapq import heappop, heappush
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))

if N >= M:
  print("0")
else:
  queue = []
  for i in range(M):
    if i + 1 < M:
      heappush(queue, -abs(X[i+1] - X[i]))

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

  print(-sum(queue))

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

Solution (Python):
import sys

nm=input().split(" ")
N=int(nm[0])
M=int(nm[1])
xi=input().split(" ")

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

X=[]
for i in range(M):
  X+=[int(xi[i])]
X.sort()
dX=[]
for i in range(M-1):
  dX+=[X[i+1]-X[i]]
dX.sort()






ans=0
for i in range(M-N):
  ans+=dX[i]
print(ans)

Solution (Python):

import sys


def solve(N: int, M: int, X: "List[int]"):
    x = sorted(X)
    x_dif = []
    for i in range(M-1):
        x_dif.append(x[i+1]-x[i])
    x_dif.sort(reverse=True)
    print(sum(x_dif[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()))
X.sort()
A = []
for i in range(M-1):
  A.append(X[i+1] - X[i])
A.sort()
if N >= M:
  print(0)
else:
  print(sum(A[i] for i in range(M - N)))

Solution (Python):
from operator import itemgetter
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
lis=[]
for i in range(m-1):
  lis.append([x[i+1]-x[i],i])
lis.sort(reverse=True)
if n>=m:
  print(0)
else:
  if n==1:
    print(max(x)-min(x))
  else:
    cnt=x[lis[0][1]]-x[0]+x[m-1]-x[lis[n-2][1]+1]
    for i in range(1,n-1):
      cnt+=x[lis[i][1]]-x[lis[i-1][1]+1]
    print(cnt)

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

if m <= n:
	print(0)
else:
	D = [0]*(m-1)
	for i in range(0, m-1):
		D[i] = X[i+1] - X[i]
	D = sorted(D)
	if n == 1:
		print(sum(D))
	else:
		print(sum(D[:-n+1]))

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

diff = []
for i in range(m-1):
    diff.append(abs(x[i]-x[i+1]))
p = 0 if len(diff) < n else len(diff)-n+1
sec = sorted(diff)[:p]

print(sum(sec))

Solution (Python):
def main():
    N, M, *X = map(int, open(0).read().split())
    X = sorted(set(X))
    D = [j - i for i, j in zip(X, X[1:])]
    D.sort()
    print(0 if M <= N else sum(D[:M - N]))
    return


main()

Solution (Python):
def main():
    N, M, *X = map(int, open(0).read().split())
    X.sort()
    D = [j - i for i, j in zip(X, X[1:])]
    D.sort()
    print(0 if M <= N else sum(D[:M - N]))
    return


main()

Solution (Python):
from sys import stdin


def main():
    N, M = map(int, input().split())
    *X, = map(int, stdin.readline().split())
    X.sort()
    D = [X[i + 1] - X[i] for i in range(M - 1)]
    D.sort()
    print(0 if M <= N else sum(D[: M - N]))
    return


main()

Solution (Python):
n, m, *x = map(int, open(0).read().split())
if n >= m:
  print(0)
  quit()
x = sorted(set(x))
lst = [j - i for i, j in zip(x, x[1:])]
lst.sort(reverse=1)
s = sum(lst[n - 1:])
print(s)

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

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

Solution (Python):
def examA():
    T, X = LI()
    ans = T/X
    print(ans)
    return

def examB():
    N = I()
    L = LI()
    ans = "Yes"
    if max(L)*2>=sum(L):
        ans = "No"
    print(ans)
    return

def examC():
    N, M = LI()
    X = LI(); X.sort()
    x = []
    for i in range(M-1):
        x.append(X[i+1]-X[i])
    x.sort()
    ans = 0
    if N>=M:
        ans = 0
    else:
        for i in range(M-N):
            ans += x[i]
    print(ans)
    return

def examD():
    N, K = LI()
    A = LI()
    d = defaultdict(int)
    maxl = len(bin(K)) -2
    for a in A:
        for j in range(maxl):
            if a&1==1:
                d[j] +=1
            a >>=1
            if a==0:
                break

    X = 0
    cur = []
    for i in range(maxl):
        c = (2**i)*(N-d[i]*2)
        cur.append([c,i])
    cur.sort()
    ans = sum(A)

    ansC = 0
    for i in cur:
        if i[1]!=maxl:
            ansC +=max(i[0],0)
    ans +=ansC

    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

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

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

ans = 0

if N < M:
    Y = sorted(X)
    sections = sorted([Y[i] - Y[i - 1] for i in range(1, M)])
    for i in range(M - N):
        ans += sections[i]

print(ans)

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

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

Solution (Python):
from collections import deque
n,m = map(int,input().split())
X = sorted(list(map(int,input().split())))
diffs = deque()
if m <= n:
  print(0)
else:
  for i in range(m - 1):
    diffs.append(X[i + 1] - X[i])
  diffs = deque(sorted(diffs,reverse = True))
  for i in range(n - 1):
    diffs.popleft()
  print(sum(diffs))

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

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

suma = 0
if N >= M:
    print(0)
else:
    for i in range(M-N):
        suma += diff[i]
    print(suma)

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

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

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

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

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

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

Solution (Python):
def func2(input_list):
    if n >= m:
        return 0

    input_list.sort()

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

    input_diff.sort(reverse=True)
    ans = input_list[-1] - input_list[0]
    for i in range(0, n-1):
        ans -= input_diff[i]

    return ans

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

print(func2(input_list))

Solution (Python):
N, M = map(int, input().split())
listx = sorted(list(map(int, input().split())))
listgap =[0]*(M-1)
for n in range(M-1):
    listgap[n] = listx[n+1]-listx[n]
listgap.sort()    
if N>=M:
    print('0')
    exit
else:
    a = M-N
    anslist = listgap[0:a]
    b = sum(anslist)
    print(b)

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np
N, M = [int(x) for x in input().rstrip().split()]
X = [int(x) for x in input().rstrip().split()]
X.sort()
X_1 = np.array(X[:M-1], np.int64)
X_2 = np.array(X[1:], np.int64)
X_ = X_2 - X_1
X_ = np.sort(X_)[::-1]
print(X_[N-1:].sum())

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

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

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

table.sort()
desk = []
for x in range(0, len(table) - 1):
    desk.append(table[x + 1] - table[x])
desk.sort()
for x in range(0, n - 1):
    if len(desk) < 2:
        break
    desk.pop(-1)
cnt = 0
for x in desk:
    cnt += x
print(cnt)

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

Solution (Python):




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

right_distance = [j-i for i,j in zip(x,x[1::])]
right_distance.sort()
ret = 0
while n < m:
    ret += right_distance.pop(0)
    m -= 1
print(ret)

Solution (Python):

import sys
input = sys.stdin.readline

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

right_distance = [j-i for i,j in zip(x,x[1::])]
right_distance.sort()
ret = 0
while n < m:
    ret += right_distance.pop(0)
    m -= 1
print(ret)

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

dis_X = [(X[i+1] - X[i]) for i in range(0, M-1)]

dis_X.sort()

if len(dis_X) <= (N-1):
    print(0)
else:
    
    
    print(sum(dis_X[:M-(N-1)-1]))

Solution (Python):
import numpy as np

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

cnt = 0
reversed_diff = sorted(np.diff(X))[::-1]

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

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

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

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

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

Z = sorted(sum_Y)

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

print(sum(Z))

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

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

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

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


if __name__ == '__main__':
    main()

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] - x[i-1] for i in range(1, m)]
l.sort(reverse=True)

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

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

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

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

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

if n == 1:
    print(sum(dif))
else:
    print(sum(dif[:-(n-1)]))

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([int(i) for i in input().split()])
xbet=sorted([abs(x[i+1]-x[i]) for i in range(m-1)],reverse=True)
y=sum(xbet)
if n>=m:
    print(0)
else:
    print(y-sum(xbet[:n-1]))

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

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

s.sort(reverse=True)

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

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

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

print(ans)

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

dist = []
for i in range(m-1):
    dist.append(X[i+1]-X[i])
dist = sorted(dist, reverse=True)
if n >= m:
    print(0)
else:
    print(sum(dist[n-1:]))

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

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

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

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

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

Solution (Python):
N,M = list(map(int,input().split()))
xarray = sorted(list(map(int,input().split())))
 
diff = []
if len(xarray) == 1:
    print(0)
elif N >= M:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))
 
    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)
        
 
        if N == 1:
            
            print(sum(diff2))
        else:
            for j in range(N-1):
                diff2[j] = 0
            
            print(sum(diff2))

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

Solution (Python):
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
g=[0]*(m-1)
for i in range(m-1):
    g[i]=x[i+1]-x[i]
g=sorted(g,reverse=True)
print(sum(g[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 == 1:
  print(sum(l))
else:
  print(sum(l[:-n+1]))

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

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

N, M = [int(x) for x in input().strip().split()]
X = sorted([int(x) for x in input().strip().split()])
D = [0] * (M - 1)
d = X[0]
if N >= M:
    print(0)
    exit()

for i, x in enumerate(X[1:]):
    D[i] = x - d
    d = x

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

Solution (Python):
from collections import deque
N, M = map(int, input().split())
X = list(map(int, input().split()))
sortX = sorted(X)
dist = []
if N-M>=0:
    print(0)
else:
    for i in range(M-1):
        dist.append(sortX[i + 1] - sortX[i])
    dist.sort()
    print(sum(dist[:M-N]))

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

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

Solution (Python):
N,M = map(int,input().split())
X = sorted([int(i) for i in input().split()])
subX = sorted([X[i]-X[i-1] for i in range(1,M)],reverse=True)
 
if N >= M:
    print(0)
    exit()
 
answer = X[-1] - X[0]
for i in range(N-1):
    answer -= subX[i]
 
print(answer)

Solution (Python):
IINF = 10 ** 18


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

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

        

        L.sort()
        if N == 1:
            ans = sum(L)
            print(ans)
        else:
            ans = sum(L[0:-(N-1)])
            print(ans)

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


if __name__ == '__main__':
    main()

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

Solution (Python):
N,M = map(int,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[:M-N])) if M > N else print(0)

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]-x[i-1] for i in range(1,m)])
    print(sum(d[:m-n]))

Solution (Python):
nPieces, nCoords = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
if nPieces >= nCoords:
    print(0)
    exit()
diffs = sorted([a[i] - a[i + 1] for i in range(nCoords - 1)], reverse=True)
print(sum(diffs[nPieces - 1:]))

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

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

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

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

x.sort()

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

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

d.sort()

print(sum(d[:-n + 1]))

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

list_aaa = sorted(list_aaa)
list_aaa_range = []

for i in range(M-1):
    list_aaa_range.append(list_aaa[i+1]-list_aaa[i])

list_aaa_range = sorted(list_aaa_range)

for i in range(1,N):
    if not list_aaa_range :
        list_aaa_range.append(0)
    else:
        list_aaa_range.pop(-1)

print(sum(list_aaa_range))

Solution (Python):
import os, sys, re, math

N,M = list(map(int,input().split(' ')))
L = sorted(list(map(int,input().split(' '))))
diff = sorted([L[i] - L[i-1] for i in range(1,M)],reverse=True)
print(sum(diff[N-1:]))

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

Solution (Python):
import sys
sys.setrecursionlimit(10000)

n, k = list(map(int, input().split(' ')))
l = sorted(list(map(int, input().split(' '))))









diffs = [l[i+1]-l[i] for i in range(len(l)-1)] 
diffs = sorted(diffs)
print(sum(diffs[:max(0, k-n)]))

Solution (Python):
n, m = [int(a) for a in input().split()]
x = sorted([int(a) for a in input().split()])
y = sorted([b - a for a, b in zip(x, x[1:])])
if n >= m:
	print (0)
else:
	print (sum(y[0 : m - n]))

Solution (Python):
n,m = map(int,input().split())
if n>=m:
  print(0)
  exit()
list1 = list(map(int,input().split()))
list1.sort()
list2 = []
for i in range(m-1):
	list2.append(list1[i+1]-list1[i])
list2.sort(reverse = True)
cut = 0
for j in range(n-1):
	cut+= list2[j]
print(max(list1)-min(list1)-cut)

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

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

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

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
cnt = 0
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()
for i in range(n-1):
    L.pop(0)
print(sum(L))