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:
# coding: utf-8
# Your code here!

import bisect
import random
import itertools
import copy

N, M = map(int, input().rstrip().split(' '))
A = sorted(list(map(int, input().rstrip().split(' '))))
#N = 5
#M = 10
#A = sorted(list(set([random.randint(0, 20) for _ in range(M)])))
#M = len(A)

#print(A)

if N >= M:
    print(0)
elif N == 1:
    print(A[-1] - A[0])
else:
    indices = [0, M]
    diff_min_key = [A[-1] - A[0]]
    diff_min_indices = [[0, M-1]]
    
    for _ in range(N-1):
        j = -1
        min_j = -1
        max_value = diff_min_key[-1]
        max_diff_min_index_range = None
        while True:
            
            diff_min_index_range = diff_min_indices[j]
    
            min_cost = 1e10
            min_i = -1
            for i in range(diff_min_index_range[0] + 1, diff_min_index_range[1]+1):
                cost = abs(A[i - 1] - A[diff_min_index_range[0]]) + abs(A[diff_min_index_range[1]] - A[i])
                if cost < min_cost:
                    min_cost = cost
                    min_i = i
                    min_j = j
                    max_diff_min_index_range = copy.copy(diff_min_index_range)
            
            j -= 1
            if -len(diff_min_key) >= j:
                break
            if diff_min_key[j] != max_value:
                break
            

        diff_min_key.pop(min_j)
        diff_min_indices.pop(min_j)
        diff_min_index_range = max_diff_min_index_range

        if abs(A[diff_min_index_range[1]] - A[min_i]) > 0:
            insert_index = bisect.bisect_left(diff_min_key, abs(A[diff_min_index_range[1]] - A[min_i]))
            diff_min_key.insert(insert_index, abs(A[diff_min_index_range[1]] - A[min_i]))
            diff_min_indices.insert(insert_index, [min_i, diff_min_index_range[1]])
        
        if abs(A[diff_min_index_range[0]] - A[min_i - 1]) > 0:
            insert_index = bisect.bisect_left(diff_min_key, abs(A[diff_min_index_range[0]] - A[min_i - 1]))
            diff_min_key.insert(insert_index, abs(A[diff_min_index_range[0]] - A[min_i - 1]))
            diff_min_indices.insert(insert_index, [diff_min_index_range[0], min_i - 1])
          
        indices.append(min_i)
        #print('ind', indices)
        #print('key', diff_min_key)
        #print('indices', diff_min_indices)
        
       
    cost = 0
    indices = sorted(indices)
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)
    #print(indices)
'''
pattern = list(itertools.combinations(range(1, M), N-1))
 
if N >= M:
    print(0)
else:
    min_cost = 1e10
    min_p = None
    for p in pattern:
        pos = [0] + list(p) + [M]
        cost = 0
        for i in range(len(pos)-1):
            cost += A[pos[i + 1] - 1] - A[pos[i]]
        
        if cost < min_cost:
            min_cost = cost
            min_p = copy.copy(p)
         
    print(min_cost)
    print(min_p)
'''

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import bisect
import random
import itertools
import copy

N, M = map(int, input().rstrip().split(' '))
A = sorted(list(map(int, input().rstrip().split(' '))))
#N = 4
#M = 7
#A = sorted(list(set([random.randint(0, 10) for _ in range(M)])))
#M = len(A)

if N >= M:
    print(0)
elif N == 1:
    print(A[-1] - A[0])
else:
    indices = [0, M]
    diff_min_key = [A[-1] - A[0]]
    diff_min_indices = [[0, M-1]]
    
    for _ in range(N-1):
        diff_min_index_range = diff_min_indices[-1]
        min_cost = 1e10
        min_i = -1
        for i in range(diff_min_index_range[0] + 1, diff_min_index_range[1]+1):
            cost = abs(A[i - 1] - A[diff_min_index_range[0]]) + abs(A[diff_min_index_range[1]] - A[i])
            if cost < min_cost:
                min_cost = cost
                min_i = i

        if len(diff_min_key) > 0:
            diff_min_key.pop(-1)
            diff_min_indices.pop(-1)

            if abs(A[diff_min_index_range[1]] - A[min_i]) > 0:
                insert_index = bisect.bisect_left(diff_min_key, abs(A[diff_min_index_range[1]] - A[min_i]))
                diff_min_key.insert(insert_index, abs(A[diff_min_index_range[1]] - A[min_i]))
                if insert_index is None:
                    insert_index = -1
                diff_min_indices.insert(insert_index, [min_i, diff_min_index_range[1]])
            
            if abs(A[diff_min_index_range[0]] - A[min_i - 1]) > 0:
                insert_index = bisect.bisect_left(diff_min_key, abs(A[diff_min_index_range[0]] - A[min_i - 1]))
                diff_min_key.insert(insert_index, abs(A[diff_min_index_range[0]] - A[min_i - 1]))
                if insert_index is None:
                    insert_index = -1
                diff_min_indices.insert(insert_index, [diff_min_index_range[0], min_i - 1])
          
        indices.append(min_i)
       
    cost = 0
    indices = sorted(indices)
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import bisect
import random
import itertools
import copy

N, M = map(int, input().rstrip().split(' '))
A = sorted(list(map(int, input().rstrip().split(' '))))
#N = 4
#M = 7
#A = sorted(list(set([random.randint(0, 10) for _ in range(M)])))
#M = len(A)

if N >= M:
    print(0)
elif N == 1:
    print(A[-1] - A[0])
else:
    indices = [0, M]
    diff_min_key = [A[-1] - A[0]]
    diff_min_indices = [[0, M-1]]
    
    for _ in range(N-1):
        diff_min_index_range = diff_min_indices[-1]
        min_cost = 1e10
        min_i = -1
        for i in range(diff_min_index_range[0] + 1, diff_min_index_range[1]+1):
            cost = abs(A[i - 1] - A[diff_min_index_range[0]]) + abs(A[diff_min_index_range[1]] - A[i])
            if cost < min_cost:
                min_cost = cost
                min_i = i

        if len(diff_min_key) > 0:
            diff_min_key.pop(-1)
            diff_min_indices.pop(-1)

            if abs(A[diff_min_index_range[1]] - A[min_i]) > 0:
                insert_index = bisect.bisect_left(diff_min_key, abs(A[diff_min_index_range[1]] - A[min_i]))
                diff_min_key.insert(insert_index, abs(A[diff_min_index_range[1]] - A[min_i]))
                if insert_index is None:
                    insert_index = -1
                diff_min_indices.insert(insert_index, [min_i, diff_min_index_range[1]])
            
            if abs(A[diff_min_index_range[0]] - A[min_i - 1]) > 0:
                insert_index = bisect.bisect_left(diff_min_key, abs(A[diff_min_index_range[0]] - A[min_i - 1]))
                diff_min_key.insert(insert_index, abs(A[diff_min_index_range[0]] - A[min_i - 1]))
                if insert_index is None:
                    insert_index = -1
                diff_min_indices.insert(insert_index, [diff_min_index_range[0], min_i - 1])
          
        bisect.insort_left(indices, min_i)
        #indices = sorted(indices)
       
    cost = 0
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import bisect
import random

N, M = map(int, input().rstrip().split(' '))
A = sorted(list(map(int, input().rstrip().split(' '))))
#N = 100
#M = 500
#A = sorted(list(set([random.randint(0, 1000) for _ in range(M)])))
#M = len(A)

if N >= M:
    print(0)
else:
    indices = [0, M]
    diff_min_key = [A[-1] - A[0]]
    diff_min_indices = [[0, M-1]]
    for _ in range(N-1):
        diff_min_index_range = diff_min_indices[-1]
        min_cost = 1e10
        min_i = -1
        for i in range(diff_min_index_range[0] + 1, diff_min_index_range[1]+1):
            cost = (A[i - 1] - A[diff_min_index_range[0]-1]) + (A[diff_min_index_range[1]] - A[i])
            if cost < min_cost:
                min_cost = cost
                min_i = i

        if len(diff_min_key) > 0:
            diff_min_key.pop(-1)
            diff_min_indices.pop(-1)

            insert_index = bisect.insort_left(diff_min_key, A[diff_min_index_range[1]] - A[min_i])
            if insert_index is None:
                insert_index = -1
            diff_min_indices.insert(insert_index, [min_i, diff_min_index_range[1]])
            
            insert_index = bisect.insort_left(diff_min_key, A[diff_min_index_range[0]] - A[min_i - 1])
            if insert_index is None:
                insert_index = -1
            diff_min_indices.insert(insert_index, [min_i - 1, diff_min_index_range[0]])
          
        bisect.insort_left(indices, min_i)
        #indices = sorted(indices)
        print(indices)
        
    cost = 0
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import bisect
import random

N, M = map(int, input().rstrip().split(' '))
A = sorted(list(map(int, input().rstrip().split(' '))))
#N = 100
#M = 500
#A = sorted(list(set([random.randint(0, 1000) for _ in range(M)])))
#M = len(A)

if N >= M:
    print(0)
else:
    sorted_diff_indices = [0]
    indices = [0, M]
    diff_min_key = [A[-1] - A[0]]
    diff_min_indices = [[0, M-1]]
    for _ in range(N-1):
        
        diff_min_index_range = diff_min_indices[-1]
        min_cost = 1e10
        min_i = -1
        try:
            #for i in range(indices[diff_min_index]+1, indices[diff_min_index+1]):
            #print(list(range(diff_min_index_range[0], diff_min_index_range[1]+1)))
            for i in range(diff_min_index_range[0] + 1, diff_min_index_range[1]+1):
                #cost = (A[i - 1] - A[indices[diff_min_index_range[1]]]) + (A[indices[diff_min_index_range[0]+1]-1] - A[i])
                cost = (A[i - 1] - A[diff_min_index_range[0]-1]) + (A[diff_min_index_range[1]] - A[i])
                #print('cost', cost, A[i - 1], A[diff_min_index_range[0]-1], A[diff_min_index_range[1]], A[i], i - 1, diff_min_index_range[0]-1, diff_min_index_range[1], i)
                if cost < min_cost:
                    min_cost = cost
                    min_i = i
        except:
            #print(diff_min_index)
            #print(diff_min_key)
            #print(diff_min_indices)
            #print(indices)
            break
           
        if len(diff_min_key) > 0:
            diff_min_key.pop(-1)
            diff_min_indices.pop(-1)

            insert_index = bisect.insort_left(diff_min_key, A[diff_min_index_range[1]] - A[min_i])
            #print('a', A[diff_min_index_range[1]] - A[min_i])
            if insert_index is None:
                insert_index = -1
            diff_min_indices.insert(insert_index, [min_i, diff_min_index_range[1]])
            
            insert_index = bisect.insort_left(diff_min_key, A[diff_min_index_range[0]] - A[min_i - 1])
            #print('b', A[min_i - 1] - A[diff_min_index_range[0]])
            if insert_index is None:
                insert_index = -1
            diff_min_indices.insert(insert_index, [min_i - 1, diff_min_index_range[0]])
          
        indices.insert(diff_min_index_range[0] + 1, min_i)
        indices = sorted(indices)
        
    cost = 0
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Wrong Answer

Solution code:
N,M=tuple(map(int,input().split()))
X = sorted(tuple(map(int,input().split())))
sa = []
for i in range(len(X)-1):
    sa.append(X[i+1]-X[i])
print(sum(sorted(sa)[: -(N-1)]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N, M = map(int, input().rstrip().split(' '))
A = sorted(list(map(int, input().rstrip().split(' '))))

if N >= M:
    print(0)
else:
    diff = [A[-1] - A[0]]
    indices = [0, M]
    for _ in range(N-1):
        
        diff_min_index = diff.index(min(diff))
        min_cost = 1e10
        min_i = -1
        for i in range(indices[diff_min_index] + 1, indices[diff_min_index+1]):
            cost = A[i - 1] - A[indices[diff_min_index]] + A[indices[diff_min_index+1]-1] - A[i]
            if cost < min_cost:
                min_cost = cost
                min_i = i
        
        diff.pop(diff_min_index)
        diff.append(A[indices[diff_min_index+1]-1] - A[min_i])
        diff.append(A[min_i - 1] - A[indices[diff_min_index]])
        indices.insert(diff_min_index + 1, min_i)
 
    cost = 0
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X = [int(x) for x in input().split()]

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

print(sum(L[:-(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:]))[:n+1]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
diff = []
 
X.sort()

if (N < M): 
  for i in range(M-1):
    dif = X[i+1] -X[i]
    diff.append(dif)
  
  diff.sort()
 
  for n in range(N):
    diff.pop(-1)
 
  for i in range(len(diff)):
    ans += diff[i]

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
line=list(map(int,input().split()))
if n >= m:
  print(0)
line.sort()
stock=0
distance=[]
for i in line:
  distance.append(i-stock)
  stock=i
distance.pop(0)
distance.sort()
ans=0
for i in range(m-n):
  ans+= distance[i]
print(ans)

Submission status: Wrong Answer

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

if (N < M):
  for n in range(N):
    diff.pop(-1)
 
  for i in range(len(diff)):
    ans += diff[i]

  print(ans)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
line=list(map(int,input().split()))
if n >= m:
  print(0)
line.sort()
stock=0
distance=[]
for i in line:
  distance.append(i-stock)
  stock=i
distance.pop(0)
distance.sort()
ans=0
for i in range(m-n):
  ans+= distance[i]
print(ans)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

N,M = [int(x) for x in input().split()]
xls = [int(x) for x in input().split()]
dls = [xls[i+1]-xls[i] for i in range(M-1)]
if N >= M:
    ans = 0
else:
    dls.sort()
    ans = sum(dls[0:M-N])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if m > 1:
    x_diff = []
    for i in range(m-1):
        x_diff.append(x[i+1] - x[i])
else:
    x_diff = [0]

x_diff = sorted(x_diff)[:-(n-1)]

print(sum(x_diff))

Submission status: Wrong Answer

Solution code:

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

if m > 1:
    x_diff = []
    for i in range(m-1):
        x_diff.append(x[i+1] - x[i])
else:
    x_diff = [0]

for i in range(n-1):
    max_num = max(x_diff)
    max_index = x_diff.index(max_num)
    x_diff[max_index] = 0

print(sum(x_diff))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()
blist = []

if n >= m :
  print(0)
else:
  for i in range(0,m-1):
    blist.append(alist[i+1] - alist[i])
  blist.sort()
  d = -1 * (n-1)
  c = sum(blist[d:])
  b1 = alist[m-1] - alist[0]
  print(b1 - c)

Submission status: Wrong Answer

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

if m == 1:
	print(0)
elif n == 1:
	print(abs(max(x)-min(x)))
else:
	diff = [0]*(m-1)
	for it in range(m-1):
		diff[it] = x[it+1]-x[it]
	diff = sorted(diff)
	diff = diff[:m-n]
	# print(diff)
	print(sum(diff))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M=list(map(int,input().split()))
X=np.array(list(map(int,input().split())))
sX=np.sort(X)
dsX=np.diff(sX)
sdsX=np.sort(dsX)
print(np.sum(sdsX[:M-N]))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M=list(map(int,input().split()))
print(np.sum(np.sort(np.diff(np.sort(np.array(list(map(int,input().split()))))))[:M-N]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
lst = sorted([int(x) for x in input().split()])
lst2 = []
for x in range(1, len(lst)):
    lst2.append(lst[x] - lst[x - 1])

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
lst = sorted([int(x) for x in input().split()])
lst2 = []
for x in range(1, len(lst)):
    lst2.append(lst[x] - lst[x - 1])

lst3 = sorted(lst2)
n = len(lst3) - N - 1
print(sum(lst3[:-n]))

Submission status: Wrong Answer

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

X.sort()
dif = [ X[i+1] - X[i] for i in range(M-1) ]
dif.sort()
print(dif)
add = max(0,M - N)
print(sum(dif[:add]))

Submission status: Wrong Answer

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

if n >= m:
    print(0)

else:
    arr.sort()

    diff = [arr[i+1] - arr[i] for i in range(m-1)]
    diff.sort()

    print(sum(arr[0:m-n]))

Submission status: Wrong Answer

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

if n >= m:
    print(0)

else:
    arr.sort()

    diff = [arr[i+1] - arr[i] for i in range(m-1)]
    diff.sort()

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

Submission status: Wrong Answer

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

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

diff = [arr[i+1] - arr[i] for i in range(m-1)]
diff.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m,*a=map(int,open(0).read().split())
a=sorted(a)
if n<m:
  print(sum(sorted([a[i+1]-a[i]for i in range(m-1)])[:-n+1]))
else:
  print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = [int(x) for x in input().split()]
x = sorted([int(y) for y in input().split()])
sub = [x[i]-x[i-1] for i in range(1, M)]
if N >= M:
    print(0)
else:
    print(sum(sorted(sub)[:-(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(M-1):
    Y.append(X[i+1] - X[i])
Y.sort()
print(sum(Y[:M-N]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split(" "))
tmplist=list(map(int,input().split(" ")))
tmplist.sort()
tmplist2=[]
tmp=tmplist[0]
sum=0
for a in range(1,M):
    tmplist2.append(tmplist[a]-tmp)
    tmp=tmplist[a]
tmplist2.sort()
if(N>=M):
    print(0)
else:
    tmplist=tmplist2[0:N+1]
    for a in tmplist[::-1]:
        sum+=a
    print(sum)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split(" "))
tmplist=list(map(int,input().split(" ")))
tmplist.sort()
tmplist2=[]
tmp=tmplist[0]
sum=0
for a in range(1,M):
    tmplist2.append(tmplist[a]-tmp)
    tmp=tmplist[a]
tmplist2.sort()
if(N>=M):
    print(0)
else:
    for a in tmplist2[0:N+1]:
        sum+=a
    print(sum)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int ,input().split()))
X = list(map(int, input().split()))
dis = []
if N >= M:
    print(str(0)+"\n")
else:
    X.sort()
    for i in range(M-1):
        dis.append(abs(X[i+1] - X[i]))
    dis.sort()
    ans = sum(dis[:-(N-1)])
    print(str(ans)+"\n")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = list(map(int ,input().split()))
X = list(map(int, input().split()))
dis = []
if N >= M:
    print(0)
else:
    X.sort()
    for i in range(M-1):
        dis.append(abs(X[i+1] - X[i]))
    dis.sort()
    print(dis)
    ans = sum(dis[: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(len(x)-1):
    d.append(x[i+1]-x[i])
d = sorted(d)
if n==1:
    print(x[-1]-x[0])
elif n>=len(x):
    print(0)
else:
    print(d)
    dis = 0
    for i in range(n-1):
        dis+=d[-i-1]
    print(sum(d)-dis)

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(len(x)-1):
    d.append(x[i+1]-x[i])
d = sorted(d)
if n==1:
    print(x[-1]-x[0])
elif n>=len(x):
    print(0)
else:
    ans = 0
    for i in range(n-1):
        ans+=d[-i-1]
    print(ans)

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(0, m - 1):
    y.append(x[i + 1] - x[i])

y.sort()

s = sum(y[0:m - n])
print(s)

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(0, m - 1):
    print(x[i+1], x[i])
    y.append(x[i + 1] - x[i])

y.sort()

s = sum(y[0:m - n])
print(s)

Submission status: Wrong Answer

Solution code:
N,M = list(map(int, input().split()))
x = list(map(int, input().split()))
x = sorted(x)
#N-1個の隙間は通らなくて済む

bet = [] #iとi+1の間隔
for i in range(1,M):
  bet.append(x[i]-x[i-1])

if N == 1:
  print(max(x)-min(x)-1)
elif N>=M:
  print(0)
else:
  for i in range(N-1):
    big = max(bet)
    bet.remove(big)
  print(sum(bet))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
def solve(string):
    n, m, *x = map(int, string.split())
    x = np.asarray(sorted(x))
    return str(sum(sorted(np.diff(x))[:-n+1]))



if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Submission status: Wrong Answer

Solution code:
#N_M = list(map(int,input().split()))
#N = N_M[0]
#M = N_M[1]
#ans = 0

#list_sum = [0] * M

#for i in range(N):
#    tmp = list(map(int,input().split()))
#    for j in tmp[1:]:
#        list_sum[j - 1] +=1
        
#for i in list_sum:
#    if i == N:
#        ans +=1
        
#print(ans)


#def gcd(a,b):
#    if b == 0:
#        return a
#    else:
#        return gcd(b,a%b)

#n = int(input())
#line = list(map(int,input().split()))

#ans = line[0]

#for i in range(1,n):
#    ans = gcd(ans,line[i])

#print(ans)

import math

N_M = list(map(int,input().split()))
Z = list(map(int,input().split()))
Z_list = [0] * len(Z)

min = -2147483647
max = 2147483647

N = N_M[0]
M = N_M[1]

ans = 0

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

Z.sort()

for i in range(1,N + 1):
    if i % 2 != 0:
        min = Z[0]
        Z.pop(0)
    else:
        max = Z[-1]
        Z.pop(-1)
maxlen = 0
if abs(Z[0] - min) > abs(max - Z[-1]):
    maxlen = abs(Z[0] - min)
else:
    maxlen = abs(max - Z[-1])

for j in range(1,len(Z)):
    if maxlen < abs(Z[j] - Z[j -1] ):
        maxlen = abs(Z[j] - Z[j -1] )

print(abs(max - min) - maxlen)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

li.sort()

dist = 0
_li = np.zeros(m)

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

_li.sort()

print(int(sum(_li[0:-(n-1)])))

Submission status: Wrong Answer

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

li.sort()

dist = 0
_li = []

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

_li.sort()

print(int(sum(_li[0:-(n-1)])))

Submission status: Wrong Answer

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

li.sort()

dist = 0
_li = []

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

_li.sort()

print(sum(_li[0:-(n-1)]))

Submission status: Wrong Answer

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

li.sort()

dist = 0
_li = []

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

_li.sort()

if(m == 1):
  print(0)       
else: 
  print(sum(_li[0:-(n-1)]))

Submission status: Wrong Answer

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

li.sort()

dist = 0
_li = []

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

_li.sort()

print(sum(_li[0:-(n-1)]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

X.sort()
d = [X[i]-X[i-1] for i in range(1,M)]
d.sort()
print(sum(d[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())  # Nはコマの数, Mは目標の座標数
X = [int(x) for x in input().split()]  # 目標の座標リスト

if N >= M:  # N >= Mならば全ての座標にコマを置いて終わるため移動しない
    print(0)
else:  # N < M
    X.sort()  # Xを昇順にソート
    L = [X[i] - X[i-1] for i in range(1, M)]
    L.sort()
    ans = sum(L[:-N+1])
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
X=input().split()
X=[int(i) for i in X]
X.sort()
Y=[]
for i in range(1,M):
	Y.append(abs(abs(X[i]) - abs(X[i-1])))
Y.sort(reverse=True)
del Y[:N-1]
ans=0
for i in Y:
	ans+=i
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

L.sort()
dist = [0 for _ in range(M-1)]
for i in range(M-1):
    dist[i] = abs(abs(L[i+1]) - L[i])
    
dist.sort()
res = sum(dist[:-N+1])
print(res)

Submission status: Wrong Answer

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

L.sort()
dist = [0 for _ in range(M-1)]
for i in range(M-1):
    dist[i] = abs(abs(L[i+1]) - abs(L[i]))
    
dist.sort()
res = sum(dist[:-N+1])
print(res)

Submission status: Wrong Answer

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

L.sort()
dist = [0 for _ in range(M-1)]
for i in range(M-1):
    dist[i] = abs(L[i+1] - L[i])
    
dist.sort()
res = sum(dist[:-N+1])
print(res)

Submission status: Wrong Answer

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

L.sort()
dist = [0 for _ in range(M-1)]
for i in range(M-1):
    dist[i] = L[i+1] - L[i]
    
dist.sort()
res = sum(dist[:-N+1])
print(res)