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):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [long(input_raw[0]), long(input_raw[1])]
    input_raw = read_func().strip().split()
    X = []
    for i in range(M):
        X.append(int(input_raw[i]))

    if len(X) <= N:
        print 0
        return
    if N == 1:
        print max(X) - min(X)
        return

    X.sort()

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

    subX.sort(reverse = True)

    count = 0
    for i in range(N):
        if i == N - 1:
            count += X[M - 1] - X[subX[i - 1][1]]
        elif i != 0:
            count += X[subX[i][1] - 1] - X[subX[i - 1][1]]
        else:
            count += X[subX[i][1] - 1] - X[0]
    print count


if __name__ == '__main__':
    main()

Solution (Python):
import math
import sys
n,m = map(int, raw_input().split())
x = map(int, raw_input().split())
if n >= m:
	print(0)
else:
	x.sort()
	memo = []
	for i in range(m-1):
		memo.append(x[i+1]-x[i])
	memo.sort()
	ans = 0
	for i in range(m-n):
		ans += memo[i]
	print(ans)

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

x.sort(reverse=True)

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

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

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

X=input().split()

for i in range(M):
	X[i] = int(X[i])

X.sort()

kyori = []
for i in range(M-1):
	kyori += [0]

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

kyori.sort()


sumkyori=0
for i in range(M-N):
	sumkyori += kyori[i]

print(sumkyori)

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

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

x = list(map(float,input().split()))
x = sorted(x)

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

    a = []
    for i in range(0,m-1):
        a.append(x[i+1]-x[i])
    a = sorted(a)

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


    print(int(lena - diff))

Solution (Python):
def solve():
  N, M = map(int, input().split())
  X = [int(i) for i in input().split()]
  if N >= M: return 0
  X.sort()
  diff = [0] * (len(X) - 1)
  for i in range(len(X) - 1):
    diff[i] = X[i + 1] - X[i]
  diff.sort(reverse=True)
  return sum(diff[N - 1:])

print(solve())

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

print(main(n,m,x))

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

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

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

for i in range(n-1):
    b.pop(0)

print(sum(b))

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(reverse=True)
ans=0
for i in range(N-1,M-1):
    ans+=d[i]
print(ans)

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

if n>=m:
    print(0)
    import sys
    sys.exit()
l=[]
ans=x[-1]-x[0]
for i in range(m-1):
    l.append(abs(x[i]-x[i+1]))
l.sort()
l.reverse()
for i in range(n-1):
    ans-=l[i]
if ans>=0:
    print(ans)
else:
    print(0)

Solution (Python):
a = input().split()
b = input().split()
c = []
for i in b:
    c.append(int(i))

if int(a[0]) >= int(a[1]):
    print(0)
else:
    c.sort()
    d = []
    for i in range(int(a[1]) - 1):
        d.append(c[i + 1] - c[i])
    d.sort()
    for i in range(int(a[0]) - 1):
        d.pop()
    ans = 0
    for i in d:
        ans += i
    print(ans)

Solution (Python):

import sys





"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが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 S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n, m = ip.IL()
    x = ip.IL()

    id.S(x)
    s = []
    for i in r(0, m-1):
        s.append(x[i+1] - x[i])

    id.S(s, 1)
    if m == 1:
        print(0)

    elif len(s) >= m:
        print(0)

    else:
        for _ in r(n-1):
            s.pop(0)
            if len(s) == 0:
                break
        print(sum(s))


main()

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

if not N == 1:
    print(sum(diffs[:-N+1]))
else:
    print(sum(diffs))

Solution (Python):


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

Solution (Python):
N, M = map(int,input().split())
X = sorted(list(map(int,input().split())))
D = [0]*M

for k in range(1,M):
    D[k] = X[k]-X[k-1]

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

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

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

Xi.sort()

Li = [Xi[i] - Xi[i-1] for i in range(1,len(Xi))]
Li.sort()

if N == M or N > M:
    print(0)
else:
    for _ in range(N-1):
        Li.pop(-1)
    print(sum(Li))

Solution (Python):
N, M = map(int, input().split())
X_list = sorted(list(map(int, input().split())))
res = []
for idx, x in enumerate(X_list):
    if idx == M-1:
        break
    else:
        res.append(X_list[idx+1] - x)
        
res.insert(0, 0)
union = zip(res, [i for i in range(M)])
union = sorted(union, key=lambda x: x[0])[-1::-1]
res_, idx_list = zip(*union)
res_, idx_list

idx_list = list(idx_list)

setting_idx =  idx_list[:N-1]
setting_idx.append(0)
setting_idx = sorted(setting_idx)

ans = sum(res)
for idx in setting_idx:
    ans -= res[idx]
print(ans)

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


def solve():
    if N >= M:
        print(0)
        return

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

    diff.sort(reverse=True)

    sum_ = sum(diff)
    r = sum(diff[:N - 1])
    print(sum_ - r)


if __name__ == '__main__':
    solve()

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

Solution (Python):

def main():
    koma_num, point_num = list(map(int, input().split()))
    data = list(map(int, input().split()))
    data.sort()
    data_list = [[ele, ele] for ele in data]
    ans = 0

    haba = 0
    while len(data_list) > koma_num:
        haba += 1
        for i in range(len(data_list) - 1)[::-1]:
            if data_list[i+1][0] - data_list[i][1] == haba:
                data_list[i][1] = data_list[i+1][1]
                data_list.pop(i+1)
                ans += haba
                
            if len(data_list) == koma_num:
                break
    print(ans)



if __name__ == '__main__':
    main()

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

if n >= m:
    print(0)
    exit()
xs = sorted(xs)
diff = [0]*(m-1)
for i in range(len(xs)-1):
    diff[i] = abs(xs[i+1] - xs[i])
diff = sorted(diff)
ans = sum(diff[:m-n])
print(ans)

Solution (Python):
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M = IL()
l = IL()
l_abs = []

l.sort()

for i in range(M-1):
    a = l[i+1] - l[i]
    l_abs.append(a)

l_abs.sort()

if N >= M:
    print(0)
else:
    for i in range(N-1):
        del l_abs[-1]
    print(sum(l_abs))

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

a.sort()

g = [0 for i in range(M-1)]

for i in range(M-1):
    g[i]=a[i+1]-a[i]
    
g.sort()
g.reverse()

print(sum(g[N-1:len(a)-1]))

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

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

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

ans = 0
X.sort()
P = [(X[i+1]-X[i]) for i in range(M-1)]
P.sort()
for i in range(M-N):
    ans += P[i]
print(ans)

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

Solution (Python):




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


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


dlist.sort()
sum = 0
for j in range(m-1-(n-1)):
    sum += dlist[j]


print(sum)

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

    y.sort()

    ans = sum(y)

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


    print(ans)

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

answer = 0

if N >= M:
    pass
else:
    diff = []
    for i in range(len(X) - 1):
        diff.append(X[i+1] - X[i])

    diff = list(reversed(sorted(diff)))
    s = 0
    for i in range(N - 1):
        s += diff[i]
    answer = X[-1] - X[0] - s

print(answer)

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




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

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

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

if M <= N:
    print(0)
else:
    X.sort()
    
    dX = np.diff(X)
    
    dX.sort()
    print(sum(dX[:M-N]))

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

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

x = []
X.sort()

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


ans = sum(x)

if N >= len(X):
    print(0)
else:
    for i in range(N - 1):
        ans -= x[i]

    print(ans)

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

X.sort()


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

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

Solution (Python):
def main():
    n, m = map(int, input().split())
    xs = sorted(map(int, input().split()))
    dists = sorted(xs[i] - xs[i - 1] for i in range(1, len(xs)))
    if n > 1:
        print(sum(dists[: -n + 1]))
    else:
        print(sum(dists))


if __name__ == "__main__":
    main()

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

if len(x) <= n:
    print('0')
else:
    x_sorted = sorted(x,reverse = True)
    x_dif = list()
    for i in range(m-1):
        x_dif.append(x_sorted[i] - x_sorted[i+1])
    x_dif.sort()
    ans = 0
    for i in range(m-n):
        ans += x_dif[i]
    print(int(ans))

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

Solution (Python):
import sys,collections,math;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n,m = Is()
Xs = sorted(Is())
dist = []
if n > m:
    print(0)
    exit()
for i in range(m-1):
    dist.append(Xs[i+1]-Xs[i])
print(sum(sorted(dist)[:m-n]))

Solution (Python):
import sys

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

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

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

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

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

if N < M:
    X.sort()
    len = []
    for i in range(M-1):
        len.append(X[i+1] - X[i])

    len.sort()
    for i in range(N-1):
        len.pop()

    print(sum(len))

else:
    print(0)

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

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

Solution (Python):
import math
import sys
from operator import itemgetter








N, M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
xintval = list(range(0))
for i in range(len(X) - 1):
	xintval.append(X[i+1] - X[i])


xintval = sorted(xintval, reverse = False)
if (N >= M):
	print(0)
else:
	sum = 0
	for i in (range(M - N)):
		sum += xintval[i]
	print(sum)

Solution (Python):
def inpl():
    return list(map(int, input().split()))


N, M = inpl()
X = inpl()

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

X.sort()
absX = [0] * (M - 1)
for i, xx in enumerate(zip(X, X[1:])):
    absX[i] = xx[1] - xx[0]
absX.sort(reverse=True)

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

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

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

X.sort(reverse=True)

diff = []
for x_l, x_s in zip(X[:-1], X[1:]):
    diff.append(x_l - x_s)

diff.sort(reverse=True)

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

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

lst=sorted(list(map(int,input().split())))[::-1]
for i in range(len(lst)-1):
    sa.append(int(abs(lst[i]-lst[i+1])))
sa = sorted(sa)[::-1]
print(sum(sa[n-1:]))

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


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

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

Solution (Python):



N,M = map(int,input().split())
lst = list(map(int,input().split()))
lst.sort(reverse = True)
sabun = []
for i in range(len(lst)-1):
    sabun.append(lst[i] - lst[i+1])
sabun.sort()
ans = 0
for i in range(len(sabun)-N+1):
    ans+=sabun[i]
print(ans)

Solution (Python):
def main():
    n, m = map(int, input().split())
    x = list(sorted(map(int, input().split())))
    if n >= m: return 0

    diffs = list(sorted((abs(x[i + 1] - x[i]) for i in range(m - 1)), reverse=True))
    return sum(diffs[i] for i in range(n - 1, m - 1))

print(main())

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

Solution (Python):

import numpy as np

def solve():
    N, M = map(int, input().split())
    X = np.array(sorted(list(map(int, input().split()))))
    Y = X[1:] - X[:-1]
    Y.sort()
    Z = Y[::-1][N-1:].sum()
    res = Z
    return str(res)
if __name__ == '__main__':
    print(solve())

Solution (Python):
import sys

n, m = map(int, input().split())
x = list(map(int, input().split()))
if m == 1:
    print(0)
    sys.exit()
if n == 1:
    print(max(x) - min(x))
    sys.exit()
if n >= m:
    print(0)
    sys.exit()
x.sort()
a = []
for i in range(m - 1):
    a.append(abs(x[i] - x[i + 1]))
ans = sum(a)
a.sort(reverse=True)
for i in range(n - 1):
    ans -= a[i]
print(max(0, 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(abs(X[i+1]-X[i]))
D.sort()

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

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

if m==1 or n>=m :
    print(0)
else :

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


    sa.sort(reverse=1)


    jo=[]
    for i in range(n-1):
        jo.append(sa[i])


    print(X[m-1]-X[0]-sum(jo))

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()
if N >= M:
  print(0)
else:
  x = a[M-1]-a[0]
  y = M-2
  for i in range(N-1):
    x -= b[y]
    y -= 1
  print(x)

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

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

ans_list.sort()

for i in range(n-1):
  if len(ans_list) == 0:
    break
  ans_list.pop()
print(sum(ans_list))

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

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

m.sort()
ans = 0
for i in range(len(m)-a+1):
    ans += m[i]

print(ans)

Solution (Python):
n,m = map(int, input().split())
l=list(map(int, input().split()))
k=[]
if len(l)==1:
    print(0)
else:
    l=sorted(l,reverse=False)
    for i in range(len(l)-1):
        k.append(l[i+1]-l[i])
    k=sorted(k,reverse=True)
    print(sum(k)-sum(k[:n-1]))

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

Solution (Python):




def main():
    n, m = list(map(int, input().split()))
    if n >= m:
        print(0)
        return
    xs = list(map(int, input().split()))
    sort_xs = sorted(xs)

    diff_xs = []
    for i in range(m-1):
        diff_xs.append(sort_xs[i+1] - sort_xs[i])
    sort_diff_xs = sorted(diff_xs)
    print(sum(sort_diff_xs[:m-n]))


if __name__ == '__main__':
    main()

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

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
L = []
for i, x in enumerate(X):
  if i == 0:
    prev = x
  else:
    L.append(x - prev)
    prev = x
L.sort(reverse=True)
ind = N -1
print(sum(L[ind:]))

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def solve():
    ans = 0

    if N >= M:
        ans = 0
    else:
        x = sorted(X)
        diff_x = [0]*(M-1)
        for i in range(1, M):
            diff_x[i-1] = x[i] - x[i-1]

        diff_x = sorted(diff_x, reverse=True)
        ans = sum(diff_x[N-1:])
    return ans


print(solve())

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

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

X.sort()

dX = list()

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

dX.sort(reverse=True)

print(sum(dX) - sum([dX[i] for i in range(N - 1)]))

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

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

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
N, M = inpl()
X = inpl()
X.sort()
X = [j - i for i, j in zip(X, X[1:])]
X.sort(reverse = True)
print(sum(X[N-1:]))

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

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


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

diffs.sort(reverse=True)

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

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

Solution (Python):



import sys  
import os
import time
import re
from pydoc import help
import string
import math
from operator import itemgetter
from collections import Counter
from collections import deque
from collections import defaultdict as dd
import fractions
from heapq import heappop, heappush, heapify
import array
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy as dcopy
import itertools



sys.setrecursionlimit(10**7)
INF = 10**20
GOSA = 1.0 / 10**10
MOD = 10**9+7
ALPHABETS = [chr(i) for i in range(ord('a'), ord('z')+1)]  
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def DP(N, M, first): return [[first] * M for n in range(N)]
def DP3(N, M, L, first): return [[[first] * L for n in range(M)] for _ in range(N)]
from inspect import currentframe
def dump(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' => '+repr(arg) for arg in args), file=sys.stderr)


def local_input():
    from pcm.utils import set_stdin
    import sys
    from pathlib import Path
    parentdir = Path(os.path.dirname(__file__)).parent
    inputfile = parentdir.joinpath('test/sample-1.in')
    if len(sys.argv) == 1:
        set_stdin(inputfile)


def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    dump(A)

    D = []
    for i in range(1, len(A)):
        D.append(A[i] - A[i-1])
    D.sort()
    dump(D)

    if N>=M:
        print(0)
        return
    else:
        res = 0
        for i in range(M-N):
            res += D[i]
        print(res)


    return 0

if __name__ == "__main__":
    try:
        local_input()
    except:
        pass
    solve()

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

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

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

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

Y = [0 for i in range(len(X)-1)]
for i in range(len(X)-1):
    Y[i] = X[i+1] - X[i]
Y.sort()
Y.reverse()
sumY = sum(Y)
summax = sum(Y[:N-1])
print(sumY - summax)

Solution (Python):


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

Y = sorted([ X[i+1] - X[i] for i in range(len(X)-1)], reverse=True)

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

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 N == 1:
    print(sum(Y))
else:
    print(sum(Y[:-N+1]))

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

Solution (Python):

def main_c(X_list, N, M):
    
    X_list_s = sorted(X_list)
    D_list = [X_list_s[i+1] - X_list_s[i] for i in range(M-1)]
    D_list_s = sorted(D_list)
    
    if M > N:
        res = sum(D_list_s[:(M - N)])
    else:
        res = 0
        
    return res

if __name__ == "__main__":
    N, M = map(int, input().split())
    X_list = list(map(int, input().split()))
    res = main_c(X_list, N, M)
    print(res)

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

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

X.sort()

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

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

Solution (Python):
import numpy as np
N, M = map(int, input().split())
X = np.sort(list(map(int, input().split())))
D = np.sort(X[1:] - X[:-1])[::-1]
print(np.sum(D[N-1:]))

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

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

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

X.sort()

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

k = 0
if M-N > 0:
    k = M-N

print(sum(d[0:k]))

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

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

Solution (Python):
N, M = map( int, input().split())
X = list( map( int, input().split()))
X.sort()
XS = [0]*(M-1)

if N >= M:
    print(0)
else:
    for i in range(M-1):
        XS[i] = X[i+1] - X[i]
    XS.sort( key = None, reverse = True)
    ans = X[-1] - X[0]
    ans -= sum( XS[:N-1])
    print(ans)

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

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