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: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
ii = 0
if a[1] >= a[0]:
    print(ii)
else:
  if a[1] == 1:
    print(ii)
  
  else:
    if a[1] > 1:
      for i in range(0, a[1] - 1):
        d = b[i + 1] - b[i]
        c.append(d)
    
    if a[0] >= 2:
      for i in range(0, a[0] - 1):
        f = max(c)
        c.remove(f) 
    if a[1] != 1:
      print(sum(c))

Submission status: Runtime Error

Solution code:
import sys

n,m=input().split()
N = int(n)
M = int(m)
X = input().split()
#MはXの要素の数
X = [int(x) for x in X]

X.sort()
if M == 1:
    print(0)
    sys.exit()
elif M < 1:
    sys.exit()

minus = [X[i]-X[i-1] for i in range(1,M)]
#print(minus)

minus.sort()
#print(minus)
sum_num = 0
if N < M:
    for i in range(N+1):
        sum_num += minus[i]
    print(sum_num)
elif N >= M:
    print(0)

Submission status: Runtime Error

Solution code:
import sys

n,m=input().split()
N = int(n)
M = int(m)
X = input().split()
#MはXの要素の数
X = [int(x) for x in X]

X.sort()
if M == 1:
    print(0)
    sys.exit()
elif M < 1:
    sys.exit()

minus = [X[i]-X[i-1] for i in range(1,M)]
#print(minus)

minus.sort()
#print(minus)
sum_num = 0
if N < M:
    for i in range(N+1):
        sum_num += minus[i]
    print(sum_num)
elif N >= M:
    print(0):

Submission status: Runtime Error

Solution code:
def main():
    N, M = map(int, input().split())
    if N >= M:
        print(0)
        return

    X = list(map(int, input().split()))
    X.sort()
    dist = [abs(X[i] - X[i+1]) for i in range(M-1)]
    ranking = [[i for i in range(N-1)], [dist[i] for i in range(N-1)]]
    for i in range(N-1, M-1):
        mind = min(ranking[1])
        if dist[i] > mind:
            ind = ranking[1].index(mind)
            ranking[0][ind] = i
            ranking[1][ind] = dist[i]

    ranking[0].sort()
    ans = 0
    start = 0
    for i in range(M-1):
        if i not in ranking[0]:
            ans += dist[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
nm = list(map(int, input().split()))
lis = list(map(int, input().split()))

lis.sort()

sum = 0
diff = []

if nm[1] - nm[0] <= 0:
    print(0)
else:
    for i in range (nm[1] - 1):
        diff[i] = lis[i + 1] - lis[i]

    diff.sort()

    for j in range (nm[0] - 1):
        diff.pop(-1)

    for k in range (nm[1] - nm [0] - 1):
        sum = sum + diff[k]
    
    print(sum)

Submission status: Runtime Error

Solution code:
import sys

n,m=input().split()
N = int(n)
M = int(m)
X = input().split()
#MはXの要素の数
X = [int(x) for x in X]

X.sort()
if M == 1:
    print(0)
    sys.exit()
elif M < 1:
    sys.exit()

minus = [X[i]-X[i-1] for i in range(1,M)]
#print(minus)

minus.sort()
#print(minus)
sum_num = 0
if N < M:
    for i in range(N+1):
        sum_num += minus[i]
    print(sum_num)
elif N >= M:
    print(0):

Submission status: Runtime Error

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

sas=[]
for i in range(0,m-1):
  sas.append(points[i+1]-points[i])
  
#print(sas)

score=0
score=max(points)-min(points)

if sas==[]:
  print(0)
  exit()

for j in range(0,n-1):
  score-=max(sas)
  sas.remove(max(sas))

print(score)

Submission status: Runtime Error

Solution code:
#数直線を切り分ける
#
# 1 2 3  7 20   2




def f(start, cuts):
    if cuts == 0:
        if start > M - 1:
            return 0
        else:
            return X[M - 1] - X[start]
        # 一回も切れないなら全長を返す
    else:
        minscr = 10e6
        for s in range(start, M):
            scr = X[s] - X[start] + f(s + 1, cuts - 1)
            if scr < minscr:
                minscr = scr
        if(minscr == 10e6):
            return 0
        return minscr

N, M = input().split()
N, M = int(N), int(M)
X = input().split()
X = [int(n) for n in X]
X = sorted(X)

best_cut = []
best_hands = 10e6


print(f(0, N - 1))

Submission status: Runtime Error

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

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
# C
N, M = map(int, input().split())
X_list = list(map(int, input().split()))
if M == 1:
    print(0)
elif N >= M:
    print(0)
else:
    X_list = np.array(sorted(X_list))
    delta_list = [0] + [X_list[i] - X_list[i-1] for i in range(1, len(X_list))]
    res = [0]
    for i in range(N-1):
        tmp = np.argmax(delta_list)
        res.append(tmp)
        delta_list[tmp] = -1
    res = sorted(res)
    result = 0
    for i, ii in zip(res[:-1], res[1:]):
        tmp = X_list[i:ii]
        result += max(tmp) - min(tmp)
    tmp = X_list[ii:]
    result += max(tmp) - min(tmp)
    print(result)

Submission status: Runtime Error

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

row_x = input().split()

if n >= m:
    print(0)
else:
    difference = []
    x = sorted(row_x)

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

    for i in range(n-1):
        difference.remove(max(difference))

    print(sum(difference))

Submission status: Runtime Error

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

if N >= M :
    ans = 0
else :
    for i in range(N) :
        if i == 0 :
            left = L[0]
            L = L[1:]
        elif i == 1 :
            right = L[-1]
            L = L[:-1]
        else :
            if abs(left-L[0]) < abs(right-L[-1]) :
                right = L[-1]
                L = L[:-1]
            else :
                left = L[0]
                L = L[1:]
    #print(L)
    ans = 0

    while len(L) :
        #print(abs(left-L[0]),abs(right-L[-1]))
        if abs(left-L[0]) < abs(right-L[-1]) :
            ans += abs(left-L[0])
            left = L[0]
            L = L[1:]
        else :
            ans += abs(right-L[-1])
            right = L[-1]
            L = L[:-1]

print(ans)
#print(L)

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
ii = 0
if a[1] >= a[0]:
  print(ii)
else:
  if a[1] == 1:
    print(ii)
  
  if a[1] > 1:
    for i in range(0, a[1] - 1):
      d = b[i + 1] - b[i]
      if d < 0:
        d = d * -1
      c.append(d)
    
  if a[0] >= 2:
    for i in range(0, a[0] - 1):
      f = max(c)
      c.remove(f) 
  if a[1] != 1:
    print(sum(c))

Submission status: Runtime Error

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

if m == 1:
    print('0')
    exit()

x = sorted(x)
prev = x[0]
diff = []
for i, v in enumerate(x[1:]):
    diff.append([i, abs(prev-v)])
    prev = v

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

split_index = list(map(lambda x: x[0]+1, diff[:n-1]))

ans = x[split_index[-1]:][-1] - x[split_index[-1]:][0]
index_from = 0
for i in range(n-1):
    index_to = split_index[i]
    ans += (x[index_from:index_to][-1] - x[index_from:index_to][0])
    index_from = index_to
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N,M = map(int,input().split())
Xs = sorted([int(i) for i in input().split()])

if N >= M:
    print(0)
else:
    margin = [Xs[i+1]-Xs[i] for i in range(len(Xs)-1)]
    margin = [(i,x) for i,x in enumerate(margin)]
    point = [x[0] for x in sorted(margin,key=lambda x:x[1],reverse=True)[:N-1]]
    move = 0
    move += Xs[point[0]] - Xs[0]
    for i in range(1,len(point)):
        move += Xs[point[i]] - Xs[point[i-1]+1]
    move += Xs[-1] - Xs[point[-1]+1]
    print(move)

Submission status: Runtime Error

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


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

	D.sort(key = lambda x:(x[1]),reverse =True)
	#print(D)
	sikiri = []
	for i in range(N-1):
		sikiri.append(D[i][0])
	sikiri.sort()
	ans = X[sikiri[0]] - X[0]
	for i in range(N-1):
		l = sikiri[i]+1
		if i == N-2:
			r = M-1
		else:
			r = sikiri[i+1]
		ans+= X[r]-X[l]
print(ans)

Submission status: Runtime Error

Solution code:
import sys

n,m=input().split()
N = int(n)
M = int(m)
X = input().split()
#MはXの要素の数
X = [int(x) for x in X]

X.sort()
if M == 1:
    print(0)
    sys.exit()
elif M < 1:
    sys.exit()

minus = [X[i]-X[i-1] for i in range(1,M)]
#print(minus)

minus.sort()
#print(minus)
sum_num = 0
for i in range(N+1):
    sum_num += minus[i]
print(sum_num)

Submission status: Runtime Error

Solution code:
import numpy as np

N,M=[int(i) for i in input().split()]
x=input().split()
y=[int(i) for i in x]
y.sort()
if(N==1):
    print(y[M-1]-y[0])
    exit()
if(M==1):
    print("0")
    exit()
diff=[]
for i in range(M-1):
    diff.append(y[i+1]-y[i])
sub=[]
array=np.array(diff)
index=np.argsort(array)
c=index[-1::-1]
for i in range(N-1):
    sub.append(c[i])
ans=0
for i in range(N):
    if(i==0):
        ans+=y[sub[i]]-y[0]
    elif(i==N-1):
        ans+=y[M-1]-y[sub[i-1]+1]
    else:
        ans+=y[sub[i]]-y[sub[i-1]+1]
print(ans)

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
ii = 0
if a[1] >= a[0]:
    print(ii)
else:
  if a[1] == 1:
    print(ii)
  
  if a[1] > 1:
    for i in range(0, a[1] - 1):
      d = b[i + 1] - b[i]
      if d < 0:
        d = d * -1
      c.append(d)
    
  if a[0] >= 2:
    for i in range(0, a[0] - 1):
      f = max(c)
      c.remove(f) 
  if a[1] != 1:
    print(sum(c))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb  3 20:43:36 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 27 20:47:58 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
    
all(nstr.count(c) for c in '753')

ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
#A, B, totAB = zip(*ABT)

"""

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

X = [0]*M
X[:] = map(int, input().split())

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

X.sort()

if N == 1:
    print(X[M]-X[0])
    sys.exit()
else:
    pass

dX = [0]*(M-1)
for m in range(M-1):
    dX[m] = X[m+1] - X[m]
dX = np.array(dX)
dX_rank = dX.argsort()[::-1]

X_div = dX_rank[0:N-1]
X_div.sort()
result =0
cnt = 0
last_idx = 0

#print(X_div)
for nx in X_div:
    if cnt == 0:
        result = result + X[nx]-X[0]
        last_idx = nx
        cnt =+ 1         
    else:
        result = result + X[nx] - X[last_idx+1]
        last_idx = nx
    #print(result)
#print(X[-1], X[last_idx+1])
result = result + X[-1] - X[last_idx+1]

print(result)

Submission status: Runtime Error

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

for i in range(N) :
    if i >= M :
        break
    if i == 0 :
        left = L[0]
        L = L[1:]
    elif i == 1 :
        right = L[-1]
        L = L[:-1]
    else :
        if abs(left-L[0]) < abs(right-L[-1]) :
            right = L[-1]
            L = L[:-1]
        else :
            left = L[0]
            L = L[1:]
#print(L)
ans = 0

while len(L) :
    #print(abs(left-L[0]),abs(right-L[-1]))
    if abs(left-L[0]) < abs(right-L[-1]) :
        ans += abs(left-L[0])
        left = L[0]
        L = L[1:]
    else :
        ans += abs(right-L[-1])
        right = L[-1]
        L = L[:-1]

print(ans)
#print(L)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
list_x = list(map(int, input().split()))
list_x.sort()
list_data = []
a = list_x[0]
for i,x in enumerate(list_x):
    if i != 0:
        diff = x - a
        a = x
        list_data.append(diff)
    else:
        pass
if len(list_data) != 0:
    for _ in range(N-1):
        list_data.remove(max(list_data))
    print(sum(list_data))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import numpy as np

nm = list(map(int, input().split()))
lis = list(map(int, input().split()))

lis.sort()

diff = []

for i in range (nm[1] - 1):
    diff[i] = lis[i + 1] - lis[i]

diff.sort()

for j in range (nm[0] - 1):
    diff.pop(-1)

print(np.sum(diff))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
Xlist = [int(_) for _ in input().split()]
Xlist.sort()

answer = 0

max_difflist = []
salist = []
ilist = []
flg = 0
for i in range(M-1):
    if flg == 1:
        min_n = min(max_difflist)
    else:
        min_n = -1
    sa = abs(Xlist[i]-Xlist[i+1])

    salist.append(sa)


    if len(max_difflist) < N-1:
        flg = 1
        ilist.append(i)
        max_difflist.append(sa)
    elif sa > min_n:
        max_difflist.sort(reverse=True)
        index = max_difflist.index(min_n)
        max_difflist[index] = sa
        ilist[index] = i


for i_ in ilist:
    salist[i_] = 0

print(sum(salist))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
list_x = list(map(int, input().split()))
list_x.sort()
list_data = []
a = list_x[0]
for i,x in enumerate(list_x):
    if i != 0:
        diff = x - a
        a = x
        list_data.append(diff)
    else:
        pass
if len(list_data) != 0:
    for _ in range(N-1):
        list_data.remove(max(list_data))
    print(sum(list_data))
else:
    print(0)

Submission status: Runtime Error

Solution code:
print(d)

Submission status: Runtime Error

Solution code:
#coding: utf-8
N, M = map(int, input().split())
points = list(map(int, input().split()))
points.sort()
koma = [points[i+1] - points[i] for i in range(M-1)]
for i in range(N-1):
  koma.remove(max(koma))
print(sum(koma))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
sabun = []
j = 1
for i in x:
    if len(x) == 1:
        break
    sabun.append([x[j] - i,j-1])
    j += 1
    if j == m:
        break
sabun.sort(key=lambda x: x[0],reverse=True)
ans = 0
kugiri = []
for i in range(n-1):
    if len(x) == 1:
        break
    kugiri.append(sabun[i][1])
kugiri.sort()
j = 0

for i in range(n):
    if len (kugiri) == 0:
        break
    if i == 0:
        ans += x[kugiri[0]]-x[0]
        #print(x[kugiri[0]]-x[0])
    elif i == n-1:
        #print(x[m-1] - x[kugiri[i-1]+1])
        ans += x[m-1] - x[kugiri[i-1]+1]
    else:
        ans += x[kugiri[i]] - x[kugiri[i-1]+1]
        #print(x[kugiri[i]] - x[kugiri[i-1]+1])
print(ans)

Submission status: Runtime Error

Solution code:
import sys

n,m=input().split()
N = int(n)
M = int(m)
X = input().split()
#MはXの要素の数
X = [int(x) for x in X]

X.sort()
if M < 2:
    print(0)
    sys.exit()

minus = [X[i]-X[i-1] for i in range(1,M)]
#print(minus)

minus.sort()
#print(minus)
sum_num = 0
for i in range(N+1):
    sum_num += minus[i]
print(sum_num)

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split())).sort()
c = []
ii = 0
if a[1] == 1:
  print(ii)
  
if a[1] > 1:
  for i in range(0, a[1] - 1):
    d = b[i + 1] - b[i]
    if d < 0:
      d = d * -1
    c.append(d)
    
if a[0] >= 2:
  for i in range(0, a[0] - 1):
    f = max(c)
    c.remove(f) 
if a[1] != 1:
  print(sum(c))

Submission status: Runtime Error

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

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


for i in range(N-1):
    difX.pop(difX.index(max(difX)))

total = 0
for x in difX:
    total+= x

print(total)

Submission status: Runtime Error

Solution code:
import sys

def split(g):
  gl = g[:-1]
  gr = g[1:]
  sub = [b - a for a, b in zip(gl, gr)]
  i = sub.index(max(sub)) + 1
  return g[:i], g[i:]


def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  g = [int(a) for a in input().split()]
  g = sorted(g)
  ans = 0
  if N < M:
    for _ in range(N-1):
      s, g = split(g)
      ans += s[-1] - s[0]
    ans += g[-1] - g[0]
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import copy
import sys

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

if (m == 1):
    print(0)
    sys.exit()

diffs = []
# それぞれの差を出してみる
for i in range(m - 1):
    diffs.append(x[i + 1] - x[i])

sortedDiffs = copy.copy(diffs)
sortedDiffs.sort()
sortedDiffs.reverse()

usedInd = []
count = 0
groupHeads = []
for i in range(m):
    # クラスタの数分だけ
    if count >= n - 1:
        break
    # 最大のやつのインデックスを取得
    ind = diffs.index(sortedDiffs[i])
    if not ind in usedInd:
        usedInd.append(ind)
        groupHeads.append(ind + 1)
        count += 1

groupHeads.append(0)
groupHeads.sort()
groupHeads.append(len(x))

cost = 0
for i in range(len(groupHeads) - 1):
    cost += x[groupHeads[i + 1] - 1] - x[groupHeads[i]]

print(cost)

Submission status: Runtime Error

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

X.sort()

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

Submission status: Runtime Error

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

if N>M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    tmp=X[old:]
    ans+=max(tmp)-min(tmp)
  print(ans)

Submission status: Runtime Error

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

X.sort()

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
list = list(map(int, input().split()))
list.sort()
dis_list = [list[i+1]-list[i] for i in range(len(list)-1)]
for i in range(n-1):
    dis_list.remove(max(dis_list))
print(sum(dis_list))

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
if a[1] == 1:
  print(0)
  
if a[1] > 1:
  for i in range(0, a[1] - 1):
    d = b[i + 1] - b[i]
    if d < 0:
      d = d * -1
    c.append(d)
    
if a[0] >= 2:
  for i in range(0, a[0] - 1):
    f = max(c)
    c.remove(f) 
if a[1] != 1:
  print(sum(c))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
sabun = []
j = 1
for i in x:
    if len(x) == 1:
        break
    sabun.append([x[j] - i,j-1])
    j += 1
    if j == m:
        break
sabun.sort(key=lambda x: x[0],reverse=True)
j = 0

for i in range(n):
    if len (kugiri) == 0:
        break
    if i == 0:
        ans += x[kugiri[0]]-x[0]
        #print(x[kugiri[0]]-x[0])
    elif i == n-1:
        #print(x[m-1] - x[kugiri[i-1]+1])
        ans += x[m-1] - x[kugiri[i-1]+1]
    else:
        ans += x[kugiri[i]] - x[kugiri[i-1]+1]
        #print(x[kugiri[i]] - x[kugiri[i-1]+1])
print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
typedef double db;
typedef string str;
typedef vector<str> vs;

#define in(x) cin >> x
#define ini(x) int x; cin >> x
#define inl(x) ll x; cin >> x
#define ins(x) str s; cin >> x

#define out(x) cout << x
#define outb(x) cout << x << ' '
#define outn(x) cout << x << '\n'

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz size()
#define len length()
#define all(x) x.begin(), x.end()
#define alln(a, n) a, a+n 

#define forn(i, s, e) for (int i = s; i < e; i++)
#define forr(i, s, e) for (int i = s; i >= e; i--)
#define ford(i, s, e, d) for (int i = s; i < e; i += d)
#define fors(i, n) for (int i = 1; i*i <= n; i++)

const int MAX = (int)1e5 + 9;

int arr[MAX];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	ini(n); ini(m);
	forn(i, 0, m) in(arr[i]);
	sort(alln(arr, m));
	int md = arr[m-1] - arr[0];
	vi d;
	forn(i, 1, m) d.pb(arr[i] - arr[i-1]);
	sort(all(d));
	reverse(all(d));
	forn(i, 0, n-1) md -= d[i];
	outn(md);
		
	return 0;
}

Submission status: Runtime Error

Solution code:
from collections import deque
def aa(l):
  a = deque()
  b = 0
  for i in l:
    if not i:
      a.append(b)
    else:
      b += i
  a.append(b)
  return a

n, m = input().split()
s = sorted([int(i) for i in input().split()])
ss = [s[i+1]-s[i] for i in range(int(m)-1)]

for i in range(int(n)-1):
  ss[ss.index(max(ss))] = 0

print(max(aa(ss)))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
X=sorted(list(map(int,input().split())))
init=[]
mario=n
tmp=0
i=1

if m<n:
  n=m

while i<len(X):
  init.append([X[i]-X[i-1],i])
  i+=1
init.sort(reverse=True,key=lambda x:x[0])
ans=0

for i in range(n-1):
  X.insert(init[i][1]+i,"あ")
i=0
start=0
while i<len(X):
  if i+1<len(X):
    if X[i+1]=="あ":
      ans+=X[i]-X[start]
      start=i+2
      i+=1
  else:
    ans+=X[i]-X[start]
  i+=1
print(ans)

Submission status: Runtime Error

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

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

li = [[0, 0]] * M
for i in range(1, M):
    li[i] = [i, X[i] - X[i-1]]
li.sort(key=lambda x: x[1], reverse=True)
cnt = 0
tmp = []
while cnt < N-1:
    tmp.append(li[cnt][0])
    cnt += 1
prev = 0
ans = 0
for t in tmp:
    ans += abs(X[t-1] - X[prev])
    prev = t
ans += abs(X[-1] - X[tmp[-1]])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
# C
N, M = map(int, input().split())
X_list = list(map(int, input().split()))
if M == 1:
    print(0)
elif N >= M:
    print(0)
else:
    X_list = sorted(X_list)
    delta_list = [0] + [X_list[i] - X_list[i-1] for i in range(1, len(X_list))]
    res = [0]
    for i in range(N-1):
        tmp = np.argmax(delta_list)
        res.append(tmp)
        delta_list[tmp] = -1
    res = sorted(res)
    result = 0
    for i, ii in zip(res[:-1], res[1:]):
        tmp = X_list[i:ii]
        result += max(tmp) - min(tmp)
    tmp = X_list[ii:]
    result += max(tmp) - min(tmp)
    print(result)

Submission status: Runtime Error

Solution code:
import numpy as np

N,M = map(int,input().split())
Xs = sorted([int(i) for i in input().split()])

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

    margin = [Xs[i+1]-Xs[i] for i in range(len(Xs)-1)]
    point = []
    for i in range(N-1):
        p = np.argmax(margin)
        point.append(p)
        margin[p] = 0
    move = 0
    move += abs(Xs[point[0]] - Xs[0])
    for i in range(1,len(point)):
        move += abs(Xs[point[i]] - Xs[point[i-1]+1])
    move += abs(Xs[-1] - Xs[point[-1]+1])
    print(move)

Submission status: Runtime Error

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

d = []

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

res = sum(d)
for i in range(N-1):
    res -= d[i]
else:
    print(res)

Submission status: Runtime Error

Solution code:
spots_and_people = input().split()
spot_values = input().split()

N_spot = int(spots_and_people[1])
N_people = int(spots_and_people[0])

spot_values = [ int(value) for value in spot_values ]
spot_values.sort()

line_length = [ spot_values[i+1] - spot_values[i] for i in range(N_spot-1) ]
line_length.sort()


if N_spot == N_people:
  travel = 0
  
for i in range(N_people-1):
  line_length.pop(-1)
  

travel = sum(line_length)

print(travel)

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
if a[1] == 1:
  print(0)
  
if a[1] > 1:
  for i in range(0, a[1] - 1):
    d = b[i + 1] - b[i]
    if d < 0:
      d = d * -1
    c.append(d)
    
if a[0] >= 2:
  for i in range(0, a[0] - 1):
    f = max(c)
    c.remove(f)    
  
print(sum(c))

Submission status: Runtime Error

Solution code:
import sys

def split(g):
  sub = []
  for i in range(len(g)-1):
    sub.append(g[i+1] - g[i])
  m = max(sub)
  i = sub.index(m) + 1
  return g[:i], g[i:]


def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  g = [int(a) for a in input().split()]
  g = sorted(g)
  if N >= M:
    ans = 0
  else:
    sp = []
    for _ in range(N-1):
      s, g = split(g)
      sp.append(s)
    sp.append(g)
    ans = 0
    for s in sp:
      ans += s[-1] - s[0]
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
spots_and_people = input().split()
spot_values = input().split()

N_spot = int(spots_and_people[1])
N_people = int(spots_and_people[0])

spot_values = [ int(value) for value in spot_values ]
spot_values.sort()

line_length = [ spot_values[i+1] - spot_values[i] for i in range(N_spot-1) ]
line_length.sort()
print(line_length)

if N_spot == N_people:
  travel = 0
  
for i in range(N_people-1):
  line_length.pop(-1)
  

print(line_length)

travel = sum(line_length)

print(travel)

Submission status: Runtime Error

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

if n >= m:
    print(0)

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

for i in range(n-1):
    m = max(li)
    li.remove(m)

print(sum(li))

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
if a[1] == 1:
  print(0)
  
if a[1] > 1:
  for i in range(0, a[1] - 1):
    d = b[i + 1] - b[i]
    if d < 0:
      d = d * -1
    c.append(d)
    
if a[0] >= 2:
  for i in range(0, a[0] - 1):
    f = max(c)
    c.remove(f)    
  
e = 0
for i in c:
  e += i

print(e)

Submission status: Runtime Error

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

if M == 1:
	print(0)
elif M == 2:
	if N == 1:
		X_list = [int(_) for _ in list(input().split())]
		print(abs(X_list[0]-X_list[1]))
	else:
		print(0)
else:
	X_list = [int(_) for _ in list(input().split())]

	X_list.sort()
	X_list.reverse()

	X_diff_list = [X_list[i-1]-X_list[i] for i in range(1, M)]
	X_diff_list.sort()
	X_diff_list.reverse()

	answer = sum(X_diff_list)

	if N == 1:
		print(answer)
	else:
		for n in range(N-1):
			answer -= X_diff_list[n]
		print(answer)

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
x.sort()
a=[]
if m==1:
    print("0")
else:
    for i in range(m-1):
        a.append(x[i+1]-x[i])
    a.sort(reverse=True)
    if n==1:
        print(max(x)-min(x))
    else:
        result=0
        for i in range(n-1):
            result+=a[i]
        print(max(x)-min(x)-result)

Submission status: Runtime Error

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
c = []
if a[1] == 1:
  print("0")
  
if a[1] > 1:
  for i in range(0, a[1] - 1):
    d = b[i + 1] - b[i]
    if d < 0:
      d = d * -1
    c.append(d)
    
if a[0] >= 2:
  for i in range(0, a[0] - 1):
    f = max(c)
    c.remove(f)    
  
e = 0
for i in c:
  e += c

print(e)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m = map(int,input().split())
x = np.array(list(map(int, input().split())))
if n>= m:
    print(0)
    exit()
x.sort()
xd = np.diff(x)
xd2=xd.tolist()
for i in range(n-1):
    xd2.remove(max(xd2))
print(sum(xd2))

Submission status: Runtime Error

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

sub_X = []
for i in range(0, M - 1):
    sub_X.append((i, abs(X[i+1] - X[i])))
sub_X.sort(key=lambda x: x[1], reverse=True)

division = set()
if len(sub_X) != 0:
    for i in range(N-1):
        division.add(sub_X[i][0])

ans = 0
start = X[0]
for i in range(M):
    if i in division:
        ans += X[i] - start
        start = X[i+1]
    if i == M-1:
        ans += X[i] - start

print(ans)

Submission status: Runtime Error

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

x.sort()

y=[]
tmp=0
for i in x:
    y.append(i-tmp)
    tmp=i

y.sort()
y.reverse()


res=x[-1]-x[0]


for i in range(n-1):
    res-=y[i]


print(res)

Submission status: Runtime Error

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

if m == 1:
    print(0)
    exit()

x.sort()

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

z.sort()
for _ in range(n - 1):
    z.pop()

print(sum(z))