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:
n,m=map(int, input().split())
x=sorted(map(int, input().split()))
diff=sorted([x[i]-x[i-1] for i in range(1, m)])
print(sum(diff[:m-n]))

Submission status: Wrong Answer

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

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

path_cnt = sorted([abs(X[i] - X[i + 1]) for i in range(M - 1)])
# print(path_cnt)
print(sum(path_cnt[:-N + 1]))

Submission status: Wrong Answer

Solution code:
import itertools 

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

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

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


X.sort()
index = range(1,M)

anslist = []
for c in itertools.combinations(index, N-1):
    ans = X[c[0]-1] - X[0]
    for n in range(N-1):
        if n == N-2:
            ans += X[M-1] - X[c[n]]
        else:
            ans += X[c[n+1]-1] - X[c[n]]
    anslist.append(ans)


print(min(anslist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = sorted(map(int,input().split()))
if X[0] < 0:
    X[0] *= -1
for i in range(1,M):
    X[i] = X[0]+X[i]
X[0] = 0
mean = max(X)/N
ans = 0
for i in range(len(X)):
    if N == 0:
        break
    for j in range(i+1,len(X)):
        if N == 0:
            break
        if X[j]-X[i] >= mean:
            ans = X[j-1]-X[i]
            N -= 1
            break
        mean = (max(X)-X[i])//N
        for j in range(i+1,len(X)):
            if X[j]-X[i] >= mean and N != 0:
                ans = X[j-1]-X[i]
                N -= 1
                break
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
l = [0] * (M - 1)
for i in range(M - 1):
    l[i] = x[i + 1] - x[i]
l.sort()
print(x[-1] - x[0] - sum(l[-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)
    exit(0)

x.sort()

d = [x[i+1] - x[i] for i in range(m-1)]
d.sort()

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

Submission status: Wrong Answer

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])
    distance.sort()
    print(sum(distance[0:-n+1]))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
def main():
	n, m = map(int, input().split())
	l = list(map(int, input().split()))
	if m <= n:
		print(0)
		return
	l.sort()
	dif = [l[i+1]-l[i] for i in range(m-1)]
	dif.sort()

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
	m, n = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	dif = [l[i]-l[i-1] for i in range(1, n)]
	dif.sort()
	if n <= m:
		print(0)
	else:
		print(sum(dif[:-m+1]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
	m, n = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	dif = [l[i]-l[i-1] for i in range(1, n)]
	dif.sort()

	print(sum(dif[:-m+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()
dis = [X[i+1]-X[i] for i in range(M-1)]
dis.sort()
print(sum(dis[:-N+1]))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
x = [int(i) for i in input().split()]
x.sort()
xx = []
for i in range(len(x)-1):
  xx.append(abs(x[i]-x[i+1]))
xx.sort()
print(sum(xx[:-(n-1)])if n!=1 else 0)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
x = [int(i) for i in input().split()]
x.sort()
xx = []
for i in range(len(x)-1):
  xx.append(abs(x[i]-x[i+1]))
xx.sort()
print(sum(xx[:-(n-1)])if n!=0 else 0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < len(X_subtraction):
    for _ in range(N-1):
        X_subtraction.remove(max(X_subtraction))

print(sum(X_subtraction))

Submission status: Wrong Answer

Solution code:
import sys

N,M=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
a=[x[i+1]-x[i] for i in range(M-1)]

if N>=M:
    print(0)
    sys.exit()
    
print(sum(a[:M-N]))

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()
    X_sub = []
    for i in range(len(X)-1):
        X_sub.append(X[i+1]-X[i])
    X_sub.sort()
    for i in range(N-1):
        del X_sub[-1]
        print(sum(X_sub))

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()
    X_sub = []
    for i in range(len(X)-1):
        X_sub.append(X[i+1]-X[i])
        X_sub.sort()
    for i in range(N-1):
        del X_sub[-1]
        print(sum(X_sub))

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()
    X_sub = []
    for i in range(len(X)-1):
        X_sub.append(X[i+1]-X[i])
        X_sub.sort()
    for i in range(N-1):
        del X_sub[-1]
        print(sum(X_sub))

Submission status: Wrong Answer

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

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

print(sum(sub[:m-2-(n-1) + 1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

kyori.sort()
b=sum(kyori[:m-n])
print(b)

Submission status: Wrong Answer

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

a.sort()
start1=a[0]
start2=a[m-1]

centre=(start1+start2)/2
a.append(centre)
a.sort()
c=a.index(centre)
cent_left=a[c-1]
cent_right=a[c+1]

print(abs(cent_left-start1) + abs(start2-cent_right))

Submission status: Wrong Answer

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 = []
list.sort(li)
for i in range(len(li) - 1):
    ls.append(li[i + 1] - li[i])
list.sort(ls)
print(sum(ls[:-(n-1)]))

Submission status: Wrong Answer

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])
    list.sort(ls)
    if n - 1 > 0:
        print(sum(ls[:-(n-1)]))
    else:
        print(0)

Submission status: Wrong Answer

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])
    list.sort(ls)
    print(ls)
    print(ls[:-(n-1)])
    if n - 1 > 0:
        print(sum(ls[:-(n-1)]))
    else:
        print(0)

Submission status: Wrong Answer

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])
    list.sort(ls)
    print(sum(ls[:-(n-1)]))

Submission status: Wrong Answer

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: Wrong Answer

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

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

x.sort()

print(x)

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

aida.sort()
aida.reverse()

del aida[:n-1]

print(sum(aida))

Submission status: Wrong Answer

Solution code:
NM = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
X.sort()
if(NM[1] <= NM[0]):
    print(0)
elif(NM[0] == 1):
    print(X[-1] - X[0])

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=sorted(list(map(int,input().split())))
l1=[]
l2=0
if n>=m:
  print(0)
else:
  for i in range(m-1):
    l1.append(l[i+1]-l[i])
  for i in range(m-n):
    l2+=l1[i]
  print(l2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

X.sort() # O(MlogM)

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

D.sort() # O(MlogM)

print(sum(D[:M-N+2])) # O(M)

Submission status: Wrong Answer

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

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

X.sort() # O(MlogM)

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

D.sort() # O(MlogM)

print(sum(D[:M-N+1])) # O(M)

Submission status: Wrong Answer

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

X = sorted([X[i+1]-X[i] for i in range(M-1)])
ans = sum(X[:-(N-1)])
if M <= N:
  ans = 0
print(ans)

Submission status: Wrong Answer

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

X = sorted([abs(X[i+1])-abs(X[i]) for i in range(M-1)])

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

Submission status: Wrong Answer

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

X = sorted([abs(X[i+1]-X[i]) for i in range(M-1)])

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

if N < M:
	dist = []
	for i in range(M-1):
		dist.append(abs(X[i+1] - X[i]))
	dist.sort()
	ans = sum(dist[:-N+1])
else:
	ans = 0

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if(N >= M):
  print(cost)
else:
 X = input().split()
 for i in range(M):
  X[i] = int(X[i]) 
 X.sort() 

 L = []
 for i in range(M):
  if(i != 0): 
    L.append(X[i]-X[i-1])
 L.sort()
 
 cost += X[M-1] - X[0]
 cost -= sum(L[-N+1:])
 print(cost)

Submission status: Wrong Answer

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

list.sort()

dif = [b-a for a,b in zip(list,list[1:])]
dif.sort()
print(sum(dif[:M-N]) if N - M > 0 else 0)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))

N,M = read_int()
X = read_int()
X.sort()

def solve():
    l = N // 2 + N % 2 - 1
    r = M - N // 2
    if N == 1:
        return X[r - 1] - X[l]
    elif N >= M:
        return 0
    else:
        res = 0
        while r - l > 1:
            dl = X[l + 1] - X[l]
            dr = X[r] - X[r - 1]
            if dl < dr:
                res += dl
                l += 1
            else:
                res += dr
                r -= 1
        return res

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# nコマ, m地点
def func(n, m, x):
    x.sort()
    dis = []
    for i in range(1, len(x)):
        dis.append(x[i] - x[i-1])
    dis.sort()
    n = min(n, m)
    # print("n:{} dis:{}".format(n, dis))
    print(sum(dis[:m-n]))
# for i in range(1, 10):
    # func(i, 7, [-10, -3, 0, 9, -100, 2, 17])
"""
func(3, 5, [1, 2, 10, 12, 14])
func(3, 7, [-10, -3, 0, 9, -100, 2, 17])
func(100, 1, [-100])
"""
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

Submission status: Wrong Answer

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

if N>=M:
    print(0)
else: 
    dist = [b-a for a, b  in zip(X, X[1:])]
    dist.sort()
    print(sum(dist[:-(N-1)]))

Submission status: Wrong Answer

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

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

dist = [b-a for a, b  in zip(X, X[1:])]
dist.sort()
print(sum(dist[:-(N-1)]))

Submission status: Wrong Answer

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]
    diff_list = np.sort(diff_list)
    diff_list = diff_list[0:-(N-1)]
    print(np.sum(diff_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
X = list(map(int, input().split()))

if N >= M:
	print(0)
	exit()
X.sort()
dis = [b - a for a, b in zip(X, X[1:])]
dis.sort()
print(dis)
print(sum(dis[:M-N]))

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
pieces_num, markpoints_num = map(int, input().split())
markpoints = sorted(map(int, input().split()))

distances = []
for markpoints_i in range(1, len(markpoints)):
    distances.append(markpoints[markpoints_i] - markpoints[markpoints_i - 1])
distances.sort()

print(sum(distances[:1 - pieces_num]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
x.sort()
dis = []
for i in range(1, len(x)):
    dis.append(x[i] - x[i-1])
dis.sort()
_sum = sum(dis)
n = min(n-1, m-1)
print(sum(dis[:-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.sort()
dis = []
for i in range(1, len(x)):
    dis.append(x[i] - x[i-1])
dis.sort()
print(sum(dis[:-n+1]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

x.sort()

d = [x[i] - x[i-1] for i in range(1,m)]

d.sort()

ans = sum(d[:m-n])

print(ans)

Submission status: Wrong Answer

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

X.sort()

dif_X = [X[i+1]-x for i,x in enumerate(X[:-1])]
dif_X.sort()
print(sum(dif_X[:M-N]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)
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: Wrong Answer

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

Submission status: Wrong Answer

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

import bisect
import random
import itertools
import copy

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

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 = dict({A[-1] - A[0] - min_cost: [[0, min_i, M]]})
    
    for _ in range(N-1):

        range_min, min_i, range_max = diff_min_indices[diff_min_key[-1]].pop(-1)

        #diff_min_indices[diff_min_key[-1]]
        diff_min_key.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])
                bisect.insort_left(diff_min_key, down_cost)
                if down_cost in diff_min_indices:
                    diff_min_indices[down_cost].append([min_i, min_i2, range_max])
                else:
                    diff_min_indices[down_cost] = [[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])
                bisect.insort_left(diff_min_key, down_cost)
                if down_cost in diff_min_indices:
                    diff_min_indices[down_cost].append([min_i, min_i2, range_max])
                else:
                    diff_min_indices[down_cost] = [[min_i, min_i2, range_max]]
                
        #print('key', diff_min_key)
        #print('indices', diff_min_indices)
        #print('ind', indices)
           
        indices.append(min_i)

    cost = 0
    indices = sorted(indices)
    for i in range(len(indices)-1):
        cost += A[indices[i+1] - 1] - A[indices[i]]
    print(cost)

Submission status: Wrong Answer

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

import bisect
import random
import itertools
import copy

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

#print(A)

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

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

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