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()
x2=[]

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

for i in range(len(x2)-N+1):
    s+=x2[i]
print(s)

Solution (Python):
n, m = [int(x) for x in input().split()]
xlist = [int(x) for x in input().split()]
if len(xlist) == 1:
    print(0)
else:
    xlist.sort()
    difflist = []
    for i in range(len(xlist)-1):
        difflist.append(abs(xlist[i] - xlist[i + 1]))
    difflist.sort(reverse=True)
    if difflist[n - 1:] != []:
        print(sum(difflist[n - 1:]))
    else:
        print(0)

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

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

result = 0
for i, yi in enumerate(Y):
    if i >= N - 1:
        result += yi

print(result)

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

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

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

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

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

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

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

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

ans=sum(Y)
for i in range(min(N-1,M-1)):
    ans-=Y[i]
print(ans)

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

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

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

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

Solution (Python):

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

X = list(map(int, input().split()))
X = sorted(X)
X = [x-X[0] for x in X]
diff = [x-y for x,y in zip(X[1:], X)]


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


print(sum)

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


if __name__ == "__main__":
    main()

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

diff = [X[i] - X[i - 1] for i in range(1, M)]
diff.sort()
ret = 0
for i in range(len(diff) - N + 1):
    ret += diff[i]
print(ret)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys


def solve(N, M, Xs):
    if M <= N:
        return 0

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

    return sum(diffs[:M - N])


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

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

Y = sorted(Y)
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()
Y = [X[i+1]-X[i] for i in range(M-1)]
Y.sort()

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

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
a = sorted(X)
b = sorted([x - y for x, y in zip(a[1:], a)])
ans = sum(b[:(-N+1)]) if N >= 2 else sum(b)
print(ans)

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

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

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

Solution (Python):
import sys
import numpy as np

input = sys.stdin.readline
n, m = map(int, input().split())
x_list = list(map(int, input().split()))
x_list.sort()
dis_list = []
start = x_list[0]
for i in range(1, m):
    dis_list.append(abs(start-x_list[i]))
    start = x_list[i]
dis_list.sort(reverse=True)
ans = sum(dis_list[n-1:])
print(ans)

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

x=sorted(x)

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

diff = sorted(diff,reverse=True)


ans =sum(diff)
for i in range(min(n-1,m-1)):
  ans = ans - diff[i]
print(ans)

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

Solution (Python):
import math as mt
import itertools as it
import functools as ft
import random as rnd

stdin = lambda type_ = "int", sep = " ": list(map(eval(type_), input().split(sep)))
joint = lambda sep = " ", *args: sep.join(str(i) if type(i) != list else sep.join(map(str, i)) for i in args)

N, M = stdin()
X = stdin()
X.sort()
dif = [0] * (len(X) - 1)
for i in range(len(X) - 1):
    dif[i] = X[i + 1] - X[i]

dif.sort()
ans = X[-1] - X[0] - sum(dif[-1:-N:-1])
print(0 if ans <= 0 else ans)

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
Y = []
for a,b in zip(X,X[1:]):
    Y.append(b-a)
Y.sort()
print(sum(Y[:max(0,M-N)]))

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

Solution (Python):

def B():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    cnt = 0
    for i in range(N - 1):
        cnt += L[i]
    if L[-1] < cnt:
        print("Yes")
    else:
        print("No")


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

Solution (Python):

import heapq as hq
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
a=[]
ans=x[-1]-x[0]
if n>=m:
    print(0)
    exit()
for i in range(m-1):
    hq.heappush(a,x[i]-x[i+1])
for i in range(n-1):
    ans+=hq.heappop(a)
print(ans)

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

Solution (Python):
N,M=list(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],i])

Y=sorted(Y,key=lambda y: y[0])
ans=0
for i in range(M-N):
    ans+=Y[i][0]
    
print(ans)

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(m-1):
        y.append(abs(x[i]-x[i+1]))
    y.sort()
    sa = m - n
    res = 0
    for i in range(sa):
        res += y[i]
    print(res)

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(a[i+1]-a[i])
b.sort(reverse=True)
ans = 0
for i in range(n-1,len(b)):
    ans += b[i]
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()

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

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

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

goal = sorted(goal)
sabun = [goal[_+1]-goal[_] for _ in range(M-1)]
sabun = sorted(sabun)

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

Solution (Python):
def read():
    return int(input())


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


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


N, M = readmap()
X = readlist()

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

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

print(ans)

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

n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
nums = []
for i in range(1, m):
    nums.append(X[i] - X[i-1])
nums.sort()
for i in range(n-1):
    if len(nums) == 0:
        break
    nums.pop()
print(sum(nums))

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def main():
    n, m = get_int_list()
    xs = get_int_list()

    if n >= m:
        print(0)
        return
    xs.sort()
    diffs = [a - b for a, b in zip(xs[1:], xs)]
    diffs.sort()
    print(sum(diffs[:m-n]))


if __name__ == '__main__':
    main()

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

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

result = sum(ds[:max(M - N, 0)])

print(result)

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

if n >= m:
    print(0)
else:
    lst.sort()
    diff_lst = []
    for i in range(len(lst)-1):
        diff = lst[i+1] - lst[i]
        diff_lst.append(diff)
    diff_lst.sort(key = lambda x: -x)
    print(sum(diff_lst[n-1:]))

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

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

hq = []
for i in range(m-1):
    heapq.heappush(hq, x[i+1] - x[i])


move = 0

for i in range(m-n):
    move += heapq.heappop(hq)

print(move)

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

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

x.sort()

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

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

distance.sort()

ans = sum(distance[:(m-n)])
print(ans)

Solution (Python):



import bisect

def IT(): 
    return int(input())
def IL(): 
    return [int(_) for _ in input().split()]
def IS(): 
    return [_ for _ in input().split()]
def ILS(n): 
    return [int(input()) for _ in range(n)]
def SLS(n): 
    return [input() for _ in range(n)]
def ILSS(n): 
    return [[int(_) for _ in input().split()] for j in range(n)]


def bit_full_search(ss):
    n = len(ss)
    for i in range(1 << n):
        s = ""
        for j in range(n + 1):
            if ((1 & i >> j) == 1):
                s += ss[j]
        print(s)
def bit_full_search2(A):
    
    value = []
    for i in range(1 << len(A)):
        output = []

        for j in range(len(A)):
            if ((i >> j) & 1) == 1:
                
                output.append(A[j])
        value.append([format(i, 'b').zfill(16), sum(output)])

    value.sort(key=lambda x:x[1])
    bin = [value[k][0] for k in range(len(value))]
    val = [value[k][1] for k in range(len(value))]
    return bin, val
def matching_bisect(list1_val, list2_val, n):
    
    j = 0
    for i in range(len(list1_val)):
        j = bisect.bisect_left(list2_val, n - list1_val[i])
        if j < len(list2_val):
            if list1_val[i] + list2_val[j] == n:
                
                break
    return j

"""ここからメインコード"""
def main():
    n, m = IL()
    x = IL()
    x.sort()

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

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


main()

Solution (Python):
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
spans = []
for i in range(len(x) - 1):
    spans.append(x[i + 1] - x[i])
spans.sort()

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

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
x = list(li())
if n >= m:
    print(0)

else:
    diffs = []
    
    x.sort()
    
    for idx in range(m-1):
        diffs.append(x[idx+1]-x[idx])
        
    diffs.sort()
    
    for _ in range(n-1):
        diffs.pop()
        
    print(sum(diffs))

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

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



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

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

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

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

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

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

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

x = sorted(x)

d = []

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

d = sorted(d)

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

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

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

Solution (Python):

def  inpl(): return list(map(int, input().split()))
N, M = inpl()
X = sorted(inpl())
D = sorted([X[i+1] - X[i] for i in range(M-1)])
if N == 1:
    print(sum(D))
else:
    print(sum(D[:-N+1]))

Solution (Python):
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
are = [0]*(m-1)
for i in range(m-1):
        are[i]= a[i+1] -a[i]
are.sort()
for i in range(n-1):
        if are:
                are.pop()
print(sum(are))

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

ans = 0
if N < M:
    L = []

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

    L.sort()
    for i in range(M-N):
        ans += L[i]
print(ans)

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):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, m = na()
xs = na()
xs.sort()

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

ds.sort()

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