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

X.sort()
cnt = 0
arr = [0]*(M-1)
if N >= M:
    ans = 0
else:
    for i in range(1,M):
        arr[i-1] = (abs(X[i-1]-X[i]))
    
    arr.sort()
    ans = sum(arr[0:M-N])
print(ans)

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

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

x.sort()

dists = []
point = x.pop(0)
for xs in x:
    dists.append(xs-point)
    point = xs
dists.sort(reverse=True)

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

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

print(sum(ans)-sum(ans[0:N-1]))

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

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

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

Solution (Python):

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

xl.sort()
difl = []

for i in range(1, m):
    difl.append(xl[i]-xl[i-1])

difl.sort(reverse=True)

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

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    n,m = map(int, input().split())
    l = list(map(int, input().split()))
    l.sort()

    ans = 0
    if n<m:
        L = []
        for i in range(m-1):
            L.append(l[i+1] - l[i])
        L.sort()
        for i in range(m-n):
            ans += L[i]
    print(ans)

if __name__=="__main__":
    main(
    )

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

Solution (Python):
import heapq
N, M = map(int, input().split())
if N >= M:
    print(0)
    exit()
X = list(map(int, input().split()))
X.sort(reverse=True)
dist = []
for i in range(1, M):
    dist.append(X[i] - X[i - 1])
dist = list(map(lambda x: (-1) * abs(x), dist))
heapq.heapify(dist)

for _ in range(N - 1):
    heapq.heappop(dist)
ans = -1 * sum(dist)
print(ans)

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

X.sort()

if N >= M:
    print(0)

elif N == 1:
    print(abs(max(X) - min(X)))

else:
    load = [X[i] - X[i - 1] for i in range(1, M)]
    load.sort()
    print(sum(load[:M - N]))

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

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

def main(n,m,x):
    if m<=n:
        return 0
    xs=sorted(x)
    
    d=[]
    for i in range(m-1):
        d.append((xs[i+1]-xs[i],i+1))
    ds=sorted(d,reverse=True,key=lambda x:x[0])
    c=[0]*m
    for i in range(n-1):
        c[ds[i][1]]+=1
    
    ans=0
    for i in range(1,m):
        if c[i]==0:
            ans+=xs[i]-xs[i-1]
    return ans

if __name__=='__main__':
    n,m,x=readinput()
    ans=main(n,m,x)
    print(ans)

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

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

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

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

    if n >= m:
        return 0
    diff_list =[0]
    for i in range(m-1):
        diff_list.append(pos_list[i+1]-pos_list[i])
    diff_list.sort()
    
    
    if n==1:
        return sum(diff_list)
    return sum(diff_list[:-n+1])

print(main())

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

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

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

if n>=m:
  print(0)
  sys.exit()
for i in range(m-1):
  dp[i]=x[i+1]-x[i]
dp.sort()

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

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

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

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
t_3 = max(x) - min(x)
t = []
t_2 = 0
x.sort()
if m == 1:
    print(0)
    exit()

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

t.sort(reverse=True)
pp = min(n-1,m-1)
for i in range(pp):
    t_2 += t[i]

print(t_3-t_2)

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

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

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

D.sort(reverse=True)
d=0
if i>1:
    for i in range(min(n-1,m-1)):
        d += D[i]    

ans = sum(D)-d
print(ans)

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

Solution (Python):
N,M=map(int,input().split())
if N>M:
    print(0)
    exit()
lst=list(map(int,input().split()))
lst.sort()
sum_1=max(lst)-min(lst)
D=[]
for i in range(1,M):
    D.append(lst[i]-lst[i-1])
D.sort(reverse=True)
sum_2=0
for i in range(N-1):
    sum_2+=D[i]
ans=0
ans=sum_1-sum_2
print(ans)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, *X = map(int, read().split())

    if N >= M:
        print(0)
        return

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

    dist.sort()
    ans = sum(dist[: M - N])

    print(ans)
    return


if __name__ == '__main__':
    main()

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(0 if M<=N else sum(D[:M-N]))

Solution (Python):
import heapq

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


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

    sum_distance = sum(distance)
    heapq.heapify(distance)

    count = 0 
    for _ in range(N-1):
        max_num = heapq.heappop(distance)
        count += max_num

    ans = abs(sum_distance - count)

print(ans)

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

else:
    x.sort()
    for i in range(m-1):
        d.append(abs(x[i] - x[i + 1]))
    d.sort()
    print(sum(d[:m-n]))

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

Solution (Python):
import math

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

diff = [0 for _ in range(m - 1)]

for i in range(m - 1):
    diff[i] = xL[i + 1] - xL[i]

diff = sorted(diff, reverse=True)

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

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

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

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

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

s = 0

if n >= m:
    s = 0
else:
    x.sort()
    d = []
    for j in range(m-1):
        d.append(x[j+1]-x[j])
    d.sort()
    s = sum(d[:m-n])

print(s)

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

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

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

X.sort()

memo = []

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

memo.sort()

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

Solution (Python):



def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    diff = list()

    for i, j in zip(x, x[1:]):
        diff.append(abs(i - j))

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


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
n,m = map(int, input().split())
X = sorted(np.array(input().split(), dtype = np.int64))
X = sorted(np.diff(X))
if m-n >= 0:
    ans = sum(X[:m-n])
else:
    ans = 0
print(ans)

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

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

L.sort()

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

Solution (Python):
n,m = map(int, input().split())
xl = list(map(int, input().split()))
xl.sort()
selected = m-n
if selected <= 0:
    print(0)
    exit()

diffs = []
for x1,x2 in zip(xl[:-1],xl[1:]):
    diff = x2-x1
    diffs.append(diff)

diffs.sort()
ans = sum(diffs[:selected])
print(ans)

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

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

Solution (Python):
def get_section_arr(X):
    return [(X[i+1]-X[i]) for i in range(len(X)-1) ]
if __name__ == "__main__":
    N,M = map(int,input().split())
    X = list(map(int,input().split()))
    X.sort()
    section_arr = get_section_arr(X)
    section_arr.sort(reverse=True)
    print((X[-1]-X[0]) - (sum(section_arr[:N-1])))

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

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

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

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

  if N >= M:
    print(0)
    return 0
  
  X = list(map(lambda x: int(x), input().split(" ")))
  X.sort()
  D = [X[i+1] - X[i] for i in range(len(X) - 1)]
  D.sort()
  print(sum(D[:M-N]))

main()

Solution (Python):
import sys
import math


def input():
    return sys.stdin.readline().rstrip()


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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
if n>=m:
  print(0)
else: 
  step = []
  div = []
  num = []
  for i in range(m-1):
    step.append([x[i+1]-x[i],i+1])
  step.sort(reverse=True)
  for i in range(n-1):
    num.append(step[i][1])
  num.sort()
  temp = []
  for i in range(m):
    if len(num)!=0:
      if i==num[0]:
        div.append(temp)
        del num[0]
        temp = []
    temp.append(x[i])
  div.append(temp)
  ans = 0
  for l in div:
    ans += l[-1]-l[0]
  print(ans)

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

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

dis = [0]*(M-1)

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

dis.sort()

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

Solution (Python):
if __name__ == '__main__':
	n,m = map(int,input().split())
	A = list(map(int,input().split()))
	A.sort()

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

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

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]







n,m = MI()
x = RI()

x.sort()


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


sa.sort(reverse=True)


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

Solution (Python):
n,m,*x=map(int,open(0).read().split())

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

x=sorted(x)

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

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

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

Solution (Python):







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

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

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

new_X=sorted(X)


if M<=N:
    print(0)

elif N==1:
    print(new_X[-1]-new_X[0])

else:
    numbers=[]
    for m in range(M-1):
        number=new_X[m+1]-new_X[m]
        numbers.append(number)
    new_numbers=sorted(numbers)
    for _ in range(N-1):
        del new_numbers[-1]
    print(sum(new_numbers))

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

import sys
if n>=m:
    print(0)
    sys.exit()

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

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

x = sorted(x)
dist = [x[i+1] - x[i] for i in range(len(x) - 1)]

dist = sorted(dist)

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

print(sum(sorted(diff)[:m-n]))

Solution (Python):
def main():
    n, m = map(int, input().split())
    Xs = sorted(list(map(int, input().split())))
    Xs_diff = []
    if n >= m:
        print(0)
        exit()

    for i in range(1, m):
        Xs_diff.append(Xs[i]-Xs[i-1])

    Xs_diff.sort()
    ans = sum(Xs_diff[:(m-n)])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, m = nm()
    X = nl()
    X = sorted(X)
    D = [X[i + 1] - X[i] for i in range(m - 1)]
    D = sorted(D, reverse=True)
    print(sum(D[n - 1:]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, m = nm()
    X = nl()
    if n >= m:
        print(0)
    else:
        X = sorted(X)
        D = [X[i + 1] - X[i] for i in range(m - 1)]
        D = sorted(D, reverse=True)
        print(sum(D[n - 1:]))


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import sys

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

X.sort()

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

Y=sorted(Y)[:max(0,M-N)]
print(sum(Y))

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

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

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

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

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

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

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

distance.sort(reverse=True)

res = 0
for i in range(min(n - 1, len(distance))):
    tmp, index = distance[i]
    koma_pos.append(index)

koma_pos.sort()
koma_pos.append(m)
for i in range(min(n, len(koma_pos) - 1)):
    l = koma_pos[i]
    r = koma_pos[i + 1]
    res += x[r - 1] - x[l]

print(res)

Solution (Python):
k, n = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
df = [0] * (n - 1)
for i in range(n - 1):
    df[i] = abs(x[i + 1] - x[i])

df = sorted(df, reverse=True)
print(sum(df[k - 1 :]))

Solution (Python):
import math
import sys
import collections
import bisect


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    print(0 if m <= n else sum(
        sorted([x[i + 1] - x[i] for i in range(m - 1)])[0:(m-n)]))


if __name__ == '__main__':
    main()

Solution (Python):
import math
import sys
import collections
import bisect


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if m <= n:
        print(0)
        return
    y = sorted([x[i + 1] - x[i] for i in range(m - 1)])
    print(sum(y[0:(m-n)]))


if __name__ == '__main__':
    main()

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

Solution (Python):
def solve():
  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(reverse=True)
  ans = X[-1]-X[0]-sum(lis[:N-1])
  return ans
print(solve())

Solution (Python):

import sys

def main():
    input = sys.stdin.readline
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
    else:
        dis = []
        for i in range(m - 1):
            dis.append(x[i + 1] - x[i])
        dis.sort(reverse=True)
        print(max(x) - min(x) - sum(dis[:n - 1]))



if __name__ == '__main__':
    main()

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

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

if n==1:print(sum(p));exit()
print(sum(sorted(p)[:1-n]))

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

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


X.sort()
diff = []
sum_dist = X[-1] - X[0]

for i, x in enumerate(X):
    diff.append(x - X[i - 1])


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

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

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

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

Solution (Python):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, m = inintm()
X = inintl()

X.sort()
diff = []

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

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

diff.sort()

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
if N>M:
  N = M
Xs = list(mapint())
Xs.sort()
dist = []
for i in range(1, M):
    dist.append(Xs[i]-Xs[i-1])
dist.sort()

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

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_sort=sorted(b,reverse=1)
print(sum(b_sort[n-1:]))

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

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

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

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

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

x_diff.sort()

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

Solution (Python):
N,M = list(map(int,input().split()))
X = list(map(int,input().split()))
sortedX = sorted(X)
deltaList = []
for i in range(len(sortedX) - 1):
    delta = sortedX[i + 1] - sortedX[i]
    deltaList.append(delta)
sortedDeltaList = sorted(deltaList)
answer = 0
count = M - 1 - N + 1
if count <= 0:
    answer = 0
else:
    for i in range(count):
        answer += sortedDeltaList[i]
print(answer)

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()
print(sum(Y[: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.sort()
  delta_X = [X[i]-X[i-1] for i in range(1,M)]
  delta_X.sort()
  print(sum(delta_X[0:M-N]))

Solution (Python):




import itertools
from bisect import bisect_right,bisect_left

INF = 10**10


def solve(n,m,x):
  if n >= m:
    return 0
  else:
    d = list(sorted(x[i+1] - x[i] for i in range(m-1)))[::-1]
    res = x[-1] - x[0]
    for i in range(min(m-1,n-1)):
        res -= d[i]
    return res

def main():
  N,M = map(int,input().split())
  x = sorted(list(map(int,input().split())))
  print(solve(N,M,x))
  return

if __name__ == '__main__':
  main()

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

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

X = sorted(X)

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

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

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

ans = sorted([X[x+1] - X[x] for x in range(M-1)])

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

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

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

Solution (Python):
import sys




N,M,*X = map(int, open(0).read().split())



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

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

Solution (Python):
import sys




N,M,*X = map(int, open(0).read().split())



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

X.sort()

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

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

Solution (Python):
import sys




N,M,*X = map(int, open(0).read().split())



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

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

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

Solution (Python):
import sys




N,M,*X = map(int, open(0).read().split())



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

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

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

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

Solution (Python):
N,M = map(int,input().split())
X = sorted(map(int,input().split()))
D = sorted(x2-x1 for x1,x2 in zip(X,X[1:]))[::-1]
print(sum(D[N-1:]))

Solution (Python):
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
D = sorted([X[n+1]-X[n] for n in range(M-1)])

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

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

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

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

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

Solution (Python):


import numpy as np

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

x = np.array(sorted(Xs))
if N == 1:
    print(np.sum(np.sort(np.diff(x))))
else:
    print(np.sum(np.sort(np.diff(x))[:-N+1]))

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

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

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

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

Solution (Python):

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

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

Solution (Python):
import numpy as np
n,m = map(int, input().split())
x = np.array([int(i) for i in input().split()])
x = np.sort(x)
d = np.diff(x)
d = np.sort(d)[::-1]
if m < n:
    ans = 0
else:
    '''
    x ---+---+----------+-+----+-
    d    3   10         1 4    -
    このとき、d = 3,1の点を選んでいる
    なぜ？
        d = 10で分割している
    分割したあとは、左から距離を足せばおｋ
    分割の回数はn-1個 -> dの大きいものからn-1個除去し、残りのsumを取る
    '''
    ans = d[n-1:].sum()
print(ans)

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