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: Wrong Answer

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

if n < m:
    x_distance = [x[i + 1] - x[i] for i in range(m - 1)]
    x_distance.sort()
    print(x_distance)
    ans = 0
    for i in range (m - n):
        ans += x_distance[i]
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
a=list(map(int, input().split()))
a.sort(reverse=True)
print(sum(a[m-1:]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import IPython

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, M = ds_in()
X_list = ds_in()
if N >= M:
    print(0)

X_list.sort()
diff = [X_list[i+1] - X_list[i] for i in range(M-1)]

diff.sort()
print(sum(diff[:M-N]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import IPython

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, M = ds_in()
X_list = ds_in()

X_list.sort()
diff = [X_list[i+1] - X_list[i] for i in range(M-1)]

diff.sort()
print(sum(diff[:M-N]))

Submission status: Wrong Answer

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

# print(n, m, x)

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


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
if n-1>0:
    print(sum(diff_list[:-(n-1)]))
else:
    print(0)    

#%%


#%%

Submission status: Wrong Answer

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

# print(n, m, x)

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


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
print(sum(diff_list[:-(max(0,n-1))]))

#%%


#%%

Submission status: Wrong Answer

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

# print(n, m, x)

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


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
print(diff_list[:-(max(0,n-1))])

#%%


#%%

Submission status: Wrong Answer

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

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, M = ds_in()
X_list = ds_in()

X_list.sort()
diff = [X_list[i+1] - X_list[i] for i in range(M-1)]

diff.sort()
tot = sum(diff)

print(tot - sum(diff[M-N:]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = [int(x) for x in input().split()]
X.sort()
if N >= M: print(0)
else:
    Xsub = [(X[i+1] - X[i], i) for i in range(M-1)]
    Xsub.append((X[M-1] - X[M-2], M-1))
    Xsub.sort(reverse = True)
    Initial = [False] * M
    for i in range(N):
        Initial[Xsub[i][1]] = True
    Move = 0
    temp = (X[0] if not Initial[0] else 10**6) 
    for i in range(1, M):
        if Initial[i] == False and temp == 10**6:
            temp = X[i]
        elif Initial[i] and temp < 10**6:
            Move += X[i] - temp
            temp = 10**6
    print(Move)

Submission status: Wrong Answer

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

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

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

d = sorted(d)
print(sum(d[0:-(n-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

d = sorted(d)
print(sum(d[0:-(n-1)]))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(' '))
arr  = sorted(list(map(int, input().split(' '))))
diff = []
for idx in range(1, m):
    diff.append(arr[idx] - arr[idx-1])
diff = sorted(diff)
if n >= m:
    ans = 0
else:
    n -= 1
    ans = sum(diff[:-n])
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(' '))
arr  = list(map(int, input().split(' ')))
arr = sorted(arr)
diff = []
for idx in range(1, m):
    diff.append(arr[idx] - arr[idx-1])
diff = sorted(diff)
# print(diff)
if n >= m:
    ans = 0
else:
    ans = sum(diff[:-n + 1])
print(ans)

Submission status: Wrong Answer

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(M <= N):
  print(sum(tmp[:M-N]))
else:
  print(0)

Submission status: Wrong Answer

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(tmp != []):
  print(sum(tmp[:M-N]))
else:
  print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

dx = [ x[i+1] - x[i] for i in range(0, len(x)-1) ]
if n >= m:
    ans = 0
else:
    ans = sum(sorted(dx)[:-n+1])
print(ans)

Submission status: Wrong Answer

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

x_sort = sorted(x_arr)

if n >= m or m==1:
    print(0)
else:
    x_sub = [x_sort[i+1]-x_sort[i] for i in range(m-1)]
    x_sub = sorted(x_sub)
    print(sum(x_sub[:m-(n-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

dx = [ x[i+1] - x[i] for i in range(0, len(x)-1) ]
ans = sum(sorted(dx)[:-n+1])
print(ans)

Submission status: Wrong Answer

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

x_sort = sorted(x_arr)

if n >= m or m==1:
    print(0)
else:
    x_sub = [x_sort[i+1]-x_sort[i] for i in range(m-1)]
    x_sub = sorted(x_sub)
    
    print(sum(x_sub[:-(n-1)]))

Submission status: Wrong Answer

Solution code:
import numpy as np
 
N, M = map(int,input().split())
 
if N >= M:
    print(0)
else:
    X_m = np.array(sorted(list(map(lambda x: int(x), input().split()))))
    X_m_sub = sorted(np.array(X_m[1:]-X_m[:-1]))
    print(sum(X_m_sub[0:-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
 
N, M = map(int,input().split())
 
if N >= M:
    print(0)
else:
    X_m = np.array(sorted(list(map(lambda x: int(x), input().split()))))
    X_m_sub = np.array(X_m[1:]-X_m[:-1])
    pos_option = X_m_sub.argsort()
    
    print(pos_option[0:-(N-1)].sum())

Submission status: Wrong Answer

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

list_X.sort()
list_distance = [abs(list_X[idx]-list_X[idx+1]) for idx in range(M-1)]

print(sum(sorted(list_distance)[0:M-N]))

Submission status: Wrong Answer

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

x = map(int, input().split(' '))

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

x_list = sorted(x)

distance_list = []

for i in range(0, M-1):
    distance = x_list[i+1] - x_list[i]
    distance_list.append(distance)

result = sum(sorted(distance_list)[N::-1])
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

list_X.sort()
list_distance = [abs(list_X[idx]-list_X[idx+1]) for idx in range(M-1)]

print(sum(sorted(list_distance)[0:-(N-1)]))

Submission status: Wrong Answer

Solution code:
input1 = input().split()
N = int(input1[0])
M = int(input1[1])
zahyo = list(map(int,input().split()))

if M == 1:
    print(0)
else:
    zahyo.sort()

    sa = [0]*(M-1)
    for i in range(M-1):
        sa[i] = zahyo[i+1] - zahyo[i]

    sa.sort()
    print(sum(sa[:M-N]))

Submission status: Wrong Answer

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

list_X.sort()
list_distance = [abs(list_X[idx]-list_X[idx+1]) for idx in range(M-1)]
print(list_distance)

print(sum(sorted(list_distance)[0:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import random

input1 = input().split()
N = int(input1[0])
M = int(input1[1])

if M == 1:
    print(0)
else:
    zahyo = list(map(int,input().split()))
    zahyo.sort()

    sa = [0]*(M-1)
    for i in range(M-1):
        sa[i] = zahyo[i+1] - zahyo[i]

    sa.sort()
    print(sum(sa[:M-N]))

Submission status: Wrong Answer

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0
r = range(len(Xs))

mins = [min([abs(Xs[i]-Xs[j]) for j in r if i<j]) for i in r[:-2]]

mins.sort()
count = sum(mins[:M-N])

if M-N > 0:
  print(count)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0
r = range(len(Xs))

mins = [min([abs(Xs[i]-Xs[j]) for j in r if i<j]) for i in r[:-2]]

mins.sort()
print(mins)
print(mins[:M-N])
count = sum(mins[:M-N])

if M-N > 0:
  print(count)
else:
  print(0)

Submission status: Wrong Answer

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

print(X)
X.sort()
print(X)

L = []
for i in range(m-1):
  L.append(X[i+1] - X[i])
print(L)
L.sort(reverse=True)
print(L)
print((X[m-1]-X[0]) - (sum(L[0:n-1])))

Submission status: Wrong Answer

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

x = map(int, input().split(' '))

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

x_list = sorted(x)

distance_list = []

for i in range(1, M):
    distance = x_list[i-1] - x_list[i]
    distance_list.append(distance)

result = abs(sum(sorted(distance_list)[::-1][:N+1]))
print(result)

Submission status: Wrong Answer

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

x = map(int, input().split(' '))

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

x_list = sorted(x)

distance_list = []

for i in range(1, M):
    distance = x_list[i-1] - x_list[i]
    distance_list.append(distance)

result = abs(sum(sorted(distance_list)[::-1][:N+1]))
print(result)

Submission status: Wrong Answer

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0

maxs = [min([abs(x-y) for y in Xs]) for x in Xs]

maxs.sort()
count = sum(maxs[:M-N])
 
print(count)

Submission status: Wrong Answer

Solution code:
fl = input()
sl = input()

koma_num, pos_num = [int (x) for x in fl.split(" ")]
poses = [int(x) for x in sl.split(" ")]
poses.sort()

if koma_num >= pos_num:
    print("0")
    exit()

dists = []
total = 0
for n in range(pos_num-1):
    dist = poses[n+1] - poses[n]
    dists.append(dist)
    total += dist

dists.sort()
dists.reverse()
if(len(dists) == 0):
    print("0")
    exit()

for n in range(koma_num-1):
    print(n)
    total -= dists[n]

print(total)

Submission status: Wrong Answer

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0

maxs = [max([abs(x-y) for y in Xs]) for x in Xs]

maxs.sort()
count = sum(maxs[:M-N])
 
print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

x = map(int, input().split(' '))
x_list = sorted(x)

distance_list = []

for i in range(1, M):
    distance = x_list[i-1] - x_list[i]
    distance_list.append(distance)

result = abs(sum(sorted(distance_list)[::-1][:N+1]))
print(result)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split(' '))
x = map(int, input().split(' '))
x_list = sorted(x)

distance_list = []

for i in range(1, M):
    distance = x_list[i-1] - x_list[i]
    distance_list.append(distance)

result = abs(sum(sorted(distance_list)[::-1][:N+1]))
print(result)

Submission status: Wrong Answer

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

X.sort()

Y = X[::-1]
Y = [-1 * y for y in Y]

P = [X[i+1] - X[i] for i in range(M-1)]
Q = [Y[i+1] - Y[i] for i in range(M-1)]
P.sort()
Q.sort()

print(min(sum(P[:(-1 * (N-1))]), sum(Q[:(-1 * (N-1))])))

Submission status: Wrong Answer

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

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

L = [X[i+1] - X[i] for i in range(M-1)]
L = sorted(L, reverse=True)
L_sum = sum(L)
ans = L_sum
for i in range(N-1):
  ans -= L[i]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = list(map(int, 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))
ys.sort()
 
if len(ys) >= n:
    out = sum(ys[0:-(n-1)])
else:
    out = 0
print(out)

Submission status: Wrong Answer

Solution code:
import sys

def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  ans = 0
  if N < M:
    g = [int(a) for a in input().split()]
    g = sorted(g)
    L = g[:-1]
    R = g[1:]
    sub = [b - a for a, b in zip(L, R)]
    sub = sorted(sub)
    ans = sum(sub[:-(N-1)])
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = list(map(int, 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))
ys.sort()
 
if len(ys) > n:
    out = sum(ys[0:-(n-1)])
else:
    out = 0
print(out)

Submission status: Wrong Answer

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

X.sort()

L = [X[i+1] - X[i] for i in range(M-1)]

L.sort()

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

Submission status: Wrong Answer

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

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

distance = [0]*(M-1)
distance[0] = x[1]-x[0]
for i in range(0,M-1):
	distance[i] = x[i+1]-x[i]

distance_sorted = sorted(distance)
print(distance_sorted)
ans = 0
for i in range(0,M-N):
	ans += distance_sorted[i]

print(ans)

Submission status: Wrong Answer

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

x_sort = sorted(x_arr)

if n >= m:
    print(0)
else:
    x_sub = [x_sort[i+1]-x_sort[i] for i in range(m-1)]
    x_sub = sorted(x_sub)
    
    print(sum(x_sub[:-(n-1)]))

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = map(int, input().split())
xs = list(map(int, input().split()))
sorted(xs)
 
ys = []
for i in range(m - 1):
    ys.append(xs[i+1] - xs[i])
sorted(ys)

if len(ys) >= n:
    out = sum(ys[0:-(n-1)])
else:
    out = 0
print(out)

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = map(int, input().split())
xs = list(map(int, input().split()))
sorted(xs)
 
ys = []
for i in range(m - 1):
    ys.append(xs[i+1] - xs[i])
sorted(ys)

out = sum(ys[0:-(n-1)])
print(out)

Submission status: Wrong Answer

Solution code:
this=input().split(" ")
piece=int(this[0])


#pieceがplace以上あれば0回になる

places=input().split(" ")

block=len(places)-piece
#print("block "+str(block))

if block<=0:
    print("clear "+str(0))
    
else:
    check=0
    p2=[]
    for i in places:
        p2.append(int(i))
    
    pp2=sorted(p2)
    #print("list "+str(pp2))
    suum=0
    bai=0
    miin=pp2[1]-pp2[0]
    #print("firstmin "+str(miin))
    minmin=0
    while check<block:
        
        for i in range(len(pp2)-1):
            
            sa=pp2[i+1]-pp2[i]
            
            if miin>sa and minmin<sa:
                miin=sa
                #print(miin)
                bai=1
                
                
            elif miin==sa and minmin<sa:
                bai+=1
                
                
            
            if check+bai>block:
                bai=block-check
            
            
                
        suum+=miin*bai
        check+=bai
        minmin=miin
        bai=1
        miin=pp2[1]-pp2[0]
        """
        print("roop")
        print("miin"+str(miin))
        print("minmin"+str(minmin))
        print("sum "+str(suum))
        """
        
    print(suum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:



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

# print(n, m, x)

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


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
print(x[-1] - x[0] - sum(diff_list[-(n-1):]))

#%%


#%%

Submission status: Wrong Answer

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

XS = sorted(X)
XD = []

if N > M:
    print(0)
for i in range(0, len(XS)-1):
    XD.append(XS[i + 1] - XS[i])

XD2 = sorted(XD)
print(sum(XD2[:M-N]))

Submission status: Wrong Answer

Solution code:
import sys

def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  g = [int(a) for a in input().split()]
  g = sorted(g)
  L = g[:-1]
  R = g[1:]
  sub = [b - a for a, b in zip(L, R)]
  sub = sorted(sub)
  ans = sum(sub[:-(N-1)])
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import numpy as np
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, M = LI()
    X = LI()
    #  print(N, M, X)

    X.sort()
    #  print(X)

    if N >= M:
        print(0)
        return
    if N == 1:
        # 端から端までの距離
        print(X[-1] - X[0])
        return

    N_position_list = []
    # 各xの差をとる
    X_diff = [X[i+1] - X[i] for i in range(len(X)-1)]
    X_diff_idx = np.argsort(X_diff)
    #  order_idx = np.argsort(X_diff)
    #  #  print(X_diff)
    #  # 最大値の両脇にコマを置く
    #  # コマが１つしかおけない場合、外側に置く
    count = 0
    torn_point = []
    
    #  while count < N - 1:
    #      torn_point.append(order_idx[-1])
    #      X_diff[order_idx[-1]] = -10**5-1
    #      order_idx = order_idx[:-1]
    #      count += 1

    while count < N -1:
        torn_point.append(X_diff_idx[-1 - count])
        count += 1


   
    #  print('torn_point', torn_point)
    # あとは移動距離の計算
    # listを分断していく
    torn_list = []
    for i in range(len(torn_point)):
        if i == 0:
            torn_list.append(X[0:torn_point[i]+1])
        else:
            torn_list.append(X[torn_point[i-1]+1:torn_point[i]+1])
    else:
        torn_list.append(X[torn_point[-1]+1:])

    #  print('torn_list', torn_list)

    move_count = 0
    for l in torn_list:
        if len(l) >= 2:
            # 端から端までを加算
            move_count += l[-1] - l[0]

    print(move_count)


main()

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
X=list(map(int,input().split()))
if(len(X)<=N):
  print(0)
else:
  #print(N,M,X)
  sort_X=sorted(X)
  #print(sort_X)
  #print(sort_X[1:],sort_X[0:len(sort_X)-1])
  X_sa=[x - y for (x, y) in zip(sort_X[1:], sort_X[0:len(X)-1])]
  #print(X_sa)
  sort_X_sa=sorted(X_sa)
  print(sort_X_sa)
  print(sort_X_sa[:M-N])
  print(sum(sort_X_sa[:M-N]))

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = input().split()
xs_str = input().split()
xs = list(map(int, xs_str))
sorted(xs)
 
ys = []
for i in range(int(m) - 1):
    ys.append(xs[i+1] - xs[i])
sorted(ys)

out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

arr.sort()

diff = [abs(b - a) for a, b in zip(arr, arr[1:])]

diff.sort()

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

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = input().split()
xs_str = input().split()
xs = list(map(int, xs_str))
sorted(xs)
 
ys = []
for i in range(int(m) - 1):
    ys.append(abs(xs[i+1] - xs[i]))
sorted(ys)
 
out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

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
kyori.sort()
mm=1
owari=n[1]-n[0]
kyori=kyori[0:owari]
idou=sum(kyori)
print(idou)

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = input().split()
xs_str = input().split()
xs = list(map(int, xs_str))
xs.sort()
 
ys = []
for i in range(int(m) - 1):
    ys.append(abs(xs[i+1] - xs[i]))
ys.sort()
 
out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import numpy as np
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, M = LI()
    X = LI()
    #  print(N, M, X)

    X.sort()
    #  print(X)

    if N >= M:
        print(0)
        return
    if N == 1:
        # 端から端までの距離
        print(X[-1] - X[0])
        return

    N_position_list = []
    # 各xの差をとる
    X_diff = [X[i+1] - X[i] for i in range(len(X)-1)]
    order_idx = np.argsort(X_diff)
    #  print(X_diff)
    # 最大値の両脇にコマを置く
    # コマが１つしかおけない場合、外側に置く
    count = 0
    torn_point = []
    
    while count < N - 1:
        torn_point.append(order_idx[-1])
        X_diff[order_idx[-1]] = -10**5-1
        order_idx = order_idx[:-1]
        count += 1

   
    #  print('torn_point', torn_point)
    # あとは移動距離の計算
    # listを分断していく
    torn_list = []
    for i in range(len(torn_point)):
        if i == 0:
            torn_list.append(X[0:torn_point[i]+1])
        else:
            torn_list.append(X[torn_point[i-1]+1:torn_point[i]+1])
    else:
        torn_list.append(X[torn_point[-1]+1:])

    #  print('torn_list', torn_list)

    move_count = 0
    for l in torn_list:
        if len(l) >= 2:
            # 端から端までを加算
            move_count += l[-1] - l[0]

    print(move_count)


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

arr.sort()

diff = [b - a for a, b in zip(arr, arr[1:])]

diff.sort()

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

Submission status: Wrong Answer

Solution code:
def psolve(k, Xs, N):
    arr = [[]]
    i = Xs[0]
    for x in Xs:
        if x <= i + k:
            arr[-1].append(x)
        else:
            arr.append([])
            arr[-1].append(x)
            i = x
            if len(arr) > N:
                break

    if len(arr) <= N:
        ans = sum([max(a) - min(a) for a in arr])
        return ans
    else:
        return 10 ** 9


def solve(N, M, Xs):
    Xs = sorted(Xs)
    ans = 10 ** 9
    for k in range(0, ((Xs[-1] - Xs[0]) + 1) // N + 1):
        nans = psolve(k, Xs, N)
        ans = min(ans, nans)
    return ans

if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    Xs = list(map(int, input().split(" ")))
    print(solve(N, M, Xs))