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:
import numpy as np

N,M = map(int,input().split())
Xlist = [int(_) for _ in input().split()]
Xlist.sort()
answer = 0
max_difflist = np.array([])
salist = np.array([])
ilist = np.array([])
min_n = -1

for i in range(M-1):
    sa = abs(Xlist[i]-Xlist[i+1])
    salist = np.append(salist,sa)

    if len(max_difflist)+1 < N:
        ilist = np.append(ilist,i)
        max_difflist = np.append(max_difflist,sa)

    elif sa > min_n:
        max_difflist = np.sort(max_difflist)[::-1]
        index = np.where(max_difflist == min_n)[0]
        max_difflist[index] = sa
        ilist[index] = i
    min_n = np.min(max_difflist)

for i_ in ilist:
    salist[int(i_)] = 0
print(int(sum(salist)))

Submission status: Runtime Error

Solution code:
import numpy as np

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

# 配列を並べ直す
X.sort()

# 距離を算出する
dist = np.array([abs(X[i]-X[i+1]) for i in range(len(X)-1)])

# 上位N-1個のインデックスを取得
index = np.argpartition(-dist, N-1)[:N-1]

# 元の配列をindex+1の部分でグループに分ける
res = np.split(X, index+1)

ans = 0
for i in range(len(res)):
    ans += np.amax(res[i])-np.amin(res[i])
print(ans)

Submission status: Runtime Error

Solution code:
n = list(map(int, input().split())) 
i = list(map(int, input().split())) 
i.sort()
while(n[0] >= n[1]):
  print(0)
  break
k=1
kyori=list()
while k < n[1]: 
  sa=i[k]-i[k-1]
  kyori.append(sa)
  k=k+1
mm=1
idou=sum(kyori)
for mm in range(0, n[0]-1,1):
  saidai=max(kyori)
  idou=idou-saidai
  kyori.remove(saidai)
print(idou)

Submission status: Runtime Error

Solution code:
def unko(arr, M):
  offset = arr[1] - arr[0]
  index = 0
  for i in range(M-1):
    tmp = arr[i+1] - arr[i]
    if offset < tmp:
      offset = tmp
      index = i
  return index

def unko2(arr):
  if len(arr) == 1:
    return 0
  else:
    return max(arr) - min(arr)

def unko3(tree):
  m = 0
  for arr in tree:
    tmp = unko2(arr)
    if tmp > m:
      a = arr
      m = tmp
  
  tree.remove(a)
  index = unko(a, len(a))
  arr1 = a[0:index+1]
  arr2 = a[index+1:]
  tree.append(arr1)
  tree.append(arr2)    
    

a = list(map(int, input().split()))
N, M = a[0], a[1]
b = list(map(int, input().split()))
b.sort()
ans = 0
tree = []
if M == 1:
  print(0)
elif N == 1:
  print(b[-1] - b[0])
else:
  index = unko(b, M)
  left = b[0:index+1]
  right = b[index+1:]
  tree.append(left)
  tree.append(right)
  N -= 1
  while N != 1:
    N-=1
    print(tree)
    unko3(tree)
  print(tree)
  for i in tree:
    ans += unko2(i)
  print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

a1 = list(int(i) for i in input().split())  
x = list(int(i) for i in input().split())  
x = np.sort(x)
n = a[0]

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

if n == 1:
    print(np.sum(delta))
else:
    delta = np.sort(delta)
    f = delta.size - (n - 2)
    print(np.sum(delta[0:f:]))

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 _ in range(N-1):
  koma.remove(max(koma))
print(sum(koma))

Submission status: Runtime Error

Solution code:
def Streamline(n,m,x,a):
	if n > m-1 or x == []:
		return 0
	elif n == 0:
		return max(x)-a
	else:
		s = ( min( x[i]-a + Streamline(n-1,m-i-2,x[i+2:],x[i+1])	for i in range(m-1) ))
		return min(s, Streamline(n-1,m-1,x[1:],x[0]))
n,m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
a = x.pop(0)
print(Streamline(n-1,m-1,x,a))

Submission status: Runtime Error

Solution code:
n = list(map(int, input().split())) 
i = list(map(int, input().split())) 
i.sort()
while(n[0] >= n[1]):
  print(0)
  break
k=1
kyori=list()
while k < n[1]: 
  sa=i[k]-i[k-1]
  kyori.append(sa)
  k=k+1
mm=1
idou=sum(kyori)
idoulist=kyori
for mm in range(0, n[0]-1,1):
  saidai=max(idoulist)
  idou=idou-saidai
  idoulist.remove(saidai)
print(idou)

Submission status: Runtime Error

Solution code:
import numpy as np

a1 = list(int(i) for i in input().split())  
x = list(int(i) for i in input().split())  
x = np.sort(x)
n = a[0]

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

if n == 1:
    print(np.sum(delta))
else:
    delta = np.sort(delta)
    print(np.sum(delta[0:delta.size - (n - 2)]))

Submission status: Runtime Error

Solution code:
import numpy as np

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

# 配列を並べ直す
X.sort()

# 距離
dist = np.array([abs(X[i]-X[i+1]) for i in range(len(X)-1)])

# 上位N-1個を取得
unsorted_max_indices = np.argpartition(-dist, N-1)[:N-1]
res = np.split(X, unsorted_max_indices+1)
ans = 0
for i in range(len(res)):
    ans += np.amax(res[i])-np.amin(res[i])
print(ans)

Submission status: Runtime Error

Solution code:
import sys

def split(g, n):
  gl = g[:-1]
  gr = g[1:]
  sub = [[(b - a), i+1] for a, b, i in zip(gl, gr, range(len(gl)))]
  sub = sorted(sub, key = lambda x: -x[0])
  j = []
  for i in range(n-1):
    j.append(sub[i][1])
  j = sorted(j)
  jl = [0] + j[:-1]
  res = []
  for l, r in zip(jl, j):
    s = g[l:r]
    res.append(s)
  res.append(g[j[-1]:])
  return res


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:
    sp = split(g, N)
    for s in sp:
      ans += s[-1] - s[0]
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

if m == 1:
    print('0')
    exit()
if n >= m:
    print(0)
    exit()
elif abs(m-n)==1:
    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)

    print(min(diff)[1])
    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 itertools

N, M = map(int, input().split())
X = input().split()
X = [int(_) for _ in X]
X.sort()
m = [_ for _ in range(M)]

if N >= M:
    print(0)
else:
    ans = 100000000000000
    for t in itertools.combinations(m, N - 1):
        a = 0

        for i in range(N):
            if(i == 0):
                a += X[t[i]] - X[0]
            elif(i == N - 1):
                if(t[N - 2] != M - 1):
                    a += X[M-1] - X[t[N-2] + 1]
            else:
                a += X[t[i]] - X[t[i-1] + 1]

        ans = min(a, ans)

    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

# 配列を並べ直す
X.sort()

# 距離
dist = np.array([abs(X[i]-X[i+1]) for i in range(len(X)-1)])

# 上位N-1個を取得
unsorted_max_indices = np.argpartition(-my_array, N-1)[:N-1]
res = np.split(X, unsorted_max_indices+1)
ans = 0
for i in range(len(res)):
    ans += np.amax(res[i])-np.amin(res[i])
print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N >= M:
    print(0)
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

total = 0
for n in range(N-1):
    max_index = diff.index(max(diff))
    left = 0
    right = 0
    for l in range(len(diff)):
        if l < max_index:
            left += diff[l]
        elif l > max_index:
            right += diff[l]
    if left == right:
        left_list = diff[:max_index]
        left_max = left_list.index(max(left_list))
        right_list = diff[max_index+1:]
        right_max = right_list.index(max(right_list))
        if left_max > right_max:
            diff = diff[:max_index]
            total += right
        else:
            diff = diff[max_index+1:]
            total += left
    elif left > right:
        diff = diff[:max_index]
        total += right
    else:
        diff = diff[max_index+1:]
        total += left

total += sum(diff)
print(total)

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

  a = []
  i = 0
  while i < m - 1:
    a.append([i, x[i + 1] - x[i]])
    i += 1

  b = sorted(a, key = lambda x : x[1])

  count = 0
  for i in range(n + 1):
    count += b[i][1]

  print(count)

Submission status: Runtime Error

Solution code:
n,m = input().split()
n = int(n)
m = int(m)
 
row_x = input().split()
for i in range(m):
    row_x[i] = int(row_x[i])
 
if n >= m:
    print(0)
else:
    difference = []
    maximun = []
    x = sorted(row_x)
 
    for i in range(m-1):
        diffe = x[i+1]-x[i]
        difference.append(diffe)
        if len(maximun) < (n-1):
            maximun.append(diffe)
        elif min(maximun) < diffe:
            maximun.remove(min(maximun))
            maximun.append(diffe)           

    print(sum(difference)-sum(maximun))

Submission status: Runtime Error

Solution code:
n, m = map(int, raw_input().split())
lis = map(int, raw_input().split())
lis = sorted(lis)
lol = []
for i in range(1, m):
    lol.append(lis[i] - lis[i-1])
lol = sorted(lol)
ans = lis[-1] - lis[0]
idx = len(lol) - 1
if idx >= 0:
    for i in xrange(n-1):
        ans -= lol[idx]
        idx-=1
print ans

Submission status: Runtime Error

Solution code:
import numpy as np

def select():
    max_=0
    index=0
    for i in X:
        temp=min(abs(a-i))+i
        if temp>max_:
            max_=temp
            index=i
    return index

def walk(a,X,ans):
    min_=float('inf')
    x=0
    ppp=0
    for i in X:
        p=abs(a-i)
        dis_min_index=np.argmin(p)
        temp=p[dis_min_index]
        if temp<min_:
            ppp=dis_min_index
            min_=temp
            x=i
    if a[ppp]>x:
        a[ppp]-=min_
    else:
        a[ppp]+=min_
    ans+=min_
    X.remove(x)
    return ans,X

N,M=list(map(int,input().split()))
X=sorted(list(map(int,input().split())))
if N==1:
    print(X[-1]-X[0])  
elif N>=M:
    print(0)
else:
    a=np.array([X[0],X[-1]])
    ans=0
    for i in range(N-2):
        a=np.append(a,select())
    for i in a:
        X.remove(i)
    while(X!=[]):
        ans,X=walk(a,X,ans)
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N, M = map(int, input().split())
list_x = list(map(int, input().split()))
list_x.sort()
list_d = np.diff(list_x)
list_data = list_d.tolist()
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

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

l = [int(i) for i in input().split()]
l.sort()
if n == 1 or m == 1 or m == 2:
    print(l[-1]-l[0])
else:
    d = [0 for i in range(m - 1)]
    for i in range(m-1):
        d[i] = l[i+1] - l[i]
    d = np.array(d)
    dl = d.argsort()[::-1]
    count = 0
    for i in range(n):
        if i == 0:
            count += l[dl[i]] - l[0]
            index = dl[i]
        elif i == n - 1:
            count += l[-1] - l[index+1]
        else:
            count += l[dl[i]] - l[index+1]
            index = dl[i]
    print(count)

Submission status: Runtime Error

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

if m == 1:
    print('0')
    exit()
if n >= m:
    print(0)
    exit()
elif n == m-1:
    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)

    print(min(diff))

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

sorted_li = sorted(li2)

salist = []

for i, temp in enumerate(sorted_li):
  if i == len(sorted_li) - 1:
    break
  sa = sorted_li[i+1] - temp
  salist.append([sa, i])

sorted_temp = sorted(salist, reverse=True)

for i, value in enumerate(range(li1[0] - 2)):
  sorted_temp.pop(0)
  
temp_li = []
for value in sorted_temp:
  temp_li.append(sorted_li[value[1]])
                      
print(max(temp_li) - min(temp_li))

Submission status: Runtime Error

Solution code:
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N >= M:
    print(0)
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

total = 0
for n in range(N-1):
    max_index = diff.index(max(diff))
    left = 0
    right = 0
    for l in range(len(diff)):
        if l < max_index:
            left += diff[l]
        elif l > max_index:
            right += diff[l]
    if left > right:
        diff = diff[:max_index]
        total += right
    else:
        diff = diff[max_index+1:]
        total += left

total += sum(diff)
print(total)

Submission status: Runtime Error

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

n, m = input().split()
xs_str = input().split()

xs = list(map(int, xs_str))
xs.sort()
 
ys = []
for x1, x2 in zip(xs[0:-1], xs[1:]):
    ys.append(abs(x2 - x1))
for i in range(1, int(n)):
    ys.remove(max(ys))

out = sum(ys)
print(out)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    if N >= M:
        print(0)
        sys.exit(0)
    N,M = map(int, lines.pop(0).split(' '))
    X = list(map(int, lines.pop(0).split(' ')))
    X.sort()
    span = []
    for i in range(M-1):
        span.append( X[i+1] - X[i] )
    span.sort()
    for i in range(N-1):
        span.pop(-1)
    print(sum(span))

#
# main
#

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Runtime Error

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

if m == 1:
    print('0')
    exit()
if n >= m:
    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:
N, M = map(int,input().split())
X = list(map(int, input().split()))
X.sort()
diff = []
if M != 1:
  for i in range(M-1):
    diff.append(X[i+1]-X[i])
else:
  diff = [0]

sorted_diff = sorted(diff)
sorted_diff.reverse()

ans = sum(sorted_diff)
for i in range(min([N-1,M])):
  ans -= sorted_diff[i]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N, M = map( int, input().split() )
X_list = np.array( sorted( list( map( int, input().split() ) ) ) )

diff=np.diff(X_list, n=1)

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

for i in range(N-1):
	tmp_max=np.amax(diff)
	diff=diff[~(diff==tmp_max)]

cnt=np.sum(diff)

print(cnt)

Submission status: Runtime Error

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

    if M == 1:
        print(0)
    elif N == 1:
        print(max(X) - min(X))
    else:
        dx = [abs(X[m] - X[m + 1]) for m in range(M - 1)]
        for _ in range(N - 1):
            dx.pop(dx.index(max(dx)))
        print(sum(dx))


main()

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(max(y)-min(y))
#    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=input()
b=input()
c=a.split()
n=int(c[0])
d=b.split()
d=[int(i) for i in d]
d.sort(reverse=True)
count=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n-1):
    ccc=len(ll)
    if ccc>=1
        del ll[-1]
print(sum(ll))

Submission status: Runtime Error

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

if n >= m:
    print(0)
else:
    xs.sort()
    diffs = [xs[i] - xs[i-1] for i in range(1, m)]
    indxs = list(np.array(diffs).argsort()[::-1][:n-1])
    indxs.sort()
    ans = sum(diffs[0:indxs[0]])
    if indxs[-1]+1 < len(diffs):
        ans += sum(diffs[indxs[-1]+1:])
    for i in range(len(indxs)-1):
        ans += sum(diffs[indxs[i]+1:indxs[i+1]])
    print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N > M:
    print(0)
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

total = 0
for n in range(N-1):
    max_index = diff.index(max(diff))
    left = 0
    right = 0
    for l in range(len(diff)):
        if l < max_index:
            left += diff[l]
        elif l > max_index:
            right += diff[l]
    if left > right:
        diff = diff[:max_index]
        total += right
    else:
        diff = diff[max_index+1:]
        total += left

total += sum(diff)
print(total)

Submission status: Runtime Error

Solution code:
import numpy as np

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

l = [int(i) for i in input().split()]
l.sort()
if n == 1 or m == 1:
    print(l[-1]-l[0])
else:
    d = [0 for i in range(m - 1)]
    for i in range(m-1):
        d[i] = l[i+1] - l[i]
    d = np.array(d)
    dl = d.argsort()[::-1]
    count = 0
    for i in range(n):
        if i == 0:
            count += l[dl[i]] - l[0]
            index = dl[i]
        elif i == n - 1:
            count += l[-1] - l[index+1]
        else:
            count += l[dl[i]] - l[index+1]
            index = dl[i]
    print(count)

Submission status: Runtime Error

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

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

abs_diff = []
for i in range(len(diff)):
    abs_diff.append(abs(diff[i]))

abs_diff = sorted(abs_diff)

if abs_diff != []:
    for _ in range(n - 1):
        abs_diff.pop(abs_diff.index(max(abs_diff)))

print(sum(abs_diff))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
 
n, m = map(int, input().split())
l = sorted(list(map(int, input().split())))
divcount = n - 1
 
l1 = l[1:]
l2 = l[:m - 1]
divl = []
 
for i in range(m - 1):
    divl.append(l1[i] - l2[i])
 
divl.sort(reverse = True)
 
if n == 1:
    print(max(l) - min(l))
else:
    for i in range(divcount):
        del divl[0]
    
    print (sum(divl))

Submission status: Runtime Error

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

if n > m:
    print(0)
else:
    xs.sort()
    diffs = [xs[i] - xs[i-1] for i in range(1, m)]
    indxs = list(np.array(diffs).argsort()[::-1][:n-1])
    indxs.sort()
    ans = sum(diffs[0:indxs[0]])
    if indxs[-1]+1 < len(diffs):
        ans += sum(diffs[indxs[-1]+1:])
    for i in range(len(indxs)-1):
        ans += sum(diffs[indxs[i]+1:indxs[i+1]])
    print(ans)

Submission status: Runtime Error

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

N, M = map(int, input().split())
X = [int(v) for v in input().split()]
lx = len(X)

# N=10^5, M=10^5ならO(NM)は間に合わない

# 動的計画法M個の地点を1~N個のコマで回ることを考える。
coma = [0] * N
X.sort()    # 昇順にソート

# n=1
diff = [x2-x1 for x2, x1 in zip(X[1:], X[:-1])]
diff.sort() # 昇順にソート
coma[0] = sum(diff)

if coma[0] == 0:
    print(0)
else:
    for i in range(1, N):
        coma[i] = coma[i-1] - diff[-1]
        del diff[-1]

    print(coma[-1])

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)]
sum_ = sum(koma)
for i in range(N-1):
  sum_ -= max(koma)
  koma.remove(max(koma))
print(sum_)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N,M = map(int, lines.pop(0).split(' '))
    X = list(map(int, lines.pop(0).split(' ')))
    X.sort()
    span = []
    for i in range(M-1):
        span.append( X[i+1] - X[i] )
    span.sort()
    for i in range(N-1):
        span.pop(-1)
    print(sum(span))

#
# main
#

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from itertools import starmap

n, m = map(int, input().split())
l = sorted(list(map(int, input().split())))
divcount = n - 1

if n == 1:
    print(max(l) - min(l))
else:
    divl = sorted(list(starmap(lambda a,b: a - b, zip(l[1:], l[:m - 2])), reverse = True)
    for i in range(divcount):
        del divl[0]
    
    print (sum(divl))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
points=list(map(int,input().split()))
points.sort()
score=0
score=points[-1]-points[0]

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

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:
N, M = map(int, input().split())
X = list(set(map(int, input().split())))
M = len(X)

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:
a=input()
b=input()
c=a.split()
n=int(c[0])
d=b.split()
d=[int(i) for i in d]
d.sort(reverse=True)
count=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n-1):
    del ll[-1]
print(sum(ll))

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)
elif 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 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])
print(sub)
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:
import numpy as np

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

l = [int(i) for i in input().split()]
l.sort()
d = [0 for i in range(m - 1)]
for i in range(m-1):
    d[i] = l[i+1] - l[i]
d = np.array(d)
dl = d.argsort()[::-1]
count = 0
if n == 1 or m == 1:
    print(l[-1]-l[0])
else:
    for i in range(n):
        if i == 0:
            count += l[dl[i]] - l[0]
            index = dl[i]
        elif i == n - 1:
            count += l[-1] - l[index+1]
        else:
            count += l[dl[i]] - l[index+1]
            index = dl[i]
print(count)

Submission status: Runtime Error

Solution code:
def solve(current, n, idx, x):
    if n >= len(x) - idx:
        return 0

    if n == 0:
        return x[-1] - current


    if current is None:
        return solve(x[idx], n - 1, idx + 1, x)

    a = x[idx] - current + solve(x[idx], n, idx + 1, x)
    b = solve(x[idx], n - 1, idx + 1, x)
    return min(a, b)

def main():
    n, m = map(int, input().split())
    x = [int(s) for s in input().split()]
    x.sort()

    print(solve(None, n, 0, x))

main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

def main(inp):
    N,M=map(int,next(inp).split())
    X=np.array(list(map(int,next(inp).split())))
    X=np.sort(X)
    
#     print(N,M,X)
    
    delta=np.empty([M-1],dtype=int)
    
    for i in range(M-1):
        delta[i]=X[i+1]-X[i]
    
#     print(X)
#     print(delta)
    
    
    #delta=np.split(delta,[1,2])
    #print(delta)
    
    splpoint=np.argpartition(np.array(delta),-N+1)[-N+1:]
    splpoint=np.sort(splpoint)
    for i in range(len(splpoint)):
        splpoint[i]+=1
    
#     delta=np.delete(delta,splpoint)
#     print(delta)    
#     print(splpoint)
    
    spl=np.split(X,splpoint)
#     print(spl)
    
    ans=0
    for i in range(len(spl)):
        ans+=spl[i][len(spl[i])-1]-spl[i][0]
        
    return ans
  
print(main(sys.stdin))

Submission status: Runtime Error

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

list.pop(-1)
list2.pop(0)

diff = []
for i in range(len(list)):
    diff.append(list[i] - list2[i])

abs_diff = []
for i in range(len(diff)):
    abs_diff.append(abs(diff[i]))

abs_diff = sorted(abs_diff)

if abs_diff != []:
    for _ in range(n - 1):
        abs_diff.pop(abs_diff.index(max(abs_diff)))

print(sum(abs_diff))

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(M==1):
    print("0")
    exit()
if(N==1):
    print(y[M-1]-y[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])
print(sub)
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:
N,M=map(int,input().split())
x=map(int,input().split())
x=sorted(x)
leng=list()

if(M==1):
    print(0)
    exit()
for i in range(0,len(x)-1):
    leng.append([x[i+1]-x[i],i])
leng=sorted(leng)

n=N
max=len(leng)-1
sets=list()
while n>1:
    n-=1
    sets.append(leng[max][1]+1)
    max-=1

for i in x:
    if(x.index(i) not in sets):
        sets.append(x.index(i))
        break
sets=sorted(sets)
ans=0

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

ans+=x[-1]-x[sets[-1]]

print(ans)

Submission status: Runtime Error

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

n, m = map(int, input().split())
l = sorted(list(map(int, input().split())))
divcount = n - 1

l1 = l[1:]
l2 = l[:m - 1]
divl = []

for i in range(m - 1):
    divl.append(l1[i] - l2[i])

divl.sort(reverse = True)

if n == 1:
    print(max(l) - min(l))
else:
    for i in range(divcount):
        del divl[0]
    
    print (sum(divl))

Submission status: Runtime Error

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

if n > m:
    print(0)
else:
    xs.sort()
    diffs = [xs[i] - xs[i-1] for i in range(1, m)]
    indxs = list(np.array(diffs).argsort()[::-1][:n-1])
    indxs.sort()
    ans = sum(diffs[0:indxs[0]]) + sum(diffs[indxs[-1]+1:])
    for i in range(len(indxs)-1):
        ans += sum(diffs[indxs[i]+1:indxs[i+1]])
    print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin

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

    if N >= M:
        print(0)
        return

    X.sort()
    n = [(i + 1, X[i+1] - X[i]) for i in range(M - 1)]
    n.sort(reverse=1, key=lambda x: x[1])
    ans, s1 = 0, 0
    for i in range(N - 1):
        s2 = n[i][0]
        ans += X[s1:s2][-1] - X[s1:s2][0]
        s1 = s2
    ans += X[s1:][-1] - X[s1:][0]
    print(ans)

input = lambda: stdin.readline()
main()

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 = []
min_n = -1

for i in range(M-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
    min_n = min(max_difflist)

for i_ in ilist:
    salist[i_] = 0
print(sum(salist))