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 = [int(i) for i in input().split()]

l = sorted([int(i) for i in input().split()])

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

Solution (Python):



"""
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = sum(l) - 2*l[-1]
if ans > 0:
    print("Yes")
else:
    print("No")
"""


import bisect
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
xb = [abs(x[i] - x[i + 1]) for i in range(m-1)]
xb.sort(reverse=True)
ans = sum(xb[n - 1::])
print(ans)

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

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

Solution (Python):
n, k = map(int, input().split())
array = sorted([int(x) for x in input().split()])
tmp = array[0]
tmp_array = []
for i in range(len(array)):
    tmp_array.append(array[i]-tmp)
    tmp = array[i]

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

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

X = sorted(X)
gaps = sorted([X[i+1] - X[i] for i in range(M-1)], reverse=True)

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

Solution (Python):
N, M = [int(x) for x in input().split(' ')]
X = [int(x) for x in input().split(' ')]
X = sorted(X)
dists = []
for i in range(len(X)-1):
    dist = X[i+1] - X[i]
    dists.append(dist)
dists = sorted(dists)

result = 0

for i in range(len(dists)-N+1):
    result += dists[i]

print(result)

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

sub = [0 for i in range(m)]
for i in range(m):
    if i == m-1:
        sub[i]=0
    else:
        sub[i] = X[i+1]-X[i]


ans = sum(sorted(sub)[:m-n+1])
if n>= m:
    ans = 0
print(ans)

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

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

x.sort()

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

z.sort()
for _ in range(n - 1):
    if len(z) > 0:
        z.pop()

print(sum(z))

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()
g = 0

if n >= m:
  None
else:
  for j in range(m-n):
    g += sa[j]

print(g)

Solution (Python):
def read():
    N, M = map(int, input().strip().split())
    X = map(int, input().strip().split())
    return N, M, X


def solve(N, M, X):
    if N >= M:
        return 0
    else:
        X = sorted(X)
        dX = [X[i + 1] - X[i] for i in range(len(X) - 1)]
        dX = sorted(dX)
        return sum(dX[:M-N])


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Solution (Python):
n, m = map(int, input().split())
listX=list(map(int, input().split()))
listX.sort()
listSa=[]
index=0
while index < len(listX)-1:
    dist = listX[index+1]-listX[index]
    listSa.append(dist)
    index+=1
listSa.sort()
index2=0
sum=0
while index2 < len(listSa)-(n-1):
    sum+=listSa[index2]
    index2+=1
print(sum)

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

    lidelta = list(reversed(sorted(lidelta)))

    for i in range(n-1):
        a -= lidelta[i]
    
    print(a)

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

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

Y.sort(reverse=True)


res = X[-1] - X[0]

for i in Y[:N-1]:
	res -= i


print(res)

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

if M <= N:
    print(0)
    quit()
    
X.sort(reverse = True)

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

D.sort(reverse = True)
ans = sum(D[N-1:])

print(ans)

Solution (Python):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=[]
if m==1:
    print(0)
    exit()
if n==1:
    print(max(a)-min(a))
    exit()
for i in range(m-1):
    ans.append(abs(a[i+1]-a[i]))
total=0
ans.sort(reverse=True)
if len(ans)<=n:
    print(0)
    exit()
for i in range(n-1):
    total+=ans[i]
print(sum(ans)-total)

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

if M == 1 and N > 0:
    print(0)
    exit()
if M <= N:
    print(0)
    exit()


X = sorted(X)
s = []
for i in range(len(X) - 1):
    s.append(abs(X[i+1]- X[i]))
ans = sum(s)
s = sorted(s)
for i in range(1,N):
    ans -= s[-i]
print(ans)

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

distX = sorted(X)

distX = [abs(x-y) for x,y in zip(distX[:-1],distX[1:])]


a = N-1
if a == 0:
    print(sum(distX))
else:
    sorDX = sorted(distX)
    print(sum(sorDX[:-a]))

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

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

dif=[]
tmp=0
for i, xi in enumerate(x) :
    if i == 0 :
        tmp = xi
        continue
    dif.append(xi-tmp)
    tmp=xi

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

print(sum(dif))

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

X=[int(_) for _ in input().split()]
X.sort()
if N>=M:
    print('0')
    exit(0)
Sum=[]
for i in range(M-1):
    Sum.append(abs(X[i+1]-X[i]))
Sum.sort()
for i in range(N-1):
    Sum.pop()
print(sum(Sum))

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

y = [x[i] - x[i - 1] for i in range(1, M)]
y = list(sorted(y))

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

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

Solution (Python):
N,M = list(map(int, input().strip().split(' ')))
X = list(map(int, input().strip().split(' ')))
if M <= N:
  print(0)
  exit()
X = sorted(X, reverse=True)
diff = [X[i]-X[i+1] for i in range(len(X)-1)]
diff = sorted(diff, reverse=True)
print(sum(diff[N-1:]))

Solution (Python):
import sys

koma_num, goal_num = [int(a) for a in input().split(' ')]

if koma_num >= goal_num:
  print(0)
  sys.exit()

goals = [int(a) for a in input().split(' ')]
goals.sort()
dist_diff = [int(goals[i+1] - goals[i]) for i in range(len(goals)-1)]
dist_diff.sort()
ans_num = goal_num - koma_num
print(sum(dist_diff[:ans_num]))

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

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

Y = [ X[i-1]-X[i] for i in range(1,M)]
Y.sort()

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

print(ans)

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

X.sort()

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

X_len.sort(reverse=True)
ans = X[-1] - X[0]    
for i in range(min(N-1,len(X_len))):
    ans -= X_len[i]    
    
print(ans)

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

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

f = False
if m==1:f=True
if n>=m:f=True



arr = [0]*(m-1)
le.sort()

if f==False:
    for i in range(0,m-1):
        arr[i]=abs(le[i]-le[i+1])
    arr.sort()
    u = n-1
    i=0
    while(u>0):
        del(arr[-1])
        u-=1

if f==False:
    print(sum(arr))

if f==True:
    print(0)

Solution (Python):
import sys
input = sys.stdin.readline
n,m = input().split()
n = int(n)
m = int(m)
x = input().split()
for i in range(m):
    x[i] = int(x[i])
x.sort()
l = [0 for i in range(m-1)]
for i in range(m-1):
    l[i] = x[i+1] - x[i]
l.sort()
sum = 0
if n >= m:
    print(0)
else:
    for i in range(n-1):
        sum += l[m-2-i]
    dist = x[m-1] - x[0]
    print(dist-sum)

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

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

ls = []

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

ls.sort()

ans = 0

for i in range(M - N):
    ans += ls[i]

print ans

Solution (Python):
import numpy as np
import math

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

x.sort()
x2 = [0] + x[:-1]

xn = np.array(x)
x2n = np.array(x2)

diff = (xn - x2n)[1:]
diff.sort()

if n > 1:
    diff = diff[:-(n-1)]


diffsum = np.sum(diff)

print(diffsum)

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

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

Solution (Python):
def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    if N >= M:
        print('0')
        return 
    X_sort = sorted(X)
    diff = []
    for i in range(M - 1):
        diff.append(X_sort[i + 1] - X_sort[i])
    diff_sort = sorted(diff)
    if N != 1:
        for _ in range(N - 1):
            diff_sort.pop()
    ans = 0
    for d in diff_sort:
        ans += d
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N,M=[int(_) for _ in input().split()]
if M <= N :
    print(0)
else:
    aX=sorted(int(_) for _ in input().split())
    aD =sorted(aX[i]-aX[i-1] for i in range(1,M))
    print(sum(aD[:M - N ]))

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

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

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

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

d.sort(key=lambda x: x[1],reverse = True)
sumd = 0
for i in range(N-1,M):
    sumd = d[i][1] + sumd
print(sumd)

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

X.sort()
D = [X[i+1] - X[i] for i in range(0, len(X)-1)]
D = [X[i+1] - X[i] for i in range(0, len(X)-1)]
D.sort(reverse=True)
E = D[min(N-1, len(D)):]
print(sum(E))

Solution (Python):
import numpy as np
N,M =list(map(int, input().split()))
X = input().split()
for i in range(len(X)):
    X[i] = int(X[i])
X = np.array(X)
X.sort()

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

Y = np.array(y)
Y.sort()

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

Solution (Python):
import sys

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

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

X.sort();

dif = [X[i+1]-X[i] for i in range(M-1)]
dif.sort()
dif.reverse()
if M==1:
    dif = [0]

ans = sum(dif)
for i in range(N-1):
    ans -= dif[i]

print(ans)

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

for xs in xstr:
    xdec.append(int(xs))
xdec.sort()

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

dif.sort()

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

print(sum)

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

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

for i in range(n-1,len(s2)):
  ans += s2[i]
print(ans)

Solution (Python):
N, M = map(int, input().split())
X_list = list(map(int, input().split()))
X_list.sort()
err_list = [X_list[i+1] - X_list[i] for i in range(len(X_list)-1)]
err_list.sort()

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

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

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

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

X = map(int,raw_input().split())

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

gap.sort()
gap.reverse()

gapsum = 0
counter = 0
if len(gap) > 0:
    while N-counter-1 > 0 and counter < len(gap):
        gapsum += gap[counter]
        counter += 1

print abs(X[0]-X[-1])-gapsum

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()
print(sum(s[:-N + 1]) if N != 1 else sum(s))

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(reverse=True)
Y = Y[N-1:]
print(sum(Y))

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(len(x)-1):
    sa.append(x[i+1] - x[i])
sa.sort()
sa.reverse()
ans = sum(sa)
for i in range(n-1):
    ans -= sa[i]
print(ans)

Solution (Python):
N,M = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
X.sort()
X_dis = []
for i in range(len(X)-1):
    X_dis.append(X[i+1]-X[i])
X_dis.sort(reverse=True)
print(sum(X_dis[N-1:]))

Solution (Python):
N,M=map(int, input().split())
L=sorted(list(map(int,input().split())))
ans=0
L2=[]

for i in range(M-1):
    L2.append(L[i+1]-L[i])

L2.sort()

for i in range(M-N):
    ans=ans+L2[i]

print(ans)

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


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

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

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


print(sum(X_d[: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(m-1)])
    print(sum(d[:m-n]))

Solution (Python):
N, M = map(int, input().split())
coma = list(map(int, input().split()))
difference = []

coma.sort(reverse = True)

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

difference.sort(reverse = True)

i = 0

print(sum(difference[i + N - 1:]))

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

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

s = M - N
t = 0
X.sort()
Y = []

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

Y.sort()
for i in range(0,s):
    t = t + Y[i]

print(t)

Solution (Python):
import numpy as np

n, m = map(int, input().split())
xl = [int(i) for i in input().split()]
xl.sort()
d = m - n
if d <= 0:
    print(0)
else:
    darray = np.sort(np.array(xl[1:]) - np.array(xl[:-1]))
    print(np.sum(darray[:d]))

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

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

ans = 0
for i in range(N-1, len(sa)):
    ans += sa[i]
print(ans)

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

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

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

y = sorted(y, reverse=True)

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

Solution (Python):
from statistics import mean, median,variance,stdev
import sys
import math
import fractions

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())


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


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





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

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

X.sort()

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

L.sort(reverse=True)

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

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

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

print(ans)

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

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

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

X.sort()

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

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

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

Solution (Python):
N,M = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
if N < M:
    A = sorted(X)
    tmp = len(A) - 1
    difA = [0] * tmp
    for i in range(tmp):
        difA[i] = A[i+1] - A[i]
    difA_sorted = sorted(difA)
    print(sum(difA_sorted[:M-N]))
else:
    print(0)

Solution (Python):
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

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

X.sort()
hoge = [0]*(M-1)
for i in range(1, M):
    hoge[i-1] = X[i] - X[i-1]

hoge = list(reversed(sorted(hoge)))

ans = sum(hoge)
if N >= M:
    ans = 0
else:
    for i in range(N-1):
        ans -= hoge[i]
print(ans)

Solution (Python):
n,m=map(int,input().split())
l=list(map(int,input().split()))
if m==1:
  print(0)
  exit()
l.sort()
l2=[]
x0=l[0]
l.remove(x0)
for x in l:
  l2.append(x-x0)
  x0=x
l2.sort()
if n==1:
  print(sum(l2))
else:
  print(sum(l2[:-(n-1)]))

Solution (Python):
n, m = map(int, input().split())
X = list(map(int, input().split()))
if n < m:
    X.sort()
    diff = [X[i]-X[i-1] for i in range(1,len(X))]
    diff.sort()
    p = sum([diff[-i] for i in range(1,n)])
    print(sum(diff) - p)
else:
    print(0)

Solution (Python):
n, m=map(int, input().split())
x=list(map(int, input().split()))
x=sorted(x)
y=[0]*(m-1)

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

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

Solution (Python):
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
[n,m]=i2()
x=i2()
x.sort()
y=[]
for i in range(m-1):
  y.append(x[i+1]-x[i])
y.sort()
s=max(m-n,0)
print(sum(y[:s]))

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

if n == 1:
    print(max(x) - min(x))
elif n >= m:
    print(0)
else:
    dif = []
    for i in range(m - 1):
        dif.append(x[i+1] - x[i])
    dif = sorted(dif)
    dif = dif[:m-n]
    print(sum(dif))

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

def solve():
  if n>=m:
    print(0)
    return
  x.sort()
  x_differ = [x[i+1]-x[i] for i in range(len(x)-1)]
  x_differ.sort(reverse=True)
  print(sum(x_differ[n-1:]))

solve()

Solution (Python):
import sys

n, m = map(int, input().split())
x_list = list(map(int, input().split()))
x_list.sort()
gap_list = []
ans = 0

if n >= m:
    print(ans)
    sys.exit(0)
elif n < m:
    for i in range(1, len(x_list)):
        gap_list.append(abs(x_list[i - 1] - x_list[i]))

    gap_list.sort()
    ans = sum(gap_list[0:m - n])

print(ans)

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

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

Solution (Python):


import sys
import math



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

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

a.sort()

dist = [None] * m

for i in range(m-1):
    dist[i] = (a[i+1]-a[i])


dist[m-1] = 1000000

dist.sort()
score = 0

for i in range(m-n):
    score += dist[i]

print(score)

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

li = []

for i in range(len(box) - 1 ):
    li.append(box[i + 1] - box[i])

lis = sorted(li, reverse = True)

for i in range(n - 1):
    if lis == []:
        break
    else:
        del lis[0]

print(sum(lis))

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

x = input().split()
X = [int(l) for l in x]
X.sort()

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

X_diff.sort(reverse=True)

result = 0
for i in range((N-1), M-1, 1):
    result += int(X_diff[i])

print(result)

Solution (Python):
def c_streamline(N, M, X):
    if N >= M:
        return 0
    X.sort()
    x_diff = [0] * M
    for i in range(1, M):
        x_diff[i] = X[i] - X[i - 1]
    x_diff.sort()
    ans = sum(x_diff[:M - N + 1])
    return ans

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

Solution (Python):
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
if n >= m:
    print(0)
    exit()
x.sort()
dif = []
for i in range(m - 1):
    dif.append((x[i + 1] - x[i], i + 1))
dif.sort(key = lambda x: x[0], reverse = True)
split = set()
for i in range(n - 1):
    split.add(dif[i][1])
ans = 0
minn, maxx = x[0], x[0]
for i in range(m):
    if i in split:
        ans += maxx - minn
        minn = x[i]
    maxx = max(x[i], maxx)
ans += maxx - minn
print(ans)

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

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

X.sort()

C=[]
for i in range(m-1):
    c=abs(X[i+1]-X[i])
    C.append(c)

if(n>=m):
    print(0)
else:
    C.sort()
    sum_c=0
    for j in range(len(C)-n+1):
        sum_c+=C[j]
    print(sum_c)

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

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

Inter.sort()


tmp=0
res=X[-1]-X[0]

if(N>=M):
    print("0")
elif(N==1):
    print(res)


else:
    for i in range(N-1):
        tmp+=Inter[M-(i+2)]
    print(res-tmp)

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

res = []
for i in range(0, M):
    try:
        res.append(X[i+1]-X[i])
    except:
        break

res.sort(reverse=True)
print(sum(res[N-1:]))

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

if N < M:
  X = sorted(X,reverse = True)
  X_ = abs(np.array(X[1:]) - np.array(X[:M-1]))
  X_ = sorted(X_,reverse = True)
  print(sum(X_[N-1:]))
else:
  print('0')

Solution (Python):
from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]
Xs = [int(x) for x in stdin.readline().rstrip().split()]
Xs.sort()
dist = []
for i in range(len(Xs)-1):
    dist.append(abs(Xs[i] - Xs[i+1]))
dist.sort(reverse=True)
ans = 0
for x in dist[N-1:]:
    ans = ans + x
print(ans)

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

X = list(set(X))
X.sort()
M = len(X)
L = [X[i+1] - X[i] for i in range(len(X)-1)]
L.sort()

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

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

if n >= m:
  print(0)
else:
  for i in range(m - n):
    total += y[i]
  print(total)

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

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

diff.sort()
print(sum(diff[:max(1, M - (N - 1))]))

Solution (Python):
from sys import stdin
from numpy import empty

N, M = [int(x) for x in stdin.readline().rstrip().split()]
X = [int(x) for x in stdin.readline().rstrip().split()]

sortX = sorted(X)
dist = empty(M)

for i in range(M-1):
    dist[i] = sortX[i+1] - sortX[i]

sortedDist = sorted(dist, reverse=True)
result = sum(sortedDist[N-1:M-1])
print(int(result))

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

X.sort()

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

diff.sort()

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

print(sum(diff))

Solution (Python):
N, M = map(int, input().split())
X = [int(s) for s in input().split()]
X.sort()
dist = [X[i] - X[i-1] for i in range(1, M)]
dist.sort(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()))
dist = list()

if M-N <= 0:
  print(0)
else:
  X.sort()

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

  dist.sort()
  
  cnt = 0
  for i in range(M-N):
    cnt += dist[i]
    
  print(cnt)

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

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

cnt = 0
for i in range(N-1, M-1):
    cnt += X_distance[i]
print(cnt)

Solution (Python):
    
"""
Created on Sun Feb  3 21:07:35 2019

@author: Yamazaki Kenichi
"""

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

if N >= M:
    ans = 0

else:
    X.sort()
    A = []
    for i in range(M-1):
        A.append(X[i+1]-X[i])
    A.sort()
    ans = X[M-1]-X[0]

    for i in range(M-2,M-2-(N-1),-1):
        ans = ans - A[i] 
print(ans)

Solution (Python):
n,m = (int(i) for i in input().split())
x = list(int(i) for i in input().split())
x = sorted(x)
if m>1:
    res = x[-1]-x[0]
    dif = []
    for i in range(m-1):
        dif.append(abs(x[i+1]-x[i]))
        
    dif = sorted(dif,reverse = True)
    if n<m:
        for i in range(n-1):
            res -= dif[i]
    else:
        res = 0
else:
    res = 0
print(res)

Solution (Python):


import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
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()
xM = LIST()
xM.sort()

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

gaps.sort(reverse=True)
print(sum(gaps[N-1:]))

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

x.sort()
x_d=[]
prv=x[0]
for xi in x[1:]:
  x_d.append(xi-prv)
  prv=xi
x_d.sort(reverse=True)
ans=sum(x_d)
for i in range(min(m-1, n-1)):
  ans-=x_d[i]
print(ans)

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

X = sorted(X)

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

d = sorted(d)

print(sum(d[:max(0,len(d)-N+1)]))

Solution (Python):

import numpy as np
N, M = map(int,input().split())
X = list(map(int,input().split()))
X = sorted(X)
d = np.diff(X)
d = sorted(d)
if N == 1:
    print (sum(d))
else:
    print (sum(d[:-(N-1)]))