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 statistics import mean
from functools import reduce
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()

width = int(len(X)/N)+1
C = [X[s:e] for s,e in [(i, i+width) for i in range(0,len(X), width)]]

while True:
  flg = True
  for i in range(len(C)):
    c = C[i]
    if i != 0 and len(c) > 1:
      if abs(c[0]-c[1]) > abs(c[0]-C[i-1][-1]):
        el = c.pop(0)
        C[i-1].append(el)
        flg = False
    if i != len(C)-1 and len(c) > 1:
      if abs(c[-1]-c[-2]) > abs(c[-1]-C[i+1][0]):
        el = c.pop(-1)
        C[i+1].insert(0, el)
        flg = False
  if flg:
    break

cnt = 0
for c in C:
  if len(c) == 1:
    continue
  cnt += sum([abs(c[i]-c[i+1]) for i in range(len(c)-1)])

print(cnt)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
num_lis = sorted(list(map(int, input().split())))
num_dif = []
ans = 0

if M == 1:
  print(0)
else:
  for n in range(M):
    if n == M-1:
      break
    n_dif = num_lis[n+1] - num_lis[n]
    n_dif = abs(n_dif)
    num_dif.append(n_dif)
  sorted_num_dif = sorted(num_dif)
  
  for i in sorted_num_dif[:-(N-1)]:
    ans += i

  print(ans)

Submission status: Wrong Answer

Solution code:
line1 = list(map(lambda x: int(x), input().split()))
N = line1[0]
M = line1[1]
X = list(map(lambda x: int(x), input().split()))

if N >= M or M == 1:
  print(0)
else:
  X.sort(key=lambda x:x, reverse=False)

  diff = list(map(lambda x: x[0]-x[1], zip(X[1:],X[:-1])))
  diff.sort(key=lambda s:s, reverse=False)

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

X.sort()

dif_X = X[1:] - X[:-1]
dif_X.sort()

print(sum(dif_X[:M-N]))

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
import numpy as np

n, m = input().split()
xs_str = input().split()
xs = list(map(int, xs_str))
xs.sort()

x1 = np.array(xs[0:-1])
x2 = np.array(xs[1:])
ys = np.abs(x2 - x1)
ys.sort()

out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

Solution code:
NM = list(map(int,input().split()))
N,M = NM[0], NM[1]
li = list(map(int,input().split()))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

xm = np.array(xm)

if n>=m:
    print(0)
else:
    xm = np.sort(xm)
    diff = xm[1:] - xm[:-1]
    diff = np.abs(diff)
    diff = np.sort(diff)
    
    print(sum(diff[:(n-m+2)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
num_lis = sorted(list(map(int, input().split())))
num_dif = []
ans = 0

if M == 1:
  print(0)
else:
  for n in range(M):
    if n == M-1:
      break
    n_dif = num_lis[n+1] - num_lis[n]
    num_dif.append(n_dif)
  sorted_num_dif = sorted(num_dif)
  
  for i in sorted_num_dif[:-(N-1)]:
    ans += i

  print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

if N >= M:
    print(0)
elif N == 1:
    print(Xs[-1]-Xs[0])
else:
    margin = [Xs[i+1]-Xs[i] for i in range(len(Xs)-1)]
    margin = [(i,x) for i,x in enumerate(margin)]
    point = [x[0] for x in sorted(margin,key=lambda x:x[1],reverse=True)[:N-1]]
    print(point)
    move = 0
    move += Xs[point[0]] - Xs[0]
    for i in range(1,len(point)):
        move += Xs[point[i]] - Xs[point[i-1]+1]
    move += Xs[-1] - Xs[point[-1]+1]
    print(move)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
sabun = []
j = 1
for i in x:
    if len(x) == 1:
        break
    sabun.append([x[j] - i,j-1])
    j += 1
    if j == m:
        break
sabun.sort(key=lambda x: x[0],reverse=True)
ans = 0
kugiri = []
for i in range(n-1):
    if len(x) == 1:
        break
    kugiri.append(sabun[i][1])
    if i == m-2:
        break
kugiri.sort()
j = 0
for i in range(n):
    if len(kugiri) == m - 1:
        break
    if len (kugiri) == 0:
        break
    if i == 0:
        ans += x[kugiri[0]]-x[0]
        #print(x[kugiri[0]]-x[0])
    elif i == n-1:
        #print(x[m-1] - x[kugiri[i-1]+1])
        ans += x[m-1] - x[kugiri[i-1]+1]
    else:
        ans += x[kugiri[i]] - x[kugiri[i-1]+1]
        #print(x[kugiri[i]] - x[kugiri[i-1]+1])
print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=a.split()
n=int(a[0])
d=b.split()
d=[float(i) for i in d]
d.sort(reverse=True)
count=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n-1):
    del ll[-1]
print(int(sum(ll)))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

xm = np.array(xm)

if n>=m:
    print(0)
else:
    xm = np.sort(xm)
    diff = xm[1:] - xm[:-1]
    diff = np.sort(diff)
    
    print(sum(np.abs(diff[:(n-m+2)])))

Submission status: Wrong Answer

Solution code:
N, M = [int(each) for each in input().split()]
corr = [int(each) for each in input().split()]
idxs = []
for i in range(N):
    idxs.append(i)
corr.sort()
dis_list = []
for i in range(len(corr)-1):
    dis_list.append((corr[i+1]-corr[i], i, i+1))
dis_list.sort(key=lambda x : x[0])
if(N >= M):
    print(0)
else:
    if(N == 1):
        ms = 0
        for each in dis_list:
            ms += each[0]
        print(ms)
    else:
        if(N % 2 == 0):
            line = int(N/2)
            ms = 0
            for i in range(len(dis_list)-line):
                ms += dis_list[i][0]
            print(ms)
        else:
            line = int(N/2)
            ms = 0
            included = []
            for i in range(len(dis_list)-line, len(dis_list)):
                included.append(dis_list[i][1])
                included.append(dis_list[i][2])
            for i in range(len(dis_list)-line):
                ms += dis_list[i][0]
            
            max_no = 0.0
            for i in range(len(dis_list)-line):
                if(dis_list[i][1] in included and dis_list[i][2] not in included):
                    max_no = dis_list[i][0]
                elif(dis_list[i][2] in included and dis_list[i][1] not in included):
                    max_no = dis_list[i][0]
                else:
                    pass
            ms -= max_no
            print(ms)

Submission status: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()
N, M = map(int, lines[0].split())
X = [int(x) for x in lines[1].split()]

X.sort()

diff = []
for i in range(0, len(X) - 1):
	diff.append(abs(X[i] - X[i+1]))

diff.sort()
print(sum(diff[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n_dices, n_points = map(int, input().split())
points = sorted(list(map(int, input().split())))
if n_dices >= n_points:
    print(0)
    sys.exit()
interval_list = [points[i + 1] - points[i] for i in range(n_points - 1)]
interval_list.sort()
print(sum(interval_list[:-n_dices+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

lx.sort()
dx = []
s = 0

for i in range(len(lx)-1):
  dx.append(lx[i+1]-lx[i])

if n == 1:
  print(lx[m-1]-lx[0])
elif m <= n:
  print(0)
else:
  dx.sort()
  print(dx)
  for i in range(len(dx)-n+1):
    s = s + dx[i]
  print(s)

Submission status: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()
N, M = map(int, lines[0].split())
X = [int(x) for x in lines[1].split()]

X.sort()

diff = []
for i in range(0, len(X) - 1):
	diff.append(abs(X[i] - X[i+1]))

diff.sort()
print(sum(diff[:-(N-1)]))

Submission status: Wrong Answer

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

D = [0 for _ in range(M-1)]
for m in range(1, M):
    D[m-1] = X[m] - X[m-1]
D.sort()

print(sum(D[0:M-N]))

Submission status: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()
N, M = map(int, lines[0].split())
X = [int(x) for x in lines[1].split()]

X.sort()

diff = []
for i in range(0, len(X) - 1):
	diff.append(abs(X[i] - X[i+1]))

diff.sort()
print(sum(diff[:-(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 = sorted(X)
distance = []
for i, j in zip(X, X[1:]):
    distance.append(j-i)
distance = sorted(distance)
print(sum(distance[:-N+1]))

Submission status: Wrong Answer

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

n, m = input().split()
xs_str = input().split()
xs = list(map(int, xs_str))
xs.sort()

ys = []
for x1, x2 in zip(xs[0:-1], xs[1:]):
    ys.append(abs(x2 - x1))
ys.sort()

out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
x_list = list(map(int, input().split()))
if N == M:
    print(0)
x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], x[0]))
cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
cnt.append(M)
cnt.sort()
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    if c != M:
        start = c
        prev = x_list[c]

print(ans)

Submission status: Wrong Answer

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

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


if len(difX) > N:

    for i in range(N-1):
        difX.pop(difX.index(max(difX)))

    total = 0
    for x in difX:
        total+= x

    print(total)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()
N, M = map(int, lines[0].split())
X = [int(x) for x in lines[1].split()]

X.sort()

diff = []
for i in range(0, len(X) - 1):
	diff.append(abs(X[i] - X[i+1]))

diff.sort()
print(sum(diff[:-(N-1)]))

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], -x[0]))
cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
cnt.append(M)
cnt.sort()
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    if c != M:
        start = c
        prev = x_list[c]

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

in_num = list(map(int, input().split()))
in_num2 = list(map(int, input().split()))
n = in_num[0]
m = in_num[1]
x = np.asarray(in_num2)

x = np.sort(x)
xsub = x[1:] - x[:-1] - 1

xsub = np.sort(xsub)[::-1]
xsum = np.max(x) - np.min(x)
xsub = np.sum(xsub[:n-1])

print(xsum + 1 - xsub - n)

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], x[0]))
cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
cnt.append(M)
cnt.sort()
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    if c != M:
        start = c
        prev = x_list[c]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

li = [[0, 0]] * M
for i in range(1, M):
    li[i] = [i, X[i] - X[i-1]]
li.sort(key=lambda x: x[1], reverse=True)
cnt = 0
tmp = []
while cnt < N-1:
    tmp.append(li[cnt][0])
    cnt += 1
prev = 0
ans = 0
for t in tmp:
    ans += abs(X[t-1] - X[prev])
    prev = t
if N-1 > 0:
    ans += abs(X[-1] - X[tmp[-1]])
else:
    ans += abs(X[-1] - X[0])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = [int(i) for i in input().split()]
sum_list = []
if K == 0:
    sum_list.append(A[0])
else:
    for i in range(K):
        b_i = bin(i)
        sum = 0
        for a in A:
            b_a = bin(a)
            sum += int(i ^ a)
        sum_list.append(sum)

print(max(sum_list))

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split(" "))
#Nはポーンの数 Mは入力の数
A=list(map(int,input().split(" ")))
A=sorted(A)
L=[A[s]-A[s+1] for s in range(M-1)]

if M<=N:
    print(0)
else:
    L=sorted(L)
    print(L)
    L[0:N-1]=[0 for i in range(N-1)]
    print(abs(sum(L)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sortedX = sorted(X)
diffX = list(np.roll(sortedX, -1) - sortedX)

print(sum(sorted(diffX[:-1])[:(N+1)]))

Submission status: Wrong Answer

Solution code:
s = list(map(int, input().split()))
print(s)
t = list(map(int, input().split()))
print(t)

if(s[0] >= s[1]):
  print(0)
else:
  t.sort()
  u=[]
  for i in range(s[1]-1):
    u.append(t[i+1]-t[i])
  u.sort()
  print(u)  
  total=0
  for i in range(s[1]-s[0]):
    total+=u[i]
  print(total)

Submission status: Wrong Answer

Solution code:
import math
 
def solve(N,M,Xs):
    if N >= M:
        return 0
 
    Ds = [abs(Xs[i]-Xs[i+1]) for i in range(len(Xs)-1)] # Ds[j] := distance between j and j+1
 
    Ds.sort()
    #print(Ds)
    #print(math.ceil(N/2))
 
    return sum(Ds[0:len(Ds)-(math.ceil(N/2))])
 
def main():
    N, M = map(int, input().split())
    Xs = list(map(int, input().split()))
    Xs.sort()
    print(solve(N,M,Xs))
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N,M=[int(i) for i in input().split()]
x=input().split()
y=[int(i) for i in x]
y.sort()
if(N==1):
    print(y[M-1]-y[0])
    exit()
if(M==1):
    print("0")
    exit()
diff=[]
for i in range(M-1):
    diff.append(y[i+1]-y[i])
sub=[]
for i in range(N-1):
    xx=diff.index(max(diff))
    sub.append(xx)
    diff[xx]=0
ans=0
for i in range(N):
    if(i==0):
        ans+=y[sub[i]]-y[0]
    elif(i==N-1):
        ans+=y[M-1]-y[sub[i-1]+1]
    else:
        ans+=y[sub[i]]-y[sub[i-1]+1]
print(ans)

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()
a=[]
if m==1:
    print("0")
else:
    for i in range(m-1):
        a.append(x[i+1]-x[i])
    a.sort(reverse=True)
    if n==1 or n>=m:
        print(max(x)-min(x))
    else:
        result=0
        for i in range(n-1):
            result+=a[i]
        print(max(x)-min(x)-result)

Submission status: Wrong Answer

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

if n>=m:
    print(0)
else:
    xm = sorted(xm)
    diff = [xm[1:][k] - xm[:-1][k] for k in range(len(xm)-1)]
    diff = sorted(diff)
    
    print(sum(diff[:(n-m+2)]))

Submission status: Wrong Answer

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

X.sort()

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = sorted([int(i) for i in input().split()])
ans = 0
d = sorted([X[i] - X[i-1] for i in range(1, M)])

c = 0
for i in d:
    if c > N:
        break
    ans += i
    c += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math

def solve(N,M,Xs):
    if N >= M:
        return 0

    Ds = [abs(Xs[i]-Xs[i+1]) for i in range(len(Xs)-1)] # Ds[j] := distance between j and j+1

    Ds.sort()
    print(Ds)
    #print(math.ceil(N/2))

    return sum(Ds[0:len(Ds)-(math.ceil(N/2))])

def main():
    N, M = map(int, input().split())
    Xs = list(map(int, input().split()))
    Xs.sort()
    print(solve(N,M,Xs))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

n, m = map(int, stdin.readline().rstrip().split())
x = [int(x) for x in stdin.readline().rstrip().split()]

if n >= m:
    print('0')
else:
    p = np.sort(np.array(x))
    dif = p[1:] - p[:-1]
    dif = np.sort(dif)

    print(np.sum(dif[: 1 - n]))

Submission status: Wrong Answer

Solution code:
from sys import stdin

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

    if N >= M:
        print(0)
    return

    X.sort()
    n = [(i + 1, X[i+1] - X[i]) for i in range(M - 1)]
    n.sort(reverse=1, key=lambda x: x[1])
    ans, s1 = 0, 0
    for i in range(N - 1):
        s2 = n[i][0]
        ans += X[s1:s2][-1] - X[s1:s2][0]
        s1 = s2
    ans += X[s1:][-1] - X[s1:][0]
    print(ans)

input = lambda: stdin.readline()
main()

Submission status: Wrong Answer

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

if m <= n:
    print(0)
else:
    lis2 = [0]
    for i in range(m-1):
        lis2.append(int(abs(lis[i+1] - lis[i])))
    lis2.sort()
    lis2.reverse()
    del lis2[0:(m - n - 2)]
    print(sum(lis2))

Submission status: Wrong Answer

Solution code:
line1 = list(map(lambda x: int(x), input().split()))
N = line1[0]
M = line1[1]
X = list(map(lambda x: int(x), input().split()))

if N >= M or M == 1:
  print(0)
else:
  X.sort(key=lambda x:x, reverse=False)

  diff = []

  for i in range(0, M-1):
    diff.append(X[i + 1] - X[i])

  print(diff)

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

  print(diff)
  print(sum(diff))

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], x[0]))
cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    start = c
    prev = x_list[c]
last = cnt[-1]
for i in range(last, M):
    if i == last:
        prev = x_list[i]
    else:
        ans += x_list[i] - prev
        prev = x_list[i]
print(ans)

Submission status: Wrong Answer

Solution code:
def func():
    pass


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

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

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

    d_s = sorted(d, reverse=True)
    top_n = d_s[:n - 1]
    top_n_index = []
    for n in top_n:
        # top_n_index.append(d.index(n))
        top_n_index.append(d.index(n) + 1)

    # print(x)
    # print(d)
    # print(top_n_index)

    ans = 0
    for i, v in enumerate(x):
        if i == 0:
            continue
        if i in top_n_index:
            continue
        if i not in top_n_index:
            ans += v - x[i - 1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

def solve(N,M,Xs):
    if N >= M:
        return 0

    Ds = [abs(Xs[i]-Xs[i+1]) for i in range(len(Xs)-1)] # Ds[j] := distance between j and j+1

    Ds.sort()
    print(Ds)
    #print(math.ceil(N/2))

    if N == 1:
        return sum(Ds)

    return sum(Ds[0:len(Ds)-(math.ceil(N/2))])

def main():
    N, M = map(int, input().split())
    Xs = list(map(int, input().split()))
    Xs.sort()
    print(solve(N,M,Xs))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# print(n, m, x)

# #%%
# n, m =2, 5
# x = [10, 12, 1, 2, 14]


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
print(sum(diff_list[:-(n-1)]))

#%%


#%%

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], -x[0]))
cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    start = c
    prev = x_list[c]
last = cnt[-1]
for i in range(last, M):
    if i == last:
        prev = x_list[i]
    else:
        ans += x_list[i] - prev
        prev = x_list[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n, m =list(map(int, input().split()))
xs = list(map(int, input().split()))
if n >= m:
    print(0)
    exit()
xs.sort()
a = [abs(xs[i] - xs[i+1]) for i in range(m-1)]
a.sort()
#print(a)
print(sum(a[:-n+1]))

Submission status: Wrong Answer

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

n, m = input().split()
xs_str = input().split()
xs = []
for x in xs_str:
    xs.append(int(x))
xs.sort()

ys = []
for x1, x2 in zip(xs[0:-1], xs[1:]):
    ys.append(abs(x2 - x1))
ys.sort()

out = 0
for y in ys[0:-(int(n)-1)]:
    out += y
print(out)

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
X = sorted(map(int, raw_input().split()))
D = [X[i+1]-X[i] for i in range(M-1)]
indexes = reversed([D.index(x) for x in sorted(D)[-(N-1):]])
start = 0
ans = 0
for index in indexes:
  ans += X[index]-X[start]
  start = index+1
ans += X[-1]-X[start]
print ans

Submission status: Wrong Answer

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

if m <= n:
    print(0)
else:
    lis2 = [0]
    for i in range(m-1):
        lis2.append(int(abs(lis[i+1] - lis[i])))
    lis2.sort()
    lis2.reverse()
    del lis2[0:(m - n - 2)]
    print(sum(lis2))
    print(lis2)

Submission status: Wrong Answer

Solution code:
def unko(arr, M):
  offset = arr[1] - arr[0]
  index = 0
  for i in range(M-1):
    tmp = arr[i+1] - arr[i]
    if offset < tmp:
      offset = tmp
      index = i
  return index

def unko2(arr):
  if len(arr) == 1:
    return 0
  else:
    return max(arr) - min(arr)

a = list(map(int, input().split()))
N, M = a[0], a[1]
b = list(map(int, input().split()))
b.sort()

if M == 1:
  print(0)
else:
  index = unko(b, M)
  print(b[0:index+1])
  print(b[index+1:])
  print(unko2(b[0:index+1]) + unko2(b[index+1:]))

Submission status: Wrong Answer

Solution code:
T, X = map(int, input().split())
x_points = list(map(int, input().split()))

if T > X:
    print(0)
    quit()

x_points.sort()

diffs = []
for x1, x2 in zip(x_points, x_points[1:]):
    diffs.append(x2-x1)

diffs.sort()
print(sum(diffs[:-(T-1)]))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter


def r(n, m, xl):
    if len(xl) == 1:
        return 0
    xl.sort()
    min_x = abs(min(xl))
    xl = [x + min_x for x in xl]
    xm = [xl[i+1] - xl[i] for i in range(len(xl)-1)]
    xm.sort()
    return sum(xm[:n*-1+1])


def main():
    input = sys.stdin.readline
    n, m = list(map(int, input().split()))
    xl = list(map(int, input().split()))

    print(r(n, m, xl))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: (-x[1], x[0]))

cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    start = c
    prev = x_list[c]
last = cnt[-1]
for i in range(last, M):
    if i == last:
        prev = x_list[i]
    else:
        ans += x_list[i] - prev
        prev = x_list[i]

Submission status: Wrong Answer

Solution code:
li0 = list(map(int,input().split()))
N,M = li0[0], li0[1]
li = list(map(int,input().split()))

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

n, m = map(int, stdin.readline().rstrip().split())
x = [int(x) for x in stdin.readline().rstrip().split()]

p = np.sort(np.array(x))
dif = p[1:] - p[:-1]
dif = np.sort(dif)

print(np.sum(dif[: 1 - n]))

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: -x[1])

cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
for i, c in enumerate(cnt[1:]):
    for j in range(start, c):
        ans += x_list[j] - prev
        prev = x_list[j]
    start = c
    prev = x_list[c]
last = cnt[-1]
for i in range(last, M):
    if i == last:
        prev = x_list[i]
    else:
        ans += x_list[i] - prev
        prev = x_list[i]

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split(" "))
X = [int(x) for x in input().split(" ")]
X.sort()
X_diff = [abs(X[i]-X[i-1]) for i in range(1,M)]
X_diff.sort()
print(sum(X_diff[0:M-N]))

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=a.split()
n=int(a[0])
d=b.split()
d=[int(i) for i in d]
d.sort(reverse=True)
count=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n-1):
    del ll[-1]
print(sum(ll))

Submission status: Wrong Answer

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

if m <= n:
    print("0")
else:
    lis2 = [0]
    for i in range(m-1):
        lis2.append(int(abs(lis[i+1] - lis[i])))
    lis2.sort()
    lis2.reverse()
    del lis2[0:(m - n - 2)]
    print(sum(lis2))
    print(lis2)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
poti=list(input().split())
poti= [ int(s) for s in poti ]
poti=sorted(poti)
sa=[]
for i in range(0,M-1):
    a=poti[i+1]-poti[i]
    sa.append(a)


if len(sa)!=0:
    sa=sorted(sa)
    #print(sa)
    sa=sa[0:(M-1)-(N-1)]
    #print(sa)

    print(sum(sa))

else:
    print(0)
#print(poti)

Submission status: Wrong Answer

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(tmp != []):
  if(len(tmp) == 1):
    print(*tmp)
  else:
    print(sum(tmp[:-(N-1)]))
else:
  print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

diffs = [0 for _ in range(M-1)]
for m in range(1, M):
    diffs[m-1] = sorted_X[m] - sorted_X[m-1]
sorted_D = sorted(diffs)

print(sum(sorted_D[0:M-N]))

Submission status: Wrong Answer

Solution code:
data = input().split()  
n = int(data[0])
m = int(data[1])
x = [int(s) for s in input().split()]

x.sort()

y=[]
tmp=0
for i in x:
    y.append(i-tmp)
    tmp=i
y.pop(0)

y.sort()
y.reverse()

res=x[-1]-x[0]

if(len(y) < n):
    n = len(y)

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

print(res)

Submission status: Wrong Answer

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