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())
a = [int(i) for i in input().split()]
a.sort()
sa  = []
for i in range(len(a)):
    if i == 0:
        continue
    tmp = max(a[i],a[i-1]) - min(a[i],a[i-1])
    sa.append(tmp)
sa.sort()
sa.reverse()
tmp = 0
for i, num in enumerate(sa):
    if i < n - 1:
        continue
    tmp += num
print(tmp)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


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


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


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, X):
    X.sort()
    diff = []
    for i in range(len(X)-1):
        diff.append(X[i+1] - X[i])
    diff.sort(reverse=True)
    diff = diff[N-1:]
    return sum(diff)


def main():
    
    N, M = read_int_n()
    
    X = read_int_n()
    print(slv(N, M, X))


if __name__ == '__main__':
    main()

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

if m <= n:
    print(0)
else:
    lis2 = [0]
    for i in range(m-1):
        lis2.append(int(abs(lis[i+1] - lis[i])))
    lis2.sort()
    lis2.reverse()
    del lis2[0:(n-1)]
    print(sum(lis2))

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

difference = []
for i in range(len(Xs)-1):
    difference.append(Xs[i+1] - Xs[i])
    
difference.sort()
sum_d = 0
for i in range(N_M[1] - N_M[0]):
    sum_d += difference[i]

print(sum_d)

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)

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

Solution (Python):
def func():
    pass


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

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

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

    top_n_d = sorted(d, reverse=True)[:n - 1]

    
    
    

    print(max(x) - min(x) + 1 - sum(top_n_d) - 1)


if __name__ == '__main__':
    main()

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

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

x_points.sort()

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

diffs.sort()
if  N == 1:
    print(sum(diffs))
    quit()

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

Solution (Python):
import sys
from operator import itemgetter


def r(n, m, xl):
    if len(xl) == 1:
        return 0
    if n >= m:
        return 0
    xl.sort()
    min_x = abs(min(xl))
    xl = [x + min_x for x in xl]
    xm = [xl[i+1] - xl[i] for i in range(len(xl)-1)]
    xm.sort()
    xxb = xm[:m-n]
    return sum(xxb)


def main():
    input = sys.stdin.readline
    n, m = list(map(int, input().split()))
    xl = list(map(int, input().split()))

    print(r(n, m, xl))


if __name__ == '__main__':
    main()

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

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

Solution (Python):
def sol(n, S):
    if S is None or len(S) == 0: return 0
    S.sort() 
    
    diffs = []
    pre = S[0]
    for i in range(1, len(S)):
        diffs.append(S[i] - pre)
        pre = S[i]
    diffs.sort(reverse=True)
    
    for x in range(n-1):
        if len(diffs) > 0:
            del diffs[0]
    return sum(diffs)



test = False
if test:
    n, m = 2, 5
    S =list( map(int, "10 12 1 2 14".split()))
    print(sol(n, S))

    n, m = 3, 7
    S =list( map(int, "-10 -3 0 9 -100 2 17".split()))
    print(sol(n, S))

    n, m = 100, 1
    S =list( map(int, "-100000".split()))
    print(sol(n, S))


else:
    n, m = map(int, input().split())
    S =list( map(int, input().split()))
    
    print(sol(n, S))

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


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

for i in range(N-1):
    if not costs:
        print(0)
        exit(0)
    costs.pop(0)




print(sum(costs))

Solution (Python):
def main():
    n, m = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]
    if n >= m:
        return 0
    x.sort()
    dxs = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    dxs.sort()
    return sum(dxs[:m - n])

print(main())

Solution (Python):

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

if N >= M:
    re = 0
else:
    X = np.sort(X) 
    diff = np.insert(X[1:], X.size - 1, 0) - X
    re = diff[0:X.size-1].sum()
    diff_sort = np.sort(diff[0:X.size-1])[::-1]
    for i in range(N - 1):
        re -= diff_sort[i]
        
print(re)

Solution (Python):
n,m=list(map(int,input().split()))
l=list(map(int,input().split()))
l.sort()
d=[]
for i in range(0,m-1):
    d.append(l[i+1] - l[i])
d.sort()
for i in range(0,n-1):
    ld = len(d)
    if ld == 0:
        print(0)
        import sys
        sys.exit()
    d.pop(ld-1)
print(sum(d))

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

Solution (Python):
Y=input()
y=input()
X=[]
x=[]
X=Y.split(" ") 
x=y.split(" ")
Z=[int(i) for i in X] 
z=[int(j) for j in x]
z.sort()
N=Z[0]
M=Z[1]
a=z[M-1]-z[0]
b=[]
c=0
if N<M :
    for i in range(0,M-1):
        b.append(int(abs(int(z[i+1])-int(z[i]))))
    b.sort()
    b.reverse()
    for j in range(0,N-1):
        c=b[j]+c
    print(a-c)
else :
    print('0')

Solution (Python):

from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007


"""
t,x = LI()
print(t/x)
"""


"""
n= II()
a = LI()
m = max(a)
if m >= sum(a)-m:
    print("No")
else:
    print("Yes")
"""


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

"""
n,k = LI()
a = LI()
l = 0
b = [0 for i in range(n)]
for i in range(n):
    b[i] = list(bin(a[i]))[2:]
    l = max(l,len(b[i]))
s = [0 for i in range(l)]
for i in range(n):
    for j in range(l-len(b[i])):
        b[i].insert(0,"0")
for i in range(n):
    for j in range(l):
        s[j] += 1-int(b[i][j])
ke = 1
for i in range(l)[::-1]:
    if s[i] <= n//2:
        s[i] = [(n-s[i])*ke,0,float("inf")]
    else:
        s[i] = [(n-s[i])*ke,s[i]*ke,ke]
    ke *= 2
s.sort(key = lambda x:x[0])
d = 0
ans = 0
key = len(list(bin(k)))-2
key -= l
ke = 2**l
print(s)
for i in range(key):
    d += ke
    ans += n*ke
    ke *= 2
for i in range(l):
    r,q,p = s[i]
    if d+p <= k:
        d += p
        ans += q
    else:
        ans += r
print(ans)
"""

Solution (Python):


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

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

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

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

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

x = input().split()

for i in range(len(x)):
    x[i] = int(x[i])

x.sort()

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

    dis.sort(reverse=True)
    for i in range(n-1):
        dis[i] = 0

    sum = 0
    for i in dis:
        sum += i

    print(sum)

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

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

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

a.sort()

su = 0

b = [[0,0] for i in range(n-1)]
for i in range(n-1):
	b[i][0] = a[i+1]-a[i]
	b[i][1] = i
	su += a[i+1]-a[i]
b.sort(key = lambda x:x[0])
b.reverse()

if m >= n:
	print(0)
else:
	for i in range(m-1):
		su -= b[i][0]
	print(su)

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(M-1):
                Y[i] = X[i+1] - X[i]
        Y.sort()
        print(sum(Y[:(M-N)]))

Solution (Python):
data = input().split()  
n = int(data[0])
m = int(data[1])
x = [int(s) for s in input().split()]

x.sort()

y=[]
tmp=0
for i in x:
    y.append(i-tmp)
    tmp=i
y.pop(0)

y.sort()
y.reverse()

res=x[-1]-x[0]

if(len(y)+1 < n):
    n = len(y)+1

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

print(res)

Solution (Python):
N,M = map(int,input().split())
x_str = input().split()
X = [0]*M
for i in range(M):
    X[i] = int(x_str[i])
X.sort()
if N >= M:
    print("0")
else:
    
    dx = [0]*(M-1)
    for j in range(M-1):
        dx[j] = X[j+1] - X[j]
    dx.sort()
    dx.reverse()
    length = 0
    for k in range(N-1,M-1):
        length += dx[k]
    print(length)

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

distance = list()
for i in range(1, m):
    distance.append(x[i] - x[i-1])

distance = sorted(distance, reverse=True)
moved = 0
for j in range(n-1, len(distance)):
    moved = moved + distance[j]

print(moved)

Solution (Python):
N, M = map(int, raw_input().split())
X = sorted(map(int, raw_input().split()))
D = [X[i+1]-X[i] for i in range(M-1)]
indexes = reversed([D.index(x) for x in sorted(D)[-(N-1):]])
start = 0
ans = 0
for index in indexes:
  ans += X[index]-X[start]
  start = index+1
ans += X[-1]-X[start]
if N == 1:
  print X[-1]-X[0]
else:
  print ans

Solution (Python):
N, M = map(int, input().split())
X_arr = list(map(int, input().split()))
X_sorted  = sorted(X_arr)
X_diff = []
for i in range(M - 1):
    X_diff.append(X_sorted[i + 1] - X_sorted[i])
X_diff_sorted  = sorted(X_diff)
for n in range(N - 1):
    if X_diff_sorted == []:
        break
    else:
        X_diff_sorted.pop()
print(sum(X_diff_sorted))

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

Solution (Python):
from sys import stdin
import numpy as np
N,M=[int(x) for x in stdin.readline().rstrip().split()]
data=[int(x) for x in stdin.readline().rstrip().split()]
if N >= M:
    ans=0
else:
    data=np.array(sorted(data))
    diff=sorted(np.diff(data))
    ans=sum(diff[0:len(diff)-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):
s = list(map(int, input().split()))
t = list(map(int, input().split()))

if(s[0] >= s[1]):
  print(0)
else:
  t.sort()
  u=[]
  for i in range(s[1]-1):
    u.append(t[i+1]-t[i])
  u.sort()
  total=0
  for i in range(s[1]-s[0]):
    total+=u[i]
  print(total)

Solution (Python):

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

if n>=m:
    print(0)
    exit()
if n==1:
    print(max(xl)-min(xl))
    exit()


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

res = sum(l) - sum(l[0:n-1])
print(res)

Solution (Python):
import numpy as np

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

xs=sorted(x)





def sabun(x):
    a=np.array(x)
    return list(a[1:]-a[:-1])



sabunx=sabun(xs)


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

Solution (Python):

"""
Created on Sun Feb  3 21:15:56 2019

@author: doppe
"""

A, B = map(int, input().split())
L = [int(i) for i in input().split()]

if (A >= B):
    print(0)
else:
    saList = []
    L.sort()    
    for i in range(0, len(L) -1):
        saList.append(L[i + 1] - L[i])
    li1 = sorted(range(len(saList)), key=lambda k: saList[k])

    splitChiten = []
    for i in range(0, A - 1):
        splitChiten.append(li1[len(li1) - 1 - i])
    splitChiten.sort()
    sum = 0
    j = 0
    for i in range(0, len(splitChiten)):
        sum += (L[splitChiten[i]] - L[j])
        j = splitChiten[i] + 1
    
    sum += (L[len(L) - 1] - L[j])
    print(sum)

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

xs = input()
xs = xs.split()
for i in range(m):
    xs[i] = int(xs[i])
xs = sorted(xs)

if (n>=m):
    print(0)
else:
    diff= []
    for i in range(m-1):
        diff.append(xs[i+1]-xs[i])
    s = np.argsort(diff)
    loop = 0
    total = sum(diff)
    if n == 1:
        print(total)
    else:
        for i in range(n-1):
            total -= diff[s[-(i+1)]]
        print(total)

Solution (Python):
from sys import stdin
import numpy as np

n, m = map(int, stdin.readline().rstrip().split())
x = [int(x) for x in stdin.readline().rstrip().split()]

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

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

x.sort()
y = [abs(x2 - x1) for x1, x2 in zip(x[1:], x[:-1])]
y.sort()

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

Solution (Python):
import numpy as np
N, M = map(int, input().split())
if N >= M:
    res = 0
else:
    array = np.array(sorted([int(i) for i in input().split()]))
    res = np.sort(np.diff(array, n=1))[::-1]
    res = sum(res[N-1:])
print(res)

Solution (Python):
n,m=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
x.sort()
a=[]
if m==1 or n>=m:
    print("0")
else:
    for i in range(m-1):
        a.append(x[i+1]-x[i])
    a.sort(reverse=True)
    if n==1:
        print(max(x)-min(x))
    else:
        result=0
        for i in range(n-1):
            result+=a[i]
        print(max(x)-min(x)-result)

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

    delta = sorted([X[i]-X[i-1] for i in range(1, M)], reverse=True)

    return sum(delta[N-1:])


def main():
    print(solve())


if __name__ == '__main__':
    main()

Solution (Python):
N,M=map(int,input().split())
poti=list(input().split())
poti= [ int(s) for s in poti ]
poti=sorted(poti)
sa=[]
if N>=M:
    print(0)
else:
    for i in range(0,M-1):
        a=poti[i+1]-poti[i]
        sa.append(a)


    if len(sa)!=0:  
        sa=sorted(sa)
        sa=sa[0:(M-1)-(N-1)]
        print(sum(sa))
    else:
        print(0)

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

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

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

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

d.sort()
s = sum(d)
print(s-sum(d[-(n-1):]))

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

if M <= N:
	print(0)
elif M == 2:
	if N == 1:
		X_list = [int(_) for _ in list(input().split())]
		print(abs(X_list[0]-X_list[1]))
	else:
		print(0)
else:
	X_list = [int(_) for _ in list(input().split())]

	X_list.sort()
	X_list.reverse()

	X_diff_list = [X_list[i-1]-X_list[i] for i in range(1, M)]
	X_diff_list.sort()
	X_diff_list.reverse()

	answer = sum(X_diff_list)

	if N == 1:
		print(answer)
	else:
		for n in range(N-1):
			answer -= X_diff_list[n]
		print(answer)

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

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

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

Solution (Python):
nm=[int(x) for x in input().split()]
x=[int(y) for y in input().split()]
margin=[]
sort_x=sorted(x)
for p in range(nm[1]-1):
    margin.append(sort_x[p+1]-sort_x[p])
sort_mar=sorted(margin)
if(nm[0]>=nm[1]):
    print(0)
else:
    print(sum(sort_mar[:nm[1]-nm[0]]))

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


b = sorted(b)
x = [[b[0],0]]

for i in range(1,a[1]):
    x.extend([[b[i],b[i]-b[i-1]]])

x = sorted(x ,key = lambda x: -x[1])

for i in range(a[1]):
    if i < a[0]-1:
        x[i].append(1)
    else:
        x[i].append(0)

x = sorted(x ,key = lambda x: x[0])

x[0][2] = 1

tmp = 0
res = 0

for i in range(1,a[1]):
    if x[i-1][2] == 1:
        tmp = x[i-1][0]
    if x[i][2]   == 1:
        res = res - tmp + x[i-1][0]
    elif i+1 == a[1]:
        res = res - tmp + x[i][0]
        
print(res)

Solution (Python):
N, M = map(int,input().split())
X = [int(x) for x in input().split()]
X.sort(reverse=True)
valX = [X[x] - X[x + 1] for x in range(M - 1)]
valX.sort(reverse=True)

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

if N == 1:
    print(max(X) - min(X))
    exit()

for i in range(N - 1):
    valX[i] = 0

print(sum(valX))

Solution (Python):


import sys


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, m = LI()
X = LI()

X.sort()

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


C.sort()
C.reverse()
ans = 0
for i in range(n - 1, m):
    if i >= m - 1:
        break
    ans += C[i]
    

print(ans)

Solution (Python):
import itertools
n,m = map(int,input().split())
zahyou_list = [i for i in map(int,input().split())]
zahyou_list.sort()

kaisa_list = []



for i in range(len(zahyou_list)-1):
    kaisa_list.append(zahyou_list[i+1] - zahyou_list[i])
mini = sum(kaisa_list)
kaisa_list.sort()

if n >= m:
    print(0)
elif n == 1:
    print(sum(kaisa_list))
else:
    print(sum(kaisa_list[:-(n-1)]))

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(tmp != []):
  if(len(tmp) == 1):
    print(*tmp)
  elif(N == 1):
    print(sum(tmp))
  else:
    print(sum(tmp[:-(N-1)]))
else:
  print(0)

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

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

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

if n >= m:
    print(0)
else:
    for j in range(n-1):
        diffsum = diffsum - diff[j]
    print(diffsum)

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split()))) 
diff = sorted([abs(e1 - e2) for e1, e2 in zip(x, [x[0]]+x[:-1])], reverse = True)


if n >= m:
    res = 0
else:
    res = sum(diff[n-1:])

print(res)

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

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

d = []

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

res = sum(d) - sum(d[0:N-1])

print(res)

Solution (Python):
def distance_to_right(p, point_list):
    if p == len(point_list) - 1:
        return None
    else:
        return point_list[p+1] - point_list[p]




num_koma, num_to_visit = [int(element) for element in input().split(' ')]
points = [int(element) for element in input().split(' ')]

points.sort()

costs = [distance_to_right(p, points) for p in range(0, num_to_visit-1)]
costs.sort()

if num_koma == 1:
    print(sum(costs))
else:
    print(sum(costs[:-(num_koma-1)]))

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

X.sort()

if N >= len(X):
    print(0)
    quit()

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

dif.sort(reverse=1)
sum = sum(dif)

for i in range(N-1):
    sum = sum - dif[i]

print(sum)

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

Solution (Python):
N,M = map(int,input().split())
array = list(map(int, input().strip().split(' ')))
array.sort()
array2 = []
for i in range(M-1):
  array2.append(array[i+1] - array[i])
array2.sort()
array2.reverse()
del array2[0:N-1]
sum = 0
for i in range(M-N):
  sum = sum + array2[i]
print(sum)

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

dist = []
for i in range(len(x)-1):
    dist.append([x[i+1]-x[i],i])
sorted_dist = sorted(dist,key=lambda x:x[0])
top_n = sorted(sorted_dist[-1*n+1:],key=lambda x:x[1])

total = 0
idx = 0
for _,i in top_n:
    total += abs(x[i] - x[idx])
    idx = i+1
total += abs(x[-1] - x[idx])

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

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

x_sort = sorted(x)
check = []
for i in range(len(x)-1):
    check.append(abs(x_sort[i+1] - x_sort[i]))

check_sorted = sorted(check)
ans = 0
if (m-n) >= 0:
    for i in check_sorted[0:m-n]:
        ans += i
    print(ans)
else:
    print(0)

Solution (Python):
spots_and_people = input().split()
spot_values = input().split()

N_spot = int(spots_and_people[1])
N_people = int(spots_and_people[0])

spot_values = [ int(value) for value in spot_values ]
spot_values.sort()

line_length = [ spot_values[i+1] - spot_values[i] for i in range(N_spot-1) ]
line_length.sort()


if N_spot <= N_people:
	travel = 0

else:
	for i in range(N_people-1):
  		line_length.pop(-1)
  

	travel = sum(line_length)

print(travel)

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

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

    division = set()
    if len(sub_X) != 0:
        for i in range(N-1):
            division.add(sub_X[i][0])
    ans = 0
    start = X[0]
    for i in range(M):
        if i in division:
            ans += X[i] - start
            start = X[i+1]
        if i == M-1:
            ans += X[i] - start

    print(ans)

Solution (Python):

N,M = map(int, input().split(" "))

X = []

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

X.sort()


Y = []

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

Y.sort()


kazu = len(Y)

kazu= kazu- (N-1)

ans2 = 0
for j in range(kazu):
    ans2 = ans2 + Y[j]

print(ans2)

Solution (Python):
import numpy as np

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

X = sorted(X)
X = np.array(X)
diff = np.diff(X)
diff = np.sort(diff)[::-1]




if N >= M:
  ans = 0
elif N == 1:
  ans = np.sum(diff)
else:
  ans = np.sum(diff[N-1:])

print(ans)

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

y.sort()


'''
for i in range(n-1):
    y.remove(max(y))
'''
if m > n: w = sum(y[:m-n])
else: w = 0
print(w)

Solution (Python):
def solve(n, m, xl):
    s = sorted(xl)
    l = s[:-1]
    r = s[1:]
    diff = []
    for i in range(len(l)):
        diff.append(r[i] - l[i])
    max_list = list(reversed(sorted(diff)))
    all_length = s[-1] - s[0]
    cur = all_length
    for i in range(n-1):
        if i >= len(max_list):
            break
        cur = cur - max_list[i]

    if cur < 0:
        cur = 0
    print(cur)

def main():
    n, m = map(int, input().split())
    xl = list(map(int, input().split()))
    solve(n, m, xl)        

main()

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


N, M = [int(elem) for elem in fin().split()]
X_list = sorted([int(elem) for elem in fin().split()])
min_X = X_list[0]
max_X = X_list[-1]
exists = [False] * (max_X - min_X + 1)
for X in X_list:
    X -= min_X
    exists[X] = True

distances = []
last_idx = 0
for i, flag in enumerate(exists[1:], start=1):
    if flag:
        distances.append(i - last_idx - 1)
        last_idx = i

distances.sort(reverse=True)


minimum_moves = max(0, max(0, len(exists) - N) - sum(distances[:N - 1]))

print(minimum_moves)

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

X.sort()
difX = [abs(i-j) for i,j in zip(X[1:],X[:-1])]
difX.sort()
if n == 1:
    ans = sum(difX)
else:
    ans = sum(difX[:-(n-1)])
print(ans)

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

sa = sorted(sa)


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

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

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

for i in range(len(X)-1):
    x[i] = X[i+1] - X[i]

x.sort()

ans = 0
for j in range(0, len(x)-(N-1)):
    ans += x[j]

print(ans)

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

if N==1:
    print(X[-1] - X[0])
else:
    D = [X[i+1]-X[i] for i in range(len(X)-1)]
    
    D.sort()
    print(sum(D[:-(N-1)]))

Solution (Python):
a = list(map(int,input().split()))
N = a[0]
M = a[1]
X = list(map(int,input().split()))
X.sort()
path = []
for i in range(1,M):
    path.append(X[i]-X[i-1])
path.sort()
choose = M - N
sum = 0
for i in range(choose):
    sum += path[i]
print(str(sum))

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

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

X = sorted(X)

if len(X) <= N:
	print(0)

else:
	l = []
	for n in range(len(X)):
		if n != 0:
			l.append(abs(X[n - 1] - X[n]))

	ans = 0
	l = sorted(l, reverse=False)
	for i in range(M - N):
		ans += l.pop(0)

	print(ans)

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

dis = [(L[i+1] - L[i],i) for i in range(M-1)]
dis1 = [L[i+1]-L[i] for i in range(M-1)]
dis = sorted(dis,key=lambda x:(x[0],-x[1]))[::-1]
l = [0]
for i in range(N-1):
    try:l.append(dis[i][1]+1)
    except:pass

l = sorted(l)
ans = 0
cnt = 0
for i in range(len(l)-1):
    cnt +=1
  
    ans += sum(dis1[l[i]:l[i+1]-1])
 
ans += sum(dis1[l[cnt]:])
print(ans)

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()
xd = sorted(np.diff(x))
print(sum(xd[:m-n]))

Solution (Python):
""" AtCoder """

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

X.sort()

Y = []
ans = 0

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

Y.sort()

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

print(ans)

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
sa = [0 for i in range(M-1)]

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

if N >= M:
    print(0)
elif N == 1:
    print(sum(sa))
else:
    print(sum(sa[0:-N+1]))

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

li = sorted(list(map(int,input().split())),reverse=True)
if n>=m:
    print(0)
    
else:
    long=max(li)-min(li)
    dis=[li[i]-li[i+1] for i in range(m-1)]
    dis_sort=sorted(dis,reverse=True)
    print(sum(dis_sort[n-1:]))

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
N, M = tuple(li())
coods = list(li())
if len(coods) == 1:
    print(0)
elif N >= M:
    print(0)
else:

    coods = sorted(coods)
    deltas = []
    former = coods[0]
    for cood in coods:
        delta = cood - former
        deltas.append(delta)
        former = cood

    deltas = sorted(deltas, reverse=True)
    total_dis = coods[-1] - coods[0]
    for i in range(N-1):
        total_dis -= deltas[i]

    print(total_dis)

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()
if n==1:
  print(sum(B))

else:
  print(sum(B[:-n+1:]))