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: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
if len(X) >1:
    l = [X[i+1]-X[i] for i in range(M-1)]
else:
    print(0)
    exit()
for i in range(N-1):
    ma = l.index(max(l))
    l[ma] = 0
print(sum(l))

Submission status: Time Limit Exceeded

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()
    
    cost = sum(x_dist)
    for i in range(n - 1) :
        max_idx = x_dist.index(max(x_dist))
        cost -= x_dist[max_idx]
        # print(x_dist[max_idx])
        x_dist[max_idx] = 0

    print(cost)

Submission status: Time Limit Exceeded

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()
for i in range(n-1):
    if len(move_cost) == 0:
        break
    move_cost = move_cost[:-1]
 
print(sum(move_cost))

Submission status: Time Limit Exceeded

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


for i in range(n-1):
    if len(move_cost) == 0:
        break
    move_cost.remove(max(move_cost))

print(sum(move_cost))

Submission status: Time Limit Exceeded

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)]
#print(y)
for i in range(n-1):
    y.remove(max(y))
print(sum(y))

Submission status: Time Limit Exceeded

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

X.sort()
diff=[]
for i in range(M-1):
    diff.append(X[i+1]-X[i])
gap=[]
for i in range(N-1):
    for j in range(M-1):
        x=diff.index(max(diff))
        gap.append(x)
        diff[x]=0
        break
gap.append(M-1)
gap=list(set(gap))
gap.sort()
offset=0
cnt=0
for i in gap:
    cnt+=X[i]-X[offset]
    offset=i+1
print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

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

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

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

for i in range(n - 1):
    ans.remove(max(ans))
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

gap = []


if( n >= m):
    print(0)
    sys.exit()
else:
    x = sorted(x)
    for i in range(m-1):
        gap.append(x[i+1] - x[i])
        gaps = sorted(gap)

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

gap = []


if( n >= m):
    print(0)
    sys.exit()
else:
    x = sorted(x)
    for i in range(m-1):
        gap.append(x[i+1] - x[i])
        gaps = sorted(gap)



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

Submission status: Time Limit Exceeded

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

x.sort()
diffx = [x[i+1]-x[i] for i in range(M-1)]

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

Submission status: Time Limit Exceeded

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

if N < M:
    cost = []
    for i in range(M-1):
        cost.append(abs(X[i] - X[i+1]))
    for i in range(N-1):
        cost.remove(max(cost))
    
    print(sum(cost))
else:
    print(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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)]
if N>=M: print(0)
else:
  for i in range(N-1):
    dist.remove(max(dist))
  print(sum(dist))

Submission status: Time Limit Exceeded

Solution code:
#C-117
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if n >= m:
  print(0)
elif n == 1:
  print(max(x) - min(x))
else:
  x.sort()
  x_diff = []
  for mi in range(m-1):
    x_diff.append(x[mi+1]-x[mi])
  #print(x_diff)
  split_list = []
  for ni in range(n-1):
    split_idx = x_diff.index(max(x_diff))
    split_list.append(split_idx)
    x_diff[split_idx] = 0
  split_list.sort()
  #print(split_list)
  #print(x)
  ans = 0
  ans += x[split_list[0]] - x[0]
  #print("ans",ans)
  for ni in range(n-2):
    l = split_list[ni]+1
    r = split_list[ni+1]
    ans += x[r] - x[l]
    #print("ans",ans)
  ans += x[-1] - x[split_list[-1]+1]
  print(ans)

Submission status: Time Limit Exceeded

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 M <= N:
    print(0)
    sys.exit()

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

pos = sorted(pos, key=lambda x:(x[1]))

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


for i in range(1, N):
    total = total - pos[i][1]

print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

x = []
for i in range(M):
    j = X[i]-min(X)
    x.append(j)
x =sorted(x)

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

z = 0
for i in range(len(y)-N+1):
    z += y[i]
print(z)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()

devide_num = min(N -1, M - 1)

devides = combinations(range(len(X)), devide_num)


mincost = X[-1] - X[0]
for devide in devides:
    previous = 0
    cost = 0
    for next_point in devide:
        cost += X[next_point] - X[previous]
        if next_point + 1 < M:
            previous = next_point + 1
        else:
            break
    # last
    cost += X[-1] - X[previous]

    mincost = min(cost, mincost)

print(mincost)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    if d == []:
        break
    else:
        d.remove(max(d))
ans = 0
for i in range(len(d)):
    ans += d[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()
x_d = list()

if n > m:
    print(0)
else:
    if m == 1:
        print(0)
    else:
#         for i in range(m-1):
#             x_d.append(x[i+1] - x[i])   
        x_d = list(np.diff(x))

        for i in range(n-1):
            
            x_d.remove(max(x_d))
            

        sumd = sum(x_d)  
        print(sumd)

Submission status: Time Limit Exceeded

Solution code:
n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()
x_d = list()

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

        for i in range(n-1):
            
            x_d.remove(max(x_d))
            

        sumd = sum(x_d)  
        print(sumd)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
N, M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()

X_list = list(np.diff(X))

if M-N < 1:
    print(0)
    sys.exit()

for _ in range(N-1):
    X_list.remove(max(X_list))
    
print(sum(X_list))

Submission status: Time Limit Exceeded

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

X_list = []
for i in range (len(X)-1):
    X_list.append(X[i+1]-X[i])

if M-N < 1:
    print(0)
    sys.exit()

for _ in range(N-1):
    X_list.remove(max(X_list))
    
print(sum(X_list))

Submission status: Time Limit Exceeded

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

if N < M : 

    X = sorted(list(map(int, input().split())))
    
    move = []
    
    for i in range(len(X)-1) : 
        move.append(X[i+1] -X[i])
        
    # print(move)
    
    for i in range(N-1) : 
        move.pop(move.index(max(move)))
    
    # print(move)
    
    sum = 0
    
    for i in move : 
        sum += i

else : 
    sum = 0
    
print(sum)

Submission status: Time Limit Exceeded

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

if N < M : 

    X = list(map(int, input().split()))
    
    X = sorted(X)
    
    move = []
    
    for i in range(len(X)-1) : 
        move.append(X[i+1] -X[i])
        
    # print(move)
    
    for i in range(N-1) : 
        move.pop(move.index(max(move)))
    
    # print(move)
    
    sum = 0
    
    for i in move : 
        sum += i

else : 
    sum = 0
    
print(sum)

Submission status: Time Limit Exceeded

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

X.sort()
dX,dX1 = [],[]
for i in range(M-1):
  dX.append(X[i+1]-X[i])
  dX1.append(X[i+1]-X[i])
seppoint = []

  
for i in range(N-1):
  dXmax = [j for j,v in enumerate(dX) if v == max(dX)]
  if len(dXmax) > 1: 
    sumdist0 = 2*10**5
    for k in dXmax:
      dXtmp = dX[:k] + dX[k+1:]
      sumdist1 = (sum(dXtmp))
      if sumdist0 > sumdist1:
        sumdist0 = sumdist1
        dX = dXtmp
        seppoint.append(k)
  else:
    dX = dX[:dXmax[0]] + dX[dXmax[0]+1:]
    seppoint.append(dXmax[0])
print(sum(dX))
#seppoint.sort()
#print(seppoint)
#for i in seppoint:
# dX1[i] = -1
#print(dX1)
#anslist = []
#tmpans = 0

#for i in range(len(dX1)):
#  if dX1[i] == -1:
#    anslist.append(tmpans)
#    tmpans = 0
#  else:
 #   tmpans += dX1[i]
 #   if i == len(dX1)-1:
 #     anslist.append(tmpans)
 #    tmpans = 0
 # print(anslist)
    
#print(max(anslist))

Submission status: Time Limit Exceeded

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

points = X[:]
points.sort()

if M<=N:
    ans = 0
else:
    import sys
    INT_MAX = sys.maxsize
    ans = INT_MAX

    import itertools
    for v in itertools.combinations(list(range(1, M)),N-1):
        v = [0] + list(v)
        #v.insert(0,0)
        v.append(M)
        #print(v)
        cost_sum = 0
        for i in range(1, len(v)):
            sub_p = points[v[i-1]:v[i]]
            cost = sub_p[-1] - sub_p[0]
            #print(sub_p, cost)
            cost_sum += cost
            if cost_sum >= ans:
                break
        ans = min(ans, cost_sum)

print(ans)

Submission status: Time Limit Exceeded

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

points = X[:]
points.sort()

if M<=N:
    ans = 0
else:
    import sys
    INT_MAX = sys.maxsize
    ans = INT_MAX

    import itertools
    for v in itertools.combinations(list(range(1, M)),N-1):
        v = list(v)
        v.insert(0,0)
        v.append(M)
        #print(v)
        cost_sum = 0
        for i in range(1, len(v)):
            sub_p = points[v[i-1]:v[i]]
            cost = sub_p[-1] - sub_p[0]
            #print(sub_p, cost)
            cost_sum += cost
            if cost_sum >= ans:
                break
        ans = min(ans, cost_sum)

print(ans)

Submission status: Time Limit Exceeded

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

points = X[:]
rem = N
points.sort()

if M<=N:
    ans = 0
else:
    import queue
    q = queue.Queue()
    q.put([[] for _ in range(N)])

    import sys
    INT_MAX = sys.maxsize
    ans = INT_MAX

    import itertools
    for v in itertools.combinations(list(range(1, M)),N-1):
        v = list(v)
        v.insert(0,0)
        v.append(M)
        #print(v)
        cost_sum = 0
        for i in range(1, len(v)):
            sub_p = points[v[i-1]:v[i]]
            cost = sub_p[-1] - sub_p[0]
            #print(sub_p, cost)
            cost_sum += cost
            if cost_sum >= ans:
                break
        ans = min(ans, cost_sum)

print(ans)

Submission status: Time Limit Exceeded

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

points = X[:]
rem = N
points.sort()

if M<=N:
    ans = 0
else:
    import queue
    q = queue.Queue()
    q.put([[] for _ in range(N)])

    import sys
    INT_MAX = sys.maxsize
    ans = INT_MAX

    import itertools
    for v in itertools.combinations(list(range(1, M)),N-1):
        v = list(v)
        v.insert(0,0)
        v.append(M)
        #print(v)
        cost_sum = 0
        for i in range(1, len(v)):
            sub_p = points[v[i-1]:v[i]]
            cost = sub_p[-1] - sub_p[0]
            #print(sub_p, cost)
            cost_sum += cost
        ans = min(ans, cost_sum)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
if n < m:
    distance = []
    for i in range(m-1):
        distance.append(x[i + 1] - x[i])
    for _ in range(n - 1):
        distance.remove(max(distance))
    print(sum(distance))
else:
    print(0)

Submission status: Time Limit Exceeded

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

X.sort()

Y = []
for i in range(M-1):
  Y.append(abs(X[i]-X[i+1]))
  Y.sort()
  
res=0
for i in range(M-N):
  res=res+Y[i]

print(res)

Submission status: Time Limit Exceeded

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

X.sort()
X_subtraction = []
for i in range(len(X)-1):
    X_subtraction.append(X[i+1] - X[i])

for _ in range(N-1):
    try:
        X_subtraction.remove(max(X_subtraction))
    except:
        break

print(sum(X_subtraction))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
li = na()
ls = []
if len(li) - 1 < n - 1:
    print(0)
else:
    list.sort(li)
    for i in range(len(li) - 1):
        ls.append(li[i + 1] - li[i])
    for j in range(n - 1):
        ls.remove(max(ls))
    print(sum(ls))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
li = na()
list.sort(li)
ls = []
if len(li) - 1 < n - 1:
    print(0)
else:
    for i in range(len(li) - 1):
        ls.append(li[i + 1] - li[i])
    for j in range(n - 1):
        ls.remove(max(ls))
    print(sum(ls))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
li = na()
list.sort(li)
ls = []
for i in range(len(li)-1):
    ls.append(li[i+1]-li[i])
if len(ls) < n - 1:
    print(0)
else:
    for j in range(n - 1):
        ls.remove(max(ls))
    print(sum(ls))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
li = na()
list.sort(li)
ls = []
for i in range(len(li)-1):
    ls.append(li[i+1]-li[i])
if len(ls) < n - 1:
    print(0)
else:
    for j in range(n - 1):
        if len(ls) > 0:
            ls.remove(max(ls))
    print(sum(ls))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
li = na()
list.sort(li)
ls = []
for i in range(len(li)-1):
    ls.append(li[i+1]-li[i])
for j in range(n - 1):
    if len(ls) > 0:
        ls.remove(max(ls))
print(sum(ls))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = map(int,input().split())
target_list = np.array([int(i) for i in input().split()])
if N>M:
    print(0)
else:
    target_list = np.sort(target_list)
    diff_list = target_list[1:] - target_list[:-1]
    for i in range(0, N-1):
        diff_list = np.delete(diff_list, np.argmax(diff_list))
    print(np.sum(diff_list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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-1)]
    X.sort()
    for _ in range(1,M):        
        flag[_-1] += X[_] - X[_-1]
    for lp in range(N-1):
        ff = flag.index(max(flag))
        flag.pop(ff)
    print(sum(flag))

Submission status: Time Limit Exceeded

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 M <= N:
    print(0)
    sys.exit()

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

pos = sorted(pos, key=lambda x:(x[1]))

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



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: Time Limit Exceeded

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 M <= N:
    print(0)
    sys.exit()

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


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



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: Time Limit Exceeded

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(' '))))
 
if N >= M:
    print(0)
elif N == 1:
    print(A[-1] - A[0])
else:
    indices = [0, M]
    
    min_cost = 1e10
    min_i = -1
    for i in range(1, M):
        cost = abs(A[i - 1] - A[0]) + abs(A[M-1] - A[i])
        if cost < min_cost:
            min_cost = cost
            min_i = i
    diff_min_key = [A[-1] - A[0] - min_cost]
    diff_min_indices = [[0, min_i, M]]
    
    for _ in range(N-1):
 
        range_min, min_i, range_max = diff_min_indices[-1]
 
        diff_min_key.pop(-1)
        diff_min_indices.pop(-1)
 
        if abs(A[range_max-1] - A[min_i]) > 0:
            min_cost = 1e10
            min_i2 = -1
            for i in range(min_i + 1, range_max):
                cost = abs(A[i - 1] - A[min_i]) + abs(A[range_max-1] - A[i])
                if cost < min_cost:
                    min_cost = cost
                    min_i2 = i
            down_cost = A[range_max - 1] - A[min_i] - min_cost
            #if min_i != - 1:
            insert_index = bisect.bisect_left(diff_min_key, down_cost)
            diff_min_key.insert(insert_index, down_cost)
            diff_min_indices.insert(insert_index, [min_i, min_i2, range_max])
        
        if abs(A[min_i-1] - A[range_min]) > 0:
            min_cost = 1e10
            min_i2 = -1
            for i in range(range_min + 1, min_i):
                cost = abs(A[i - 1] - A[range_min]) + abs(A[min_i-1] - A[i])
                if cost < min_cost:
                    min_cost = cost
                    min_i2 = i
            down_cost = A[min_i - 1] - A[range_min] - min_cost
            #if min_i2 != - 1:
            insert_index = bisect.bisect_left(diff_min_key, down_cost)
            diff_min_key.insert(insert_index, down_cost)
            diff_min_indices.insert(insert_index, [range_min, min_i2, min_i])
           
        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: Time Limit Exceeded

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

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

        range_min, min_i, range_max = diff_min_indices[-1]

        diff_min_key.pop(-1)
        diff_min_indices.pop(-1)

        if abs(A[range_max-1] - A[min_i]) > 0:
            min_cost = 1e10
            min_i2 = -1
            for i in range(min_i + 1, range_max):
                cost = abs(A[i - 1] - A[min_i]) + abs(A[range_max-1] - A[i])
                if cost < min_cost:
                    min_cost = cost
                    min_i2 = i
            down_cost = A[range_max - 1] - A[min_i] - min_cost
            if min_i != - 1:
                insert_index = bisect.bisect_left(diff_min_key, down_cost)
                diff_min_key.insert(insert_index, down_cost)
                diff_min_indices.insert(insert_index, [min_i, min_i2, range_max])
        
        if abs(A[min_i-1] - A[range_min]) > 0:
            min_cost = 1e10
            min_i2 = -1
            for i in range(range_min + 1, min_i):
                cost = abs(A[i - 1] - A[range_min]) + abs(A[min_i-1] - A[i])
                if cost < min_cost:
                    min_cost = cost
                    min_i2 = i
            down_cost = A[min_i - 1] - A[range_min] - min_cost
            if min_i2 != - 1:
                insert_index = bisect.bisect_left(diff_min_key, down_cost)
                diff_min_key.insert(insert_index, down_cost)
                diff_min_indices.insert(insert_index, [range_min, min_i2, min_i])
           
        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: Time Limit Exceeded

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])
for i in range( (min(N,M)-1) ):
    sa.pop(sa.index(max(sa)))
print(sum(sa))

Submission status: Time Limit Exceeded

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

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

if N >= M:
    print(0)
else:
    min_cost = 1e10
    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
         
    print(min_cost)

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
L = [X[i+1] - X[i] for i in range(len(X)-1)]
k = 0
while k < N-1 and L != []:
    L.remove(max(L))
    k += 1
print(sum(L) if X != [] else 0)

Submission status: Time Limit Exceeded

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]

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: Time Limit Exceeded

Solution code:
import numpy as np
N, M = map(int,input().split())
P = list(map(int,input().split()))
P.sort()
Q = [0] + P[:len(P)-1]
P = np.array(P)
Q = np.array(Q)
R = P-Q
R[0] = 0
for i in range(1,N):
    index = np.argmax(R)
    R[index] = 0
print(sum(R))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    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: Time Limit Exceeded

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

import numpy as np
if n >= m:
  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: Time Limit Exceeded

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

x = list(map(int, input().split()))
x.sort()
if n >= m:
    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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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):
	if Y != []:
		x = max(Y)
		Y.remove(x)
	else:
		break

print(sum(Y))

Submission status: Time Limit Exceeded

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