Problem:
 Score : 300 points    Problem Statement We will play a one-player game using a number line and N pieces.  First, we place each of these pieces at some integer coordinate.  Here, multiple pieces can be placed at the same coordinate.  Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:  Move : Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1 .  Note that the coordinates where we initially place the pieces are already regarded as visited.  Find the minimum number of moves required to achieve the objective.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq M \leq 10^5  -10^5 \leq X_i \leq 10^5  X_1, X_2, ..., X_M are all different.         Input Input is given from Standard Input in the following format:  N  M  X_1  X_2  ...  X_M       Output Find the minimum number of moves required to achieve the objective.        Sample Input 1 2 5
10 12 1 2 14      Sample Output 1 5  The objective can be achieved in five moves as follows, and this is the minimum number of moves required.   Initially, put the two pieces at coordinates 1 and 10 .  Move the piece at coordinate 1 to 2 .  Move the piece at coordinate 10 to 11 .  Move the piece at coordinate 11 to 12 .  Move the piece at coordinate 12 to 13 .  Move the piece at coordinate 13 to 14 .        Sample Input 2 3 7
-10 -3 0 9 -100 2 17      Sample Output 2 19       Sample Input 3 100 1
-100000      Sample Output 3 0

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

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

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

d.sort()
print(sum(d[:-(n-1)])) if n-1 > 0 else print(sum(d))

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

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

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

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

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

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

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

    if N >= M:
        return 0

    x = [j-i for i, j in zip(t, t[1:])]
    x.sort(reverse=True)
    return t[-1] - t[0] - sum(x[:N-1])


print(main())

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

    if N >= M:
        return 0

    x = [j-i for i, j in zip(t, t[1:])]
    x.sort(reverse=True)
    return (t[-1] - t[0]) - sum([x[i] for i in range(N - 1)])


print(main())

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

n, m = getNM()
point = getList()
point.sort()
diff = []
for i,j in zip(point, point[1:]):
    diff.append(abs(i-j))

diff.sort()
if n != 1:
    diff = diff[:-n+1]
print(sum(diff))

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(M <= N):
  print(0)
else:
  print(sum(tmp[:M-N]))

Solution (Python):
def main():
    n, m = map(int, input().split())
    t = list(map(int, input().split()))
    if n >= m:
        return 0
    t.sort()
    x = [[t[i], t[i+1] - t[i]] for i in range(m-1)]
    x.append([t[m-1], 0])
    x.sort(key=lambda a: a[1], reverse=True)
    sum_dis = t[m-1] - t[0]
    skip_dis = sum([x[i][1] for i in range(n - 1)])
    return sum_dis - skip_dis


print(main())

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

Solution (Python):



def main():
  n, m = map(int, input().split())
  xs   = list(map(int, input().split()))
  xs.sort()
  dxs = [abs(xs[i]-xs[i+1]) for i in range(len(xs)-1)]

  if n >= m:
    print(0)

  else:
    dxs.sort(reverse=True)
    del dxs[:(n-1)]
    print(sum(dxs))


if __name__=='__main__':
  main()

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

if n < m:
    x_far = [abs(x[i + 1] - x[i]) for i in range(m - 1)]
    x_far.sort()
    ans = 0
    for i in range (m - n):
        ans += x_far[i]
    print(ans)
else:
    print(0)

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

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

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(len(X)-1)]
diff.sort()
diff.reverse()

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

Solution (Python):
import numpy as np

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

p = np.sort(np.array(x))
dif = np.sort(p[1:] - p[:-1])
if n > 1:
    print(np.sum(dif[: 1 - n]))
else:
    print(np.sum(dif))

Solution (Python):

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()

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

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

x_sort = sorted(x_arr)

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

Solution (Python):
I=lambda:map(int,input().split());n,m=I();x=sorted(I());print(sum(sorted(j-i for i,j in zip(x,x[1:]))[:max(0,m-n)]))

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(" "))
XList = list(map(int,input().split()))

XList = sorted(XList)
deltaList = [0]

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


for i in range(N-1) :
    deltaList[i] = 0
    if i == len(deltaList)-1 :
        break
print(sum(deltaList))

Solution (Python):






import math
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
if n>=m:
    print(0)
else:
    dimX=[x[i]-x[i-1]for i in range(1,m)]
    
    dimX.sort(key=lambda x:-x)
    ans=x[-1]-x[0]
    for i in range(n-1):
        ans-=dimX[i]
    print(ans)

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
if n==1:
    print(max(x)-min(x))
elif m==1 or n>=m:
    print(0)
else:
    x.sort()
    goukei=0
    l=[0]
    for i in range(m-1):
        goukei+=x[i+1]-x[i]
        l.append(x[i+1]-x[i])
    i=0
    l.sort()
    for i in range(n-1):
        goukei-=l[-1-i]
    print(goukei)

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

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

x.sort()
d = [0] * (m - 1)

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

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

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(map(int,input().split()))
d=sorted(j-i for i,j in zip(X,X[1:]))[::-1]
print(sum(d[N-1:]))

Solution (Python):

input1 = input().split()
N = int(input1[0])
M = int(input1[1])
zahyo = list(map(int,input().split()))

if M == 1 or N >= M:
    print(0)
else:
    zahyo.sort()

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

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

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

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

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

x_list = sorted(x)

distance_list = []

for i in range(0, M-1):
    distance = x_list[i+1] - x_list[i]
    distance_list.append(distance)

result = sum(sorted(distance_list)[::-1][N-1:])
print(result)

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

margin = []
prev = x_list[0]
for i in range(1, M):
    margin.append(x_list[i] - prev)
    prev = x_list[i]

margin.sort(reverse=True)
print(x_list[-1] - x_list[0] - sum(margin[:N-1]))

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

sorted_place_list = sorted(place_list)

dis_list = []

if n >= m :
    ans = 0 
else:
    for i,place in enumerate(sorted_place_list[:m-1]):
        dis = abs(sorted_place_list[i+1]-place)
        dis_list.append(dis)

    sorted_dis_list = sorted(dis_list)
    ans = sum(sorted_dis_list[:m-n])

print(ans)

Solution (Python):
n,m=map(int,input().split(" "))
if n>=m:
  print(0)
  exit()
s=list(map(int,input().split(" ")))
s.sort()
t=[]
for i in range(1,m):
  t.append(s[i]-s[i-1])
t.sort()
for j in range(n-1):
  t.pop() 
print(sum(t))

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

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


X.sort()


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

L.sort(reverse=True)

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

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

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

if(N >= M):
    print(0)
else:
    L = []
    for i in range(1, M):
        L.append(X[i] - X[i-1])
    L.sort()
    res = X[-1] - X[0]
    for _ in range(N-1):
        res -= L.pop()
    print(res)

Solution (Python):
import copy
import itertools

def funcF(p):
    return p

def funcG(p):
    return funcF(p) + p*p

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

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
















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

print("{}".format(X[M-1]-X[0]-tmpL))

Solution (Python):
N, M = map(int, input().split())
x = list(map(int, input().split()))
x = list(sorted(x))
d = [x1 - x0 for x1, x0 in zip(x[1:], x)]
d = list(sorted(d))

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

Solution (Python):
def c_streamline(N, M, X):
    if N >= M:
        return 0  

    X.sort()
    x_distance_from_left = [0] * M
    for i in range(1, M):
        x_distance_from_left[i] = X[i] - X[i - 1]
    x_distance_from_left.sort(reverse=True)
    
    
    ans = (X[-1] - X[0]) - sum(x_distance_from_left[: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):

import sys

def split_with_func(string, func):
    return [func(x) for x in string.split()]

def get_line(func=None):
    string = sys.stdin.readline().rstrip()
    if func is not None:
        return split_with_func(string, func)
    else:
        return string

N, M = get_line(int)
data = sorted(list(set(get_line(int))))

M = len(data)
if M - N <= 0:
    print(0)
    exit()
ans = sum(sorted([data[i+1] - data[i] for i in range(M-1)])[:M-N])

print(ans)

Solution (Python):
fl = input()
sl = input()

koma_num, pos_num = [int (x) for x in fl.split(" ")]
poses = [int(x) for x in sl.split(" ")]
poses.sort()

if koma_num >= pos_num:
    print("0")
    exit()

dists = []
total = 0
for n in range(pos_num-1):
    dist = poses[n+1] - poses[n]
    dists.append(dist)
    total += dist

dists.sort()
dists.reverse()

if(len(dists) == 0):
    print("0")
    exit()

for n in range(koma_num-1):
    total -= dists[n]

print(total)

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

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

Solution (Python):
n, m = list(map(int, input().split()))
xxx = list(map(int, input().split()))
 
if n >= m:
    print(0)
    exit()
xxx.sort()
ddd = [b - a for a, b in zip(xxx, xxx[1:])]
ddd.sort()
print(sum(ddd[:m - n]))

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

X = sorted(X)
L = [X[i+1] - X[i] for i in range(M-1)]
L = sorted(L, reverse=True)
L_sum = sum(L)
ans = L_sum
for i in range(N-1):
  ans -= L[i]
print(ans)

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

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

Solution (Python):

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

Solution (Python):

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

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


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

X.sort()

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

Y.sort()

ans = 0

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

print(ans)

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

    else:
        X.sort()
        dist = [abs(X[i] - X[i+1]) for i in range(M-1)]
        dist.sort()
        ans = sum(dist[:(M-N)])
        print(ans)


if __name__ == "__main__":
    main()

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

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

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

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

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

X.sort()
Y=X[1:]-X[:-1]
Y.sort()

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

Solution (Python):
line = input().split(" ")
n = int(line[0])
m = int(line[1])
line = input().split(" ")
x = []
s = []

for i in line:
    x.append(int(i))

x.sort()
count = x[m-1] - x[0]

for i in range(len(x)-1):
    s.append(int(x[i+1] - x[i]))

s.sort(reverse=True)
print(count - sum(s[0:(n - 1)]))

Solution (Python):



import numpy as np

def calc_distance(goals):
    dist_list = []
    for i in range(len(goals) - 1):
        dist_list.append(goals[i+1] - goals[i])
    return dist_list

points_num, goal_num = map(int, input().split())

goals = list(map(int, input().split()))
goals = sorted(goals)


dist_list = calc_distance(goals)
dist_list = sorted(dist_list)

if goal_num <= points_num:
    print(0)
else:
    print(sum(dist_list[:goal_num-points_num]))

Solution (Python):
import numpy as np
import math
nm = list(map(int,input().split()))
x = list(map(int,input().split()))
xs = sorted(x)
n=nm[0]
m=nm[1]

def dis(a):
    amax = np.argmax(a)
    amin = np.argmin(a)
    if len(a) == 1:
        return(0)
    else:
        return a[amax]-a[amin]

if n == 1:
    print(dis(xs))
elif m ==1:
    print(0)
else:
    sk = [0 for i in range(m-1)]
    for i in range(m-1):
        sk[i]=xs[i+1]-xs[i]
    sks = sorted(sk)
    sksr = list(reversed(sks))
    print(dis(xs)-sum(sksr[:(n-1)]))

Solution (Python):

 
n, m = list(map(int, input().split()))
xs_str = input().split()
xs = list(map(int, xs_str))
xs.sort()
 
ys = []
for x1, x2 in zip(xs[0:-1], xs[1:]):
    ys.append(abs(x2 - x1))
ys.sort()

for i in range(n - 1):
    if ys == []:
        break
    else:
        ys.pop()
print(sum(ys))

Solution (Python):

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

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


def main() -> None:
    global Y
    for i in range(m - 1):
        Y += [X[i + 1] - X[i]]

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

    if len(Y) == 0:
        print(0)
    else:
        print(sum(Y))


if __name__ == "__main__":
    main()

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


def main() -> None:
    global Y
    for i in range(m - 1):
        Y += [X[i + 1] - X[i]]

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

    if len(Y) == 0:
        print(0)
    else:
        print(sum(Y))


if __name__ == "__main__":
    main()

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

print(ans)

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

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

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

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

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

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

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

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

        xsa=sorted(xsa)



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

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

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

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

distance = [0]*(M-1)
distance[0] = x[1]-x[0]
for i in range(0,M-1):
	distance[i] = x[i+1]-x[i]

distance_sorted = sorted(distance)
ans = 0
for i in range(0,M-N):
	ans += distance_sorted[i]

print(ans)

Solution (Python):
import sys

def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  ans = 0
  if N < M:
    g = [int(a) for a in input().split()]
    g = sorted(g)
    L = g[:-1]
    R = g[1:]
    sub = [b - a for a, b in zip(L, R)]
    sub = sorted(sub)
    ans = sum(sub[:(M-N)])
  print(ans)


if __name__ == '__main__':
  main()

Solution (Python):
N,M = map(int,input().split())
L = list(map(int,input().split()))
L = sorted(L)
A = []
for a,b in zip(L, L[1:]) :
    A.append(b-a)
A = sorted(A)

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

Solution (Python):

def solve(N, M, Xs):
    Xs = sorted(Xs)
    diffs = sorted([Xs[i+1] - Xs[i] for i in range(M-1)], reverse=True)
    return Xs[-1] - Xs[0] - sum(diffs[:N-1])


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    Xs = list(map(int, input().split(" ")))
    print(solve(N, M, Xs))

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

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

XS = sorted(X)
XD = []

if N > M:
    print(0)

else:
    for i in range(0, len(XS)-1):
        XD.append(XS[i + 1] - XS[i])

    XD2 = sorted(XD)
    print(sum(XD2[:M-N]))

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
if(len(X)<=N):
  print(0)
else:
  
  sort_X=sorted(X)
  
  
  X_sa=[x - y for (x, y) in zip(sort_X[1:], sort_X[0:len(X)-1])]
  
  sort_X_sa=sorted(X_sa)
  
  
  print(sum(sort_X_sa[:M-N]))

Solution (Python):
n = list(map(int, input().split())) 
i = list(map(int, input().split())) 
i.sort()
if(n[0] >= n[1]):
  print(0)
else:
	k=1
	kyori=list()
	while k < n[1]: 
	  sa=i[k]-i[k-1]
	  kyori.append(sa)
	  k=k+1
	kyori.sort()
	mm=1
	owari=n[1]-n[0]
	kyori=kyori[0:owari]
	idou=sum(kyori)
	print(idou)

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

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

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

sas=[]

for i in range(0,m-1):
  sas.append(points[i+1]-points[i])
sas.sort()


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

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

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

a=input()
num=[]
n=a.split()
for i in(n):
  num.append(int(i))

num.sort()
sa=[]
sa_a=[]

for i in range(len(num)-1):
  n=num[i]-num[i+1]
  if(n<0): n*=-1
  sa.append(n)
  sa_a.append(n)
sa.sort()
sa.reverse()
m=0
for i in range(N-1,len(sa)):
  m+=sa[i]
print(m)

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

def m():
    a = [0] * M
    b = [0] * M
    for i in range(1, M):
        a[i] = abs(X[i] - X[i-1])
        b[i] = i

    if M <= N:
        return 0
    a.sort(reverse=True)
    for i in range(N-1):
        a[i] = 0

    return sum(a)


print(m())

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

def diffculc(st, ed):
    if ed < st:
        tmp = ed
        ed = st
        st = tmp
    if st < 0 and ed < 0:
        diff = abs(st - ed)
    elif -1 < st and ed < 0:
        diff = abs(ed - st)
    elif st < 0 and -1 < ed:
        diff = abs(st - ed)
    elif -1 < st and -1 < ed:
        diff = ed - st
    return diff

xList = [int(x) for x in input().split()]
sortedList = sorted(xList)


diffcache = {}
diff = 0
for i, x in enumerate(sortedList):
    if i == len(sortedList) - 1:
        break
    diffcache[i] = diffculc(sortedList[i+1], x)

rangeset = []
counter = 0
for k, v in sorted(diffcache.items(), key=lambda x: -x[1]):
    if counter == N - 1:
        break
    diffcache.pop(k)
    counter = counter + 1

result = 0
for k,v in diffcache.items():
    result = result + v
print(result)

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

Solution (Python):
import sys

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


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


X.sort()


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

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

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

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

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

Solution (Python):
from sys import stdin





import numpy as np


input = stdin.readline().split()
m = int(input[0])
n = int(input[1])

data = np.zeros(n)
d = stdin.readline().split()
for i,x in enumerate(d):
    data[i] = int(x)
data = sorted(data)

dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]

out = 0



dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]
out = sum(sorted(dif, reverse=True)[m-1:])

            

print(int(out))

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 = x[-1] - x[0]
for i in split:
    ans -= x[i] - x[i - 1]
print(ans)

Solution (Python):
import numpy as np
N, M = map(int, input().split())
list_x = list(map(int, input().split()))
list_x.sort()
list_d = np.diff(list_x)
list_data = list_d.tolist()
if len(list_data) != 0:
    if N >= 2:
        list_data.sort()
        del list_data[-(N-1):]
        print(sum(list_data))
    if N == 1:
        print(sum(list_data))
else:
    print(0)

Solution (Python):
import copy

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

if N > M:
    ans = 0
else:
    ans = 0
    X.sort()
    diff = []
    for i in range(len(X) - 1):
        diff.append(X[i + 1] - X[i])
    diff_orig = copy.copy(diff)
    diff.sort()
    index = []
    for i in range(N - 1):
        index.append(diff_orig.index(diff.pop()))
    index.append(-1)
    index.sort()
    i = len(index) - 1
    index.append(len(X) - 1)
    while i >= 0:
        ans += X[index[i + 1]] - X[index[i] + 1]
        i -= 1
print(ans)

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

    if M == 1:
        print(0)
    elif N == 1:
        print(max(X) - min(X))
    else:
        if N > M:
            N = M
        dx = sorted([abs(X[m] - X[m + 1]) for m in range(M - 1)])
        print(sum(dx[:M - N]))


main()

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

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

Solution (Python):
N, M = input().split()
N = int(N)
M = int(M)
X = list(map(int, 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)
elif N == 1:
    print(sum(L))
else:
    print(sum(L[:-N+1]))

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(abs(max(X) - min(X)))
else:
    X.sort()
    X_ = [((j - i), c) for c, (i, j) in enumerate(zip(X, X[1:]))]
    X_s = sorted(X_, reverse=True)
    koma_c = sorted([t[1] for t in X_s[:N-1]])

    if koma_c[-1] != M-1:
        koma_c.append(M-1)

    p = 0
    c = 0
    for i in koma_c:
        c += X[i] - X[p]
        p = i + 1

    print(c)

Solution (Python):
import numpy as np

a1 = list(int(i) for i in input().split())  
x = list(int(i) for i in input().split())  
x = np.sort(x)
n = a1[0]

delta = []
if len(x) <= n:
    print(0)
else:
    for i in range(0,len(x) - 1):
        delta.append(x[i + 1] - x[i])

    if n == 1:
        print(np.sum(delta))
    else:
        delta = np.sort(delta)
        f = delta.size - (n - 2)
        print(np.sum(delta[0:-(n-1):]))

Solution (Python):
from sys import stdin

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

    if N >= M:
        print(0)
        return

    X.sort()
    n1 = [(i + 1, X[i+1] - X[i]) for i in range(M - 1)]
    n1.sort(reverse=1, key=lambda x: x[1])
    n2 = n1[:N-1]
    n2.sort()

    ans, i = 0, 0
    for j, _ in n2:
        ans += X[i:j][-1] - X[i:j][0]
        i = j
    ans += X[i:][-1] - X[i:][0]
    print(ans)

input = lambda: stdin.readline()
main()

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

d = []
X.sort()
p = 0
for x in X:
    d.append(x-p)
    p = x 

d.pop(0)
d.sort()
for i in range(N-1):
    if(len(d)>0):
        d.pop()

if(len(d)>0):
    print(sum(d))
else:
    print(0)

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

Solution (Python):
import numpy as np

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

xm = np.array(xm)

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

Solution (Python):

def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def db(x): 
    global debug
    if debug: print(x)
debug = False
N,M = getIntList()
A = getIntList()
db((N,M))
db(A)
A.sort()
db(A)
b = zeros(M-1)
for i in range(M-1):
    b[i] = A[i+1] - A[i]
db(b)
b.sort()
b.reverse()
db(b)
sm = 0
for i in range(N-1,M-1):
    sm += b[i]
print(sm)

Solution (Python):
N, M = map(int, input().split())
X = [int(x) for x in input().split()]
X.sort()
 
D = [0 for _ in range(M-1)]
for m in range(1, M):
    D[m-1] = X[m] - X[m-1]
D.sort()

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

Solution (Python):






def main():
    N, M = [int(i) for i in raw_input().strip().split(' ')]
    X = [int(i) for i in raw_input().strip().split(' ')]

    if N >= M:
        print 0
        exit()

    sortx = sorted(X)
    dist = []
    for i in range(len(sortx) - 1):
        dist.append(sortx[i+1] - sortx[i])
    sortd = sorted(dist, reverse=True)
    print sum(sortd[N-1:])


if __name__ == '__main__':
    main()

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

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  Y=Y[:N-1]
  Y.sort()
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)