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:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,M = LMIIS()
    X = LMIIS()
    X.sort()
    place = [False]*M
    if N>=M:
        print(0)
        return
    
    diff = []
    pre = X[0]

    for i,x in enumerate(X):
        diff.append((x-pre,i))
        pre = x

    
    sorted_d = sorted(diff,reverse=True)
    # print(diff)
    # 答えは絶対にM//N 以下
    N -= 1 #一番左端に一つおく
    place[0] = True
    num = 0
    for d,i in sorted_d:
        if num == N:
            break
        place[i] = True
        num += 1
    # print(place)
    # print(diff)
    sum_l = 0
    for i,b in enumerate(place):
        if b:
            pass
        else:
            d,di = diff[i]
            # print(b,diff[i])
            sum_l += d
    print(sum_l)
        


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()
memo = []
for i in range(m-1):
    memo.append(x[i+1]-x[i])
memo.sort()
ans = 0
for i in range(m-n):
    ans += memo[i]
print(ans)

Submission status: Accepted

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

    interval = sorted([X[i + 1] - X[i] for i in range(M - 1)], reverse=True)
    return sum(interval[N - 1:])  # コマ N 個によって、距離が大きな区間 N-1 箇所を動かさずに済む

print(c_streamline())

Submission status: Accepted

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

if N == 1:
    print(abs(H[M-1] - H[0]))
else:
    A = []
    for i in range(M-1):
        A.append(H[i+1] - H[i])
    A = sorted(A,reverse = False) 
    print(sum(A[:-N+1]))

Submission status: Accepted

Solution code:
n,m,*a=map(int,open(0).read().split())
a.sort()
b=[i-j for i,j in zip(a[1:],a)]
b.sort()
print(sum(b[:max(0,m-n)]))

Submission status: Accepted

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

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

t.sort()
memo = []
for i in range(m-1):
    memo.append(t[i+1]-t[i])
memo.sort()
#print(memo)
ans = 0
for i in range(m-n):
    #print(i)
    ans += memo[i]
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
C = []
if m <= n:
	print(0)
	exit()
for i in range(1, m):
	C.append(abs(X[i] - X[i-1]))
C.sort()
for i in range(n-1):
	C[m-2-i] = 0
print(sum(C))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if n>=m:
    answer = 0
else:
    remainder_lst = []
    for i in range(m-1):
        x1 = x_lst[i]
        x2 = x_lst[i+1]

        remainder = x2 - x1
        remainder_lst.append(remainder)

    remainder1_lst = sorted(remainder_lst, reverse=True)
    delete = 0

    for i in range(n-1):
        remainder1 = remainder1_lst[i]
        delete += remainder1
    answer =  sum(remainder_lst) - delete

print(answer)

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:
    D = [0]*(m-1)
    for i in range(1, m):
        D[i-1] = abs(X[i] - X[i-1])
    D.sort()
    print(sum(D[:m-n]))

Submission status: Accepted

Solution code:
import heapq as h

N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
if N>=M:
    print(0)
else:
    data=[]
    h.heapify(data)
    for i in range(0,M-1):
        h.heappush(data,-X[i+1]+X[i])
    s=0
    for i in range(0,N-1):
        s+=(-1)*h.heappop(data)
    print(X[M-1]-X[0]-s)

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    diff = sorted([abs(x[i-1] - x[i]) for i in range(1, m)], reverse=True)
    print(sum(diff[n-1:]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m, *X = map(int, open(0).read().split())
X.sort()
if n >= m:
  print(0)
else:
  Y = [0] * m
  for i in range(m-1):
      Y[i] = X[i+1] - X[i]
  Y.sort()
  print(sum(Y[:m-n+1]))

Submission status: Accepted

Solution code:
N , M = (map(int , input().split()))
X = list(map(int , input().split()))
sorted_x = sorted(X)
total_kukann = 0
kukann_kyori = []
a = N-1
for i in range(len(sorted_x)-1):
    kukann_kyori.append(sorted_x[i + 1] - sorted_x[i])
    total_kukann += sorted_x[i + 1] - sorted_x[i]
kukann_kyori.sort()
if N >= M:
    total_kukann = 0
else:
    for j in range(1,a+1):
        total_kukann -= kukann_kyori[-j]
print(total_kukann)

Submission status: Accepted

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

if n >= len(x):
    print(0)
    sys.exit()
lis = []    
for i in range(m-1):
    lis.append(abs(x[i+1]-x[i]))
lis = sorted(lis,reverse = True)    
print(sum(lis) - sum(lis[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#117_C
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
D=[X[i+1]-X[i] for i in range(M-1)]
D.sort()
print(sum(D[:M-N]) if N<M else 0)

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
X = np.array(input().split(), dtype=np.int64)
X.sort()
print(sum(sorted(X[1:] - X[:-1], reverse=True)[(N - 1):]))

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    *x, = map(int, input().split())
    if n >= m: return 0
    x = sorted(x)
    sec_x = [abs(x[i] - x[i+1]) for i in range(m-1)]
    sec_x = sorted(sec_x)
    ans = sum(sec_x[:(m-1 - (n-1))])
    return ans

print(main())

Submission status: Accepted

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

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

A.sort()
ans=0
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())) # len(X) = M
X.sort()

if N >= M:
  print(0)
else:
  m = M - N
  diff = []
  for xl, xr in zip(X, X[1:]):
    diff.append(xr - xl)
  
  diff.sort()
  print(sum(diff[:m]))

Submission status: Accepted

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

if n >= m or m == 1:
    print(0)
else:
    move = sorted([x[i] - x[i - 1] for i in range(1, m)])

    ans = sum(move[:-n + 1]) if n > 1 else sum(move)
    print(ans)

Submission status: Accepted

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

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

l = list(map(int, input().split()))
l.sort()
x =[]
for i in range(m-1):
    x.append(l[i+1]-l[i])
x.sort()
z = []
for j in range(len(x)-n+1):
    z.append(x[j])
print(sum(z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

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

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

a.sort()
if M!=1:
    d = max(a) - min(a)

    da = [-a[i]+a[i-1] for i in range(1,M)]

    heapq.heapify(da)

    for i in range(N-1):
        if not len(da):break
        d += heapq.heappop(da)
else:d = 0
print(d)

Submission status: Accepted

Solution code:
import numpy as np
N,M,*X=map(int,open(0).read().split())
X.sort()
a=np.array(X)
d=np.diff(a)
d.sort()
all=X[-1]-X[0]
left=d[::-1][:N-1].sum() #rverse + slice + sum
print(all-left)

Submission status: Accepted

Solution code:
import numpy as np
N,M,*X=map(int,open(0).read().split())
X.sort()
a=np.array(X)
d=np.diff(a)
d.sort()
all=X[-1]-X[0]
left=d[-1:-N:-1].sum()
print(all-left)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if n-m>=0: print(0)
else:
    l=[x[i+1]-x[i] for i in range(len(x)-1)] #階差
    ll=sorted(l, reverse=True)
    print(x[-1]-x[0]-sum(ll[0:n-1]))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
*a,=map(int,input().split())
a.sort()
d=[a[i+1]-a[i] for i in range(m-1)]
d=sorted(d,reverse=True)
if m<=n:
	print(0)
else:
	print(sum(d[(n-1):]))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calculate(n,m,arr):

    if n >= m:
        print(0)
        return

    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    drr = drr[:(m-n)]
    print(sum(drr))



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

Submission status: Accepted

Solution code:
def calculate(n,m,arr):
    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    total = 0
    for i in range(m-n):
        total += drr[i]
    print(total)



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

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

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

Submission status: Accepted

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

#n=2
#m=5
#x=[10 ,12,1,2,14]

#n=3
#m=7
#x=[-10,-3,0,9,-100,2,17]

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

Submission status: Accepted

Solution code:
import itertools
import math
import sys

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

Submission status: Accepted

Solution code:
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
"""
入力Pを何個かの塊にわかる
塊の中では1個の駒による操作が行われる、
1回にとなりの1ますに歯科移動できない
すなわちそのなかでの最大と最小の差の絶対値

"""
#solution:
#潜 影 蛇 手 ! !
N,M=pin()
P=sorted(list(pin()))#大小関係を整理する
#print("*",P)
PP=[abs(P[j+1]-P[j]) for j in range(M-1)]
#print(PP)
Q=sorted(PP)
#print(Q)
if N>=M:#この場合必ず1個の拠点に1個以上のコマが置けるのでこのままでいい
    print(0)
else:
    for i in range(N-1):
        Q.pop()
    print(sum(Q))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
L = list(map(int, input().split()))
L.sort()
s = len(L)
L_dif = [[i,L[i+1]-L[i]] for i in range(s-1)]
L_dmax = L_dif.copy()
L_dmax.sort(key=lambda tup: tup[1:], reverse=True)
size = L[K-1]-L[0]
piv = min(K,N)
rem_dif = [L_dmax[i][1] for i in range(piv-1)]
ans = size - sum(rem_dif)
print(ans)

Submission status: Accepted

Solution code:
# ABC 117 C - Streamline
N, M = map(int, input().split())
X = list(map(int, input().split()))

X.sort()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = abs(X[i+1] - X[i])

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

Submission status: Accepted

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

ans = 0

if not l:
    pass
else:
    ans = sum(l[n-1:])

print(ans)

Submission status: Accepted

Solution code:
N_koma, M_visit = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
diff = []

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

diff.sort()

if (N_koma >= M_visit):
    print(0)
    exit()

answer = sum(diff[: M_visit - N_koma])

print(answer)

Submission status: Accepted

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

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

distance = []

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

distance.sort()

print(sum(distance)-sum(distance[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:
  del x[m:]
  x.sort()
  d = []

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

  d.sort()

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

Submission status: Accepted

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

ans=0

if N>=M:
    print(ans)
else:
    Y=Y[::-1]
    ans=sum(Y[N-1:])
    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 = sorted(x)
y = []
for i in range(m-1):
    y.append(abs(X[i] - X[i+1]))

Y = sorted(y,reverse = True)

ans = sum(Y)
for i in range(n-1):
  ans = ans - Y[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

x.sort()

dis = []

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

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

dis.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, _ = map(int, input().split())
    X = sorted(map(int, input().split()))

    diff = sorted([b - a for a, b in zip(X[:-1], X[1:])], reverse=True)
    print(sum(diff[N-1:]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

x.sort()
d=[abs(x[i]-x[i+1]) for i in range(m-1)]
d.sort(reverse=True)
print(abs(x[m-1]-x[0])-sum([d[i] for i in range(n-1)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
D=[0 for i in range(m-1)]
for i in range(m-1):
    D[i]=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:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
ans = 0
dis = []
for i in range(m-1):
    dis.append(x[i+1] - x[i])
dis.sort()
#print(dis)
for i in range(len(dis)):
    if i < m - n:
        ans += dis[i]
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if m <= n:
    print(0)
    exit()

x = sorted(x)
diffs = []
for i in range(m - 1):
    diffs.append(x[i + 1] - x[i] - 1)
diffs = sorted(diffs, reverse=True)
print(x[-1] - x[0] + 1 - sum(diffs[: n - 1]) - n)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Xlist = sorted(list(map(int, input().split())))
sbn = [abs(Xlist[i] - Xlist[i + 1]) for i in range(M - 1)]
sbn.sort(reverse=True)
print(sum(sbn[N - 1:]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(sorted(D)[::-1][n-1:]))

Submission status: Accepted

Solution code:
import sys

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

li.sort()
x = []

if N == 1:
  print(abs(li[-1] - li[0]))
  sys.exit()
elif N >= M:
  print(0)
  sys.exit()

for index, i in enumerate(li):
  if index+1 > len(li) - 1:
    break

  x.append(abs(li[index] - li[index+1]))

x.sort(reverse=True)

for i in range(N-1):
  x.pop(0)


print(sum(x))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = sorted(map(int, input().split()))
if n >= m:
  print(0)
else:
  d = [x[i + 1] - x[i] for i in range(m - 1)]
  s = sorted(d, reverse=True)[:n - 1]
  i = [0] + [d.index(si) + 1 for si in s] + [0]
  ans = 0
  for k in range(n):
    ans += x[i[k + 1] - 1] - x[i[k]]
  print(ans)

Submission status: Accepted

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

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

L = sorted([x[i+1]-x[i] for i in range(m-1)], reverse=True)
print(sum(L[n-1:]))

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
M,N=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
l=[None]*(N-1)
for i in range(N-1):
    l[i]=X[i+1]-X[i]
l.sort()
if N>M:
    print(sum(l[:N-M]))
else:
    print(0)

Submission status: Accepted

Solution code:
import heapq

n, m = map(int, input().split())
if n >= m:
    print(0)
else:
    x = sorted([int(i) for i in input().split()])
    diff = []
    for i in range(m-1):
        diff.append(x[i]-x[i+1])
    heapq.heapify(diff)

    for _ in range(n-1):
        heapq.heappop(diff)
    print(-sum(diff))

Submission status: Accepted

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

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

l=[]
for i in range(m-1):
    l+=[x[i+1]-x[i]]

l=sorted(l)
print(sum(l)-sum(l[-n+1:]) if n!=1 else sum(l))

Submission status: Accepted

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

for i in range(M-1):
    diff[i]=X[i+1]-X[i]
    
diff.sort()
if M-N>=0:
    ans=sum(diff[:M-N])
else:
    ans=0
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 = sorted(X)
# print(X)
d = [X[i + 1] - X[i] for i in range(len(X) - 1)]
d = sorted(d)
# print('d', d)
if N == 1:
    print(sum(d))
else:
    print(sum(d[:-N + 1]))

Submission status: Accepted

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

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

dis = [x[i+1] - x[i] for i in range(m-1)]
dis = sorted(dis, reverse=True)

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

Submission status: Accepted

Solution code:
def solve(n, m, X):
    if n >= m:
        return 0
    X.sort()
    distances = sorted(X[i + 1] - X[i] for i in range(0, m - 1))
    return sum(distances[:m - n])

_n, _m = map(int, input().split())
_X = list(map(int, input().split()))
print(solve(_n, _m, _X))

Submission status: Accepted

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

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N, M = MAP()
    X = LIST()

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

    X = sorted(X)

    H = []
    heapify(H)
    for i in range(len(X)-1):
        heappush(H, (X[i+1] - X[i])*(-1))

    for i in range(N-1): heappop(H)

    print(sum(H)*(-1))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
# 差分の大きい値（移動距離が長い）は駒の数-1でショートカットできる
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)
 
ans = 0
if K-N >= 0:
  ans = sum(X[:K-N])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
# 差分の大きい値（移動距離が長い）は駒の数-1でショートカットできる
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)

ans = 0
if K-N >= 0:
  ans = sum(X[:K-N])
print(ans)

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:
	print(0)
else:
	sorted_x = sorted(x)
 
	diff_x = []
	diff = 0
	result = 0
 
	for i in range(m-1):
		diff = sorted_x[i+1] - sorted_x[i]
		result += diff
		diff_x.append(diff)

	sorted_diff = sorted(diff_x, reverse=True)[:n-1]
 
	for i in range(n-1):
		result -= sorted_diff[i]
	 
	print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N,M = cin.mapInt()
    X = cin.mapInt()
    S = [0] * (M - 1)
    X.sort()
    for i in range(M - 1):
        S[i] = X[i + 1] - X[i]
    ans = sum(S)
    S.sort(reverse = True)
    for i in range(min(N - 1,M - 1)):
        ans -= S[i]
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
#print(x)
if m-n <= 0: print(0)
else:
    dist = []
    for i in range(m-1):
        dist.append(x[i+1] -x[i]) #差を計算する
    dist.sort() #差を小さい順に並べる
    #print(dist)
    print(sum(dist[:m-n]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC117_C

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

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

Submission status: Accepted

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

ans = positions[-1] - positions[0]
distances = [abs(positions[i] - positions[i-1]) for i in range(1, m)]
distances = sorted(distances)[::-1]

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

Submission status: Accepted

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

diff = []

if n >= m:
    ans = 0
else:

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

    diff = sorted(diff,reverse=True)

    for i in range(n-1):
        diff.pop(0)
    
    ans = sum(diff)
    

print(ans)

Submission status: Accepted

Solution code:
n,m =map(int, input().split( ))
if n>=m:
    print(0)
    exit()
###1ターンで「こまを一つ選び」動かす
#並列に動かすのではない
x = list(map(int, input().split( )))
x.sort()
y=[]

for i in range(m-1):
    y.append(x[i+1]-x[i])
y.sort()
#print(y)
tmp = 0
for i in range(n-1):###n-1個避けれる
    tmp += y[-i-1]
print(x[-1]-x[0]-tmp)

Submission status: Accepted

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

Submission status: Accepted

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

xsort = sorted(x)

if n >= m:
    print("0")
else:
    ans = xsort[m - 1] - xsort[0]
    masum = [0] * m
    for i in range(m - 1):
        masum[i] = xsort[i + 1] - xsort[i]
    sum = sorted(masum)
    for i in range(n - 1):
        ans -= sum[len(sum) - 1 - i]
    print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

X.sort()

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

L.sort(reverse=True)
print(X[M-1]-X[0]-sum(L[:N-1]))

Submission status: Accepted

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

a = list(map(int, input().split()))
a.sort()
mark = []
d = []
for i in range(m-1):
    d.append(a[i+1] - a[i])
d.sort()
ans = sum(d)

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

print(ans)