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:
import sys
N,M = map(int,input().split())
if M<=N:
  print(0)
  sys.exit()
Xs = sorted(list(map(int,input().split())))
ranges = sum(sorted([abs(Xs[i]-Xs[i-1]) for i in range(1,len(Xs))])[:-N+1])
print(ranges)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
Xs = sorted(list(map(int,input().split())))
ranges = sum(sorted([abs(Xs[i]-Xs[i-1]) for i in range(1,len(Xs))])[:-N+1])
print(ranges)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = [int(v) for v in input().split()]
if N >= M:
    print(0)
else:
    Xs = list(sorted(X))
    Xd = [Xs[i + 1] - Xs[i] for i in range(M - 1)]
    Xds = list(sorted(Xd))
    Xdsl = Xds[:-(M-N - 2)]
    print(sum(Xdsl))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()
if m!=1:
  
  #d=np.array([L[i+1]-L[i] for i in range(m-1)])
  d=[L[i+1]-L[i] for i in range(m-1)]
  ans=L[m-1]-L[0]

  for i in range(n-1):
    #a=np.argmax(d)
    a=d.index(max(d))
    d[a]=0
    ans-=(L[a+1]-L[a])
    print(ans)
  

else:
  print(0)

Submission status: Wrong Answer

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

if N>=M:
	print(0)
else:
	if N%2==0:
		X.sort()
		X = X[int(N//2)-1 : M-int(N//2)+1]
		d_max = 0
		for i in range(0, len(X)-1):
			d_max = max(d_max, X[i+1]-X[i])
		print(X[-1] - X[0] - d_max)

	else:
		X.sort()
		X = X[int(N//2) : M-int(N//2)+1]
		d_max = 0
		for i in range(0, len(X)-1):
			d_max = max(d_max, X[i+1]-X[i])
		print(X[-1] - X[0] - d_max)

Submission status: Wrong Answer

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

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

x = sorted(x)

dist = [0]*(m-1)
for i in range(m-1):
    dist[i] = x[i+1] - x[i] - 1

dist = sorted(dist, reverse=True)

print(x[m-1] - x[0] - sum(dist[0:n]))

Submission status: Wrong Answer

Solution code:
def solve():
    n, m = map(int, input().split())
    if n >= m:
        return 0
    x = sorted(list(map(int, input().split())))
    diff = sorted([x[i + 1] - x[i] for i in range(len(x) - 1)])
    return sum(diff[:-(n - 1)])


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    diff = sorted([x[i + 1] - x[i] for i in range(len(x) - 1)])
    return sum(diff[:-(n - 1)])


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

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

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

X.sort()

buff = []

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


buff.sort()
buff = buff[::-1]

for i in range(min(N-1, len(buff))):
    print(buff.pop(0))

print(sum(buff))

Submission status: Wrong Answer

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

if N >= M :
	print(0)
	exit()
	
darr = [Xs[i+1] - Xs[i]for i in range(M-1)]

print(darr)

# ??
for i in range(N-1):
	darr.pop(darr.index(max(darr)))
	
print(sum(darr))

Submission status: Wrong Answer

Solution code:
import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

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

#x.sort()
list.sort(x)
#print(x)

for i in range(1, m):
    a.append(x[i] - x[i-1])

list.sort(a, reverse=True)
#print(a)

count = max(x) - min(x)
for i in range(n-1):
    if len(a) == 0:
        break
    a.pop()
#    count -= a[i]

#print(count)
print(sum(a))

Submission status: Wrong Answer

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

if N >= M:
  print(0)
elif N == 1:
  ans = max(X) - min(X)
  print(ans)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Wrong Answer

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

X.sort()

diff_list = sorted([(i, X[i + 1] - X[i]) for i in range(len(X) - 1)],
                   key=lambda x: -x[1])

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

divide_pos_list = diff_list[:(N - 1)]

op_count = 0
start_pos = 0
for divide_pos in divide_pos_list:
    op_count += X[divide_pos[0]] - X[start_pos]
    start_pos = divide_pos[0] + 1

if N > 1:
    op_count += X[-1] - X[start_pos]

print(op_count)

Submission status: Wrong Answer

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

X.sort()

diff_list = sorted([(i, X[i + 1] - X[i]) for i in range(len(X) - 1)],
                   key=lambda x: (-x[1], x[0]))

divide_pos_list = diff_list[:(N - 1)]

op_count = 0
start_pos = 0
for divide_pos in divide_pos_list:
    op_count += X[divide_pos[0]] - X[start_pos]
    start_pos = divide_pos[0] + 1

if N > 1:
    op_count += X[-1] - X[start_pos]

print(op_count)

Submission status: Wrong Answer

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

X.sort()

diff_list = sorted([(i, X[i + 1] - X[i]) for i in range(len(X) - 1)],
                   key=lambda x: -x[1])

divide_pos_list = diff_list[:(N - 1)]

op_count = 0
start_pos = 0
for divide_pos in divide_pos_list:
    op_count += X[divide_pos[0]] - X[start_pos]
    start_pos = divide_pos[0] + 1

if N > 1:
    op_count += X[-1] - X[start_pos]

print(op_count)

Submission status: Wrong Answer

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

X.sort()

diff_list = sorted([(i, X[i + 1] - X[i]) for i in range(len(X) - 1)],
                   key=lambda x: -x[1])

divide_pos_list = diff_list[:(N - 1)]

op_count = 0
start_pos = 0
for divide_pos in divide_pos_list:
    op_count += X[divide_pos[0]] - X[start_pos]
    start_pos = divide_pos[0] + 1

print(op_count)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
Distance = [0] * (M-1)

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

Distance.sort()
print(sum(Distance[0:M-1-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(sorted([x[i] - x[i-1] for i in range(1, m)]))
diff = sum(sorted([abs(x[i] - x[i-1]) for i in range(1, m)])[:-n+1])
print(diff)

Submission status: Wrong Answer

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

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

diff = sum(sorted([x[i] - x[i-1] for i in range(1, m)])[:-n+1])
print(diff)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def iread():
    return int(input())


def sread():
    return input()


def aread_int():
    tmp = input().split()
    ret = [int(i) for i in tmp]
    return ret


def aread_str():
    return input().split()

if __name__ == '__main__':
    n, m = map(int, input().split())
    
    x = aread_int()
    x = sorted(x)
    x_dist = [0] * (m - 1)
    for i in range(m - 1) :
        x_dist[i] += abs(x[i] - x[i + 1])
    
    if n > m :
        print(0)
        exit()
    
    x_dist = sorted(x_dist)
    print(x_dist)
    idx = len(x_dist) - 1
    cost = sum(x_dist)
    for i in range(n - 1) :
        cost -= x_dist[idx]
        x_dist[idx] = 0
        idx -= 1

    print(cost)

Submission status: Wrong Answer

Solution code:
def iread():
    return int(input())


def sread():
    return input()


def aread_int():
    tmp = input().split()
    ret = [int(i) for i in tmp]
    return ret


def aread_str():
    return input().split()

if __name__ == '__main__':
    n, m = map(int, input().split())
    
    x = aread_int()
    x = sorted(x)
    x_dist = [0] * (m - 1)
    for i in range(m - 1) :
        x_dist[i] += abs(x[i] - x[i + 1])
    
    if n > m :
        print(0)
        exit()
    
    x_dist = sorted(x_dist)
    cost = sum(x_dist)
    for i in range(n - 1) :
        idx = len(x_dist) - 1
        cost -= x_dist[idx]
        x_dist[idx] = 0

    print(cost)

Submission status: Wrong Answer

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

steps = []
for i in range(M - 1):
    steps.append(abs(x[i] - x[i + 1]))
steps.sort()

if N >= M:
    res = 0
else:
    res = sum(steps[0:- N + 1])
print(res)

Submission status: Wrong Answer

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

steps = []
for i in range(M - 1):
    steps.append(abs(x[i] - x[i + 1]))
steps.sort()

res = sum(steps[0:- N + 1])
print(res)

Submission status: Wrong Answer

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

diff = [(i, b-a) for i, (a, b) in enumerate(zip(xs, xs[1:]))]
diff.sort(key=lambda x: x[1])

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

steps = 0
for i_start, i_end in zip(start_indexes, start_indexes[1:] + [m]):
    steps += xs[i_end - 1] - xs[i_start]
print(steps)

Submission status: Wrong Answer

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

if N >= M:
    print(0)
else:
    x_list.sort()

    diff_list = []
    for i in range(M-1):
        diff_list.append(x_list[i+1]-x_list[i])

    diff_list.sort()

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

Submission status: Wrong Answer

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

x_list.sort()

diff_list = []
for i in range(M-1):
    diff_list.append(x_list[i+1]-x_list[i])

diff_list.sort()

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

Submission status: Wrong Answer

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

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

else:
  for i in range(1,m):
    dst.append(lst[i]-lst[i-1])
  dst.sort()
  ans = sum(dst[:-n+1])
  print(ans)

Submission status: Wrong Answer

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

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

for i in range(1,m):
    dst.append(lst[i]-lst[i-1])
dst.sort()
ans = sum(dst[:-n+1])
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
nums = list(map(int, input().split()))
nums.sort()
diffNums = [abs(nums[i+1] - nums[i]) for i in range(M-1)]
diffNums.sort()
print(nums[-1] - nums[0] - sum(diffNums[-(N-1):]))

Submission status: Wrong Answer

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

if N >= M:
    print(0)
else:
    diffNums = [abs(nums[i+1] - nums[i]) for i in range(M-1)]
    diffNums.sort()
    print(sum(diffNums[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
nums = list(map(int, input().split()))
nums.sort()
diffNums = [abs(nums[i+1] - nums[i]) for i in range(M-1)]
diffNums.sort()
print(sum(diffNums[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
nums = list(map(int, input().split()))
nums.sort()
diffNums = [nums[i+1] - nums[i] for i in range(M-1)]
diffNums.sort()
print(sum(diffNums[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
N = int(input().split()[0])
X=sorted([int(i) for i in input().split()])

Y=list()
before=X[0]
for i in X:
  diff = i - before
  if diff == 0:
    continue
  Y.append(diff)
  before = i

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

Submission status: Wrong Answer

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

if n>=m:
    print(1)
    exit()
else:
    p=[0]+list(map(int,input().split()))
    ps=sorted(p)

dis=[]
for i in range(m):
    dis.append(ps[i+1]-ps[i])
    dis2=sorted(dis)
    dis3=dis2[:m-n+1]
if 0 in dis3:
    print(sum(dis3))
else:
    print(sum(dis3)-1)

Submission status: Wrong Answer

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

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

diff = sorted([x[i + 1] - x[i] for i in range(m-1)])
print(sum(diff[:-n + 1]))

Submission status: Wrong Answer

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

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

    covers = diff[:-n + 1]
    print(sum([x[1] for x in covers]))

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


def main():
	n,m = map(int,input().split())
	x = list(map(int,input().split()))
	if(n>=m):
		print('0')
		exit()
	x.sort()
	ans = x[-1] - x[0] 
	y = []
	for i in range(m-1):
		y.append(x[i+1]-x[i])
	y.sort()
	for i in range(n):
		ans -= y[-i]
	print(ans+n-1)

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Wrong Answer

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

if n >= m:
    print(0)

dif = sorted([x[i+1] - x[i] for i in range(m-1)])

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

Submission status: Wrong Answer

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

dif = sorted([x[i+1] - x[i] for i in range(m-1)])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# ABC 117 C

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

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

diff = [0] * (M-1)

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

diff.sort()
idx = len(diff) - N + 1
print(sum(diff[:idx]))

Submission status: Wrong Answer

Solution code:
# ABC 117 C

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

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

diff = [0] * (M-1)

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

diff.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

li.sort()
move_cost = [li[i+1] - li[i] for i in range(m-1)]
move_cost.sort()
if n >= m:
    print(0)
else:
    print(sum(move_cost[:-n+1]))

Submission status: Wrong Answer

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

li.sort()
move_cost = [li[i+1] - li[i] for i in range(m-1)]
move_cost.sort()
if n > m:
    print(0)
else:
    print(sum(move_cost[:-n+1]))

Submission status: Wrong Answer

Solution code:

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

li.sort()
move_cost = [li[i+1] - li[i] for i in range(m-1)]
move_cost.sort()
if n >= m:
    print(0)
else:
    print(move_cost[:-n+1])
    print(sum(move_cost[:-n+1]))

Submission status: Wrong Answer

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

diff = []
for i in range(M-1):
    diff.append(X[i+1]-X[i])
print(sum(sorted(diff)[:min(0,M-N)]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
xm = [int(i) for i in input().split()]
xm.sort()
dist = [0]*(m-1)

for i in range(m-1):
  dist[i] = abs(xm[i]-xm[i+1])

dist.sort()
print(sum(dist[:m-n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = sorted(x)
diff = [x[i+1]-x[i] for i in range(m-1)]
diff = sorted(diff)
print(sum(diff[:len(diff)-(n-1)]))

Submission status: Wrong Answer

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

x = sorted(x)
diff = [x[i+1]-x[i] for i in range(m-1)]

print(sum(sorted(diff)[:-(n-1)]))

Submission status: Wrong Answer

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

Y = sorted(X)
ans = sum(sorted(a - b for a, b in zip(Y[1:], Y))[:-(N - 1)])

print(ans)

Submission status: Wrong Answer

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

X =  sorted(X)
d = []

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

d = sorted(d)
del d[len(d)-N+1:len(d)]
S = sum(d)
print(S)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

li.sort()

sa = [li[i+1] - li[i] for i in range(len(li)-1)]

sa.sort()

print(sum(sa[:-n+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


b = sum(li[:(n-1)])
d = abs(max(w)) + abs(min(w))

print(d-b)

Submission status: Wrong Answer

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

if n%2 == 0:
    c = n
elif n%2 == 1:
    c = n-1
b = sum(li[:c])
d = abs(max(w)) + abs(min(w))

print(d-b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC117 C

n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
#print(x)
if n >= m:
    print(0)
    quit()
y = [x[i+1]-x[i] for i in range(m-1)]
y.sort()
#print(y)
print(sum(y[:-n+1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC117 C

n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
#print(x)
if n >= m:
    print(0)
    quit()
y = [x[i+1]-x[i] for i in range(m-1)]
y.sort()
print(sum(y[:n+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

diff = sorted([X[i] - X[i - 1] for i in range(1, M)])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = sorted(X)
diff = []
for n in range(1, M):
	diff.append(x[n] - x[n - 1])
diff.sort()

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

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
if a >= b:
    print(0)
else:
    c = list(map(int, input().split()))
    c.sort()
    d = []
    for i in range(1, b):
        d.append([abs(c[i] - c[i-1]), i])
    d = sorted(d, key=lambda x: x[0], reverse=True)
    e = []
    for i in range(a-1):
        e.append(d[i])
    e.sort()
    f = []
    print(c)
    print(d)
    print(e)
    for i in range(len(e)):
        print(c[e[i][1]])
        f.append(c[e[i][1]])
        f.append(c[e[i][1]-1])

    f.append(c[0])
    f.append(c[len(c)-1])
    f.sort()
    print("-------------")
    g = 0
    for i in range(1, len(f), 2):
        g += f[i] - f[i-1]
    print(g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
if m <= n:
    print(0)
    quit()

x.sort()
dist_l = [0] * (m-1)

for i in range(m-1):
    dist_l[i] = x[i+1] - x[i]

dist_l.sort()
print(sum(dist_l[:-(n-1)]))

Submission status: Wrong Answer

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

for i in range(m-1):
    dist_l[i] = x[i+1] - x[i]

dist_l.sort()
print(sum(dist_l[:-(n-1)]))

Submission status: Wrong Answer

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

# X = []
# for n in range(N):
#     X[n] = input()

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

X.sort()
max = 0
L = []
for i in range(1, len(X)):
    L.append(X[i] - X[i-1])

L.sort(reverse=True)
print(sum(L[:N-1]))

Submission status: Wrong Answer

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

if N >= M:
    print(0)
else:
    print(sum(X_interval[:-(N - 1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = []
for i in range(M-1):
    d.append(abs(l[i+1]-l[i]))
d = list(sorted(d))
print(d)
while N != 1 and len(d) !=0:
    del d[len(d)-1]
    N -= 1
sum = 0
if len(d) >= 1:
    for i in range(len(d)):
        sum += d[i]
print(sum)

Submission status: Wrong Answer

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

d = []
for i in range(M-1):
    d.append(abs(l[i+1]-l[i]))
d = list(sorted(d))
while N != 1 and len(d) !=0:
    del d[len(d)-1]
    N -= 1
sum = 0
if len(d) != 1:
    for i in range(len(d)):
        sum += d[i]
print(sum)

Submission status: Wrong Answer

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

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

l = list(sorted(l))

d = []
for i in range(M-1):
    d.append(abs(l[i+1]-l[i]))

while N != 1 and len(d) !=0:
    ind = d.index(max(d))
    del d[ind]
    N -= 1
sum = 0
if len(d) != 1:
    for i in range(len(d)):
        sum += d[i]
print(sum)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
x_dif=[b-a for a,b in zip(x[:-1],x[1:])]
x_dif.sort()
print(sum(x_dif)-sum(x_dif[-(n-1):]))