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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
        exit()
    minimum = x[0]
    maximum = x[-1]
    arr = []
    for i in range(len(x)-1):
        arr.append(abs(x[i+1]-x[i]))

    arr.sort()

    print(maximum-minimum-sum(arr[-(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(reverse=False)
Y=[]
for i in range(M-1):
    Y.append(abs(X[i]-X[i+1]))

Y.sort(reverse=False)


if N==1:
    print(sum(Y))


else:
    
    print(sum(Y[:len(Y)-(N-1)]))

Submission status: Wrong Answer

Solution code:
N,M= map(int,input().split())
X=list(map(int,input().split()))
X.sort(reverse=False)
Y=[]
for i in range(M-1):
    Y.append(abs(X[i]-X[i+1]))

Y.sort(reverse=False)

print(sum(Y[:len(Y)-(N-1)]))

Submission status: Wrong Answer

Solution code:
N,M= map(int,input().split())
X=list(map(int,input().split()))
X.sort(reverse=False)
Y=[]
for i in range(M-1):
    Y.append(abs(X[i]-X[i+1]))

Y.sort(reverse=False)

print(sum(Y[:N+1]))

Submission status: Wrong Answer

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

s = sorted(X)
dist = [s[n]-s[n-1] for n in range(1,len(s))]
dist = sorted(dist)[:N+1]
if N >= M:
  print(0)
else:
  res = N-1
  for n in range(1,len(dist)):
    res += dist[n]
  print(res)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
# from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,M = map(int, input().split())
    XS = list(map(int, input().split()))
    XS.sort()
    intervals = [0]*(M-1)
    for i in range(M-1):
        intervals[i] = XS[i+1]-XS[i]
    intervals.sort()
    print(XS[M-1]-XS[0]-sum(intervals[-min(M-1,N-1):]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
# from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,M = map(int, input().split())
    XS = list(map(int, input().split()))
    XS.sort()
    intervals = [0]*(M-1)
    for i in range(M-1):
        intervals[i] = XS[i+1]-XS[i]
    intervals.sort()
    print(XS[M-1]-XS[0]-sum(intervals[-(N-1):]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

N, M = map(int, input().split())
X = np.array(sorted(map(int, input().split())))

distances = sorted(X[1:] - X[:-1])
print(sum(distances[:-N+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
x=map(int,input().split())
if n>=m:
    print(0)
else:
    y=[int(i) for i in x]
    y.sort()
    z=[]
    for h  in range(1,len(y)):
        z.append(y[h]-y[h-1])
    z.sort()
    print(z)
    w=z[:m-n]
    print(sum(w))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if n>=m:
    print(0)
else:
    x=map(int,input().split())
    y=[int(i) for i in x]
    y.sort()
    z=[]
    for h  in range(1,len(y)):
        z.append(y[h]-y[h-1])
    z.sort()
    print(z)
    w=z[:m-n]
    print(sum(w))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if m==n:
    print(0)
else:
    x=map(int,input().split())
    y=[int(i) for i in x]
    y.sort()
    z=[]
    for h  in range(1,len(y)):
        z.append(y[h]-y[h-1])
    z.sort()
    w=z[:m-n]
    print(sum(w))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if m==1:
    print(0)
else:
    x=map(int,input().split())
    y=[int(i) for i in x]
    y.sort()
    z=[]
    for h  in range(1,len(y)):
        z.append(y[h]-y[h-1])
    z.sort()
    w=z[:m-n]
    print(sum(w))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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(M-1):
    Y.append(X[i+1] - X[i])

Y.sort()
ans = sum(Y[:M-N])
print(ans)

Submission status: Wrong Answer

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

if n >= m:
  print(0)
else:
  Diffs.sort()
  print(sum(Diffs[:-(n-1)]))

Submission status: Wrong Answer

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

B=[0]*(M-1)
for i in range(M-1):
	B[i]=A[i+1]-A[i]
B.sort()

print(sum(B[: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()
    spot = [0]*M
    diff = [[0, 0]]*(M-1)
    for i in range(M-1):
        diff[i] = [X[i+1]-X[i], i]
    diff.sort(key=lambda x: x[0])
    if N == 1:
        print(X[-1]-X[0])

    else:
        while N > 0:
            tmp = diff[-1][1]
            if tmp == 0 or tmp == N - 2:
                spot[tmp] = 1
                N -= 1
                diff.pop()
            else:
                if spot[tmp] == 0:
                    spot[tmp] = 1
                    N -= 1
                if spot[tmp + 1] == 0:
                    if N > 0:
                        spot[tmp] = 1
                        N -= 1
                        diff.pop()
                else:
                    diff.pop()
        ans = 0
        for x in diff:
            ans += x[0]
        print(ans)

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()
  spot=[0]*M
  diff=[[0,0]]*(M-1)
  for i in range(M-1):
    diff[i]=[X[i+1]-X[i],i]
  diff.sort(key=lambda x:x[0])
  if N == 1:
    print(X[-1]-X[0])
    
  else:
    while N>0:
      tmp = diff[-1][1]
      if tmp == 0 or tmp == N - 2:
        spot[tmp] = 1
        N -= 1
        diff.pop()
      else:
        if spot[tmp] == 0:
          spot[tmp] = 1
          N -= 1
        if spot[tmp + 1] == 0:
          if N > 0:
            spot[tmp] = 1
            N -= 1
            diff.pop()
        else:
          diff.pop()
      ans=0
      for x in diff:
        ans+=x[0]
      print(ans)

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()
  spot=[0]*M
  diff=[[0,0]]*(M-1)
  for i in range(M-1):
    diff[i]=[X[i+1]-X[i],i]
  diff.sort(key=lambda x:x[0])
  if N == 1:
    print(X[-1]-X[0])
  else:
    while N>0:
      tmp = diff[-1][1]
      if tmp == 0 or tmp == N - 2:
        spot[tmp] = 1
        N -= 1
        diff.pop()
      else:
        if spot[tmp] == 0:
          spot[tmp] = 1
          N -= 1
        if spot[tmp + 1] == 0:
          if N > 0:
            spot[tmp] = 1
            N -= 1
            diff.pop()
        else:
          diff.pop()

        
  ans=0
  for x in diff:
    ans+=x[0]
  print(ans)

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()
  spot=[0]*M
  diff=[[0,0]]*(M-1)
  for i in range(M-1):
    diff[i]=[X[i+1]-X[i],i]
  diff.sort(key=lambda x:x[0])
  if N == 1:
    print(X[-1]-X[0])
    
  else:
    while N>0:
      tmp = diff[-1][1]
      if tmp == 0 or tmp == N - 2:
        spot[tmp] = 1
        N -= 1
        diff.pop()
      else:
        if spot[tmp] == 0:
          spot[tmp] = 1
          N -= 1
        if spot[tmp + 1] == 0 and N > 0:
          spot[tmp] = 1
          N -= 1
          diff.pop()

        
  ans=0
  for x in diff:
    ans+=x[0]
  print(ans)

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()
  spot=[0]*M
  diff=[[0,0]]*(M-1)
  for i in range(M-1):
    diff[i]=[X[i+1]-X[i],i]
  diff.sort(key=lambda x:x[0])
  if N == 1:
    print(X[-1]-X[0])
    
  else:
    while N>0:
      tmp = diff[-1][1]
      if tmp == 0 or tmp == N - 2:
        spot[tmp] = 1
        N -= 1
        diff.pop()
      else:
        if spot[tmp] == 0:
          spot[tmp] = 1
          N -= 1
        if spot[tmp + 1] == 0:
          spot[tmp] = 1
          N -= 1
        diff.pop()

        
  ans=0
  for x in diff:
    ans+=x[0]
  print(ans)

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()
  diff=[[0,0]]*(M-1)
  for i in range(M-1):
    diff[i]=[X[i+1]-X[i],i]
  diff.sort(key=lambda x:x[0])
  if N == 1:
    print(X[-1]-X[0])
    
  else:
    while N>0:
      if diff[-1][1]==0 or diff[-1][1]==M-2:
        N-=1
        diff.pop()
      elif N > 1:
        N-=2
        diff.pop()
      else:
        N-=1
  ans=0
  for x in diff:
    ans+=x[0]
  print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import heapq

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

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

Q = []
for i in range(M-1):
    heapq.heappush(Q,(-(X[i+1]-X[i]),i))

Q2 = []
heapq.heappush(Q2,0)
for i in range(1,N):
    tmp = heapq.heappop(Q)[1]
    heapq.heappush(Q2,tmp)
    heapq.heappush(Q2,tmp+1)
heapq.heappush(Q2,M-1)

sum = 0
for q in Q2:
    left = heapq.heappop(Q2)
    right = heapq.heappop(Q2)
    sum = sum + (X[right]-X[left])

print(sum)

Submission status: Wrong Answer

Solution code:
import sys
import heapq

N,M = map(int,input().split())
if N>=M:
    print(0)
    sys.exit()
X = list(map(int,input().split()))
X.sort()
sum = 0
Q = []
for i in range(M-1):
    heapq.heappush(Q,(-(X[i+1]-X[i]),i))
Q2 = []
heapq.heappush(Q2,0)
for q in Q:
     heapq.heappush(Q2,heapq.heappop(Q)[1])
heapq.heappush(Q2,M-1)
for q in Q2:
    left = heapq.heappop(Q2)
    right = heapq.heappop(Q2)
    sum = sum + (X[right]-X[left])

print(sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(m-1):
    li.append(abs(x[i] - x[i+1]))
li = sorted(li)
print(sum(li[:m-n]))

Submission status: Wrong Answer

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

for i in range(m-1):
    li.append(abs(x[i] - x[i+1]))
li = sorted(li)
print(sum(li[:n+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M=map(int, input().split())
X=sorted(list(map(int, input().split())))
d=sorted([X[i]-X[i-1] for i in range(1, M)])
if N>=M:
  print(0)
else:
  print(X[-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())))
d=sorted([X[i]-X[i-1] for i in range(1, M)])
if N>=M:
  print(0)
else:
  print(sum(d[:-N+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

koma_dist = []

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

if n<m:
    print(sum(sorted(koma_dist)[:-(n - 1)]))
else:
    print(0)

Submission status: Wrong Answer

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

koma_dist = []

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

print(sum(sorted(koma_dist)[:-min(n-1,m-1)]))

Submission status: Wrong Answer

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

koma_dist = []

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

print(sum(sorted(koma_dist)[:-min(n-1,m)]))

Submission status: Wrong Answer

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

koma = []
cnt_koma = [0]*n
cnt = 0

for i in range(n):
    koma.append(x[m*i//n])

while len(x)>0:
    for i in range(n):
        if koma[i] in x:
            x.remove(koma[i])
            cnt += cnt_koma[i]
            cnt_koma[i] = 0
        koma[i] += 1
        cnt_koma[i] += 1

print(cnt)

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

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    if n >= m:
        print(0)
    else:
        x = list(map(int, input().rstrip('\n').split()))
        x.sort()
        ls = collections.defaultdict(list)
        for i in range(1, m):
            ls[abs(x[i] - x[i-1])] += [[i-1, i]]
        ls = sorted(ls.items(), reverse=True)
        ad = [0, m-1]
        print(x)
        for i, v in enumerate(ls):
            if i < n - 1:
                print(v)
                for vv in v[1]:
                    for vvv in vv:
                        ad.append(vvv)
            else:
                break
        print(ad)
        ad.sort()
        t = 0
        for i in range(1, len(ad), 2):
            t += (abs(x[ad[i]] - x[ad[i-1]]))
        print(t)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    x.sort()
    ls = collections.defaultdict(list)
    for i in range(1, m):
        ls[abs(x[i] - x[i-1])] += [i-1, i]
    ls = sorted(ls.items(), reverse=True)
    ad = [0, m-1]
    for i, v in enumerate(ls):
        if i < n - 1:
            ad.append(v[1][0])
            ad.append(v[1][1])
        else:
            break
    ad.sort()
    t = 0
    for i in range(1, len(ad), 2):
        t += (abs(x[ad[i]] - x[ad[i-1]]))
    print(t)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

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

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

sum = 0
for i in range(M-N):
    sum += R[i]

print(sum)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
X = list(map(int, input().split()))
A = []
if n >= m:
  print(0)
else:
  Y = sorted(X)
  for i in range(m-1):
    A.append(Y[i+1]-Y[i])
  B = sorted(A)
  del B[-(n-1):]
  print(sum(a for a in B))

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if n >= m:
        print(0)
    else:
        x.sort()
        y = []
        for i, v in enumerate(x):
            if i != 0:
                y.append([abs(v - x[i-1]), i])
        y = sorted(y, key=lambda x: x[0], reverse=True)
        cnt = 0
        l = 0
        for i in range(n-1):
            if i == 0:
                cnt += abs((x[0] - x[y[i][1]-1]))
            else:
                cnt += abs((x[l] - x[y[i][1]-1]))
            l = y[i][1]
        cnt += abs((x[l] - x[-1]))
        print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if n >= m:
        print(0)
    else:
        x.sort()
        y = []
        for i, v in enumerate(x):
            if i != 0:
                y.append([abs(v - x[i-1]), i])
        y = sorted(y, key=lambda x: x[0], reverse=True)
        cnt = 0
        l = 0
        for i in range(n-1):
            print(i)
            if i == 0:
                cnt += abs((x[0] - x[y[i][1]-1]))
            else:
                cnt += abs((x[l] - x[y[i][1]-1]))
            l = y[i][1]
        cnt += abs((x[l] - x[-1]))
        print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
# print('input >>')
N, M = map(int,(input().split()))
X = list(map(int,(input().split())))
X.sort()
Xdiff = [X[i + 1] - X[i] for i in range(len(X) - 1)]
Xdiff.sort()
ans = sum(Xdiff[:N+1])
if N <= M:
    ans = 0

# print('----------output----------')
print(ans)

Submission status: Wrong Answer

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

# print('----------output----------')
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
d = []
for i in range(M - 1):
    d.append(X[i + 1] - X[i])
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()))
X.sort()
di=[]

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

di.sort()
print(sum(di[:M-N]))

Submission status: Wrong Answer

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

B=[0]*(M-1)
for i in range(M-1):
	B[i]=A[i+1]-A[i]
B.sort()

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n,m = inpl()
x = inpl()
x.sort()
dd = []
for i in range(len(x)-1):
    dd.append(abs(x[i] - x[i+1]))
dd.sort()
# print(dd)
print(sum(dd[:m-n]))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, M = map(int, input().split())
X = list(map(int, input().split()))
X_sort = np.array(sorted(X))[::-1]
X_diff = X_sort[:M-1] - X_sort[1:]

X_diff_sort = sorted(X_diff)[::-1]

print(max(sum(X_diff_sort[M-N-2:]), 0))

Submission status: Wrong Answer

Solution code:
N,M = [int(x) for x in input().split()]
if N>=M:
  print(0)
  exit()
X = [int(x) for x in input().split()]
X.sort()
L = []
for i in range(M-1):
  L.append(X[i+1]-X[i])
L.sort()
print(L)
ans = sum(L[:M-N])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, m = LI()
L = sorted(LI())


total = L[-1] - L[0]
diff_list = sorted([L[i+1] - L[i] for i in range(m-1)], reverse=True)
if n >= m - 1:
    print(0)
else:
    print(total - sum(diff_list[:n-1]))

Submission status: Wrong Answer

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

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

res = sum(diff[:-(N - 1)]) if M > N else 0
print(res)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def main():
    N,_ = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    D = []
    for i in range(len(X)-1):
        D.append(X[i+1] - X[i])
    D.sort(reverse=True)
    D = D[math.ceil(N/2):]
    n = 0
    for d in D:
        n += d
    print(n)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
xs = sorted(set(map(int, input().split())))
m = len(xs)
ds = sorted([y - x for x, y in zip(xs, xs[1:])])
print(sum(ds[:max(1, m - n)]))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
xs = sorted(map(int, input().split()))
ds = sorted([y - x for x, y in zip(xs, xs[1:])])
print(sum(ds[:max(1, m - n)]))

Submission status: Wrong Answer

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

tmp = sorted(h, reverse=True)
print(sum(tmp))

Submission status: Wrong Answer

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

Solution code:
import itertools

n,m=map(int, input().split())
lst=sorted(list(map(int, input().split())))
sub_lst=[]
for i in range(1, m):
  sub_lst.append(lst[i]-lst[i-1])
print(sum(sorted(sub_lst)[:-n+1]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
Y = [X[i+1]-X[i] for i in range(M-1)] #差のリスト
Y = sorted(Y)
print(sum(Y[:(M-N)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
print(X)
Y = [X[i+1]-X[i] for i in range(M-1)] #差のリスト
Y = sorted(Y)
print(Y)
print(sum(Y[:(M-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()
dis = []
for i in range(1,M):
    dis.append(X[i]-X[i-1])
dis.sort()
print(sum(dis[:M-N]))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
x=sorted([int(i)for i in 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([int(i)for i in input().split()])
l=sorted([x[i+1]-x[i]for i in range(m-1)])
print(sum(l[:-n+1])if m>n else 0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = []
for i in range(1, M):
    d.append(X[i] - X[i - 1])
d.sort()

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m, *x = map(int, stdin.read().split())
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m - n]))

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m, *x = map(int, stdin.read().split())
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:-n + 1]) if n < m else 0)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m, *x = map(int, stdin.read().split())
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:-n]) if n < m else 0)

Submission status: Wrong Answer

Solution code:
import sys
n, m = map(int, input().split())
if n >= m:
  print(0)
  sys.exit()
x = list(map(int, input().split()))
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())
x = list(map(int, input().split()))
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, *x = map(int, open(0).read().split())
x.sort()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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])
    div.sort()
    print(sum(div[:-N+1]))

Submission status: Wrong Answer

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])
    div.sort()
    print(sum(div[:-(M-N-2)]))