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 = list(map(int, input().split()))
x.sort()
if m == 1:
    print(0)
else:
    u = [0] * (100010)
    for i in range(m-1):
        u[i] = x[i+1]-x[i]
    u.sort(reverse=True)
    c = 0
    for i in range(n-1):
        c += u[i]
    print(sum(u)-c)

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

ans=0
if n>=m:
    ans=0
else:
    x.sort()
    diff=[0]*(m-1)

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

    diff.sort()

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

print(ans)

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


if __name__ == "__main__":
    main()

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

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

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

dist = []
x.sort()
if M < N:
    print(0)
    exit()
for i in range(M-1):
    dist.append(abs(x[i+1]-x[i]))

dist.sort()

for i in range(N-1):
    dist.pop()
 
print(sum(dist))

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

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

Solution (Python):
import sys

N, M = map(int, input().split())
x = sorted(map(int, input().split()))

if N >= M:
    print(0)
    sys.exit()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort()
ans = sum(diff[:M-N])
print(ans)

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

if __name__ == '__main__':
    main()

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

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

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

def quicksort(a, left: int, right: int):
    
    pl=left
    pr=right
    pivot= a[(left +right)//2]

    

    while pl<=pr:
        while a[pl] <pivot: pl+=1
        while a[pr] >pivot: pr-=1
        if pl <= pr:
            a[pl], a[pr]= a[pr], a[pl]
            pl+=1
            pr-=1

    if left < pr: quicksort(a, left, pr)
    if pl < right: quicksort(a, pl, right)

def quick_sort(a):
    quicksort(a, 0, len(a)-1)

quick_sort(x)
dis_list=[0]*(m-1)
dis_sm=0
for i in range(m-1):
    dis=x[i+1]-x[i]
    dis_list[i]=dis
    dis_sm+=dis

quick_sort(dis_list)
for i in range(n-1):
    dis_sm-=dis_list[-(i+1)]

print(dis_sm)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = []
for s, t in zip(x, x[1:]):
    y.append(t - s)
y.sort()
if n >= m:
    print(0)
else:
    print(sum(y[:-(n-m)]))

Solution (Python):
import heapq
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
diff=[]
heapq.heapify(diff)
if n>=m:
    print(0)
else:
    for i in range(m-1):
        heapq.heappush(diff,x[i]-x[i+1]+1)
    temp=0
    for _ in range(n-1):
        temp-=heapq.heappop(diff)
    print(x[m-1]-x[0]+1-temp-n)

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

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, m, *x = map(int, read().split())

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

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

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

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

Solution (Python):

n, m, *x = map(int, open(0).read().split())
if n >= m:
    print(0)
    quit()
x.sort()
a = []
for i in range(m-1):
    a += [x[i+1]-x[i]]
a.sort()
ans = sum(a[: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()
A = []
for i in range(M-1):
    A.append(X[i+1]-X[i])
A.sort()
print(sum(A[:M-N]))

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

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

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

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

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

Solution (Python):
import numpy as np
import sys
n, m = map(int, input().split())
if n >= m:
    print(0)
    sys.exit()
x = [int(i) for i in input().split()]
x = np.array(sorted(x))
diff = np.abs(np.diff(x))
diff = sorted(diff)
if n == 1:
    print(sum(diff))
else:
    print(sum(diff[:-n + 1]))

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

ans = 0
if n < m:
    X.sort()
    L = sorted([X[i + 1] - X[i] for i in range(m - 1)], reverse=True)
    ans = sum(L[n - 1: ])

print(ans)

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)[::-1]
print(sum(s[N-1:]))

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

lis =sorted(lis)

if n > m:
    print(0)
    exit()
    
tmp = []
for i in range(m-1):
    tmp.append(lis[i+1] - lis[i])
    
tmp = sorted(tmp)

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

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

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

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

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

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
x = sorted(x,reverse=True)
dist = []
for i in range(len(x)-1):
    dist.append(x[i]-x[i+1])
dist = sorted(dist,reverse=True)
ext = n-1
for i in range(ext):
    if not dist:
        print(0)
        exit()
    dist.pop(0)
print(sum(dist))

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

Solution (Python):
def main():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    x = sorted(x)
    y = []
    s = x[m - 1] - x[0]
    for i in range(1, m):
        y.append(x[i] - x[i - 1])
    y = sorted(y, reverse=True)
    m = sum(y[0: n - 1])
    print(s - m)
    
if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(N: int, M: int, X: "List[int]"):
    if N >= M:
        return 0
    X = sorted(X)
    D = [x2 - x1 for x1, x2 in zip(X, X[1:])]
    return sum(sorted(D)[:M-N])



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    X = [int(next(tokens)) for _ in range(M)]  
    print(solve(N, M, X))

def test():
    import doctest
    doctest.testmod()

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.sort()
  dist = []
  
  for i in range(M-1):
    dist.append(X[i+1] - X[i])
    
  dist.sort()
  ans = 0
  
  for i in range(M-N):
    ans += dist[i]
    
  print(ans)

Solution (Python):
N,M=map(int,input().split())
S=list(map(int,input().split()))
S.sort()
L=[0]*(M-1)
for i in range(M-1):
    L[i]=abs(S[i]-S[i+1])

L.sort()
goukei=0
if M-N>=1:
    for i in range(M-N):
        goukei+=L[i]
else:
    print(0)
    exit()
print(goukei)

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

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

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

li.sort()

for i in range(m-1):
    ans.append(li[i+1]-li[i])

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

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

Solution (Python):
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.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)
        return
    X.sort()
    distance = []
    for i in range(M-1):
        distance.append(X[i+1] - X[i])
    distance.sort()
    for _ in range(N-1):
        distance.pop()
    ans = sum(distance)
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):
n, k = map(int, input().split())
pp = tuple(sorted(map(int, input().split())))

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

intv = []
for i in range(1, len(pp)):
    intv.append(pp[i] - pp[i - 1])
intv = sorted(intv)

ans = pp[-1] - pp[0]
for i in range(n - 1):
    ans -= intv[-i - 1]
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    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.sort(reverse=True)

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


if __name__ == "__main__":
    main()

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

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

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

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



a.sort()

b = []
for i in range(m-1):
    b.append(abs(a[i+1]-a[i]))
b.sort()
b.reverse()

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

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

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

arr=np.sort(np.array(x))
dif=np.diff(arr)

lis=dif.tolist()
lis=sorted(lis)

for i in range(n-1):
    if len(lis)>0:
        lis.pop()

print(sum(lis))

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

arr=np.array(sorted(x))
dif=np.diff(arr)

lis=dif.tolist()
lis=sorted(lis)

for i in range(n-1):
    if len(lis)>0:
        lis.pop()

print(sum(lis))

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

arr=np.array(sorted(x))
dif=np.diff(arr)

lis=dif.tolist()
lis=sorted(lis,reverse=True)

for i in range(n-1):
    if len(lis)>0:
        lis.pop(0)

print(sum(lis))

Solution (Python):
import fractions
import sys
from functools import reduce
import math

input = sys.stdin.readline

def main():
    n, m = input_list()
    if n >= m:
        print(0)
        exit(0)
    x = list(sorted(input_list()))
    diffs = []
    for i in range(1, m):
        a = abs(x[i] - x[i-1])
        diffs.append((i, a))
    dd = list(sorted(diffs, key=lambda v: v[1], reverse=True))
    ans = 0
    for i, v in enumerate(dd):
        if i < n-1:
            continue
        ans += v[1]
    print(ans)
    
    
    
    
    
    


def divide_list(l, args):
    result = []
    pre = 0
    for i, a in enumerate(args):
        if i != 0:
            pre = args[i-1]
        result.append(l[pre:a])
        if i == len(args) - 1:
            result.append(l[a:])
    return result

def divide_two(n):
    c = 0
    while True:
        if c >= 2:
            break
        if n % 2 != 0:
            break
        n //= 2
        c += 1
    return c

def get_camulative(l):
    import itertools
    
    return [0] + list(itertools.accumulate(l))

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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


def input_list_str():
    return list(map(str, input().split()))


def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())
X = sorted(list(map(int, input().split())))
A = []
for a, b in zip(X, X[1:]):
	A.append(b - a)
A.sort()
print(sum(A[:m-n]) if m - n > 0 else 0)

Solution (Python):
n, m = map(int, input().split())
X = sorted(list(map(int, input().split())))
A = []
for a, b in zip(X, X[1:]):
	A.append(b - a - 1)
A.sort(reverse = True)
print(X[-1] - X[0] + 1 - sum(A[:n-1]) - n if n < m else 0)

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

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

    idxs = sorted([0] + [diff[1] + 1 for diff in diffs], reverse=True)

    ans = 0
    pos = -1
    for i in range(m):
        if idxs and i == idxs[-1]:
            pos = x[idxs.pop()]
        else:
            ans += x[i] - pos
            pos = x[i]
    print(ans)

Solution (Python):
n,m=map(int,input().split())
l=sorted(list(map(int,input().split())))
if n>=m:
    print(0)
    exit()
s=l[-1]-l[0]
l=sorted([a-b for a,b in zip(l[1:],l)], reverse=True)
print(s-sum(l[:n-1]))

Solution (Python):



n, m = map(int, input().strip().split())
xs = sorted(list(map(int, input().strip().split())))
if n >= m:
    print(0)
elif n == 1:
    print(xs[-1] - xs[0])
else:
    intervals = sorted([(xs[i + 1] - xs[i], i) for i in range(m - 1)], reverse=True)

    divides = sorted([ind for _, ind in intervals[:n - 1]])

    total = 0
    for i in range(len(divides) + 1):
        if i == 0:
            interval = xs[divides[i]] - xs[0]
        elif i < len(divides):
            interval = xs[divides[i]] - xs[divides[i - 1] + 1]
        else:
            interval = xs[-1] - xs[divides[i - 1] + 1]

        total += interval

    print(total)

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
index = []
ans = 0
s = 0
for i in range(len(X)-1):
    index.append([i,X[i+1]-X[i]])
index.sort(key=lambda x: x[1],reverse=True)
if N<M:
    for i in range(N-1,len(index)):
        ans += index[i][1]

print(ans)

Solution (Python):


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

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

x.sort()

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

diff.sort()


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

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

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

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

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

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

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

ans = 0

if N >= M:
  pass
else:
  X.sort()
  dist = []

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

  ans = sum(dist[N-1:])

print(ans)

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort(key=int)
p=[0]*M
for i in range(1,M): p[i]=X[i]-X[i-1]
p.sort(key=int)
for i in range(min(N-1,M)): p.pop(-1)
print(sum(p))

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

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

ld = sorted([lx[i+1]-lx[i] for i in range(m-1)])
if n == 1:
    print(sum(ld))
else:
    print(sum(ld[:-(n-1)]))

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

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

x.sort()

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

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

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

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

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

Solution (Python):
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *

from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction



mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]








N, M = map(int,input().split())
Xm = list(map(int,input().split()))
 
if N >= M:
  print(0)
  sys.exit()
  
Xm.sort()
ans = Xm[M-1]-Xm[0]
 
Li = [Xm[i+1]-Xm[i] for i in range(M-1)]
Li.sort(reverse=True)
ans -= sum(Li[0:N-1])
print(ans)

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

if N >= M:
  print(0)
  sys.exit()
  
Xm.sort()
ans = Xm[M-1]-Xm[0]

Li = [Xm[i+1]-Xm[i] for i in range(M-1)]
Li.sort(reverse=True)
ans -= sum(Li[0:N-1])
print(ans)

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


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

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

l = sorted(l)[::-1]

del l[0: n - 1]

print(sum(l))

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

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

l = sorted(l)[::-1]

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

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


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

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

l = sorted(l)[::-1]

del l[0: n - 1]

print(sum(l))

Solution (Python):
import sys

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

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

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

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

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

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

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

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

if n==1:
    print(max(x) - min(x))
    exit(0)

x= sorted(x)

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

dist = sorted(dist,reverse=True)

total=max(x)-min(x)
for j in range(n-1):
    total -=dist[j]


print(total)

Solution (Python):
import numpy as np
n,m = map(int,input().split())
x = list(map(int,input().split()))
if n>=m:
    print(0)
else:
    b = sorted(x)
    d = list(np.diff(b))
    d.sort(reverse=True)
    sum = sum(d[:n-1])
    print(b[m-1]-b[0]-sum)

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

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

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

print(ans)

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

Solution (Python):
def main():
    import numpy as np
    n, m = map(int, input().split())
    inlis = list(map(int, input().split()))
    if n >= m:
        print(0)
    else:
        inlis.sort()
        arr = np.array(inlis)
        
        diff = np.diff(arr)
        diff = list(diff)
        
        diff.sort()
        
        for _ in range(n-1):
            diff.pop()
        print(sum(diff))



if __name__ == "__main__":
    main()

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

X.sort()
ans = X[-1] - X[0]
D = []

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

for i in range(N-1):
  ans -= D[i]
print(ans)

Solution (Python):
n, m = map(int, input().split())
X = sorted(map(int, input().split()))
diff = []
for x, y in zip(X, X[1:]):
    diff.append(y-x)
diff.sort()
print(sum(diff[:m-n]) if 0 <= m-n else 0)

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7



N, M = MAP()
X = LIST()
X.sort()

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7



N, M = MAP()
X = LIST()
X.sort()

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

if N != 1:
	del Y[-(N-1):]


print(sum(Y))

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

    A.sort()
    tot = A[-1] - A[0]

    B = [a1 - a0 for a0, a1 in zip(A, A[1:])]
    B.sort()

    for _ in range(N - 1):
        if not B: break
        tot -= B.pop()
        if tot is 0: break

    print(tot)


if __name__ == '__main__':
    main()

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

D = [abs(X[i] - X[i+1]) for i in range(M-1)]
D.sort(reverse=True)

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

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n,m=map(int,readline().split())
    x=list(map(int,readline().split()))
    x.sort()
    d=[0]*(m-1)
    for i in range(1,m):
        d[i-1]=x[i]-x[i-1]

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

if __name__=="__main__":
    main()

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

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

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

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

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

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

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

DIFFS.sort()
for i in range(N - 1):
    if DIFFS != []:
        DIFFS.pop(-1)

print(sum(DIFFS))

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

l = []

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

for i in range(n-1):
  l[i] = 0
  
print(sum(l))

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

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

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

for i in range(n-1):
  ans -= y[i]
  
print(ans)

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

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

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

d.sort()

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

print(sum(d))

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