Problem:


Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0



Sample input:
2 5
10 12 1 2 14


Sample output:
5

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
if N > M :
    print(0)
else:
    flag = [0 for fl in range(M)]
    X.sort()
    for _ in range(1,M+1):
        flag[_] += X[_] - X[_-1]
    for lp in range(len(flag)):
      	ff = flag.index(max(flag))
        flag.pop(ff)
        print(sum(flag))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
if N > M :
    print(0)
else:
    flag = [0 for fl in range(M)]
    X.sort()
    for _ in range(1,M):
        flag[_] += X[_] - X[_-1]
   for lp in range(N-1):
        flag.pop(flag.index(max(flag)))
        print(sum(flag))

Submission status: Runtime Error

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

pos = [[0] * 2 for i in range(N)]
total = 0

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

for i in range(N):
    pos[i][0] = i
    pos[i][1] = abs(X[i+1] - X[i])
    total = total + abs(X[i+1] - X[i])


for j in range(N, M-1):
    total = total + abs(X[j+1]-X[j])
    pos = sorted(pos, key=lambda x:(x[1]))
    if pos[0][1] < abs(X[j+1]-X[j]):
        pos[0][0] = j
        pos[0][1] = abs(X[j+1]-X[j])



pos = sorted(pos, key=lambda x:(x[1]), reverse = True)
for i in range(N-1):
    total = total - pos[i][1]

print(total)

Submission status: Runtime Error

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

X.sort()

memo = dict()
def get_min(n,x):
    key = str((n,x))
    tmp = memo.get(key,None)
    if tmp != None:
        return tmp
    if x == []:
        tmp = 10**8
        memo[key] = tmp
        return tmp
    elif len(x) == 1:
        memo[key] = 0
        return 0
    x0 = x[0]
    if n == 1:
        tmp = x[-1]-x0
        memo[key] = tmp
        return tmp
    tmp = min([y - x0 + get_min(n-1,x[i+1:]) for i,y in enumerate(x)])
    memo[key] = tmp
    return tmp
if N >= M:
    print(0)
else:
    print(get_min(N,X))

Submission status: Runtime Error

Solution code:
X.sort()

memo = dict()
def get_min(n,x):
    key = str((n,x))
    tmp = memo.get(key,None)
    if tmp != None:
        return tmp
    if x == []:
        tmp = 10**8
        memo[key] = tmp
        return tmp
    elif len(x) == 1:
        memo[key] = 0
        return 0
    x0 = x[0]
    if n == 1:
        tmp = x[-1]-x0
        memo[key] = tmp
        return tmp
    tmp = min([y - x0 + get_min(n-1,x[i+1:]) for i,y in enumerate(x)])
    memo[key] = tmp
    return tmp
if N >= M:
    print(0)
else:
    print(get_min(N,X))

Submission status: Runtime Error

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

X.sort()

memo = dict()
def get_min(n,x):
    key = str((n,x))
    tmp = memo.get(key,None)
    if tmp != None:
        return tmp
    if x == []:
        tmp = 10**8
        memo[key] = tmp
        return tmp
    elif len(x) == 1:
        memo[key] = 0
        return 0
    x0 = x[0]
    if n == 1:
        tmp = x[-1]-x0
        memo[key] = tmp
        return tmp
    tmp = min([y - x0 + get_min(n-1,x[i+1:]) for i,y in enumerate(x)])
    memo[key] = tmp
    return tmp

print(get_min(N,X))

Submission status: Runtime Error

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])
ssa = sorted(sa)
for i in range(N-1):
    ssa.pop()
    
print(sum(ssa))

Submission status: Runtime Error

Solution code:
N, M = map(int,input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)
if N>M:
    ans = 0
else
    distance = [abs(a[i+1] - a[i]) for i in range(M-1)]
    #print(distance)
    distance.sort()
    #print(distance)
    del distance[M-N:]
    ans = 0 
    for j in distance:
        ans += j
print(ans)

Submission status: Runtime Error

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
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Runtime Error

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]])
          
        indices.insert(diff_min_index_range[0] + 1, 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: Runtime Error

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

import bisect

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

if N >= M:
    print(0)
else:
    sorted_diff_indices = [0]
    indices = [0, M]
    diff_min_key = [A[-1] - A[0]]
    diff_min_indices = [0]
    for _ in range(N-1):
        
        diff_min_index = diff_min_indices[-1]
        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
            
        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[indices[diff_min_index+1]-1] - A[min_i])
            if insert_index is None:
                insert_index = -1
            diff_min_indices.insert(insert_index, min_i)
            
            insert_index = bisect.insort_left(diff_min_key, A[min_i - 1] - A[indices[diff_min_index]])
            if insert_index is None:
                insert_index = -1
            diff_min_indices.insert(insert_index, 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: Runtime Error

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

import itertools

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

print(A)

pattern = list(itertools.combinations(range(1, M), N-1))

if N >= M:
    print(0)
else:
    min_cost = 1e10
    for p in pattern:
        pos = [0, *p, M]
        cost = 0
        for i in range(len(pos)-1):
            cost += A[pos[i + 1] - 1] - A[pos[i]]
            #print(pos[i + 1] - 1, pos[i], A[pos[i + 1] - 1] - A[pos[i] ])
        
        if cost < min_cost:
            min_cost = cost
         
    print(min_cost)

Submission status: Runtime Error

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()
 
for n in range(N):
  diff.pop(-1)
 
for i in range(len(diff)):
  ans += diff[i]

print(ans)

Submission status: Runtime Error

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)
for i in range(n-1):
  distance.pop(distance.index(max(distance)))
ans=0
for i in distance:
  ans+= i
print(ans)

Submission status: Runtime Error

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

for n in range(N):
  diff.pop(-1)

for i in range(len(diff)):
  ans += diff[i]

Submission status: Runtime Error

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:
  stock=i
  distance.append(i-stock)
distance.pop(0)
for i in range(n-1):
  distance.pop(distance.index(max(distance)))
ans=0
for i in distance:
  ans+= i
print(ans)

Submission status: Runtime Error

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


print(ans)

Submission status: Runtime Error

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

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

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: Runtime Error

Solution code:
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
gap = sorted([x[i+1]-x[i] for i in range(m-1)])
length = len(gap)
def dp(n):
    global gap
    global length
    if n == 1:
        return max(x) - min(x)
    else:
        if length > 0:
            maxGap = gap[length - 1]
            gap = gap[:length-1]
        else:
            maxGap = 0
        return dp(n-1) - maxGap
if n < m:
    print(dp(n))
else:
    print(0)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
gap = [x[i+1]-x[i] for i in range(m-1)]
def dp(n):
    if n == 1:
        return max(x) - min(x)
    else:
        if len(gap) > 0:
            maxGap = max(gap)
            gap.remove(maxGap)
        else:
            maxGap = 0
        return dp(n-1) - maxGap
if n < m:
    print(dp(n))
else:
    print(0)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
gap = [x[i+1]-x[i] for i in range(m-1)]
def dp(n):
    if n == 1:
        return max(x) - min(x)
    else:
        if len(gap) > 0:
            maxGap = max(gap)
            gap.remove(maxGap)
        else:
            maxGap = 0
        return dp(n-1) - maxGap
print(dp(n))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()
blist = []
for i in range(0,m-1):
  blist.append(alist[i+1] - alist[i])
b1 = alist[m-1] - alist[0]
b2 = max(blist)
print(b1 - b2)

Submission status: Runtime Error

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:
	# print(diff)
	diff = [0]*(m-1)
	for it in range(m-1):
		diff[it] = x[it+1]-x[it]
	for it in range(n-1):
		max_d = max(diff)
		idx = diff.index(max_d)
		diff = diff[:idx] + diff[idx+1:]
	print(sum(diff))

Submission status: Runtime Error

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:
	# print(diff)
	diff = []
	for it in range(m-1):
		diff.append(x[it+1]-x[it])
	for it in range(n-1):
		max_d = max(diff)
		idx = diff.index(max_d)
		diff = diff[:idx] + diff[idx+1:]
	print(sum(diff))

Submission status: Runtime Error

Solution code:
import pdb
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
d=[]
if len(x)!=1:
    for i in range(0,len(x)-1):
        d.append(x[i+1]-x[i])
    d.sort(reverse=True)
    for i in range(n-1):
        d.pop(0)
    ans=sum(d)
    print(ans)
else:
    print(0)
#pdb.set_trace()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

n, m = map(int, input().split())
l1 = list(map(int, input().split()))
l1.sort()
l2 = []
l2.append(l1)
def divide(l):
    diff = 0
    tmp = []
    for i in l:
        if len(i) > diff:
            tmp = i
    index = bisect_left(l[0], int(sum(tmp) / len(tmp)))
    l.remove(tmp)
    l.append(tmp[:index])
    l.append(tmp[index:])
    return l

i = 0
while i < n - 1:
    l2 = divide(l2)
    i += 1
    
count = 0
for l in l2:
  count += l[-1] - l[0]

print(count)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[int(i) for i in input().split()]
l.sort()
ans=[0]
for i in range(m-1):
    ans.append(l[i+1]-l[i])
ans.sort()
for i in range(n-1):
    ans.pop()
    if len(ans)==1: break
print(sum(ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

move = 0

if n >= m:
  print(move)
elif n==1:
  move = max(x) - min(x)
  print(move)
else:
  x_diff = [None]*(m-1)
  for i in range(m-1):
    x_diff[i] = [i+1, x[i+1] - x[i]]
  x_diff = sorted(x_diff, key=lambda x:-x[1])
  for j in range(n):
    if j==0:
      move += max(x[0:x_diff[j][0]])-min(x[0:x_diff[j][0]])
    elif j==n-1:
      move += max(x[x_diff[j-1][0]:])-min(x[x_diff[j-1][0]:])
    else:
      move += max(x[x_diff[j-1][0]:x_diff[j][0]])-min(x[x_diff[j-1][0]:x_diff[j][0]])
  print(move)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# coding: UTF-8

import itertools

class Area:
    def __init__(self,s,e):
        self.start = s
        self.end = e

    start = 0
    end = 0

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


TargetList = []
for i in input().split():
    TargetList.append(int(i))

TargetList.sort()

minSum = -1
for sep in itertools.combinations(range(1,m) , n-1):
        


    PrevEnd = 0 #前回の後ろ
    print(sep)
    sum = 0
    for s in sep:
        sum += abs(TargetList[s-1] - TargetList[PrevEnd])
        print(TargetList[s-1] , TargetList[PrevEnd])
        PrevEnd = s
    sum += abs(TargetList[m-1] - TargetList[sep[-1]])
    print(TargetList[m-1] , TargetList[PrevEnd])
    print("temp sum",sum)

    if minSum == -1 or (minSum > sum):
        minSum = sum
minSum = 0 if minSum == -1 else minSum
print(minSum)

Submission status: Runtime Error

Solution code:
#python3.4
def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()

if(N >= M):
    print(0)
else:
  new_list = sorted(X)
  diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
  new_dif_list = sorted(diff_list, reverse = True)

  if(N  >= 2):
    for i in range(N-1):
        new_dif_list.pop(0)

print(sum(new_dif_list))

Submission status: Runtime Error

Solution code:
#python3.4
def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()

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

new_list = sorted(X)
diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
new_dif_list = sorted(diff_list, reverse = True)
else:
  if(N  >= 2):
      for i in range(N-1):
          new_dif_list.pop(0)

  print(sum(new_dif_list))

Submission status: Runtime Error

Solution code:
#python3.4
def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()

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

new_list = sorted(X)
diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
new_dif_list = sorted(diff_list, reverse = True)

if(N  >= 2):
    for i in range(N-1):
        new_dif_list.pop(0)

print(sum(new_dif_list))

Submission status: Runtime Error

Solution code:
#python3.4
def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()

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

new_list = sorted(X)
diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
new_dif_list = sorted(diff_list, reverse = True)

if(N  >= 2):
    for i in range(N-1):
      new_dif_list.pop(0)

print(sum(new_dif_list))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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):
        dis.append(abs(X[i+1] - X[i]))
    dis.sort()
    print(dis)
    ans = sum(dis[:-(N-1)])
    print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split(" "))
X = list(map(int, input().split(" ")))
X.sort()
total = X[-1]-X[0]
dif = [X[i+1] - X[i] for i in range(len(X)-1)]
dif.sort()
Y = [0 for i in range(N-1)]
for i in range(N-1):
	Y[i] = dif.pop()
print(total-sum(Y))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split(" "))
X = list(map(int, input().split(" ")))
X.sort()
total = X[-1]-X[0]
dif = [X[i+1] - X[i] for i in range(len(X)-1)]
dif.sort()
print(dif)
Y = [0 for i in range(N-1)]
for i in range(N-1):
	Y[i] = dif.pop()
print(total-sum(Y))

Submission status: Runtime Error

Solution code:
num, num_of_coordinates = map(int, input().split(" "))
coordinates = list(map(int, input().split(" ")))
total = 0
abs_distance = []
coordinates.sort()
for i in range(0, len(coordinates)-1):
  dist = abs(coordinates[i] - coordinates[i+1])
  total += dist
  abs_distance.append(dist)  

#でかいやつ順に並べる
abs_distance.sort()
abs_distance.reverse()
for i in range(num-1):
  total -= abs_distance[i]

print(total)

Submission status: Runtime Error

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

import numpy as np
if m == 1:
  print(0)
else:
  xs = np.sort(xs)
  ds = [xs[i]-xs[i-1] for i in range(1, len(xs))]
  if n == 1:
    print(sum(ds))
  else:
    for i in range(n-1):
      ds.remove(max(ds))
    print(sum(ds))

Submission status: Runtime Error

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

if m == 1:
  print(0)
else:
  xs = sorted(xs)
  ds = [xs[i]-xs[i-1] for i in range(1, len(xs))]
  if n == 1:
    print(sum(ds))
  else:
    for i in range(n-1):
      ds.remove(max(ds))
    print(sum(ds))

Submission status: Runtime Error

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

if m == 1:
  print(0)
else:
  xs = sorted(xs)
  ds = [xs[i]-xs[i-1] for i in range(1, len(xs))]
  for i in range(n-1):
    ds.remove(max(ds))
  print(sum(ds))

Submission status: Runtime Error

Solution code:
nm_ent = list(map(int, input().split()))
n = nm_ent[0]
m = nm_ent[1]

x = list(map(int, input().split()))
x.sort()
if m == 1:
    print(0)
else:
    xl = []
    for i in range(0, m-1):
        xl.append(x[i+1] - x[i])
    k = 1
    while k < n:
        xl.remove(max(xl))
        k += 1
    print(sum(xl))

Submission status: Runtime Error

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])
if n==1:
    print(x[-1]-x[0])
else:
    for i in range(n-1):
        d.remove(max(d))
    print(sum(d))

Submission status: Runtime Error

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

Y = sorted(X)
Y = list(map(lambda x,y:x-y, Y[1:],Y[:-1]))

for i in range(N-1):
	x = max(Y)
	Y.remove(x)

print(sum(Y))

Submission status: Runtime Error

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

dis=[]
if n>m: 
    print(0)
else:
    for i in range(m-1):
        dis[i] = x[i+1] - x[i]

    dis.sort() 

    print(sum(dis[:m-n]))

Submission status: Runtime Error

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

dis=[]
if n>m: 
    print(0)
else:
    for i in range(len(x)-1):
        dis[i] = x[i+1] - x[i]

    dis.sort()

    print(sum(dis[:m-n]))

Submission status: Runtime Error

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

dis=[]
res=0
if n>m: 
    print(0)
else:
    for i in range(len(x)-1):
        dis[i] = x[i+1] - x[i]

    dis.sort()

    for j in range(n-1):
        res += dis[j]

    print(sum(dis) - res)

Submission status: Runtime Error

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

dis=[]
res=0
if n>m: 
    print(0)
else:
    for i in range(len(x)-1):
        dis[i] = x[i+1] - x[i]

    dis.sort()

    for j in range(n-1):
        res += y[j]

    print(sum(dis) - res)

Submission status: Runtime Error

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

dis=[]
res=0
if n>m: ans=0
else:
    for i in range(len(x)-1):
        dis[i] = x[i+1] - x[i]

    y.sort()

    for j in range(n-1):
        res += y[j]

    return sum(y) - res

Submission status: Runtime Error

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

dis=[]
ans=0
if n>m: ans=0
else:
    for i in range(len(x)-1):
        dis[i] = x[i+1] - x[i]

    y.reverse()

    for j in range(n-1):
        ans += y[j]

    return ans

Submission status: Runtime Error

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

if len(X) >= 2:
    diff = list()
    for i in range(len(X)-1):
        diff.append(abs(X[i+1]-X[i]))

    for i in range(N-1):
        idx = diff.index(max(diff))
        diff.pop(idx)

    print(sum(diff))
else:
    print(0)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math
import sys


def solution(arr, moves):
    if len(arr) <= moves:
        return 0

    arr.sort()
    if moves == 1:
        return abs(arr[0] - arr[-1])

    diff = [None] * len(arr)
    diff[0] = abs(arr[0] - arr[1])
    diff[-1] = abs(arr[-2] - arr[-1])
    for i in range(1, len(arr) - 1):
        diff[i] = abs(arr[i] - arr[i - 1]) if  abs(arr[i] - arr[i - 1]) > abs(arr[i] - arr[i + 1]) else abs(arr[i] - arr[i + 1])
    d = enumerate(diff)

    visited = {}
    current_positions = {}

    for i in list(reversed(sorted(d, key=lambda x: x[1])))[:moves]:
        visited[i[0]] = True
        current_positions[i[0]] = True

    counter = 0
    while len(visited) < len(arr):
        min_on_position = sys.maxsize
        minimum_element = None
        side = None
        for i in current_positions:
            if i - 1 not in visited and i > 0:
                if min_on_position > abs(arr[i - 1] - arr[i]):
                    min_on_position = abs(arr[i - 1] - arr[i])
                    minimum_element = i
                    side = i - 1

            if i + 1 not in visited and i < len(arr) - 1:
                if min_on_position > abs(arr[i + 1] - arr[i]):
                    min_on_position = abs(arr[i + 1] - arr[i])
                    minimum_element = i
                    side = i + 1


        current_positions[side] = True
        visited[side] = True
        counter += abs(arr[minimum_element] - arr[side])

    return counter

for i,line in enumerate(sys.stdin):
    if i == 0:
        N, M = int(line.split(" "))
    if i == 1:
        A = [int(i) for i in line.split(" ")]
        solution(A, M)

# line = "-10 -3 0 9 -100 2 17"
#
# lines = [int(i) for i in line.split(" ")]
# print("before")
# a = [10,12,1,2,14]
# b = [-100000]
# print(solution(lines, 3))
# print(solution(a, 2))
# print(solution(b, 1))

Submission status: Runtime Error

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

if N >= M:
    print(0)
else:
    distance = []
    X.sort()
    for i in range(M-1):
        distance = distance.append(X[i+1]-X[i])
    distance.sort(reverse=True)
    print(sum(distance[N:]))

Submission status: Runtime Error

Solution code:
import math
import sys


def solution(arr, moves):
    if len(arr) <= moves:
        return 0

    arr.sort()
    diff = [None] * len(arr)
    diff[0] = abs(arr[0] - arr[1])
    diff[len(arr) - 1] = abs(arr[len(arr) - 2] - arr[len(arr) - 1])
    for i in range(1, len(arr) - 1):
        diff[i] = abs(arr[i] - arr[i - 1]) if  abs(arr[i] - arr[i - 1]) > abs(arr[i] - arr[i + 1]) else abs(arr[i] - arr[i + 1])
    d = enumerate(diff)
    visited = {}
    current_positions = {}
    for i in list(reversed(sorted(d, key=lambda x: x[1])))[:moves]:
        visited[i[0]] = True
        current_positions[i[0]] = True
    counter = 0
    while len(visited) < len(arr):
        min_on_position = sys.maxsize
        minimum_element = None
        side = None
        for i in current_positions:
            if i - 1 not in visited and i > 0:
                if min_on_position > abs(arr[i - 1] - arr[i]):
                    min_on_position = abs(arr[i - 1] - arr[i])
                    minimum_element = i
                    side = "Left"

            if i + 1 not in visited and i < len(arr) - 1:
                if min_on_position > abs(arr[i + 1] - arr[i]):
                    min_on_position = abs(arr[i + 1] - arr[i])
                    minimum_element = i
                    side = "Right"

        del current_positions[minimum_element]
        if side == "Left":
            current_positions[minimum_element - 1] = True
            visited[minimum_element - 1] = True
            counter += abs(arr[minimum_element] - arr[minimum_element - 1])
        else:
            current_positions[minimum_element + 1] = True
            visited[minimum_element + 1] = True
            counter += abs(arr[minimum_element] - arr[minimum_element + 1])

    return counter


for i,line in enumerate(sys.stdin):
    if i == 0:
        M, N = line.split(" ")
    if i == 1:
        A = [int(i) for i in line.split(" ")]
        print(solution(A, int(M)))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
A_ = [2*i for i in A]
A_ = [i%A[0] for i in A_]
A_ = sorted(set(A_))
if A_ == [0]*len(A_):
  print(A[0])
else:
  print(A_[1])