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=list(map(int,input().split()))
x.sort()
x_dif=[b-a for a,b in zip(x[:-1],x[1:])]
x_dif.sort()
print(sum(x_dif[:-(n-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools as it

N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
if N >= M:
	print(0)
else:
	min = 999999999999999999999
	for combi in it.combinations(range(M), N):
		combi = list(map(int, combi))
		score = 0
		print(combi)
		if combi[0] == 0:
			for i in range(N):
				if i == N - 1:
					score += X[-1] - X[combi[i]]
				else:
					score += X[combi[i + 1] - 1] - X[combi[i]]
				print(score)
			if score < min:
				min = score
			
	print(min)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
lst = sorted(set(map(int, input().split())))
print(lst)
dis = []
for i, j in zip(lst, lst[1:]):
    dis += [j - i]
    print(dis)
print(sum(sorted(dis, reverse=True)[n - 1:]))

Submission status: Wrong Answer

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

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

print(n-m)

Submission status: Wrong Answer

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

xlist.sort()
#print(xlist)

xintv=[-1]*(M-1)
for i in range(M-1):
  xintv[i]=xlist[i+1]-xlist[i]
  
xintv.sort()
#print(xintv)
print(sum(xintv[0:-N+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 左から歩いてN-1区間スキップ
import numpy as np
N,M = map(int,input().split())
X = np.array(input().split(), dtype=np.int64)
X.sort()
X = np.diff(X)
X.sort()
answer = X[:M-N].sum()
print(answer)

Submission status: Wrong Answer

Solution code:
# 左から歩いてN-1区間スキップ
import numpy as np
N,M = map(int,input().split())
X = np.array(input().split(), dtype=np.int32)
X.sort()
X = np.diff(X)
X.sort()
answer = X[:M-N].sum()
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = sorted(X)
x_diff = [abs(x[m] - x[m - 1]) for m in range(1, M)]
x_diff.sort()

print(abs(x[-1] - x[0]) - sum(x_diff[-(N - 1):]))

Submission status: Wrong Answer

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

x = sorted(X)
x_diff = [abs(x[m] - x[m - 1]) for m in range(1, M)]
x_diff.sort()

print((x[-1] - x[0]) - sum(x_diff[-(N - 1):]))

Submission status: Wrong Answer

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

x = sorted(X)
x_diff = []
for m in range(1, M):
	x_diff += [x[m] - x[m - 1]]
x_diff.sort()

print((x[-1] - x[0]) - sum(x_diff[-(N - 1):]))

Submission status: Wrong Answer

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


x = sorted(X)
diff = []
for n in range(1, M):
	diff += [abs(x[n] - x[n - 1])]
diff.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# Xの区間幅が大きい方からN-1個をとってこれを仕切りにする
Y = [0] * (M - 1)  # Y[i]はX[i+1]-X[i]の値
for i in range(M - 1):
	Y[i] = X[i + 1] - X[i]
Z = sorted(Y)[:: -1][: N - 1]  # ZはYの大きい方からN-1個

ans = 0
start = X[0]
end = 0
for j in range(M):
	if j == M - 1 or Y[j] in Z:
		end = X[j]
		ans += end - start
		if j < M - 1:
			start = X[j + 1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


diff = deque()

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

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

X.sort()

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

diff = list(diff)
diff.sort()

print(diff)

for i in range(N-1):
    diff.pop()

print(sum(diff))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()
  l=LI()

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

  l2.sort()

  return sum(l2[:-n+1])

print(main())

Submission status: Wrong Answer

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

x_diff = [x[i+1] - x[i] for i in range(len(x)-1)]
x_diff.sort()

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

Submission status: Wrong Answer

Solution code:

def solve(N, M, X):
    if N == M:
        return 0
    if N == 1:
        return X[-1] - X[0]

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

    D = sorted(D, reverse=True)
    D = sorted([stop for (gap, stop) in D[:N-1]])
    # index (gap) index+1
    # stop        start
    total = 0
    start = 0
    for stop in D:
        total += X[stop] - X[start]
        start = stop + 1

    if start < M:
        total += X[-1] - X[start]
    
    return total 

#print(solve(2, 5, [10,12,1,2,14]))
N, M = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
print(solve(N, M, X))

Submission status: Wrong Answer

Solution code:
n, m = [ int(v) for v in input().split() ]
position_list = sorted([ int(v) for v in input().split() ])

interval_list = sorted([ position_list[i+1] - position_list[i] for i in range(m-1) ])
k = min(n-1,m-1)
print(sum(interval_list[:-k]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

X_array.sort()
# print(X_array)
diff_array = [(X_array[i + 1] - X_array[i]) for i in range(M - 1)]
# print(diff_array)
diff_array.sort()
print(sum(diff_array[:-(N - 1)]))

Submission status: Wrong Answer

Solution code:
N, M, *X = map(int, open(0).read().split())
X.sort()

y = sorted([X[i + 1] - X[i] for i in range(len(X) - 1)])
print(sum(y[:-(N - 1)]))

Submission status: Wrong Answer

Solution code:
N, M, *X = map(int, open(0).read().split())

y = sorted([X[i + 1] - X[i] for i in range(len(X) - 1)])
print(sum(y[:-(N - 1)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
if M-1-N < 0:
    pass
else:
    X.sort()
    dist = [X[i+1]-X[i] for i in range(M-1)]
    dist.sort()
    for i in range(M-1-N):
        ans += dist[i]
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
if M-1-N <= 0:
    pass
else:
    X.sort()
    dist = [X[i+1]-X[i] for i in range(M-1)]
    dist.sort()
    for i in range(M-1-N):
        ans += dist[i]
print(ans)

Submission status: Wrong Answer

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

data = [int(s) for s in input().split()]
data.sort()
l = []
for i in range(m-1):
    l.append(data[i+1]-data[i])
l.sort(reverse = True)

x = 0
for i in range(m-1):
    x += l[i]
print(data[m-1] - data[0] - x)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x = sorted(x)
count = [0] * (m-1)
ans = 0

if n > m:
    ans += 0
else:
    for i in range(1, m):
        count[i-1] = x[i] - x[i-1]
    count = sorted(count, reverse=True)
    for i in range(1, n):
        ans = sum(count[i:])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x.sort()
diff = []
for i in range(m-1):
    diff.append(x[i+1] - x[i])
diff.sort(reverse= True)
ans = sum(diff) - sum(diff[0:n])
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = [int(n) for n in input().split()]
S = input().split() if M != 1 else [input()]
X = [int(s) for s in S]

def main():
    x = sorted(X)
    costs = []
    for i in range(M - 1):
        costs.append(abs(x[i] - x[i + 1]))
    costs.sort()
    ans = sum(costs[:-(N - 1)])
    print(ans)

if __name__== '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M = [int(n) for n in input().split()]
S = input().split() if M != 1 else [input()]
X = [int(s) for s in S]

def main():
    x = sorted(X)
    costs = []
    for i in range(M - 1):
        costs.append(x[i + 1] - x[i])
    costs.sort()
    ans = sum(costs[:-(N - 1)])
    print(ans)

if __name__== '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
C - Streamline
https://atcoder.jp/contests/abc117/tasks/abc117_c

"""
import sys


def solve(N, M, positions):
    gaps = sorted([b-a for a, b in zip(positions, positions[1:])])
    if len(gaps) <= N:
        return 0
    return sum(gaps[:-(N-1)]) if N > 1 else sum(gaps)


def main(args):
    N, M = map(int, input().split())
    positions = sorted(map(int, input().split()))
    ans = solve(N, M, positions)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
C - Streamline
https://atcoder.jp/contests/abc117/tasks/abc117_c

"""
import sys


def solve(N, M, positions):
    gaps = sorted([b-a for a, b in zip(positions, positions[1:])])
    if len(gaps) <= N:
        return 0
    return sum(gaps[:-(N-1)])


def main(args):
    N, M = map(int, input().split())
    positions = sorted(map(int, input().split()))
    ans = solve(N, M, positions)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,M = inpl()
XX = inpl()
XX.sort()
dist = []
for i in range(1,M):
    dist.append(XX[i] - XX[i-1])

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

Submission status: Wrong Answer

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

x_s = sorted([int(c) for c in input().split()])

n_s = []
for i in range(1, len(x_s)):
    n_s.append(abs(x_s[i-1] - x_s[i]))

ans = sorted(n_s)[:n-1]
print(sum(ans))

Submission status: Wrong Answer

Solution code:
from itertools import combinations
n, m = map(int, input().split(" "))
if n >= m:
  print(0)
else:
  ls = list(sorted(map(int, input().split(" "))))
  idxes = set( i for i in range(m-1) )
  routes = []
  for v in combinations(idxes, n-1):
    print(v)
    c = 0
    tmp = 0
    for i in v:
      tg = ls[tmp:i+1]
      c += tg[-1] - tg[0]
      tmp = i+1
    c += ls[-1] - ls[tmp]
    routes.append(c)
  print(min(routes))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

sub.sort()

print(sum(sub[:-(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()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X = sorted(X)

gap = 0

index = 0
for i in range(len(X)-1):
  t = X[i+1]-X[i]
  if t >= gap:
    gap = t

print(X[-1]-X[0]-gap)

Submission status: Wrong Answer

Solution code:
import sys

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

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

distance_list = [b - a for a, b in zip(X,X[1:])]
distance_list.sort()

print(sum(distance_list[:-N+1]))

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

X.sort()

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=[int(i) for i in input().split()]
c.sort()
for i in range(b-1):
    c[-i-1]=c[-i-1]-c[-i-2]
c[0]=0
c.sort()
print([sum(c[:-a+1]),0][a >=b])

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=[int(i) for i in input().split()]
c.sort()
for i in range(b-1):
    c[-i-1]=c[-i-1]-c[-i-2]
c[0]=0
c.sort()
print(sum(c[:-a+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n>=m or n == 1:
    S =0
else:
    d = sorted([l[i] - l[i+1]  for i in range(m-1)], reverse=True)
    S = max(l) - min(l)
    for i in range(n-1):
        S -= d[i]

print(S)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if N >= M:
    print(0)
    
x.sort()
for i in range(M-1):
    dis.append(abs(x[i]-x[i+1]))  
dis.sort()

print(sum(dis[0:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import math
N,M = list(map(int,input().split(" ")))
X = list(map(int,input().split(" ")))
X.sort()
diffs = []

if N >= M:
    print(0)
else:
    for i,x in enumerate(X[1:]):
        diff = abs(x-X[i])
        diffs.append(diff)
    diffs.sort()
    # print(diffs)

    if N == 1:
        print(diffs[0])
    else:
        print(sum(diffs[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import math
N,M = list(map(int,input().split(" ")))
X = list(map(int,input().split(" ")))
X.sort()
diffs = []

if N >= M:
    print(0)
else:
    for i,x in enumerate(X[1:]):
        diff = abs(x-X[i])
        diffs.append(diff)
    diffs.sort()
    # print(diffs)
    print(sum(diffs[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import math
N,M = list(map(int,input().split(" ")))
X = list(map(int,input().split(" ")))
X.sort()
diffs = [0]

for i,x in enumerate(X[1:]):
    diff = abs(x-X[i])
    diffs.append(diff)
diffs.sort()
# print(diffs)
print(sum(diffs[:-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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] = abs(X[i+1] - X[i])

L.sort()

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

Submission status: Wrong Answer

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

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

x.sort()
kyori = [0]*(M-1)
for i in range(M-1):
    kyori[i] = (x[i+1] - x[i], i)
kyori.sort(key=lambda k: k[0], reverse=True)
kyori = kyori[:N-1]
kyori.sort(key=lambda k: k[1])
cost = 0
now = 0
for i in range(len(kyori)):
    t = kyori[i][1]
    if now < M:
        cost += x[t]-x[now]
    now += t+1
if now < M:
    cost += x[-1] - x[now]
print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
if N >= M:
    print(0)
else:
    diff = []
    for m in range(M-1):
        diff.append(X[m+1] - X[m])
    diff.sort()
    print(sum(diff[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
r=input().split()
n=int(r[0])
m=int(r[1])

if n<m:

    x=sorted([int(i) for i in input().split()])[::-1]
    xd=sorted([x[i]-x[i+1] for i in range(len(x)-1)])

    ans=(x[0]-x[-1])-sum(xd[-(n-1):])
else:
    ans=0
print(ans)

Submission status: Wrong Answer

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

X = list(map(int, input().split()))
X = sorted(X)

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

ans = X[M-1] - X[0]

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

print(ans - sum(diffs))

Submission status: Wrong Answer

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

X_list = list(np.diff(X))

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

X_list.sort(reverse=True)
sum(X_list[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]-X[i-1] for i in range(1, len(X))]
Y.sort()
length = len(Y) - N + 1
print(sum(Y[:length]))

Submission status: Wrong Answer

Solution code:
def solve(n, x_list, m):
    if n >= m:
        return 0

    # time O(M log(M))
    x_list.sort()

    # time O(M)
    diff_list = []
    for key in range(m - 1):
        diff = x_list[key + 1] - x_list[key]
        diff_list.append(diff)

    # time O(M log(M))
    diff_list.sort()

    return sum(diff_list[:-n+1])


def main():
    n, m = map(int, input().split())
    x_list = list(map(int, input().split()))

    ans = solve(n, x_list, m)
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
def solve(n, x_list):
    # time O(M log(M))
    x_list.sort()

    # time O(M)
    diff_list = []
    for key in range(len(x_list) - 1):
        diff = x_list[key + 1] - x_list[key]
        diff_list.append(diff)

    # time O(M log(M))
    diff_list.sort()

    return sum(diff_list[:-n+1])


def main():
    n, m = map(int, input().split())
    x_list = list(map(int, input().split()))

    ans = solve(n, x_list)
    print(ans)


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
B = [0]

N,M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
print(X)
for i in range(1,M):
    x = X[i]-X[i-1]
    B.append(x)
B = sorted(B)
B = B[::-1]
print(B)
print(X[-1]-X[0]-sum(B[:N-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
X = [int(_) for _ in stdin.readline().rstrip().split()]
X.sort()
diffs = [X[i+1]-X[i] for i in range(M-1)]
diffs.sort()
print(sum(diffs[:M-N]))

Submission status: Wrong Answer

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

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

Xs_sorted = sorted(Xs)

dists = [Xs_sorted[i+1]-Xs_sorted[i] for i in range(len(Xs_sorted)-1)]
dists_sorted = sorted(dists)

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

Submission status: Wrong Answer

Solution code:
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]

# n: コマ
# x: 目的地 len(x)=M
def main(N, M, Xm):
    # コマ数N >= 目的地数M: クリア
    if N >= M:
        print(0)
        return
    # 目的地は個体区別しないので、簡単のためソート
    Xm.sort()
    # コマ数N == 1: 1コマで端から端まで移動
    if N == 1:
        d = Xm[M-1] - Xm[0]
        print(d)
        return
    # 2 <= コマ数N < 目的地数M: どうする？
    # 発想を変えて、目的地間の距離に注目
    Dm = [Xm[i] - Xm[i-1] for i in range(1, M)]
    # 区間は、間隔が短い順に並ぶとする
    Dm.sort()
    print(Dm)
    # 全てのコマの移動方向は右とする
    # コマ1は一番左の目的地からスタート
    # コマ2～Nは右端の目的地から埋めていく
    # 1             5     4      3       2      コマ
    # x-x--x---x----x-----x------x-------x      目的地（区間が短い順）
    # あとは、コマ1が、コマNの隣まで進めばよい、その距離が答えとなる
    d = 0
    for i in range(M-N):
        d += Dm[i]
    print(d)

N, M = i2nn()
Xm = i2nn()
main(N, M, Xm)

"""
C - Streamline
https://atcoder.jp/contests/abc117/tasks/abc117_c

2 5
10 12 1 2 14
    5

3 7
-10 -3 0 9 -100 2 17
    19

100 1
-100000
    0
"""

Submission status: Wrong Answer

Solution code:
import numpy as np
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
a = np.array(a)

diff = a[:-1] - a[1:]
diff.sort()

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
if N >= M:
    print(0)
    exit()
diff = []
for i in range(1, len(X)):
    diff.append(X[i]-X[i-1])
diff.sort(reverse=True)
total = sum(diff)
for i in range(N-1):
    total -= diff[i]
    print(total)

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

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

Submission status: Wrong Answer

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

points = X[:]
points.sort()

diff_p = []
for i in range(1, len(points)):
    diff_p.append(points[i] - points[i-1])

diff_p.sort()
#print(diff_p)

ans = sum(diff_p[:M-N])
print(ans)

Submission status: Wrong Answer

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

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

import queue
q = queue.Queue()
q.put([[] for _ in range(N)])

import sys
INT_MAX = sys.maxsize
ans = INT_MAX

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N, M = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
diff = [abs(x[i] - x[i+1]) for i in range(M-1)]
diff.sort()
print(sum(diff[:M-N+1]) if N<=M else 0)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N, M = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
diff = [abs(x[i] - x[i+1]) for i in range(M-1)]
diff.sort()
ans = 0
print(ans + sum(diff[:M-N]))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N, M = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
diff = [abs(x[i] - x[i+1]) for i in range(M-1)]
diff.sort()
ans = 0
print(ans + sum(diff[:-N+1]))

Submission status: Wrong Answer

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