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 = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

x.sort()

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

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

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 M < N:print(0)
else:print(sum(L[:(M-N)]))

Submission status: Accepted

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

btw = []
for i in range(M):
  if i == M - 1:
    break
  else:
    btw.append(X[i+1] - X[i])
btw = sorted(btw, reverse=True)

ans =0
for e in btw[N-1:]:
  ans += e
 
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in reversed(range(len(X))):
    if i - 1 < 0:
        break
    if (X[i] >= 0 and X[i-1] >= 0) or (X[i] <= 0 and X[i-1] <= 0):
        L.append(abs(X[i] - X[i-1]))
    else:
        L.append(abs(X[i]) + abs(X[i-1]))

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

print(L_min)

Submission status: Accepted

Solution code:
import math
N, M = input().split()
N=int(N)
M=int(M)
values = [int(i) for i in input().split()]
values.sort()
app = []
for i in range(M-1):
    app.append(values[i+1]-values[i])
app.sort()
ans = 0
for i in range(M-N):
    ans += app[i]
print(ans)

Submission status: Accepted

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

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

X.sort()

# print(X)

L = [0] * (M - 1)

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

# print(L)

L.sort(reverse=True)

result = X[-1] - X[0] - sum(L[:N - 1])

print(result)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])
X_distance.sort(reverse=True)

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

Submission status: Accepted

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

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 = [x[i+1] - x[i] for i in range(m - 1)]

    l.sort(reverse=True)

    ans = x[m-1] - x[0]
    for i in range(n-1):
        ans -= l[i]

    print(ans)

Submission status: Accepted

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

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

X.sort()

dX=[]

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

dX.sort()

out=0

for i in range(M-N):
    out+=dX[i]

print(out)

Submission status: Accepted

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

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

x.sort()

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = [int(i) for i in input().split()]
X.sort()
if M == 1:
    print(0)
else:
    ls = []
    for x, y in zip(X, X[1:]):
        ls.append(y-x)
    ls.sort(reverse=True)
    if N >= M:
        print(0)
    else:
        num = 0
        for i in range(N-1):
            num += ls[i]
        print(sum(ls)-num)

Submission status: Accepted

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

N, M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
dif = [None] * (M-1)
for i in range(M-1):
    dif[i] = X[i+1] - X[i]
    
dif = sorted(dif)
res = 0
for i in range(M-N):
    res += dif[i]
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
	n, m = map(int, input().split())
	l = list(map(int, input().split()))
	if m <= n:
		print(0)
		return
	l.sort()
	dif = [l[i+1]-l[i] for i in range(m-1)]
	dif.sort()
	result = 0
	for i in range(m-n):
		result += dif[i]
	print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

X.sort()

Y = []
for i in range(M-1):
  Y.append(abs(X[i]-X[i+1]))
  
Y.sort()
  
res=0
for i in range(M-N):
  res=res+Y[i]

print(res)

Submission status: Accepted

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

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

if N>=2:
    print(sum(dis[:-N+1]))
else: print(sum(dis))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

N, M = map(int, input().split())
if N >= M:
    print(0)
else:
    X = [int(_) for _ in input().split()]
    X = sorted(X)
    diff = [0]*(M-1)

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

X_subtraction.sort(reverse=True)
del X_subtraction[0:N-1]

print(sum(X_subtraction))

Submission status: Accepted

Solution code:
import sys

N,M=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=[x[i+1]-x[i] for i in range(M-1)]
a.sort()

if N>=M:
    print(0)
    sys.exit()
    
print(sum(a[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
L.sort()
K = []
for i in range(M-1):
  K.append(L[i+1]-L[i])
if N >= M:
  print(0)
else:
  K.sort()
  K.reverse()
  ans = L[M-1]-L[0]
  for i in range(N-1):
    ans -= K[i]
  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()
    X_sub = []
    for i in range(len(X)-1):
        X_sub.append(X[i+1]-X[i])
    X_sub.sort()
    for i in range(N-1):
        del X_sub[-1]
    print(sum(X_sub))

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

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

X_dif = sorted([X[i + 1] - X[i] for i in range(M - 1)], reverse=True)
X_dif = X_dif[N - 1:]

print(sum(X_dif))

Submission status: Accepted

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

if N >= M:
    print(0)
else:
    X = sorted(X)
    X_diff = [X[i] - X[i-1] for i in range(1, len(X))]
    X_diff = sorted(X_diff, reverse=True)
    print(sum(X_diff[N-1:]))

Submission status: Accepted

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

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 = np.sort(X)

diff = np.diff(np.sort(X), n=1)
print(np.sum(np.sort(diff)[::-1][N-1:]))

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

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

n, m = na()
li = na()
ls = []
list.sort(li)
for i in range(len(li) - 1):
    ls.append(li[i + 1] - li[i])
ls = sorted(ls, reverse=True)
print(sum(ls[n-1:]))

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -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 S(): return input()
def pf(s): return print(s, flush=True)

N, M = LI()
X = LI()
X = sorted(X)
X_dist = []
for i in range(M-1):
    X_dist.append(abs(X[i] - X[i+1]))
#  print(X_dist)
X_dist = sorted(X_dist, reverse=True)
print(sum(X_dist[N-1:]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())  # 複数数値入力
X = list(map(int, input().split()))  # リスト入力（数値）  map関数はリストの各要素に対して関数を適用してくれ
X.sort()

if N >= M:
    print(0)
else:
    list = list()
    last = 0
    flg = True
    for x in X:
        if flg == True:
            last = x
            flg = False
            continue

        list.append(x - last)
        last = x

    list.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

#https://severrabaen.hatenablog.com/entry/2019/02/03/224150

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

x.sort()

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

aida.sort()
aida.reverse()

del aida[:n-1]

print(sum(aida))

Submission status: Accepted

Solution code:
def main():
    N, M = [int(n) for n in input().split()]
    X = [int(n) for n in input().split()]
    X.sort()
    diffs = [X[i+1]-X[i] for i in range(M-1)] # diffs[i]:=X[i+1]-X[i]
    diffs.sort(reverse=True)
    # print(diffs)
    cut = sum(diffs[:N-1])
    # print(cut)
    print(X[-1] - X[0] - cut)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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


"""
N個のコマ：N-1個の閉経路が存在する
各座標の距離を求める(M-1個)
M：座標の数　N:駒の数
移動しなくても通っている座標の数: N
M - N
"""

Submission status: Accepted

Solution code:
N, M = map(int, input().split())  # N個のコマ, M個の訪れるべき座標

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = list(map(int, input().split()))
x_new = sorted(x)
intervals = [0]*(m-1)
for i in range(m-1):
  intervals[i] = abs(x_new[i+1] - x_new[i])
c = sorted(intervals, reverse=True)
s = 0
if n < m:
    for j in range(n-1):
        s += c[j]
    min = sum(intervals) - s
    print(min)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split(" "))
destination = list(map(int, input().split(" ")))
dis = []
ans = 0
destination.sort()
for i in range(M-1):
    dis.append(destination[i+1] - destination[i])
dis.sort()
for j in range(M-N):
    ans += dis[j]
print(ans)

Submission status: Accepted

Solution code:
NM = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
X.sort()
if(NM[1] <= NM[0]):
    print(0)
else:
    list1 = []
    for i in range(NM[1] - 1):
      list1.append(X[i + 1] - X[i])
    list1.sort()
    while(NM[0] > 1):
        del list1[-1]
        NM[0] -= 1
    print(sum(list1))

Submission status: Accepted

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

Submission status: Accepted

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

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

dist = sorted(dist, key=lambda t: t[0], reverse=True)[:N-1]
dist.append((0, 0))
dist = sorted(dist, key=lambda t: t[1])
dist.append((0, 0))

ans = 0
for i in range(len(dist)-1):
    ans += X[dist[i+1][1]-1] - X[dist[i][1]]

print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
l = list(map(int,input().split()))
if M <= N:
    print('0')
else:
    l.sort()
    li = []
    for i in range(M-1):
        difference  = abs(l[i]-l[i+1])
        li.append(difference)
    li.sort()
    li.reverse()
    dif = 0
    for i in range(N-1):
        dif += li[i]
    print(l[M-1]-l[0]-dif)

Submission status: Accepted

Solution code:
#encoding: utf-8

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

X.sort()


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

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()
if n>=m:
    print('0')
else:
    ans=x[m-1]-x[0]-sum(y[i] for i in range(m-n,m-1))
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    di = sorted([xi[i+1]-xi[i] for i in range(m-1)])
    print(sum(di[:m-n]))

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() # O(MlogM)

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

D.sort() # O(MlogM)

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

Submission status: Accepted

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

diff = [b-a for a,b in zip(list,list[1::])]
diff.sort(reverse=True)
print(sum(diff[N-1:]) if M-N >0 else 0)

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    if len(distance) > 0:
        del distance[-1]
    else:
        print(0)
        quit()
print(sum(distance))

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    try:
        del distance[-1]
    except:
        print(0)
        quit()
print(sum(distance))

Submission status: Accepted

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

X = sorted([X[i+1]-X[i] for i in range(M-1)])
ans = sum(X[:M-N])
if M <= N:
  ans = 0
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
w=[[] for i in range(m-1)]
for i in range(m-1):
    w[i]=[x[i],x[i+1]-x[i]]
w=sorted(w,key=lambda y:y[1])
w.reverse()
skip=[]
if m!=1:
    for i in range(n-1):
        skip.append(w[i])
        if i==m-2:
            break
            
    skip=sorted(skip,key=lambda y:y[0])
ans=[]
k=x[0]
for i in skip:
    ans.append(i[0]-k)
    k=i[0]+i[1]
ans.append(x[m-1]-k)
print(sum(ans))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = [int(i) for i in input().split()]
if m > n:
    x = sorted(x)
    diff = [x[i+1] - x[i] for i in range(len(x)-1)]
    diff =sorted(diff)
    ans = sum(diff[0:m-n])
    print(ans)
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+1]-X[i] for i in range(m-1)]
X_diff.sort(reverse=True)

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
X = list(map(int,input().split()))
if N < len(X):
    D =[]
    S = 0
    X.sort()
    for i in range(M-1):
        D.append(X[i+1]-X[i])
    E =sorted(D,reverse = True)
    S = sum(E)
    for j in range(N-1):
        S -= E[j]
    print(S)
else:
    print("0")

Submission status: Accepted

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

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

if M<=N:
  print(0)
else:
  print(X[-1] - X[0] - sum(X_diff[:N-1]))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

del L[:N-1]
print(sum(L))

Submission status: Accepted

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

if(N >= M):
  cost = 0
  print(cost)
else:
 X = input().split()
 for i in range(M):
  X[i] = int(X[i]) 
 X.sort() 

 L = []
 for i in range(M):
  if(i != 0): 
    L.append(X[i]-X[i-1])
 L.sort()

 #cost1 = (X[M-1]-X[0]) - sum(L[-(N-1):])
 #print(cost1)

 tmp = 0
 for i in range(N-1):
  tmp += L.pop(-1)
 cost2 =  (X[M-1]-X[0]) - tmp
 print(cost2)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
X = sorted(list(map(int,input().split())))
num = []
for i in range(m-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())
list = list(map(int,input().split()))

list.sort()

dif = [b-a for a,b in zip(list,list[1:])]
dif.sort()
print(sum(dif[:M-N]) if M-N > 0 else 0)

Submission status: Accepted

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

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

a = sorted(a)
diff = []
for i in range(1, m):
    diff += [abs(a[i] - a[i-1])]

diff = sorted(diff, reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
n,m,*x=map(int,open(0).read().split())
x.sort()
d=[x[i+1]-x[i] for i in range(m-1)]
d.sort(reverse=True)
print(sum(d[n-1:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# nコマ, m地点
def func(n, m, x):
    x.sort()
    dis = []
    for i in range(1, len(x)):
        dis.append(x[i] - x[i-1])
    dis.sort()
    n = min(n, m)
    # print("n:{} dis:{}".format(n, dis))
    print(sum(dis[:m-n]))
# for i in range(1, 10):
    # func(i, 7, [-10, -3, 0, 9, -100, 2, 17])
"""
func(3, 5, [1, 2, 10, 12, 14])
func(3, 7, [-10, -3, 0, 9, -100, 2, 17])
func(100, 1, [-100])
"""
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
func(n, m, x)

Submission status: Accepted

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

if N>=M:
    print(0)
else: 
    dist = [b-a for a, b  in zip(X, X[1:])]
    dist.sort()
    if (N-1)==0:
        print(sum(dist[:M-N]))
    else:
        print(sum(dist[:-(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)
else: 
    dist = [b-a for a, b  in zip(X, X[1:])]
    dist.sort()
    print(sum(dist[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, M: int, X: "List[int]"):
    X.sort()
    intervals = []
    for i in range(1, M):
        intervals.append(X[i] - X[i - 1])
    intervals.sort(reverse=True)
    ret = X[-1] - X[0] - sum(intervals[:N - 1])
    print(ret)
    return


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

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

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

d.sort()
print(sum(d[:m-n]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
lis = [int(x) for x in input().split()]
lis.sort()
if N >= M:
  print(0)
else:
  lis2 = []
  for i in range(M - 1):
    lis2.append(lis[i + 1] - lis[i])
  lis2.sort(reverse = True)
  k = 0
  for i in range(N - 1, M - 1):
    k += lis2[i]
  print(k)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
X = list(map(int, input().split()))

if N >= M:
	print(0)
	exit()
X.sort()
dis = [b - a for a, b in zip(X, X[1:])]
dis.sort()
#print(dis)
print(sum(dis[: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()
X.sort()
flag = [b-a for a,b in zip(X,X[1:])]
flag.sort()
print(sum(flag[: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()
    x_next = X[0]
    flag = [X[_] - X[_-1] for _ in range(1,M)]
    flag.sort()
    flag = flag[:M-N:]
    print(sum(flag))

Submission status: Accepted

Solution code:
pieces_num, markpoints_num = map(int, input().split())
markpoints = sorted(map(int, input().split()))

distances = []
for markpoints_i in range(1, markpoints_num):
    distances.append(markpoints[markpoints_i] - markpoints[markpoints_i - 1])
distances.sort()

print(sum(distances[:1 - pieces_num] if pieces_num != 1 else distances))

Submission status: Accepted

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

Submission status: Accepted

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

if N >= M:
  print(0)
else:
  X_list = [int(x) for x in input().split()]
  X_list.sort()

  L_list = []
  for i in range(1, M):
    L_list.append(X_list[i] - X_list[i-1])
  L_list.sort(reverse=True)
  print(sum(L_list[N-1:]))

Submission status: Accepted

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

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

y = sorted(x)

l = [0] * (m-1)

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

ll = sorted(l)

s = 0

for i in range(0,m-n):
    s = s + ll[i]

print(s)

Submission status: Accepted

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

ans = 0
if N >= M:
	pass
else:
	X.sort()
	# print(X)
	Y = [0]*(M-1)
	for i in range(M-1):
		Y[i] = X[i+1] - X[i]
	Y.sort(reverse=True)
	# print(Y)
	L = 0
	for i in range(N-1):
		L += Y[i]
	ans = X[M-1] - X[0] - L
print(ans)

Submission status: Accepted

Solution code:
import re
import numpy as np

N, M = [int(item) for item in re.split(r"\s", input())]
X = [int(item) for item in re.split(r"\s", input())]

if N >= M:
    print(0)
elif N == 1:
    s_X = np.array(sorted(X))
    print(s_X[-1] - s_X[0])
else:
    splitter = N - 1
    s_X = np.array(sorted(X))

    diffs = np.sort(np.diff(s_X))[::-1] - 1
    diff_sum = diffs[:splitter].sum()
    print(s_X[-1] - s_X[0] + 1 - N - diff_sum)

Submission status: Accepted

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

X.sort()
dis = []
for i in range(len(X)-1):
    dis.append(X[i+1] - X[i])
ans=0
dis.sort()
for i in range(len(dis)-N+1):
    ans += dis[i]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

X.sort()
dX = np.array([X[i+1] - X[i] for i in range(len(X) - 1)])

argsrt = np.argsort(dX)[::-1][:N-1]
idxes = sorted(argsrt)

def calc(l):
    if len(l) == 1:
        return 0

    return l[len(l) - 1] - l[0]

s = 0
last_idx = 0
for i in idxes:
    s += calc(X[last_idx:i+1])
    last_idx = i + 1

s += calc(X[last_idx:])

print(s)

Submission status: Accepted

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

if __name__ == "__main__":
    n,m = map(int, input().split())
    l = list(map(int, input().split()))

    if n >= m:
        print("0")
    else:
        d = (m-1) * [0]

        l = sorted(l)
        for i in range(1,m):
            d[i-1] = l[i] - l[i-1]
        d = sorted(d, key=lambda x: -x)

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

        print(l[-1] - l[0] - ans)

Submission status: Accepted

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