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):
import sys
import os
import heapq
N,M = map(int,input().split())

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

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

Q = []
for i in range(M-1):
    heapq.heappush(Q,(-(X[i+1]-X[i]),i))

sum = 0
for i in range(1,N):
    sum = sum + (-heapq.heappop(Q)[0])
print(X[M-1]-X[0]-sum)

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

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

    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    x.sort()
    ls = [0, m - 1]
    d = []
    for i in range(1, len(x)):
        d.append([abs(x[i] - x[i-1]), i, i - 1])
    d.sort(reverse=True)
    for i in range(min(n-1, len(d))):
        ls.append(d[i][1])
        ls.append(d[i][2])
    ls.sort()
    t = 0
    for i in range(1, len(ls), 2):
        t += (abs(x[ls[i]] - x[ls[i-1]]))
    print(t)


if __name__ == '__main__':
    slove()

Solution (Python):
n, m = [ int(v) for v in input().split() ]
num_list = sorted([ int(v) for v in input().split() ])
diff_list = sorted([ abs(num_list[i] - num_list[i+1]) for i in range(m-1) ])

ans = 0
if n < m:
    for i in range(m-n):
        ans += diff_list[i]    
print(ans)

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

if M == 1:
  print (0)
  sys.exit()
Y = []
for i in range(M-1):
  Y.append(X[i+1]-X[i])
Y = sorted(Y)
for i in range(N-1):
  if len(Y) == 0:
    print (0)
    sys.exit()
  Y.pop()
print (sum(Y))

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

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

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

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

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

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

dist.sort()
ans = 0
if N < M:
    for i in range((M-1)-(N-1)):
        ans += dist[i]

print(ans)

Solution (Python):
import heapq

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

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

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


heapq.heapify(dist)
for i in range(N-1):
    if len(dist) == 0:
        break
    heapq.heappop(dist)

ans = 0
if len(dist) != 0:
    ans = -sum(dist)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    if n>=m:
        print(0)
        return
    X=list(map(int,input().split()))
    X.sort()
    D=[0]*(m-1)
    for i in range(m-1):
        D[i]=(i,X[i+1]-X[i])
    from operator import itemgetter
    D.sort(key=itemgetter(1),reverse=1)
    ans=0
    ppos=0
    for i in range(n-1):
        pos=D[i][0]
        ans+=X[pos]-X[ppos]
        ppos=pos+1
    print(ans+X[-1]-X[ppos])
resolve()

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

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

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

d.sort()

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

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

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

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

d.sort()

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

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

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

x.sort()

d = [x[i] - x[i-1] for i in range(1,m)]

d.sort()

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

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

koma_dist = []

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

if n<m:
    print(sum(sorted(koma_dist)[:m-n]))
else:
    print(0)

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

Solution (Python):
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])
lis.sort()
ans=0
if n<m:
    for i in range(m-n):
        ans+=lis[i]
print(ans)

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

Solution (Python):

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

Solution (Python):
N, M = map(int, input().split())
array = list(map(int, input().split()))
array.sort()
diff = [0] * (M-1)
for i in range(M-1):
    diff[i] = abs(array[i+1] - array[i])
diff.sort(reverse=True)
for i in range(N-1):
    if len(diff) == 0:
        break
    diff.pop(0)
print(sum(diff))

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

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

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

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

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

print(sum(diff[:max(0, m-n)]))

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

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

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7


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

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

X.sort()


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

cost.sort()
for i in range(N-1):
    cost.pop()

print(sum(cost))

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if n >= m:
        print(0)
    else:
        x.sort()
        y = []
        for i, v in enumerate(x):
            if i != 0:
                y.append([abs(v - x[i-1]), i, i-1])
        y = sorted(y, key=lambda x: x[0], reverse=True)
        cnt = 0
        z = [0, m-1]
        for i in range(n-1):
            z.append(y[i][1])
            z.append(y[i][2])
        z.sort()
        for i in range(1, len(z), 2):
            cnt += abs((x[z[i]] - x[z[i-1]]))
        print(cnt)


if __name__ == '__main__':
    slove()

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

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

Solution (Python):
import numpy as np

n,m=map(int,input().split())
x=[int(i) for i in input().split()]
if n>=m:
    print(0)
else:
    x.sort()

    dif=np.diff(x)

    dif.sort()

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

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

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

    for i in range(0,zan):
        res +=y[i]
    
    print(res)

Solution (Python):
N,M=map(int,input().split())
x=sorted(list(map(int,input().split())))
y=[]
for i in range(M-1):
   y.append(x[i+1]-x[i])
y=sorted(y)
z=0
for i in range(M-N):
  z+=y[i]
print(z)

Solution (Python):

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


print(ans)

Solution (Python):

n,m = map(int,input().split())
x = list(map(int,input().split()))
l = []

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

ans = x[m-1] - x[0]
l.sort(reverse=True)

for i in range(min(n-1,m-1)):
    ans -= l[i]
print(max(ans,0))

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

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

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


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

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

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

Solution (Python):
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print(0)
    elif N < M: 
        X.sort()
        diff = [0 for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = X[i+1] - X[i]
        diff.sort(reverse=True)
        
        ans = X[-1] - X[0]
        for i in range(N-1):
            ans -= diff[i]
        print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print(0)
    elif N < M: 
        X.sort()
        diff = [[0,0] for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = [X[i+1] - X[i],i]
        diff.sort(reverse=True)
        diff = diff[:N-1]
        diff.sort(key=lambda p:p[1])

        ans = 0
        cnt = 0
        for i in range(N-1):
            ans += X[diff[i][1]] - X[cnt]
            cnt = diff[i][1] + 1
        ans += X[-1] - X[cnt]
        print(ans)
if __name__ == "__main__":
    main()

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

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


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

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

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

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

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

Solution (Python):
if __name__ == '__main__':
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    del_X = [0]*M
    for i in range(1, M):
        del_X[i] = abs(X[i] - X[i-1])
    
    del_X.sort()
    
    if N == 1:
        pass
    
    for n in range(N-1):
        if len(del_X) > 0:
            del_X.pop()
    
    print(sum(del_X))

Solution (Python):

n,m=map(int,input().split())
if n>=m:
    print(0)
    exit()
x = list(map(int,input().split()))
ans=0
x.sort()
data=[(float('inf'),0)]*m
for i in range(1,m):
    data[i]=(x[i]-x[i-1],i)
data2 = sorted(data ,reverse=True)
data3 = sorted([data2[i][1] for i in range(n)]+[len(x)])
for i in range(1,n+1):
    ans += x[data3[i]-1] - x[data3[i-1]]
print(ans)

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

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

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

dif = {}
for i in range(M-1):
  dif[i] = X[i+1]-X[i]
  
sorted_dif = sorted(dif.items(), key=lambda x:x[1], reverse=True)
difs = 0
for i in range(N-1):
  difs += sorted_dif[i][1]

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

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

Solution (Python):


N, M = list(map(int, input().split()))
x_list = list(map(int, input().split()))
x_list = sorted(x_list)
d_list = [abs(a - b) for a, b in zip(x_list[:-1], x_list[1:])]
d_list = sorted(d_list, reverse=True)

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

print(ans)

Solution (Python):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n,m = inpl()
x = inpl()
x.sort()
dd = []
for i in range(m-1):
    dd.append(abs(x[i] - x[i+1]))
dd.sort()
s = 0
for i in range(m-n):
    s += dd[i]
print(s)

Solution (Python):
import numpy as np

N, M = map(int, input().split())
X = list(map(int, input().split()))
X_sort = np.array(sorted(X))[::-1]
X_diff = X_sort[:M-1] - X_sort[1:]

X_diff_sort = sorted(X_diff)[::-1]

print(max(sum(X_diff_sort[N-1:]), 0))

Solution (Python):
N,M = [int(x) for x in input().split()]
if N>=M:
  print(0)
  exit()
X = [int(x) for x in input().split()]
X.sort()
L = []
for i in range(M-1):
  L.append(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()
r=[0]*(len(x)-1)
for i in range(len(x)-1):
    r[i] = x[i+1]-x[i]
print(sum(sorted(r, reverse=True)[n-1:]))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, m = LI()
L = sorted(LI())


total = L[-1] - L[0]
diff_list = sorted([L[i+1] - L[i] for i in range(m-1)], reverse=True)
if n >= m:
    print(0)
else:
    print(total - sum(diff_list[:n-1]))

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

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

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

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

Solution (Python):
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
print(sum(list(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 M == 1 or N >= M: print(0); exit()

X = sorted(list(map(int, input().split())))
if N == 1: print(max(X) - min(X)); exit()

dif = list(sorted([[X[i+1] - X[i], i] for i in range(M-1)], reverse=True))[:N-1]
col = [x[1] for x in dif]
if col[0]:
	l = col[0]
	s = X[l] - X[0]
else:
	l = 0
	s = 0

col.append(M-1)

for i in col:
	s += X[i] - X[l]
	l = i+1
print(s)

Solution (Python):




N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
l = [X[i + 1] - X[i] for i in range(M - 1)]
l.sort()
for _ in range(N - 1):
  if len(l):
    l.pop()
print(sum(l))

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

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

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

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

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

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

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

if __name__ == '__main__':
    main()

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

def main():
    N,_ = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    D = []
    for i in range(len(X)-1):
        D.append(X[i+1] - X[i])
    D.sort(reverse=True)
    D = D[N-1:]
    n = 0
    for d in D:
        n += d
    print(n)

if __name__ == '__main__':
    main()

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

if N >= M:
  print(0)
  exit()
Ds = []
for i in range(1, M):
  D = Xs[i]-Xs[i-1]
  Ds.append(D)
Ds.sort()

r = sum(Ds[:M-N])
print(r)

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

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

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

if N >= M:
    print(0)
else:
    X.sort()
    dX = [(i, X[i + 1] - X[i]) for i in range(M - 1)]
    dX.sort(key=lambda x: x[1], reverse=True)
    print(sum(d[1] for d in dX[N - 1:]))

Solution (Python):
n, m = map(int, input().split())
xs = sorted(set(map(int, input().split())))
ds = sorted([y - x for x, y in zip(xs, xs[1:])])
ds = ds[:max(0, len(ds) - n + 1)]
if len(ds) > 0:
    print(sum(ds))
else:
    print(0)

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

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

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

Solution (Python):
n,m = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
if m <= n:
  print(0)
else:
  Dif_X = []
  for i in range(m-1):
    Dif_X += [X[i+1]-X[i]]
  Sum_Dif_X = sum(Dif_X)  
  Sorted_Dif_X = sorted(Dif_X, reverse = True)
  count = 0  
  while count < n-1:
    Sum_Dif_X -= Sorted_Dif_X[count]
    count += 1
  print(Sum_Dif_X)

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

if N >= M:
    print(0)
else:
    dX = [(i, X[i + 1] - X[i]) for i in range(M - 1)]
    dX.sort(key=lambda x: x[1], reverse=True)
    print(sum(d[1] for d in dX[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[:m-n]) if m>n else 0)

Solution (Python):
a,b = map(int,input().split())

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

d = []
for i in range(b-1):
    d.append(c[i+1] - c[i])

d.sort()



for k in range(a - 1):
    if d == []:
        print(0)
        exit()
    d.pop()

print(sum(d))

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

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

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

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

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

dis = []
for i in range(M-1):
    dis.append(abs(X[i]-X[i+1]))
dis1 = sorted(dis)

ans = 0
if N >= M:
    print(0)
else:
    for j in range(M-N):
        ans += dis1[j]
    print(ans)

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

Solution (Python):

N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
x = sorted(X)
i = 0
diff = []
while i + 1 < len(x):
    diff.append(x[i] - x[i + 1])
    i += 1
d = sorted(diff)[N - 1:]
print(abs(sum((d))))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
t = sorted(X, reverse=True)
h = [0] * M
for i in range(1, M):
    h[i] = abs(t[i - 1] - t[i])

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

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
x = LI()
x.sort()
if n >= m:
    print(0)
    quit()
y = [abs(x[i] - x[i+1]) for i in range(m-1)]
y.sort()
ans = sum(y[:m-n])
print(ans)

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

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

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

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

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

gap.sort()

ans = sum(gap)
for i in range(N-1):
    ans -= gap[-1-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(0)

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

gap.sort()
gap.reverse()
t = sum(gap)
for i in range(N-1):
    t -= gap[i]

print(t)

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

Solution (Python):
import itertools

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

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

X.sort()
A = list(y-x for x, y in zip(X, X[1:]))
A.sort(reverse=True)

print(sum(iter(A[N-1:])))

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

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

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

l2 = sorted([l[i] - l[i-1] for i in range(1,m)],reverse = True)

if len(l2) < n:
    print(0)
else:
    print(sum(l2[n-1:]))

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

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


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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    N,M = map(int, input().split())
    X = list(map(int, input().split()))
    X = sorted(X)
    X = X[::-1]
    Y = []
    for i in range(M-1):
        Y.append(X[i]-X[i+1])
    Y = sorted(Y)
    Y = Y[::-1]
    
    Ans = X[0] - X[-1]
    for i in range(min(N-1,M-1)):
        Ans -= Y[i]
    print(Ans)
    
    
    
main()

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

if n >= m:
  print(0)
  exit()
t = sorted([x[i]-x[i-1] for i in range(1, m)])
print(sum(t[:m-n]))

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

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

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

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


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

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

    sa = []
    for i in range(M-1):
        sa.append(x[i+1] - x[i])
    sa = sorted(sa, reverse=True)
    
    if N >= M:
        print(0)
    else:
        print(sum(sa[N-1:]))


if __name__ == "__main__":
    main()

Solution (Python):
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce



def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



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

data.sort()

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

disdata.sort(reverse=True)

ans = 0
for i in range(n-1, m-1):
    ans += disdata[i]

print(ans)

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

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

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

    if N >= M:
        print(0)
        exit()
    sort_X = sorted(X)
    diff = []
    for i in range(M-1):
        diff.append(sort_X[i+1]-sort_X[i])

    sort_diff = sorted(diff)

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

    print(ans)
    

main()

Solution (Python):

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

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

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



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

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

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

sa.sort(reverse=True)

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

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

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

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

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

if N >= M:
    print(ans)
else:
    for i in range(M - 1):
        d[i] = abs(X[i + 1] - X[i])
    d.sort(reverse=True)
    b = d[:N - 1]
    for i in range(M - 1):
        if d[i] in b:
            b.remove(d[i])
        else:
            ans += d[i]
    print(ans)

Solution (Python):

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

Solution (Python):
from sys import stdin
n, m, *x = map(int, stdin.read().split())
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m - n]) if n < m else 0)