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 = list(map(int, input().split()))
xs = list(map(int, input().split()))
xs.sort()

dists = [abs(i - j) for i, j in zip(xs[1:], xs)]
dists.sort()
ans = 0 if n >= m else sum(dists[: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)
else:
    X.sort()
    b=[]
    for i in range(M-1):
        b.append(X[i+1]-X[i])
    b.sort()
    for _ in range(N-1):
        b.pop()
    print(sum(b))

Submission status: Accepted

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

Submission status: Accepted

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

x.sort()

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

y.sort()

s = 0
if m > n:
    s = sum(y[0:m - n])
print(s)

Submission status: Accepted

Solution code:
N,M = list(map(int, input().split()))
x = list(map(int, input().split()))
x = sorted(x)
#N-1個の隙間は通らなくて済む

bet = [] #iとi+1の間隔
for i in range(1,M):
  bet.append(x[i]-x[i-1])

if N == 1:
  print(max(x)-min(x))
elif N>=M:
  print(0)
else:
  bet = sorted(bet)
  print(sum(bet[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
import heapq

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

x = sorted(x)
x = np.diff(x)
#print(x)

if n >= m:
    print(0)

elif n == 1:
    print(sum(x))

else:
    print(sum(heapq.nsmallest(len(x)-n+1, x)))

Submission status: Accepted

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

A.sort()

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

diff.sort()
print(sum(diff[::-1][N_M[0]-1:]))

Submission status: Accepted

Solution code:
import numpy as np


def solve(string):
    n, m, *x = map(int, string.split())
    x = sorted(np.diff(sorted(x)), reverse=True)
    return str(sum(x[n - 1:]))


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Submission status: Accepted

Solution code:
import numpy as np


def solve(string):
    n, m, *x = map(int, string.split())
    x = sorted(np.diff(sorted(x)))
    return str(sum(x)) if n == 1 else str(sum(x[:-n + 1]))


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Submission status: Accepted

Solution code:
def run(N, M, X):
    if N >= M:
        return 0
    X = sorted(X)
    diff = []
    for i in range(M-1):
        diff.append(X[i+1] - X[i])
    diff = sorted(diff)
    ret = X[-1] - X[0]
    for i in range(N-1):
        ret -= diff[-i-1]
    return ret


def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    print(run(N, M, X))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

# coding: utf-8

# In[12]:


import numpy as np


# In[33]:


N, M = map(int,input().split())
X = np.array([int(x) for x in input().split()])


# In[34]:


if N >= M:
    print(0)
else:
    sorted_x = np.sort(X)
    dist = sorted_x[1:] - sorted_x[:-1]
    ans = np.sort(dist)[::-1][N-1:].sum()
    print(ans)

Submission status: Accepted

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

def play(n, m, x):
    if n >= m:
        return 0

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

    # 右から左に動くように統一することで、うまく計算できるか
    # x = [1, 2, 10, 12, 14, 15]
    # a = [1, 8, 2, 2, 1]
    b = []
    for i, j in enumerate(a):
        b.append((j, i))
    b.sort(reverse=True)

    index = []
    for i in range(n):
        if i == 0:
            index.append(len(x) - 1)
        else:
            index.append(b[i-1][1])

    index.sort()
    move = []
    for i in range(len(index)):
        if i == 0 and index[i] == 0:
            move.append(0)
        elif i == 0:
            move.append(x[index[i]] - x[0])
        else:
            move.append(x[index[i]] - x[index[i-1] + 1])

    #debug
    #print('x =', x)
    #print('a =', a)
    #print('index =', index)
    #print('move =', move)
    return sum(move)

print(play(n, m, x))

Submission status: Accepted

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

X.sort()
diffX = list(map(lambda x,y:x-y, X[1:], X[:-1]))
diffX.sort(reverse = True)

if N-1 >= len(diffX):
	print(0)
else:
	print(sum(diffX[N-1:]))

Submission status: Accepted

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N,M=rl()
X=rl()
X.sort()
d = [X[i+1]-X[i] for i in range(M-1)]
d.sort()
to = M - N
print(sum(d[0:to]) if to>0 else 0)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    if M <= N:
        print(0)
        return
    X.sort()
    diff = [0] * (M-1)
    for i in range(M-1):
        diff[i] = X[i+1] - X[i]
    diff.sort()
    print(sum(diff[:M-N]))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

li.sort()

dist = 0
_li = np.zeros(m)

for i in range(len(li)-1):
    _li[i] = (li[i+1] - li[i])

ans = sorted(_li, reverse=True)

print(int(sum(ans[(n-1):])))

Submission status: Accepted

Solution code:
import numpy as np
N, M = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
X = sorted(X)
ans = np.zeros(M)
for i in range(M-1):
    ans[i] = X[i+1] - X[i]

ans = sorted(ans, reverse=True)

print(int(sum(ans[N-1:])))

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
x=list(map(int,input().split()))
l=[]
s=0

if N >= M:
  print(0)
  quit()
else:
  x.sort()
  for i in range(1,M):
    l.append(x[i]-x[i-1])
  l.sort(reverse=True)
  for j in range(N-1):
    s += l[j]
  
  print(x[M-1]-x[0]-s)

Submission status: Accepted

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

X.sort()
#print(X)
d = [X[i]-X[i-1] for i in range(1,M)]
d.sort()
#print(d)
#print(sum(d[:-(N-1)]) if M-N>0 else 0)
print(sum(d[0:(M-N)]) if (M-N)>0 else 0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())  # Nはコマの数, Mは目標の座標数
X = [int(x) for x in input().split()]  # 目標の座標リスト

if N >= M:  # N >= Mならば全ての座標にコマを置いて終わるため移動しない
    print(0)
else:  # N < M
    X.sort()  # Xを昇順にソート
    L = [X[i] - X[i-1] for i in range(1, M)]
    L.sort()
    ans = X[-1] - X[0]
    for i in range(1, N):
        ans -= L[-i]
    print(ans)

Submission status: Accepted

Solution code:
#abc117c

n,m=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
l=[a[i+1]-a[i] for i in xrange(m-1)]
l.sort(reverse=True)
print sum(l[n-1:])

Submission status: Accepted

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

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

B.sort(reverse = True)

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

Submission status: Accepted

Solution code:
def get_int(): return int(input())
def get_float(): return float(input())
def get_line(): return input().split()
def get_lines(v): return [get_line() for _ in range(v)]
def get_int_line(): return list(map(int, get_line()))
def get_int_lines(v): return [get_int_line() for _ in range(v)]
def get_float_line(): return list(map(float, get_line()))
def get_float_lines(v): return [get_float_line() for _ in range(v)]

N, M = get_int_line()
X = get_int_line()

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

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

sub = 0
for i in range(N - 1):
  sub += x_dist[-(i + 1)]

print(sum(x_dist) - sub)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

L.sort()
dist = [0 for _ in range(M-1)]
for i in range(M-1):
    dist[i] = L[i+1] - L[i]
    
dist.sort(reverse=True)
res = sum(dist[N-1:])
print(res)

Submission status: Accepted

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

l.sort(reverse=True)
# print(l)

d = list(map(lambda x: l[x] - l[x + 1], range(m - 1)))
d.sort(reverse=True)

# print(l, d)

x = l[0]-l[-1]-sum(d[:n-1])

# print(l[0]-l[-1])
print(x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys
from heapq import heappush, heappop


def solution(arr, moves):
    if len(arr) <= moves:
        return 0

    arr.sort()
    if moves == 1:
        return abs(arr[0] - arr[-1])

    heapq_max = []
    # place them then scan the array
    # right side of longest interval

    for i in range(0, len(arr) - 1):
        heappush(heapq_max, (-abs(arr[i] - arr[i + 1] ), i , i + 1))

    placed = [0]
    for _ in range(moves - 1):
        val = heappop(heapq_max)
        placed.append(val[2])

    placed.sort()
    counter = 0
    for i in range(len(placed) - 1):
        if (abs(placed[i] -  placed[i + 1]) > 1):
            counter += abs(arr[placed[i]] - arr[placed[i + 1] - 1])
    if placed[-1] != len(arr) - 1:
        counter += abs(arr[placed[-1]] - arr[-1])
    return counter

    # visited = {}
    # current_positions = {}
    #
    # for i in list(reversed(sorted(d, key=lambda x: x[1])))[:moves]:
    #     visited[i[0]] = True
    #     current_positions[i[0]] = True
    #
    # counter = 0
    # while len(visited) < len(arr):
    #     min_on_position = sys.maxsize
    #     minimum_element = None
    #     side = None
    #     for i in current_positions:
    #         if i - 1 not in visited and i > 0:
    #             if min_on_position > abs(arr[i - 1] - arr[i]):
    #                 min_on_position = abs(arr[i - 1] - arr[i])
    #                 minimum_element = i
    #                 side = i - 1
    #
    #         if i + 1 not in visited and i < len(arr) - 1:
    #             if min_on_position > abs(arr[i + 1] - arr[i]):
    #                 min_on_position = abs(arr[i + 1] - arr[i])
    #                 minimum_element = i
    #                 side = i + 1
    #
    #
    #     current_positions[side] = True
    #     visited[side] = True
    #     counter += abs(arr[minimum_element] - arr[side])
    #
    # return counter

for i,line in enumerate(sys.stdin):
    if i == 0:
       M, N = line.split(" ")
    if i == 1:
        A = [int(i) for i in line.split(" ")]
        print(solution(A, int(M)))

# line = "-10 -3 0 9 -100 2 17"
#
# lines = [int(i) for i in line.split(" ")]
# a = [10,12,1,2,14]
# b = [-100000]
# print(solution(lines, 3))
# print(solution(a, 2))
# print(solution(b, 1))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

X.sort() #昇順に

if N>=M:
  print(0)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#1203から
import sys
N, M = map(int, input().split())
if N >= M:
    print(0)
    sys.exit()
li = list(map(int, input().split()))
li.sort()
diff_list = []
for i in range(M - 1):
    diff_list.append(li[i + 1] - li[i])

diff_list.sort(reverse=True)
ans = sum(diff_list)
for i in range(N - 1):
    ans -= diff_list[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s1=[int(i) for i in input().split()]
N=s1[0]
M=s1[1]
s2=[int(i) for i in input().split()]
a=sorted(s2)


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

s_sabun=sorted(sabun)

trysum=0
for i in range(M-N):
  trysum=trysum+s_sabun[i]

if N>=M:
  print(0)
else:  
  print(trysum)

Submission status: Accepted

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

def main():
    if N >= M:
        print(0)
        return
    XList.sort()
    dX = [XList[i+1]-XList[i] for i in range(len(XList)-1)]
    dX.sort(reverse=True)
    ans = XList[-1]-XList[0]-sum(dX[:N-1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

d = []

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

d.sort(reverse=True)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Streamline 

def main():
    N = list(map(int, (input().strip().split(' '))))
    M = N[1]
    N = N[0]
    X = list(map(lambda xi: int(xi), input().strip().split(' ')))
    X = sorted(X)

    if False and N >= M: # 不要
        min_steps = 0
    else:
        L = list()
        i = 0
        while i+1 < len(X):
            L.append(X[i+1]-X[i])
            i += 1
        L = sorted(L, reverse=True)
        min_steps = X[-1] - X[0] - sum(L[0:N-1])

    print(min_steps)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

if m <= n:
    print(0)
else:
    points.sort()
    dx = [ abs(points[i]-points[i+1]) for i in range(0,len(points)-1) ]
    dx.sort()
    dx.reverse()
    print(sum(dx[n-1:]))

Submission status: Accepted

Solution code:
s = input().split()
n, m = map(int, s)
if n >= m:
    print(0)
    exit(0)
x = input().split()
x = [int(i) for i in x]
x.sort()
#print(x)
dist = []
for i in range(1, len(x)):
    dist.append(x[i] - x[i-1])
dist.sort()
#print(dist)
dist = dist[:m-n]
print(sum(dist))

Submission status: Accepted

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

if n < m:
    x.sort()
    d = [x[i] - x[i - 1] for i in range(1, len(x))]
    d.sort()

    sum = 0
    for i in range(0, m - n):
        sum += d[i]
    print(sum)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = list(map(int, input().split()))
x = sorted(x)
y = [xb - xa for xa, xb in zip(x, x[1:])]
y = sorted(y, reverse=True)
print(sum(y) - sum(y[:N - 1]))

Submission status: Accepted

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

Xs.sort()
L=[]
for i in range(M-1):
  L.append(Xs[i+1]-Xs[i])
  
L.sort()
result = sum(L[:1-N]) if N>1 else sum(L)

print(result)

Submission status: Accepted

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

X = sorted(X)
Sub = []

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

Sub = sorted(Sub, reverse = True )

answer = X[-1] - X[0]

for x in Sub[:N-1] :
    answer -= x

print(answer)

Submission status: Accepted

Solution code:

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

aida = []
for i in range(len(x)-1):
    a = x[i]
    b = x[i + 1]
    sa = b - a
    aida.append(sa)

if n == 1:
    print(x[-1]-x[0])
else:
    sum_ = sum(sorted(aida)[:-n+1])
    print(sum_)

Submission status: Accepted

Solution code:
# C
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    X.sort()
    new_list = []
    for j in range(1, M):
        new_list.append(abs(X[j] - X[j-1]))
    new_list.sort()
    new_list.reverse()
    
    for i in range(M-N):
        new_list.pop()
    ans=(max(X)-min(X))-sum(new_list)
    
    print(ans)

Submission status: Accepted

Solution code:
def game(n, m, x_list):
  if n >= m:
    return 0
  
  x_list.sort()
  interval = []
  for i in range(m - 1):
    interval.append(x_list[i + 1] - x_list[i])
  interval.sort(reverse = True)
  sum_interval = 0
  for i in range(n - 1):
    sum_interval += interval[i]
  return x_list[m - 1] - x_list[0] - sum_interval

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

print(game(n, m, x))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
if n >= m:
    print("0")
else:
    x.sort()
    ans = max(x) - min(x)
    dif = []
    for i in range(m-1):
        dif.append(abs(x[i+1]-x[i]))
    dif.sort()
    dif.reverse()
    for i in range(n-1):
        ans += -dif[i]
    print(ans)

Submission status: Accepted

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

if(N>=M):
    print(0)
else:
    X_list.sort()
    L_list=list()
    
    for i in range(M-1):
        L_list.append(abs(X_list[i+1]-X_list[i]))
    
    L_list.sort()
    L_list.reverse()
    
    for i in range(M-N):
        L_list.pop()
        
    ans=(max(X_list)-min(X_list))-sum(L_list)
    
    print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def find_min_moves_num(N, X):
        X.sort()
        D = []
        for i in range(len(X) - 1):
                D.append(X[i + 1] - X[i])
        D.sort()
        min_moves = 0
        for i in range(len(D) - (N - 1)):
                min_moves += D[i]
        return min_moves


N, _ = [int(v) for v in input().split()]
X = [int(v) for v in input().split()]
print(find_min_moves_num(N, X))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X.sort()
Xdif = []

for i in range(M-1):
    if i != M-1: Xdif.append( X[i+1]-X[i] )
Xdif.sort(reverse=True)

if N >= M: print(0)
else:
    ans = max(X) - min(X) # = X[M-1] - X[0]
    for i in range(N-1):
        ans -= Xdif[i]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
if m-n<=0:
  print(0)
  exit()

data = [int(i) for i in input().split()]
data.sort()
data1 = data.copy()
data1.insert(0,0)
data1= np.array(data1[0:-1])
data = np.sort((np.array(data)-data1)[1:])
print(data[:m-n].sum())

Submission status: Accepted

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

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

    if n != 1:
        for i in range(n-1):
            distance.pop()
    
    print(sum(distance))

Submission status: Accepted

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

X.sort()

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

Y.sort()

for i in range(N-1):
    if len(Y) != 0:
        Y.pop()


print(sum(Y))

Submission status: Accepted

Solution code:
N, M= map(int, input().split())
zahyo = []
for i in map(int,input().split()):
    zahyo += [i]
sum=0
#マージソート用関数
# merge(p, q): あらかじめソートされた配列 p と q をまとめる。
def merge(p,q):
    result=[]
    i,j=0,0
    #p[i]とq[j]を比べて小さい方をresultに加える。
    while i<len(p) and j<len(q):
        #.appendで加える。小さかった方の数字を一つ進める。
        #これで小さいものから順にresultに加えることができる。
        if p[i] <= q[j]:
            result.append(p[i])
            i+=1
        else:
            result.append(q[j])
            j+=1

    #片方の配列が全て比較済みとなると、そこで上のwhile分は終了してしまう
    #なのでまだ比較していない要素が残っている配列をそのままresultに連結する
    #これは.extendを使う。まだ比較していない要素はスライスで簡単に分けることができる。
    if i<len(p):
        result.extend(p[i:])
    if j<len(q):
        result.extend(q[j:])
    #resultを返して終了
    return result

# mergesort(a): 配列 a をマージソートしたものを返す。
def mergesort(a):
    #要素が一つになるまで分割したらそれ自体を返す。
    if len(a)==1:
        return a
    #最初の半分を切り取ってソート
    p=mergesort(a[:len(a)//2])
    #残りの半分を切り取ってソート
    q=mergesort(a[len(a)//2:])
    #merge関数で連結させる
    return merge(p,q)

if M==1:
    print(0)
else:
    zahyo = mergesort(zahyo)
    gap = []
    for i in range(M-1):
        gap += [zahyo[i+1]-zahyo[i]] 
    gap = mergesort(gap)
    if N >= M:
        print(0)
    else:
        for i in gap[:M-N]:
            sum += i
        print(sum)

Submission status: Accepted

Solution code:

(N,M) = list(map(int,input().split()))
xlis = list(map(int,input().split()))
if N >= M:
    print("0")
else:
    xlis.sort()
    dist_lis = []
    for i in range(1,len(xlis)):
        dist_lis.append(xlis[i] - xlis[i-1])
    dist_lis.sort()

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

Submission status: Accepted

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

l.sort()
d = l[len(l)-1] - l[0]
distance = []
sum = 0

for j in range(len(l)-1):
    distance.append( l[j+1] - l[j] )

distance.sort(reverse=True)

if n >= m:
    print("0")
else:
    for i in range(n-1):
        sum += distance[i]
    print(d-sum)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())#駒の数N,地点の数M
 
xList = [int(i) for i in input().split()]
xList.sort()#地点座標xList
 
distanse_list = []

#駒の数が座標の数以上のとき動かす必要がない
if N > M:
    N = M

if N == M:
    print("0")
#
else:
    for i in range(len(xList)-1):
        distanse = xList[i+1] - xList[i]
        distanse_list.append(distanse)
 
    distanse_list.sort()
    print(sum(distanse_list[0:int(M - N)]))

Submission status: Accepted

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

X.sort()

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

for i in range(N - 1):
  if T == []:
    break
  del T[-1]
  
Ans = 0
for i in T:
  Ans += i

print(Ans)

Submission status: Accepted

Solution code:
N, M = map(float, input().split())

xList = [int(i) for i in input().split()]
xList.sort()

distanse_list = []
if N > M:
    N = M

if N == M:
    print("0")
else:
    for i in range(len(xList)-1):
        distanse = xList[i+1] - xList[i]
        distanse_list.append(distanse)

    distanse_list.sort()
    print(sum(distanse_list[0:int(M - N)]))

Submission status: Accepted

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

x = sorted(x)
diff = np.asarray([x[i + 1] - x[i] for i in range(len(x) - 1)])
argsort = diff.argsort()[::-1][:n - 1]

start = 0
sum_length = 0
for split_idx in argsort:
    sum_length += x[split_idx] - x[start]
    start = split_idx + 1
sum_length += x[-1] - x[start]

print(sum_length)

Submission status: Accepted

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

if(n>m):
    print("0")
else:
    print(sum(s[:m-n]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

if M != 1:
    for i in range(0,M-1):
        tousa.append(abs(Xls[i+1] - Xls[i]))




tousa.sort()


if N<M:
    for i in range(0,N-1):
        tousa.pop()
else:
    print(0)
    exit()
ret = 0

for i in tousa:
    ret = i + ret

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

if N >= M:
    c = 0
else:
    X = np.array(sorted(X))
    dist = np.sort(X[1:] - X[:-1])
    c = np.sum(dist[:M-N])

print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力

import copy

n, m = map(int, input().split())
ary = [int(s) for s in input().split()]

if n < m :
    ary.sort()
    sa = []
    
    for i in range(0, m - 1):
        sa.append(ary[i + 1] - ary[i])
    
    #print(sa)
    
    sa.sort()
    
    for i in range(0, n - 1):
        sa.pop(-1)
    
    print(sum(sa))
else :
    print(0)

Submission status: Accepted

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