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



Sample input:
2 5
10 12 1 2 14


Sample output:
5

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
X=sorted(set(list(map(int,input().split()))))
s=[]
if M<N:
    print(0)
    exit()
for i in range(M-1):
    s.append(X[i+1]-X[i])
s=sorted(s)
print(sum(s[:M-N]))

Submission status: Accepted

Solution code:
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(len(L)-(N-1)):
  ans+=L[i]
print(ans)

Submission status: Accepted

Solution code:
def func2(input_list):
    if n >= m:
        return 0

    input_list.sort()

    input_diff = []
    for i in range(m-1):
        diff = input_list[i+1] - input_list[i]
        input_diff.append(abs(diff))

    input_diff.sort(reverse=True)
    ans = input_list[-1] - input_list[0]
    for i in range(0, n-1):
        ans -= input_diff[i]

    return ans

n, m = map(int, input().split())
input_list = [int(i) for i in input().split()]

print(func2(input_list))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
listx = sorted(list(map(int, input().split())))
listgap =[0]*(M-1)
for n in range(M-1):
    listgap[n] = listx[n+1]-listx[n]
listgap.sort()    
if N>=M:
    print('0')
    exit
else:
    a = M-N
    anslist = listgap[0:a]
    b = sum(anslist)
    print(b)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
N, M = [int(x) for x in input().rstrip().split()]
X = [int(x) for x in input().rstrip().split()]
X.sort()
X_1 = np.array(X[:M-1], np.int64)
X_2 = np.array(X[1:], np.int64)
X_ = X_2 - X_1
X_ = np.sort(X_)[::-1]
print(X_[N-1:].sum())

Submission status: Accepted

Solution code:
first_line = [int(x) for x in input().split()]
n, m = first_line[0], first_line[1]

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

table = [int(x) for x in input().split()]

table.sort()
desk = []
for x in range(0, len(table) - 1):
    desk.append(table[x + 1] - table[x])
desk.sort()
for x in range(0, n - 1):
    if len(desk) < 2:
        break
    desk.pop(-1)
cnt = 0
for x in desk:
    cnt += x
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# #import sys
# input = sys.stdin.readline

n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
#各座標の、右隣の座標までの距離を求める
right_distance = [j-i for i,j in zip(x,x[1::])]
right_distance.sort()
ret = 0
while n < m:
    ret += right_distance.pop(0)
    m -= 1
print(ret)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
#各座標の、右隣の座標までの距離を求める
right_distance = [j-i for i,j in zip(x,x[1::])]
right_distance.sort()
ret = 0
while n < m:
    ret += right_distance.pop(0)
    m -= 1
print(ret)

Submission status: Accepted

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

dis_X = [(X[i+1] - X[i]) for i in range(0, M-1)]

dis_X.sort()

if len(dis_X) <= (N-1):
    print(0)
else:
    # print(dis_X)
    # print(dis_X[:M-(N-1)-1])
    print(sum(dis_X[:M-(N-1)-1]))

Submission status: Accepted

Solution code:
import numpy as np

N, M = (int(i) for i in input().split())  
X = [int(i) for i in input().split()] 
X.sort()

cnt = 0
reversed_diff = sorted(np.diff(X))[::-1]

print(max(X) - min(X) - sum(reversed_diff[:N-1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Z = sorted(sum_Y)

for i in range(N - 1):
    if Z == []:
        break
    else:
        Z.pop()

print(sum(Z))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
diff=[0]*(m-1)
for i in range(m-1):
  diff[i]=x[i+1]-x[i]
diff=sorted(diff)[::-1]
print((x[-1]-x[0])-sum(diff[:n-1]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
b = []
for i in range(1, m):
    b.append(a[i] - a[i-1])
b = sorted(b, reverse=True)
print(sum(b[n-1:]))

Submission status: Accepted

Solution code:
def main():
    n,m = map(int,input().split())
    X = list(map(int,input().split()))

    X.sort()
    X_diff = []
    for i in range(m-1):
        X_diff.append(X[i+1]-X[i])
    X_diff.sort()
    if n ==1:
        print(sum(X_diff))
    else:
        print(sum(X_diff[0:-(n-1)]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

x.sort()
#print(x)
l = [x[i] - x[i-1] for i in range(1, m)]
l.sort(reverse=True)
#print(l)
print(sum(l[n-1:]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
dist = []

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted([int(i) for i in input().split()])
xbet=sorted([abs(x[i+1]-x[i]) for i in range(m-1)],reverse=True)
y=sum(xbet)
if n>=m:
    print(0)
else:
    print(y-sum(xbet[:n-1]))

Submission status: Accepted

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

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

s.sort(reverse=True)

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B = [0 for _ in range(M-1)]
for i in range(M-1):
  B[i] = A[i+1] - A[i]
B.sort()
if N!=1:
  B=B[:-(N-1)]
print(sum(B))

Submission status: Accepted

Solution code:
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(reverse=True)
ans = X[-1] - X[0]
if N < M:
    for i in range(N-1):
        ans -= Y[i]
if N >= M:
    ans = 0

print(ans)

Submission status: Accepted

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

dist = []
for i in range(m-1):
    dist.append(X[i+1]-X[i])
dist = sorted(dist, reverse=True)
if n >= m:
    print(0)
else:
    print(sum(dist[n-1:]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, 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()
    print(sum(Y[:M-N]))

Submission status: Accepted

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

if N>=M:
  answer=0
else:
  A.sort()
  B=[]
  for i in range(1,M):
    B +=[A[i]-A[i-1]]
  B=sorted(B)
  answer=sum(B[:M-N])
print(answer)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
x = list(map(int, input().split()))
x.sort()
r = [abs(x[i+1]-x[i]) for i in range(m-1)]
r.sort()
result = 0
for i in range(max(-1, m-n)):
    result += r[i]
print(result)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
xarray = sorted(list(map(int,input().split())))
 
diff = []
if len(xarray) == 1:
    print(0)
elif N >= M:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))
 
    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)
        
 
        if N == 1:
            
            print(sum(diff2))
        else:
            for j in range(N-1):
                diff2[j] = 0
            
            print(sum(diff2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
X = sorted(map(int,input().split()))
l = sorted([X[i+1] - X[i] for i in range(m-1)])
if n == 1:
  print(sum(l))
else:
  print(sum(l[:-n+1]))

Submission status: Accepted

Solution code:
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()
ans=0
for i in range(m-n):
  ans+=d[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N, M = [int(x) for x in input().strip().split()]
X = sorted([int(x) for x in input().strip().split()])
D = [0] * (M - 1)
d = X[0]
if N >= M:
    print(0)
    exit()

for i, x in enumerate(X[1:]):
    D[i] = x - d
    d = x

print(sum(sorted(D, reverse=True)[(N-1):]))

Submission status: Accepted

Solution code:
from collections import deque
N, M = map(int, input().split())
X = list(map(int, input().split()))
sortX = sorted(X)
dist = []
if N-M>=0:
    print(0)
else:
    for i in range(M-1):
        dist.append(sortX[i + 1] - sortX[i])
    dist.sort()
    print(sum(dist[:M-N]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = sorted([int(i) for i in input().split()])
subX = sorted([X[i]-X[i-1] for i in range(1,M)],reverse=True)
 
if N >= M:
    print(0)
    exit()
 
answer = X[-1] - X[0]
for i in range(N-1):
    answer -= subX[i]
 
print(answer)

Submission status: Accepted

Solution code:
IINF = 10 ** 18


def main():
    N, M = list(map(int, input().split()))
    X = list(map(int, input().split()))
    X = sorted(X)
    # left = -IINF
    # right = IINF

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

        # print(L)

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

    # for i in range(N):
    #     if len(X) == 0:
    #         break
    #     elif i == 0:
    #         left = X[0]
    #         X.pop(0)
    #     elif i == 1:
    #         right = X[-1]
    #         X.pop(len(X)-1)
    #     else:
    #         if abs(X[1] - X[-1]) < abs(X[-2] - X[0]):
    #             left = X[0]
    #             X.pop(0)
    #         else:
    #             right = X[-1]
    #             X.pop(-1)

    # ans = 0
    # while len(X) > 0:
    #     # print(ans, X)
    #     if abs(left - X[0]) < abs(right - X[-1]):
    #         ans += abs(left - X[0])
    #         left = X[0]
    #         X.pop(0)
    #     else:
    #         ans += abs(right - X[-1])
    #         right = X[-1]
    #         X.pop(len(X)-1)
    #     # b = min(abs(left - X[0]), abs(right - X[-1]))
    #     # print(b, X)
    #     # print(abs(X[0] - X[-1]) + b)
    # print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nPieces, nCoords = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
if nPieces >= nCoords:
    print(0)
    exit()
diffs = sorted([a[i] - a[i + 1] for i in range(nCoords - 1)], reverse=True)
print(sum(diffs[nPieces - 1:]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

x.sort()

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

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

d.sort()

print(sum(d[:-n + 1]))

Submission status: Accepted

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

list_aaa = sorted(list_aaa)
list_aaa_range = []

for i in range(M-1):
    list_aaa_range.append(list_aaa[i+1]-list_aaa[i])

list_aaa_range = sorted(list_aaa_range)

for i in range(1,N):
    if not list_aaa_range :
        list_aaa_range.append(0)
    else:
        list_aaa_range.pop(-1)

print(sum(list_aaa_range))

Submission status: Accepted

Solution code:
import os, sys, re, math

N,M = list(map(int,input().split(' ')))
L = sorted(list(map(int,input().split(' '))))
diff = sorted([L[i] - L[i-1] for i in range(1,M)],reverse=True)
print(sum(diff[N-1:]))

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(' ')))
l = sorted(list(map(int, input().split(' '))))
print(sum(sorted([l[i+1]-l[i] for i in range(len(l)-1)])[:max(0, k-n)]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000)

n, k = list(map(int, input().split(' ')))
l = sorted(list(map(int, input().split(' '))))
#print(l)
#l =     [1,2,10,12, 14]]
#diffs = [1, 8, 2, 2]
# f(n,k) = hoge
# f(5,5) = 0
# f(4,5) = 1
# f(3,5) = 3
# f(2,5) = 5
# f(1,5) = 13
diffs = [l[i+1]-l[i] for i in range(len(l)-1)] 
diffs = sorted(diffs)
print(sum(diffs[:max(0, k-n)]))

Submission status: Accepted

Solution code:
n, m = [int(a) for a in input().split()]
x = sorted([int(a) for a in input().split()])
y = sorted([b - a for a, b in zip(x, x[1:])])
if n >= m:
	print (0)
else:
	print (sum(y[0 : m - n]))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
if n>=m:
  print(0)
  exit()
list1 = list(map(int,input().split()))
list1.sort()
list2 = []
for i in range(m-1):
	list2.append(list1[i+1]-list1[i])
list2.sort(reverse = True)
cut = 0
for j in range(n-1):
	cut+= list2[j]
print(max(list1)-min(list1)-cut)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
ans=0
d=[]
if n>=m:
  print(0)
else:
  for i in range(1,m):
    y=x[i]-x[i-1]
    d.append(y)
  
  d.sort(reverse=True)
  for j in range(n-1):
    ans+=d[j]
    
  print(x[m-1]-x[0]-ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
cnt = 0
if n > m:
    print(0)
    exit()
x.sort()
L = []
for i in range(m - 1):
    L.append(x[i + 1] - x[i])
L.sort()
L.reverse()
for i in range(n-1):
    L.pop(0)
print(sum(L))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
MOD = 10**9+7

n,m = MAP()
x = MAP()
x.sort()

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
l = []
for i in range(1, M):
    l.append(X[i] - X[i - 1])
l.sort()
print(sum(l[0:max(0, M - N)]))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
if n>=m:
  print(0)
else:
  d=sorted([x[i+1]-x[i] for i in range(len(x)-1)])[::-1]
  l=0
  for i in range(n-1):
    l+=d[i]
  print(x[m-1]-x[0]-l)

Submission status: Accepted

Solution code:
import numpy as np

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

X_sorted = np.sort(X)[::-1]
X_diff = X_sorted[:-1] - X_sorted[1:]
X_diff_sorted = np.sort(X_diff)[::-1]

print(X_sorted[0] - X_sorted[-1] - X_diff_sorted[:N-1].sum())

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
d=sorted(x)
d2=[]
ans=0
if m>1 and n<=m:
  for i in range(m-1):
    d2.append([d[i+1]-d[i],i])
  d2.sort(reverse=True)
  z=d[0]
  for i in range(n):
    if i!=n-1:
      ans+=d[d2[i][1]]-z
      z=d[d2[i][1]+1]
    else:
      ans+=d[m-1]-z
      
  print(ans)
else:
  print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
y = []
for i in range(1, m):
    y.append(x[i] - x[i - 1])
y.sort()
print([0, sum(y[:m - n])][n < m])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np

N,M = map(int,stdin.readline().strip().split())
X = np.array(list(map(int,stdin.readline().strip().split())))
X.sort()
dX = np.diff(X,n=1)
dX.sort()

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

Submission status: Accepted

Solution code:
import numpy as np
n,m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
x_diff = np.sort(np.diff(x))[::-1]
ans = 0
for i in range(n-1, m-1):
  ans += x_diff[i]
print(ans)

Submission status: Accepted

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

xl = sorted(xl)

aidal = []
before_x = xl[0]
for idx,x in enumerate(xl):
    aida = x - before_x
    aidal.append((aida,idx))
    before_x = x
    
aidal = sorted(aidal,reverse=True)
aidal.pop()
ans = 0
before_x = xl[0]
new = sorted([i[1] for i in sorted(aidal[:n-1])])
for idx in new:
    ans += xl[idx-1] - before_x
    before_x = xl[idx]
    
ans += xl[-1] - before_x
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
xli = list(map(int, input().split()))

ans = 0
if n < m:
    xli.sort()
    wli = list()
    for i in range(len(xli)-1):
        wli.append(xli[i+1]-xli[i])
    sortWli = list(reversed(sorted(wli)))
    su = 0
    for i in range(n-1):
        su += sortWli[i]
    ans = xli[-1] - xli[0] - su
print(ans)

Submission status: Accepted

Solution code:
# 2019-11-12 13:20:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m, *x = (int(i) for i in sys.stdin.read().split())
    x.sort()
    if n >= m:
        print(0)
        sys.exit()

    dist = []
    # 座標順に並び替えて、m-1個の区間の距離を列挙していく
    for i in range(m-1):
        dist.append(x[i+1] - x[i])

    # n個は初期で設置するので、それらを起点として((m-1)-(n-1))回ポイント間を移動した場合、ポイントが重複しないように移動した時の距離の合計の最小値
    # ポイントの間の距離の組み合わせを列挙して、それらのうち長い方からn-1個の組み合わせの区間は移動するべきではないと考える。
    # イメージ的には隣り合う距離が長い２点の組み合わせから順にコマを置いていけばいい
    dist.sort()
    ans = sum(dist[:(m-1)-(n-1)])

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[int(i) for i in input().split()]
if n>=m:
    print(0)
    exit()
l.sort() 
diff=[l[i]-l[i-1] for i in range(1,m)]
diff.sort() 
for i in range(n-1):
    diff.pop()
print(sum(diff))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
X_dis = []
for m in range(M-1):
    X_dis.append(X[m+1]-X[m])
X_dis.sort()
ans = 0
if N < M:
    for i in range(M-N):
        ans += X_dis[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

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

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

L.sort()

ans = sum(L[:M-N])
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if M <= N:
    print(0)

else:
    X.sort()
    diffs = [X[i+1] - X[i] for i in range(M-1)]
    diffs.sort(reverse=True)
    n = N-1
    ans = sum(diffs[n:])
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, X: "List[int]"):
    if N >= M:
        print(0)
    else:
        X_s = sorted(X)
        b = []
        for i in range(0, M-1):
            b.append(X_s[i+1] - X_s[i])
        b_s = sorted(b, reverse=True)
        print(X_s[-1] - X_s[0] - sum(b_s[:N-1]))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    X = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

x.sort()
l = []
for i in range(len(x)):
    if i == 0:
        continue
    l.append(x[i] -  x[i-1])
l.sort(reverse=True)
sum = sum(l)
for i in range(n-1):
    sum -= l[i]

print(sum)

Submission status: Accepted

Solution code:
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()
if N != 1:
  del diff[-N+1:]
print(sum(diff))

Submission status: Accepted

Solution code:
from sys import stdin
import heapq

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

li.sort()
sa = []

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

for i in range(1,m):
    sa.append(li[i]-li[i-1])

sa.sort()

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

print(sum(sa))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
XX = sorted([X[i+1] - X[i] for i in range(M-1)])
for i in range(N-1):
	if len(XX) == 0:
		break
	XX.pop()
print(sum(XX))

Submission status: Accepted

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

X.sort()

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

Submission status: Accepted

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

A=sorted(A)
B=[0]*(M-1)

for i in range(len(A)-1):
    B[i]=A[i+1]-A[i]

B=sorted(B)
if(N>=M):
    print("0")
    exit()
else:
    print(sum(B[0:M-N]))

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(len(X) - 1):
    num.append(abs(X[i] - X[i + 1]))
num.sort(reverse = True)

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

Submission status: Accepted

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

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

sub = sorted(sub, reverse=True)
ans = sum(sub[N-1:])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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