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:
n,m = map(int,input().split())
x = sorted(map(int,input().split()))
l = sorted([x[i+1]-x[i]for i in range(m-1)])
if n >=m:
    print(0)
else:
    print(sum(l)-sum([-i-1]for i in range(n-1)))

Submission status: Runtime Error

Solution code:
    N, M = [int(n) for n in input().split()]
    X = sorted([int(n) for n in input().split()])
    if N>=M:
        print(0)
    else:
        dx = []
        for j in range(M-1):
            dx.append(X[j+1]-X[j])
        ans = sorted(dx, reverse=True)
        print(sum(ans[N-1:]))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
if n >= m:
  print(o)
  exit()
l = list(map(int,input().split()))
l.sort()
ans = []
for i in range(m-1):
  ans.append(l[i+1]-l[i])
  
ans.sort()
mi = 0
for i in range(m-2,m-1-n,-1):
  mi += ans[i]
print(sum(ans)-mi)

Submission status: Runtime Error

Solution code:
import sys

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

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

    if N == 1:
        print(X[M] - X[0])
        exit()

    X = sorted(X)

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

    kukan = sorted(kukan, reverse=True)

    jogai = 0
    for j in range(N-1):
        jogai += kukan[j]

    print(X[M-1] - X[0] - jogai)

main()

Submission status: Runtime Error

Solution code:
import sys

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

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

    X = sorted(X)

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

    kukan = sorted(kukan, reverse=True)

    jogai = 0
    for j in range(N-1):
        jogai += kukan[j]

    print(X[M-1] - X[0] - jogai)

main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#間隔が大きいところを含まないように分割すればいいのでは？
import bisect
n,m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
ans = []
xmin = X.pop(0)
prev = xmin
for x in X:
	if len(ans) < n-1:
		bisect.insort(ans, abs(prev - x))
	elif ans[0] < abs(prev - x):
		ans[0] = abs(prev - x)
	prev = x
if n >= m:
	print(0)
else:
	print(X[m-2]-xmin - sum(ans))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split()) 
X_lines = str(input())
X_lines = X_lines.split()
X = []
for i in range(len(X_lines)):
    X.append(int(X_lines[i]))

X_arrays = X
X_arrays.sort()      
X_max = X_arrays[len(X_arrays)-1]
X_min = X_arrays[0]
X_kyokai = [X_min]
if not N == 1:
    for i in range(N-1):
        X_kyokai.append((1/N)*((i+1)*X_max +(N-i-1)*X_min))
X_kyokai.append(X_max)

X_all_lines = [[]]
if not len(X_kyokai)-2 == 0:
    for i in range(len(X_kyokai)-2):
        X_all_lines.append([])
for i in range(len(X_kyokai)-1):
    for j in range(M):
        if X_kyokai[i] <= X_arrays[j] and X_arrays[j] < X_kyokai[i+1]:
            X_all_lines[i].append(X_arrays[j])
X_all_lines[len(X_kyokai)-2].append(X_max) 

sum_dif = 0
count = 0
for i in range(len(X_all_lines)):
    if len(X_all_lines[i]) == 0:
        count = count + 1
    else:
        i_max = X_all_lines[i][len(X_all_lines[i])-1]
        i_min = X_all_lines[i][0]
        dif = i_max - i_min
        sum_dif = sum_dif + dif

diff_arrays = []

for i in range(N):
    if len(X_all_lines[i]) >= 2:
        for j in range(len(X_all_lines[i])-1):
            diff_arrays.append(X_all_lines[i][j+1]-X_all_lines[i][j])
            
diff_arrays.sort()
diff_arrays.reverse()

if count >= 1:
    for i in range(count):
        sum_dif = sum_dif - diff_arrays[i]

print(sum_dif)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split()) 
X_lines = str(input())
X_lines = X_lines.split()
X = []
for i in range(len(X_lines)):
    X.append(int(X_lines[i]))

X_arrays = X
X_arrays.sort()      
X_max = X_arrays[len(X_arrays)-1]
X_min = X_arrays[0]
X_kyokai = [X_min]
if not N == 1:
    for i in range(N-1):
        X_kyokai.append((i+1/N)*(X_max+X_min))
X_kyokai.append(X_max)

X_all_lines = [[]]
if not len(X_kyokai)-2 == 0:
    for i in range(len(X_kyokai)-2):
        X_all_lines.append([])
for i in range(len(X_kyokai)-1):
    for j in range(M):
        if X_kyokai[i] <= X_arrays[j] and X_arrays[j] < X_kyokai[i+1]:
            X_all_lines[i].append(X_arrays[j])
X_all_lines[len(X_kyokai)-2].append(X_max) 

sum_dif = 0
for i in range(len(X_all_lines)):
    i_max = X_all_lines[i][len(X_all_lines[i])-1]
    i_min = X_all_lines[i][0]
    dif = i_max - i_min
    sum_dif = sum_dif + dif
    
print(sum_dif)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a, b, c = map(int, input().split())

cnt = 0
while c > 0:
    c -= 1
    cnt += 1
    if b > 0:
        b -= 1
        cnt += 1
        continue
    elif a > 0:
        a -= 1
        continue
    else:
        break

print(cnt)

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = sorted(list(map(int,input().split())))
 
diff = []
if len(xarray) == 1:
    print(0)
elif N == M:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))
 
    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)
        
 
        if N == 1:
            
            print(sum(diff2))
        else:
            for j in range(N-1):
                diff2[j] = 0
            
            print(sum(diff2))

Submission status: Runtime Error

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

diff = []
if len(xarray) == 1:
    print(0)
elif N == M:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))

    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)
        

        if N == 1:
            
            print(sum(diff2))
        else:
            for j in range(N-1):
                diff2[j] = 0
            
            print(sum(diff2))

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = list(map(int,input().split()))
xarray.sort()
print(xarray)
diff = []
if len(xarray) == 1:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))
    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)
        print(diff)
        print(diff2)

        if N == 1:
            
            print(sum(diff2))
        else:
            for j in range(N-1):
                diff2[j] = 0
            print(sum(diff2))

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = list(map(int,input().split()))
xarray.sort()
diff = []
if len(xarray) == 1:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))
    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)

        if N == 1:
            diff2[0] = 0
            print(sum(diff2))
        else:
            for j in range(N-1):
                diff2[j] = 0
            print(sum(diff2))

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = list(map(int,input().split()))
diff = []
if len(xarray) == 1:
    print(0)
else:
    for i in range(len(xarray)-1):
        diff.append(abs(xarray[i+1]-xarray[i]))
    if len(diff) == 0:
        print(0)
    else:
        diff2 = sorted(diff, reverse = True)
        for j in range(N):
            diff2[j] = 0
        print(sum(diff2))

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = list(map(int,input().split()))
diff = []
for i in range(len(xarray)-1):
    diff.append(abs(xarray[i+1]-xarray[i]))

if len(diff) == 0:
    print(0)
else:
    diff2 = sorted(diff, reverse = True)
    for j in range(N):
        diff2[j] = 0
    print(sum(diff2))

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = list(map(int,input().split()))
diff = []
for i in range(len(xarray)-1):
    diff.append(abs(xarray[i+1]-xarray[i]))
diff2 = sorted(diff, reverse = True)
if len(diff2) == 0:
    print(0)
else:
    for j in range(N):
        diff2[j] = 0
    print(sum(diff2))

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
xarray = list(map(int,input().split()))
diff = []
for i in range(len(xarray)-1):
    diff.append(abs(xarray[i+1]-xarray[i]))
diff2 = sorted(diff, reverse = True)
for j in range(N):
    diff2[j] = 0
print(sum(diff2))

Submission status: Runtime Error

Solution code:
from collections import deque
N, M = map(int, input().split())
X = list(map(int, input().split()))
sortX = sorted(X)
dist = []
if M == 1:
    print(0)
else:
    for i in range(M-1):
        dist.append(sortX[i + 1] - sortX[i])
    for i in range(N - 1):
        dist.pop(dist.index(max(dist)))
    print(sum(dist))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
IINF = 10 ** 18


def main():
    N, M = list(map(int, input().split()))
    X = list(map(int, input().split()))
    X = sorted(X)
    left = -IINF
    right = IINF

    for i in range(N):
        if len(X) == 0:
            break
        elif i == 0:
            left = X[0]
            X.pop(0)
        elif i == 1:
            right = X[-1]
            X.pop(len(X)-1)
        else:
            if abs(X[1] - X[-1]) < abs(X[-2] - X[0]):
                left = X[0]
                X.pop(0)
            else:
                right = X[-1]
                X.pop(-1)

    ans = 0
    while len(X) > 0:
        # print(ans, X)
        if abs(left - X[0]) < abs(right - X[-1]):
            ans += abs(left - X[0])
            left = X[0]
            X.pop(0)
        else:
            ans += abs(right - X[-1])
            right = X[-1]
            X.pop(len(X)-1)
        # b = min(abs(left - X[0]), abs(right - X[-1]))
        # print(b, X)
        # print(abs(X[0] - X[-1]) + b)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

list_aaa = sorted(list_aaa)
list_aaa_range = []

for i in range(M-1):
    list_aaa_range.append(list_aaa[i+1]-list_aaa[i])

list_aaa_range = sorted(list_aaa_range)

for i in range(1,N):
    list_aaa_range.pop(-1)

print(sum(list_aaa_range))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
#print(a)
c=a[1]
d=0
cnt=[0]
count=1
s=((a[len(a)-1]-a[0])/m)
if n>=m:
    print(0)
else:
    mini=100000000
    if n==1:
        print(a[m-1]-a[0])
    elif n==2:
        for i in range(m-1):
            d=abs(a[i]-a[0])
            d1=abs(a[len(a)-1]-a[i+1])
            if d+d1<mini:
                mini=d+d1
        print(mini)
    else:
        for i in range(1,m):
            d=a[i]-c
            #print(d,s)
            if d>abs(s):
                cnt.append(i)
                c=a[i]
                count+=1
            if count==n:
                break
            if i==m-1:
                cnt.append(i)
                c=a[i]
                count+=1
        answer=0
        for j in range(0,n-1):
            answer+=min(a[cnt[j+1]-1]-a[cnt[j]],a[cnt[j+1]]-a[cnt[j]+1])
        print(answer)
        #print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=x[len(x)-1]-x[0]
cnt=[0]
d=0
d1=0
mini=100000000
if m<=n:
    print(0)
elif n==1:
    print(a)
else:
    if n==2:
        for i in range(m-1):
            d=abs(x[i]-x[0])
            d1=abs(x[len(x)-1]-x[i+1])
            if d+d1<mini:
                mini=d+d1
        print(mini)
    else:
        i=0
        a=0
        ae=0
        while i<n//2:
            a=x[0]
            ae=x[len(x)-1]
            x.remove(x[0])
            x.remove(x[len(x)-1])
            i+=1
        if n%2==1:
            print(x[len(x)-1]-x[0])
        else:
            for i in range(m-1):
                d=abs(x[i]-a)
                d1=abs(ae-x[i+1])
                if d+d1<mini:
                    mini=d+d1
            print(mini)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=x[len(x)-1]-x[0]
cnt=[0]
d=0
d1=0
mini=100000000
if m<=n:
    print(0)
elif n==1:
    print(a)
else:
    if n==2:
        for i in range(m-1):
            d=abs(x[i]-x[0])
            d1=abs(x[len(x)-1]-x[i+1])
            if d+d1<mini:
                mini=d+d1
        print(mini)
    else:
        ans=sum(x)/n
        c=0
        for i in range(n-1):
            cnt.append(bisect.bisect_left(x[c:],ans))
            c=bisect.bisect_left(x[c:],ans)
            ans-=x[i]/(n-i)
            ans*=(n-i)/(n-i-1)
        #print(cnt)
        a=sum(x)
        answer=0
        for j in range(1,n-1):
            answer+=min(x[cnt[j+1]-1]-x[cnt[j]],x[cnt[j+1]]-x[cnt[j]+1])
        print(answer)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=x[len(x)-1]-x[0]
cnt=[0]
d=0
d1=0
mini=100000000
if m<=n:
    print(0)
elif n==1:
    print(a)
else:
    if n==2:
        for i in range(m-1):
            d=abs(x[i]-x[0])
            d1=abs(x[len(x)-1]-x[i+1])
            if d+d1<mini:
                mini=d+d1
        print(mini)
    else:
        ans=sum(x)/n
        c=0
        for i in range(n-1):
            cnt.append(bisect.bisect_left(x[c:],ans))
            c=bisect.bisect_left(x[c:],ans)
            ans-=x[i]*(n-i)/(n-i-1)
        #print(cnt)
        a=sum(x)
        answer=0
        for j in range(1,n-1):
            answer+=min(x[cnt[j+1]-1]-x[cnt[j]],x[cnt[j+1]]-x[cnt[j]+1])
        print(answer)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=x[len(x)-1]-x[0]
cnt=[0]
d=0
d1=0
mini=100000000
if m<=n:
    print(0)
elif n==1:
    print(a)
else:
    if n==2:
        for i in range(m-1):
            d=abs(x[i]-x[0])
            d1=abs(x[len(x)-1]-x[i+1])
            if d+d1<mini:
                mini=d+d1
        print(mini)
    else:
        ans=sum(x)/n
        for i in range(n-1):
            cnt.append(bisect.bisect_left(x[i:],ans))
            ans-=x[i]*(n-i)/(n-i-1)
        #print(cnt)
        a=sum(x)
        answer=0
        for j in range(1,n-1):
            answer+=x[cnt[j+1]-1]-x[cnt[j]]
        print(answer)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
list1 = list(map(int,input().split()))
list1.sort()
list2 = []
for i in range(m-1):
	list2.append(list1[i+1]-list1[i])
list2.sort(reverse = True)
cut = 0
for j in range(n-1):
	cut+= list2[j]
print(max(list1)-min(list1)-cut)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ans = 0
if n < m:
    xli.sort()
    wli = list()
    for i in range(len(xli)-1):
        wli.append(xli[i+1]-xli[i])
    sortWli = list(reversed(sorted(wli)))
    su = 0
    for i in range(n-1):
        su += sortWli[i]
    ans = xli[-1] - xli[0] - su
print(ans

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

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

l = sorted(cost)
ind = [0]
for i in range(n-1):
    ind.append(cost.index(l[i]) + 1)
ind.append(m)
ind.sort()

X = []
for i in range(n):
    X.append(x[ind[i]:ind[i+1]])

ans = 0
for i in X:
    ans += i[-1] - i[0]
print(ans)

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

l = sorted(cost)
ind = [0]
for i in range(n-1):
    ind.append(cost.index(l[i]) + 1)
ind.append(m)

X = []
for i in range(n):
    X.append(x[ind[i]:ind[i+1]])

ans = 0    
for i in X:
    ans += i[-1] - i[0]

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

    # 目的地よりコマが多いなら操作は不要
    if N >= M:
        print(0)
        return

    Xs.sort()
    distance_list = []
    for i in range(M):
        if i == M-1:
            break
        distance = abs(Xs[i] - Xs[i+1])
        distance_list.append(distance)
    distance_list.sort(reverse=True)

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

    print(sum(distance_list))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
for i in range(n-1):
  nums3.remove(max(nums3))
print(sum(nums3))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
def main():
    n, m, *x = map(int, open(0).read().split())
    x.sort()

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

    piece = [0]*(n-1)
    itvl = [[x[i+1]-x[i], i]for i in range(m-1)]
    j = 0
    for i in sorted(itvl, key=lambda x:-x[0]):
        if piece[-1]: break
        piece[j] = i[1]
        j += 1

    ans = x[-1]-x[0]
    for i in piece: ans -= itvl[i][0]
    print(ans)

if __name__=="__main__":main()

Submission status: Runtime Error

Solution code:
# coding: utf-8
N,M = map(int, input().split())
X = list(map(int, input().split()))

if(M==1):
  print(0)
elif(N==1 and M!=1):
  print(X[-1]-X[0])
else:
  X.sort()
  max=0
  _sum=0
  diff_list = []
  for i in range(M-1):
    diff=X[i+1]-X[i]
    diff_list.append(diff)
    _sum+=diff
  diff_list.sort(reverse=True)
  diff_max = []
  for i in range(N-1):
    diff_max.append(diff_list[i])
  diff_sum = sum(diff_max)
  print(_sum-diff_sum)

Submission status: Runtime Error

Solution code:
# coding: utf-8
N,M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
if(M==1):
  print(0)
elif(N==1):
  print(X[-1]-X[0])
else:
  max=0
  _sum=0
  diff_list = []
  for i in range(M-1):
    diff=X[i+1]-X[i]
    diff_list.append(diff)
    _sum+=diff
  diff_list.sort(reverse=True)
  diff_max = []
  for i in range(N-1):
    diff_max.append(diff_list[i])
  diff_sum = sum(diff_max)
  print(_sum-diff_sum)

Submission status: Runtime Error

Solution code:
# coding: utf-8
N,M = map(int, input().split())
X = list(map(int, input().split()))

if(M==1):
  print(0)
else:
  X.sort()
  max=0
  _sum=0
  diff_list = []
  for i in range(M-1):
    diff=X[i+1]-X[i]
    diff_list.append(diff)
    _sum+=diff
  diff_list.sort(reverse=True)
  diff_max = []
  for i in range(N-1):
    diff_max.append(diff_list[i])
  diff_sum = sum(diff_max)
  print(_sum-diff_sum)

Submission status: Runtime Error

Solution code:
n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if m == 1: print(0)
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dis.sort()
    ans = dis[::-1]
    for _ in range(n-1): ans.pop(0)
    print(sum(ans))

Submission status: Runtime Error

Solution code:
n, m = (int(x) for x in input().split())
if m == 1: print(0)
else:
    x = [int(x) for x in input().split()]
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dis.sort()
    ans = dis[::-1]
    for _ in range(n-1): ans.pop(0)
    print(sum(ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

X.sort()
Y = []

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

for i in range(N-1):
    idx = Y.index(max(Y))
    Y[idx] = 0
    
print(sum(Y))

Submission status: Runtime Error

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

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

distance = []

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

distance.sort()

for i in range(n - 1):
    distance.pop()

print(sum(distance))

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()
  diff=[[0,0]]*(M-1)
  for i in range(M-1):
    diff[i]=[X[i+1]-X[i],i]
  diff.sort(key=lambda x:x[0])
  if N == 1:
    print(X[-1]-X[0])
    
  else:
    while N>0:
      if diff[-1][1]==0 or diff[-1][1]==M-2:
        N-=1
        diff.pop()
      elif N > 1:
        N-=2
        diff.pop()
      else:
        N-=1
  ans=0
  for x in diff:
    ans+=x[0]
print(ans)

Submission status: Runtime Error

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

if M == 1:
  print (0)
  sys.exit()
Y = []
for i in range(M-1):
  Y.append(X[i+1]-X[i])
Y = sorted(Y)
for i in range(N-1):
  Y.pop()
print (sum(Y))

Submission status: Runtime Error

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

Submission status: Runtime Error

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(len(x)-1)]

if len(diff) != 0:
    pos_edge = []

    for i in range(n-1):
        pos_edge.append(diff.index(max(diff)))
        diff[diff.index(max(diff))] = 0

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

    intervals.append(x[m-1] - x[pos_edge[len(pos_edge)-1]+1])
else:
    intervals = []

print(sum(intervals))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if n >= m:
        print(0)
    else:
        x.sort()
        y = []
        for i, v in enumerate(x):
            if i != 0:
                y.append([abs(v - x[i-1]), i, i-1])
        y = sorted(y, key=lambda x: x[0], reverse=True)
        cnt = 0
        z = [0, m-1]
        for i in range(n-1):
            z.append(y[i][1])
            z.append(y[i][2])
        z.sort()
        for i in range(1, m, 2):
            cnt += abs((x[z[i]] - x[z[i-1]]))
        print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Runtime Error

Solution code:
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print(0)
    elif N < M: 
        X.sort()
        diff = [0 for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = [X[i+1] - X[i]]
        diff.sort(reverse=True)

        ans = X[-1] - X[0]
        for i in range(N-1):
            ans -= diff[i]
        print(ans)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

X=list(map(int,input().split()))
X.sort()
di=[]


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

Submission status: Runtime Error

Solution code:
N, M = map(int,input().split())
if M == 1:
  print(0)
  exit()
X = list(map(int,input().split()))
X.sort()
dif = {}
for i in range(M-1):
  dif[i] = X[i+1]-X[i]
  
sorted_dif = sorted(dif.items(), key=lambda x:x[1], reverse=True)
difs = 0
for i in range(N-1):
  difs += sorted_dif[i][1]

print(X[-1]-X[0]-difs)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#xi以外の場所にコマを置く理由がない
#コマを置くというのは担当区間の分割
#xi x(i + 1)の間隔を無視するということ
#距離がでかいところから分割してやれば良い
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
l = [X[i + 1] - X[i] for i in range(M - 1)]
l.sort()
for _ in range(N - 1):
  l.pop()
print(sum(l))

Submission status: Runtime Error

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

def main():
    N,_ = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    D = []
    for i in range(len(X)-1):
        D.append([i+1] - X[i])
    D.sort(reverse=True)
    D = D[math.ceil(N/2):]
    n = 0
    for d in D:
        n += d[1]
    print(n)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())

c = list(map(int,input().split()))
c.sort()

d = []
for i in range(b-1):
    d.append(c[i+1] - c[i])

d.sort()

#print(c,d)

for k in range(a - 1):
    d.pop()

print(sum(d))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
big = 10**5 + 1
small = -1 * big

def streamline(n,m,x):
    if n >= m:
        return 0

    _x = sorted(x)
    diff = [[big,0] for _ in range(m-1)]

    for i in range(m-1):
        diff[i][0] = _x[i+1] - _x[i]
        diff[i][1] = i

    _diff = sorted(diff,reverse=True)

    start = 0
    end = 0
    count = 0

    for i in range(n-1):
        end = _diff[i][1] + 1
        count += abs(max(_x[start:end]) - min(_x[start:end]))
        start = end
    else:
        count += abs(max(_x[end:]) - min(_x[end:]))

    return count
if __name__ == '__main__':
    n,m = map(int,raw_input().split())
    x = list(map(int,raw_input().split()))

    print streamline(n,m,x)