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:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect

setrecursionlimit(10**7)

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

X.sort()

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

l.sort()
l=l[:N+1]
print(sum(l))

Submission status: Wrong Answer

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

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

# print(X_d, N-1)
print(sum(X_d[:M-N]))

Submission status: Wrong Answer

Solution code:
import numpy as np

n, m = map(int, input().split())
y = np.diff(sorted([int(i) for i in input().split()]))
print(sum(sorted(y)[:m-n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    xs = sorted(map(int, input().split()))
    dists = sorted(xs[i] - xs[i - 1] for i in range(1, len(xs)))
    print(sum(dists[: (-n + 1)]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

# print(X_d, N-1)
print(sum(X_d[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
from statistics import mean, median,variance,stdev
import sys
import math
import fractions

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

#n = ip()                              #入力整数1つ
n,m= (int(i) for i in input().split())      #入力整数横2つ
#x,y,z = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列

#jの変数はしようできないので注意

if m == 1 or n==m:
    print(0)
    exit(0)

a.sort()
b =[]
for i in range(m-1):
    b.append(a[i+1]-a[i])
b.sort()
print(sum(b[:m-n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M = map(int, raw_input().split())
X = map(int, raw_input().split())
X.sort()
Y = [X[i+1]-X[i] for i in range(M-1)]
Y.sort()
print sum(Y[0:M-N])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
Y = []
for a,b in zip(X,X[1:]):
    Y.append(b-a)
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 = list(sorted(x))

y = [x[i] - x[i - 1] for i in range(1, M)]
y = list(sorted(y))
print(sum(y[:-(N - 1)]))

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

n, m = getNM()
point = getList()
point.sort()
diff = []
for i,j in zip(point, point[1:]):
    diff.append(abs(i-j))

diff.sort()

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = sorted([int(a) for a in input().split()])
Y = []
for i in range(M-1):
    Y.append(X[i+1]-X[i])

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

Submission status: Wrong Answer

Solution code:
N,M=[int(i) for i in input().split(" ")]
X=[int(i) for i in input().split(" ")]
X=sorted(X)
delta=[abs(X[i+1]-X[i]) for i in range(M-1)]
delta=sorted(delta)
print(sum(delta[:M-N:]))

Submission status: Wrong Answer

Solution code:
def inpl():
    return list(map(int, input().split()))


N, M = inpl()
X = inpl()
X.sort()
absX = [0] * (M - 1)
for i, xx in enumerate(zip(X, X[1:])):
    absX[i] = xx[1] - xx[0]
absX.sort()
print(sum(absX[:N + 1]))

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)
  list = []
  for i in range(M-1):
    list.append(X[i+1]-X[i])
    list = sorted(list)
    list1 = list[:M-N]
  print(sum(list1))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
x = sorted(LIST())
ans = math.inf
v = [0]*n

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

for v[1:n] in itertools.combinations(range(1,m),n-1):
    tmp = 0
    for i in range(n):
        if(i<n-1):
            tmp += x[v[i+1]-1]-x[v[i]]
        else:
            tmp += x[m-1]-x[v[i]]
    ans = min(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

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

diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort(reverse=True)
diff = diff[:N-1]

    
if N >= M:
    print(0)
elif N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)

Submission status: Time Limit Exceeded

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

diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff = diff[:N-1]

if N >= M:
    print(0)
elif N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
x = sorted([int(x) for x in input().split()])
diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff = diff[:N-1]


if N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import *

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

if n>=m:
    print(0)
else:
    for i in range(n-1):
        dis.remove(max(dis))
    print(sum(dis))

Submission status: Time Limit Exceeded

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    n,m = map(int, stdin.readline().rstrip().split())
    x = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if n>m:
        print(0)
        exit()
    spl = n-1
    sa = [0]*(m-1)
    x.sort()
    for i in range(m-1):
        sa[i] = x[i+1] - x[i]
    for i in range(spl):
        sa.remove(max(sa))
    print(sum(sa))



main()

Submission status: Time Limit Exceeded

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    n,m = map(int, stdin.readline().rstrip().split())
    x = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if n>m:
        print(0)
        exit()
    spl = n-1
    sa = [0]*(m-1)
    x.sort()
    for i in range(m-1):
        sa[i] = x[i+1] - x[i]
    for i in range(spl):
        sa.pop(sa.index(max(sa)))
    print(sum(sa))



main()

Submission status: Time Limit Exceeded

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    n,m = map(int, stdin.readline().rstrip().split())
    x = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if n>m:
        print(0)
        exit()
    spl = n-1
    sa = []
    x.sort()
    for i in range(m-1):
        sa.append(x[i+1] - x[i])
    for i in range(spl):
        sa.pop(sa.index(max(sa)))
    print(sum(sa))



main()

Submission status: Time Limit Exceeded

Solution code:
N,A = list(map(int,input().split()))
listA = list(map(int,input().split()))
listA.sort()
listB = []
ans = 0
if N >= len(listA):
    print(0)
    exit()
for i in range(A-1):
    listB.append(listA[i+1]-listA[i])
ans = sum(listB)
for i in range(N-1):
    M = max(listB)
    ans -= M
    listB.remove(M)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,A = list(map(int,input().split()))
listA = list(map(int,input().split()))
listA.sort()
listB = []
if N >= len(listA):
    print(0)
    exit()
for i in range(A-1):
    listB.append(listA[i+1]-listA[i])
for i in range(N-1):
    listB.remove(max(listB))
print(sum(listB))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def kansu(N,M,X):

    x_abs = [X[m+1] -X[m] for m in range(M-1)]    
#    print(x_abs)
    cnt_n = N
    
    while cnt_n-1 > 0:
        x_abs[x_abs.index(max(x_abs))]  = 0
        cnt_n -= 1

    koma_max = 0
    for m in range(M-1):
        koma_max += x_abs[m]

    print(koma_max)


N,M,X = nyu()
#print(X)

if M == 1:
    print(0)
else:
    kansu(N,M,X)

#print(S)

Submission status: Time Limit Exceeded

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


def kansu(N,M,X):

    x_abs = [X[m+1] -X[m] for m in range(M-1)]    
#    print(x_abs)
    cnt_n = N
    
    while cnt_n-1 > 0:
        x_abs[x_abs.index(max(x_abs))]  = -1
        cnt_n -= 1

    koma_max = 0
    sum = 0
    for m in range(M-1):
        if x_abs[m] != -1:
            koma_max += x_abs[m]

    print(koma_max)

 


N,M,X = nyu()
#print(X)

if M == 1:
    print(0)
else:
    kansu(N,M,X)

#print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

xlist.sort()

diflist=[]

for i in range(m-1):
    diflist.append(xlist[i+1]-xlist[i])

    diflist.sort()

if n >= m:
    print(0)
else:
    print(sum(diflist[:(m-n)]))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
C = []
if m <= n:
	print(0)
	exit()
for i in range(1, m):
	C.append(abs(X[i] - X[i-1]))
for i in range(n-1):
	t = max(C)
	C[C.index(t)] = 0
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N, M = LI()
    X = LI()
    X = sorted(X)
    diff = []
    for i in range(M-1):
        d = X[i+1] - X[i]
        diff.append(d)
    diff = sorted(diff)
    while N - 1> 0 and len(diff) > 0:
        diff = diff[:-1]
        N -= 1
    ans = sum(diff)
    print(ans)
    '''
    X = sorted(X)
    min_dist = [0] * M
    if N == 1:
        return X[-1] - X[0]
    print(X)
    for i in range(1, M-1):
        min_dist[i] = min(abs(X[i] - X[i-1]), abs(X[i+1] - X[i]))
        print(min_dist)
    min_dist = sorted(min_dist)[::-1]
    print(min_dist)
    for i in range(N-2):
        if len(min_dist) > 0:
            min_dist.pop(-1)
    print(sum(min_dist))
    '''
main()

Submission status: Time Limit Exceeded

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

if len(diffs) != 0:
    for i in range(N-1):
        diffs[diffs.index(max(diffs))] = 0

print(sum(diffs))

Submission status: Time Limit Exceeded

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

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [X[i + 1] - X[i] for i in range(len(X) - 1)]
    for _ in range(N - 1):
        x_diff.remove(max(x_diff))
    ans = sum(x_diff)

print(ans)

Submission status: Time Limit Exceeded

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

diff = []
for i in range(m-1):
    diff.append(abs(x[i]-x[i+1]))
partition = sorted(diff, reverse=True)[:(n-1)]

ans = 0
for _d in diff:
    if _d in partition:
        partition.remove(_d)
        continue

    ans += _d

print(ans)

Submission status: Time Limit Exceeded

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

def func(input_list):
    if n > m:
        return 0

    input_list.sort()

    input_diff = []
    for i in range(m-1):
        diff = input_list[i+1] - input_list[i]
        input_diff.append(abs(diff))

    choice_pivot = [0]
    for i in range(n-1):
        pivot = max(input_diff)
        index = input_diff.index(pivot)
        input_diff[index] = 0
        choice_pivot.append(index+1)

    ans = 0
    choice_pivot.append(m)
    for i in range(len(choice_pivot)-1):
        start = choice_pivot[i]
        end = choice_pivot[i+1] - 1
        ans += input_list[end] - input_list[start]

    return ans

print(func(input_list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
dp=[[0 for i in range(m)]for i in range(m)]
s=[a[i]-a[i-1] if i!=0 else 0 for i in range(1,m) ]
s.sort()
if n!=1:
    print(sum(s[:-n+1]))
else:
    print(sum(s))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
arr = [abs(x[i] - x[i + 1]) for i in range(m-1)]
for _ in range(n - 1):
    if len(arr) <= 0:
        break
    arr.remove(max(arr))

print(sum(arr))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
arr = [abs(x[i] - x[i + 1]) for i in range(m-1)]
for _ in range(n - 1):
    if len(arr) <= 0:
        break
    arr.remove(max(arr))

print(sum(arr))

Submission status: Time Limit Exceeded

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

print(sum(arr))

Submission status: Time Limit Exceeded

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

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

l = sorted(cost)
ind = [0]
for i in range(n-1):
    j = cost.index(l[i])
    ind.append(j+ 1)
    cost[j] = 0
    
ind.append(m)
ind.sort()

X = []
for i in range(n):
    X.append(x[ind[i]:ind[i+1]])
    
ans = 0
for i in X:
    ans += i[-1] - i[0]
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

A=sorted(A)
B=[0]*(M-1)

for i in range(len(A)-1):
    B[i]=A[i+1]-A[i]

for i in range(N-1):
    if(len(B)==0):
        print("0")
        exit()
    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.sort(reverse=True)
diff = []
for i in range(len(X) - 1):
    diff.append(X[i] - X[i + 1])
for _ in range(N - 1):
    if not diff:
        print("0")
        exit()
    else:
        diff.remove(max(diff))
print(sum(diff))

Submission status: Time Limit Exceeded

Solution code:
n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print(0)
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    for _ in range(n-1): dis.remove(max(dis))
    print(sum(dis))

Submission status: Time Limit Exceeded

Solution code:
def main():
    # ABC117c
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)

    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    if(m == 1):
        print(0)
        exit(0)
    if(n >= m):
        print(0)
        exit(0)
    x.sort()
    sa = [0]
    for i in range(1, m):
        sa.append(x[i]-x[i-1])
    # print(x)
    # print(sa)
    # print('aa')
    sa.sort()
    for _ in range(min(n-1, len(sa)-1)):
        sa = sa[:-1]
    # print(sa)
    print(sum(sa))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# ABC117c
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
sa = [0]
for i in range(1, m):
    sa.append(x[i]-x[i-1])
# print(x)
# print(sa)
# print('aa')
sa.sort()
for _ in range(min(n-1, len(sa)-1)):
    sa = sa[:-1]
# print(sa)
print(sum(sa))

Submission status: Time Limit Exceeded

Solution code:
# ABC117c
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
sa = [0]
for i in range(1, m):
    sa.append(x[i]-x[i-1])
# print(x)
# print(sa)
# print('aa')

for _ in range(min(n-1, len(sa)-1)):
    sa.remove(max(sa))
# print(sa)
print(sum(sa))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
x = sorted(list(map(int, input().split())))
l = [0] * (m-1)
ans = 0
if n < m:
  for i in range(m-1):
    l[i] = x[i+1] - x[i]
  for _ in range(m-n):
    ans += min(l)
    l.pop(l.index(min(l)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
x = list(sorted(map(int, input().split())))
l = [0] * (m-1)
ans = 0
if n < m:
  for i in range(m-1):
    l[i] = x[i+1] - x[i]
  for _ in range(m-n):
    ans += min(l)
    l.pop(l.index(min(l)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
l = [0] * (m-1)
ans = 0
if n < m:
  for i in range(m-1):
    l[i] = x[i+1] - x[i]
  for i in range(m-n):
    ans += min(l)
    l.pop(l.index(min(l)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import bisect
import copy

def move(positions, tmp_X, N, tmp_ans):
  min_dif = float('inf')
  min_idx = None
  min_x = None
  for x in tmp_X:
    idx = bisect.bisect_left(positions,x)
    if idx == N:
      tmp_dif = abs(x-positions[idx-1])
      tmp_idx = idx -1
    elif idx == 0:
      tmp_dif = abs(x-positions[idx])
      tmp_idx = idx
    else:
      dif1, dif2 = abs(x-positions[idx-1]), abs(x-positions[idx])
      if dif1 >= dif2:
        tmp_dif = dif2
        tmp_idx = idx
      else:
        tmp_dif = dif1
        tmp_idx = idx - 1

    if min_dif > tmp_dif:
      min_dif = tmp_dif
      min_idx = tmp_idx
      min_x = x
  
  del positions[min_idx]
  idx = bisect.bisect_left(positions, min_x)
  positions.insert(idx, min_x)

  tmp_X.remove(min_x)
  return tmp_ans + min_dif

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

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

ans = float('inf')
for cmb in combinations(X,N):
  tmp_ans = 0
  tmp_X = copy.deepcopy(X)
  positions = sorted(list(cmb))
  for position in positions: tmp_X.remove(position)
  
  while len(tmp_X) > 0:
    tmp_ans = move(positions, tmp_X, N, tmp_ans)
  ans = min(tmp_ans, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import bisect
import copy

def move(positions, tmp_X, N, tmp_ans):
  min_dif = float('inf')
  min_idx = None
  min_x = None
  for x in tmp_X:
    idx = bisect.bisect_left(positions,x)
    if idx == N:
      tmp_dif = abs(x-positions[idx-1])
      tmp_idx = idx -1
    elif idx == 0:
      tmp_dif = abs(x-positions[idx])
      tmp_idx = idx
    else:
      dif1, dif2 = abs(x-positions[idx-1]), abs(x-positions[idx])
      if dif1 >= dif2:
        tmp_dif = dif2
        tmp_idx = idx
      else:
        tmp_dif = dif1
        tmp_idx = idx - 1

    if min_dif > tmp_dif:
      min_dif = tmp_dif
      min_idx = tmp_idx
      min_x = x
  
  positions[min_idx] = min_x
  positions.sort()
  tmp_X.remove(min_x)
  return tmp_ans + min_dif

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

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

ans = float('inf')
for cmb in combinations(X,N):
  tmp_ans = 0
  tmp_X = copy.deepcopy(X)
  positions = sorted(list(cmb))
  for position in positions: tmp_X.remove(position)
  
  while len(tmp_X) > 0:
    tmp_ans = move(positions, tmp_X, N, tmp_ans)
  ans = min(tmp_ans, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
if m <= n:
  print(0)
else:
  Dif_X = []
  for i in range(m-1):
    Dif_X += [X[i+1]-X[i]]
  count = 0  
  while count < n-1:
    Dif_X[Dif_X.index(max(Dif_X))] = 0
    count += 1
  print(sum(Dif_X))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
if m <= n:
  print(0)
else:
  Dif_X = []
  for i in range(m-1):
    Dif_X += [X[i+1]-X[i]]
  while Dif_X.count(0)<n-1:
    Dif_X[Dif_X.index(max(Dif_X))] = 0
  print(sum(Dif_X))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
x = sorted(X)
i = 0
diff = []
while i + 1 < len(x):
    diff.append(x[i] - x[i + 1])
    i += 1
d = sorted(diff)[:N - 1]
i = 0
sum = 0
for _ in x:
    if i + 1 < len(x):
        if x[i] - x[i + 1] in d:
            d.remove(x[i] - x[i + 1])
        else:
            sum = sum + abs(x[i] - x[i + 1])
    i += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
t = sorted(X, reverse=True)
h = [0] * M
for i in range(1, M):
    h[i] = abs(t[i - 1] - t[i])

for i in range(N - 1):
    tmp = h.index(max(h))
    h[tmp] = 0
print(sum(h))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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