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:
import sys
input = sys.stdin.readline

#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''
n, m = map(int, input().split())
x=list(map(int, input().split()))
x.sort()
#print(x)
sa=[]
for i in range(m-1):
    sa.append(x[i+1]-x[i])
sa.sort()
if n<m:
    print(sum(sa[:m-n]))
else:
    print(0)

Submission status: Accepted

Solution code:
n, m=map(int, input().split())
x=list(map(int, input().split()))
x=sorted(x)
y=[0]*(m-1)

for i in range(m-1):
    y[i]=x[i+1] - x[i]
z=sorted(y)

if n>=m:
    print(0)
else:
    print(sum(z[0:m-n]))

Submission status: Accepted

Solution code:
# C - Streamline

N, M = map(int, input().split())
X = list(int(x) for x in input().split())
X.sort()
diff_x = []
for i in range(1, M):
    diff_x.append(X[i] - X[i-1])
diff_x.sort(reverse=True)

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if M <= N:
    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[-1]-X[0]-sum(L[:N-1]))

Submission status: Accepted

Solution code:

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

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

t = []
t = l[:len(l)-(n-1)]
print(sum(t))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
a=[0]*(m-1)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np


N,M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
Diff = [X[k+1]-X[k] for k in range(len(X)-1)]
Diff.sort()
for _ in range(N-1):
  if len(Diff) > 0:
    Diff.pop(-1)
  else:
    break

print(sum(Diff))

Submission status: Accepted

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

if (N>= M) or (M == 1):
    print(0)
else:
    l.sort()
    l_dist =[]

    for i in range(1, M):
        l_dist.append(abs(l[i]-l[i-1]))

    l_dist.sort()

    ans = sum(l_dist[:len(l_dist) - (N-1)])

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

x_ = sorted(X)
d = []
f = x_[0]
ans = 0
for n in x_[1:]:
    d.append(n - f)
    ans += n - f
    f = n

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if m <= n:
    print(0)
else:
    length = abs(x[0]-x[-1])
    abs_x = [0]*(m-1)
    for i in range(m-1):
        abs_x[i] = abs(x[i+1] - x[i])
    abs_x.sort(reverse=True)
    for i in range(n-1):
        length -= abs_x[i]
    print(length)

Submission status: Accepted

Solution code:
import itertools
import math

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

x.sort()

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

dis.sort(reverse=True)
for i in range(min(n-1,len(dis))):
    dis[i] = 0

print(sum(dis))

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0

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

    sub.sort()

    for i in range(m-n):
        ans += sub[i]


    print(ans)

Submission status: Accepted

Solution code:
import heapq
import sys

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

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

X.sort()
Y=[]
#Y=heapq.heapify(Y)

for i in range(m-1):
	heapq.heappush(Y,(X[i]-X[i+1]))

for _ in range(n-1):
	heapq.heappop(Y)

print(sum(Y)*(-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n,m = list(map(int, input().split()))
x = sorted(list(map(int, input().split())))
x = np.array(x)
diff = sorted(x[1:]-x[:-1])[::-1]
print(sum(diff[n-1:]))

Submission status: Accepted

Solution code:
import numpy as np
n,m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
x = np.array(x)
diff = x[1:]-x[:-1]
low = np.argsort(diff)[::-1][n-1:]
print(sum(diff[low]))

Submission status: Accepted

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


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

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

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

Submission status: Accepted

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

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

l = sorted(l,key=lambda x:x[1],reverse=True)

l = l[:n-1]

l.sort()
l.append([m-1,0])
ans = x[l[0][0]] - x[0]

for i in range(len(l)-1):
	ans += x[l[i+1][0]] - x[l[i][0] + 1]
print(ans)

Submission status: Accepted

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

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

x_list = sorted([int(x) for x in input().split()])
gap_list = sorted([x_list[i] - x_list[i - 1] for i in range(1, m)], reverse=True)
print(sum(gap_list[(n - 1):]))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
if n>=m:
  print(0)
else:
  d=[0]*(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 = sorted(list(map(int,input().split())))

if N>=M:
  print(0)
else:
  distance = [0]*(M-1)
  for i in range(M-1):
    distance[i] = X[i+1]-X[i]
  r_sorted_distance = list(reversed(sorted(distance)))
  print(X[M-1]-X[0]-sum(r_sorted_distance[:N-1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, m, *x = map(int, open(0).read().split())
x.sort()
print(sum(sorted(j - i for i, j in zip(x, x[1:]))[-n::-1]))

Submission status: Accepted

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

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

    distances = sorted(distances, reverse=True)
    ans = sum(distances)

    for i in range(N-1):
        ans -= distances[i]

    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, m, *x = map(int, open(0).read().split())
x.sort()
print(sum(sorted(j - i for i, j in zip(x, x[1:]))[-n::-1]))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc117/tasks/abc117_c

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

if n >= m:
    print(0)
else:
    nums.sort()
    dist = []
    total = 0
    for i in range(m - 1):
        d = nums[i + 1] - nums[i]
        dist.append(d)
        total += d
    dist.sort(reverse=True)

    for i in range(n - 1):
        total -= dist[i]
    print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

# 一番遠い距離から、2点間の距離の中から大きい順に引いていくだけ
res = X[-1] - X[0]
for i in range(min(len(diffs), N-1)):
  res -= diffs[i]

print(res)

Submission status: Accepted

Solution code:
N,A = list(map(int,input().split()))
listA = list(map(int,input().split()))
listB = []
if N >= len(listA):
    print(0)
    exit()
listA.sort()
max_n = listA[-1] - listA[0]
for i in range(A-1):
    listB.append(listA[i+1]-listA[i])
listB.sort(reverse=True)
max_n -= sum(listB[:N-1])
print(max_n)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
if n >= m:
  print(0)
  exit()
s = set(map(int,input().split()))
l = list(s)
l.sort()
gap = []
for i in range(len(l)-1):
  gap.append(l[i+1]-l[i])
gap.sort()
print(sum(gap[:1-n]) if n!= 1 else sum(gap))

Submission status: Accepted

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

li.sort()
base = max(li) - min(li)
cost = []

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

cost.sort()
if n == 1:
    res = 0
else:
    res = sum(cost[-n+1:])

print(base - res)

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()
L = []
for i in range(1,M):
    L.append(X[i]-X[i-1])
L.sort()
L.reverse()
res = sum(L)
for i in range(N-1):
    res -= L[i]
print(res)

Submission status: Accepted

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

points.sort()

points_diff = []
for i in range(len(points)):
    if i != (len(points) - 1):
        points_diff.append(abs(points[i] - points[i+1]))

points_diff.sort()

result = []
for i in range(m - n):
    result.append(points_diff[i])
    
print(sum(result))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
S = [0]*(M-1)
for i in range(M-1):
    S[i] = X[i+1] - X[i]
S.sort()
S.reverse()
print(X[-1]-X[0]-sum(S[: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=[0]*(m-1)
    for i in range(m-1):
        l[i]=x[i+1]-x[i]
    l.sort(reverse=True)
    lsum=0
    for i in range(n-1):
        lsum+=l[i]
    print(x[m-1]-x[0]-lsum)

Submission status: Accepted

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

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

X = sorted(X)

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

A = sorted(A)

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

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]


list.sort(L,reverse=True)



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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, m, *x = map(int, read().split())
x.sort(reverse=True)
memo = [0] * m
for i, (bf, af) in enumerate(zip(x, x[1:])):
    memo[i] = bf - af
memo.sort(reverse=True)
print(sum(memo[n - 1:]))

Submission status: Accepted

Solution code:
import numpy as np
from collections import deque
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
done = [0 for _ in range(M)]
X = sorted(np.diff(np.array(X)))[::-1]
q = deque(X)
for i in range(N):
    if i > 0:
        try:
            q.popleft()
        except:
            print("0")
            exit()
print(sum(q))

Submission status: Accepted

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

Submission status: Accepted

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

xSort = sorted(x)

dif = [0] * (m - 1)
for i in range(m - 1):
    dif[i] = xSort[i + 1] - xSort[i]
ans = sum(dif)

if (n == 1 or m == 1):
    print(ans)

elif (n >= m):
    print('0')
else:
    tar = len(dif) - n + 1
    difSort = sorted(dif)
    difS_2 = difSort[:tar]
    ans = sum(difS_2)
    print(ans)
'''  
    for j in range(n - 1):
        tar = j + 1
        tarInd = tar * -1
        tarVal = sorted(dif)[tarInd]
        ans -= tarVal
'''
# print(ans)
# >>>TLE
#
# n番目の要素を取得
# sorted(list)[-1] > 1番大きい
# sorted(list)[-２] > 2番大きい
'''
  import numpy as np
  difArray = np.array(dif)
  difIndex = difArray.argsort()

  for i in range(n - 1):
    tar = m - i
    maxIndex = difIndex[tar]
    maxVal = diff
'''

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

aida = []
kyori_sum = 0
for i in range(M-1):
    kyori_sum += abs(X[i]-X[i+1])
    aida.append(abs(X[i]-X[i+1]))

aida.sort(reverse=True)
for i in range(N-1):
    kyori_sum -= aida[i]
print(kyori_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n,m=map(int,input().split())
  x=sorted(list(map(int,input().split())))
  if n>=m:
    print(0)
  else:
    y=sorted([x[i]-x[i-1] for i in range(1,m)])
    ans=x[-1]-x[0]-sum(y[m-n:])
    print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
dis=[]
bx=x[0]
if n>=m:
    print(0)
else:
    for ax in x[1:]:
        dis.append(ax-bx)
        bx=ax
    dis.sort(reverse=True)
    print(sum(dis[n-1:]))

Submission status: Accepted

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

dif = []
for i in range(1,len(x)):
  dif.append(x[i] - x[i-1])
dif.sort()
dif.reverse()

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=sorted(list(map(int,input().split())))
s=[]
for i ,x in zip(l,l[1::]):
    s.append(x-i)
ans=l[-1]-l[0]
s.sort(reverse=1)
print(ans-sum(s[:n-1]))

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop

import collections

n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
diff = []
visit = [False]*m
visit[0] = True
if m == 1 or n>=m:
    print(0)
    sys.exit()
for i in range(m-1):
    d = x[i+1] - x[i]
    diff.append(d)
diff.sort(reverse= True)
for i in range(n-1):
    diff[i] = 0
print(sum(diff))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

    if N >= M:
        print(0)
    else:
        X.sort()
        dlist = list()
        for i in range(M-1):
            d = abs(X[i] - X[i+1])
            dlist.append(d)
        dlist.sort()
        print(sum(dlist[:len(dlist) - (N-1)]))
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

x.sort()

dif = []
bef = x[0]
for now in x:
  dif.append(now - bef)
  bef = now

dif.sort()

print(sum(dif[0:max(m-1-n+2, 0)]))

Submission status: Accepted

Solution code:
def myAnswer(N:int,M:int,X:list) -> int:
   if (M == 1): return 0
   if(N == 1): return max(X) - min(X)
   X.sort()
   sub = []
   pre = X[0]
   for n in range(1,M):
      sub.append(X[n] - pre)
      pre = X[n]
   sub.sort(reverse =True)
   return sum(sub[N-1:])

def modelAnswer():
   return
def main():
   N,M = map(int,input().split())
   X = list(map(int,input().split()))
   print(myAnswer(N,M,X[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

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

Submission status: Accepted

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

li.sort()
all_li = []
for i,l in enumerate(li):
    if i==0:
        continue
    all_li.append(l-li[i-1])
all_li.sort(reverse=True)
#print(n,all_li,all_li[n-1:])
print(sum(all_li[n-1:]))

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
#template
N,M = inputlist()
X = inputlist()
X.sort()
if N >= M:
    print(0)
    exit()
d = []
for i in range(1,M):
    d.append(X[i]-X[i-1])
d.sort(reverse=True)
da = d[(N-1):]
print(sum(da))

Submission status: Accepted

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

s.sort()
term=[]
if n-m>=0:
    print(0)
else:
    for i in range(m-1):
        term.append(s[i+1]-s[i])
    
    term.sort()

    for i in range(n-1):
        del term[-1]
    
    print(sum(term))

Submission status: Accepted

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

s.sort()
term=[]
if n >= m:
    print(0)
else:
    for i in range(m-1):
        term.append(s[i+1]-s[i])

    term.sort()

    for i in range(n-1):
        del term[-1]
    
    print(sum(term))

Submission status: Accepted

Solution code:


def solve():

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

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

    X.sort()
    L = []
    for i in range(M-1):
        # 各点の距離を格納
        L.append(X[i+1]-X[i])
    L.sort()
    # 遠い距離を除いた分の距離の合計
    print(sum(L[:M-N]))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
if n >= m:
  print(0)
  quit()
X = list(map(int, input().split()))
X = sorted(X)
M = max(X)
mi = min(X)
ans = M - mi
itl = []
for i in range(m-1):
  k = X[i] - X[i+1]
  itl.append(k)
itl =sorted(itl)
for j in range(n-1):
  ans += itl[j]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N,M=map(int,input().split())
X=list(map(int,input().split()))
X=sorted(X)
Y=[X[i+1]-X[i] for i in range(M-1)]
ans=sum(Y)
Z=np.argsort(Y)[::-1]
N-=1
for z in Z:
  if N==0:
    break
  ans-=Y[z]
  N-=1
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import heapq

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

zentai = x[m-1] - x[0]
kyori = []

for i in range(m-1):
    kyori.append(x[i] - x[i+1])
heapq.heapify(kyori)
tobasi = 0

for i in range(n-1):
    tobasi += heapq.heappop(kyori)
    
print(zentai + tobasi)

Submission status: Accepted

Solution code:
inf = 10**9+7
mod = 10**9+7
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()
for i in range(n-1):
    if sub:
        sub.pop()
if sub:
    print(sum(sub))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def Streamline(n, m, xl):

    xl.sort()
    count = []
    for i in range(m-1):
        count.append(xl[i+1]-xl[i])

    count.sort(reverse=True)
    ans = xl[-1]-xl[0] - sum(count[:n-1])

    return ans

def main():
    n, m = map(int, input().split())
    xl = list(map(int, input().split()))
    print(Streamline(n, m, xl))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
X = np.array(lr())
X.sort()
if N >= M:
    print(0); exit()

Xdiff = np.abs(np.diff(X))
Xdiff.sort()
need = M - N
answer = Xdiff[:need].sum()
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

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

max_X = max(X)
min_X = min(X)
number_of_moves = max_X - min_X

intervals = [X[i] - X[i - 1] for i in range(1, len(X))]
# print(intervals)
intervals.sort(reverse=True)

for i in range(0, N - 1):
    number_of_moves -= intervals[i]

print(number_of_moves)

Submission status: Accepted

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

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

intervals = [X[i] - X[i - 1] for i in range(1, len(X))]
# print(intervals)
intervals.sort(reverse=True)

max_X = max(X)
min_X = min(X)

number_of_moves = max_X - min_X

for i in range(0, N - 1):
    number_of_moves -= intervals[i]

print(number_of_moves)

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

intervals = [X[i] - X[i - 1] for i in range(1, len(X))]
# print(intervals)
intervals.sort(reverse=True)

max_X = max(X)
min_X = min(X)

number_of_moves = max_X - min_X

for i in range(0, N - 1):
    number_of_moves -= intervals[i]

print(number_of_moves)

Submission status: Accepted

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

def main():
    N, M = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))

    if N >= M:
        print(0)
        return

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N, M = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))

    if N >= M:
        print(0)
        return

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

d_sum = sum(distance)
distance.sort()
if N>=M:
    d_sum = 0

elif N !=1:
    for i in range(N-1):
        d_sum-=distance[(M-2)-i]

print(d_sum)

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:
    diff = []
    for i in range(len(X)-1):
        diff.append(X[i+1] - X[i])
    
    diff_sort = sorted(diff)
    
    print(sum(diff_sort[0:M-N]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# solution

import fractions

nim,mike = map(int,input().split())
sets = sorted(map(int,input().split()))
array=[]
for i in range(mike-1):
    array.append(sets[i+1]-sets[i])
array = sorted(array)[::-1]
print(sum(array[nim-1:]))

Submission status: Accepted

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

import numpy as np

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

X_sort = np.diff(arr, n= 1).tolist()
X_sort.sort(reverse=True)
if N >= M:
    print(0)
else:
    print(sum(X_sort[N-1:]))

Submission status: Accepted

Solution code:
import sys
from sys import stdin

n, m = map(int, stdin.readline().rstrip().split())
x = list(map(int, stdin.readline().rstrip().split()))

x.sort()
tmp = []
for i in range(1, len(x)):
    tmp.append(abs(x[i] - x[i - 1]))

tmp = sorted(tmp)
tmp.reverse()

if len(tmp)+1 <= n:
    print(0)
else:
    print(sum(tmp[n-1:]))

Submission status: Accepted

Solution code:
import heapq

n, m = map(int, input().split())
x = list(map(int, input().split()))
if n >= m:
    print(0)
    exit()
dist = []
heapq.heapify(x)
tmp = heapq.heappop(x)
minlen = tmp
for i in range(m - 1):
    tmp1 = heapq.heappop(x)
    leng = (tmp1 - tmp) * (-1)
    dist.append(leng)
    if i == m - 2:
        maxlen = tmp1
    tmp = tmp1
totallen = maxlen - minlen
heapq.heapify(dist)
if n == 1:
    print(totallen)
else:
    for i in range(n - 1):
        length = heapq.heappop(dist) * (-1)
        totallen -= length
    print(totallen)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  
  N, M = (int(_) for _ in input().split())
  X = sorted([int(_) for _ in input().split()])
  if N >= M:
    print(0)
    return
  Y = sorted([X[i] - X[i-1] for i in range(1, M)])
  print(sum(Y[:M-N]))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

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

  dis=0
  for i in range(n-1):
    dis+=dises[i]

  ans=x[m-1]-x[0]-dis
  print(ans)