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 numpy as np
N, M = map(int, input().split())
list_x = list(map(int, input().split()))
list_x.sort()
list_d = np.diff(list_x)
list_data = list_d.tolist()
if len(list_data) != 0:
    list_data.sort()
    del list_data[-(N-1):]
    print(sum(list_data))
else:
    print(0)

Submission status: Wrong Answer

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

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

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

L = sorted(L)

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

Submission status: Wrong Answer

Solution code:
def psolve(k, Xs, N):
    arr = [[]]
    i = Xs[0]
    for x in Xs:
        if x <= i + k:
            arr[-1].append(x)
        else:
            arr.append([])
            arr[-1].append(x)
            i = x
            if len(arr) > N:
                break

    if len(arr) <= N:
        ans = sum([max(a) - min(a) for a in arr])
        return ans
    else:
        return 10 ** 9


def solve(N, M, Xs):
    Xs = sorted(Xs)
    ans = 10 ** 9
    diffs = list(set(([Xs[i + 1] - Xs[i] for i in range(M - 1)])))
    k = 0
    for d in [0] + diffs:
        k += d
        ans = min(ans, psolve(k, Xs, N))
    return ans


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    Xs = list(map(int, input().split(" ")))
    print(solve(N, M, Xs))

Submission status: Wrong Answer

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

def diffculc(st, ed):
    if ed < st:
        tmp = ed
        ed = st
        st = tmp
    if st < 0 and ed < 0:
        diff = abs(st - ed)
    elif -1 < st and ed < 0:
        diff = abs(ed - st)
    elif st < 0 and -1 < ed:
        diff = abs(st - ed)
    elif -1 < st and -1 < ed:
        diff = ed - st
    return diff

xList = [int(x) for x in input().split()]
sortedList = sorted(xList)

# 0番目と1番目の間のdiffはdiffcache[0]
diffcache = {}
diff = 0
for i, x in enumerate(sortedList):
    if i == len(sortedList) - 1:
        break
    diffcache[i] = diffculc(sortedList[i+1], x)

rangeset = []
counter = 0
for k, v in sorted(diffcache.items(), key=lambda x: -x[1]):
    if counter == N - 1:
        break
    rangeset.append(k)
    counter = counter + 1

rangeset = sorted(rangeset)

result = 0
cache = 0
for i, x in enumerate(rangeset):
    if i == 0:
        st = sortedList[0]
        ed = sortedList[x]
    else:
        st = cache
        ed = sortedList[x]

    if st == ed:
        if x < len(sortedList) - 1:
            cache = sortedList[x+1]
        continue

    result = result + diffculc(st, ed)
    if x < len(sortedList) - 1:
        cache = sortedList[x+1]

# 最後の処理
if len(rangeset) > 0 and len(sortedList) > 0:
    if len(sortedList) > rangeset[len(rangeset)-1] + 1:
        st = sortedList[rangeset[len(rangeset)-1]+1]
        ed = sortedList[len(sortedList)-1]
        if st != sortedList[len(sortedList)-2]:
            result = result + diffculc(st, ed)
print(result)

Submission status: Wrong Answer

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

def m():
    a = [0] * M
    b = [0] * M
    for i in range(1, M):
        a[i] = abs(X[i] - X[i-1])
        b[i] = i

    if M <= N:
        return 0
    if N == 1:
        return sum(a)
    if N == 2:
        return sum(a) - max(a)
    c = list(zip(b,a))
    d = dict(c)
    c.sort(reverse=True,key=lambda x:x[1])
    for k in range(N-2):
        i, v = c[k]
        d[i] = 0
        if d[i-1] == 0:
            continue
        if i == M-1:
            continue
        if d[i+1] == 0:
            continue
        d[i+1] += v

    return sum(d.values()) - max(d.values())


print(m())

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
points=list(map(int,input().split()))
points.sort()
score=0
score=points[-1]-points[0]
sas=[]

for i in range(0,m-1):
  sas.append(points[i+1]-points[i])
sas.sort()

score-=sum(sas[-n+1:]) 
print(score)

Submission status: Wrong Answer

Solution code:
# 整数の入力
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()

# 距離の和が最小：範囲間の隙間が最大値だとよい。
# よって、ソートしたでかい順に引けばよい
X.sort()


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

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

    
    ans = sa
    
    for i in range(N-1):
        ans = ans-Y[M-i-2]
        

    
    print(ans)

Submission status: Wrong Answer

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

# crending
X.sort()
if min(X) < 0:
    X = list(map(lambda x:x+abs(min(X)), X))

# differencial
X2 = [X[i] - X[i-1] for i in range(1, cnt)] + [0]

# slice
s,r = 0,0
for i in range(N-1):
    if len(X2) <= N:
        X2 = [0]
        break
    if len(X2) == 0:
        break
    key = X2.index(max(X2))
    tmp = X2[s:key]
    r += sum(tmp)
    s = key
    X2 = X2[s+1:]
r += sum(X2)

print(r)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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:
# coding:utf-8

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

X.sort()

def hoge(N, M, X):

    # 昇順と降順のポインタ
    min_ptr = 0
    max_ptr = 0

    # ポインタ配置
    cnt = 0
    for i in range(N):
        if cnt >= 2:
            if (abs(min_ptr - X[0]) >= abs(max_ptr - X[-1])):
                min_ptr = X.pop(0)
            else:
                max_ptr = X.pop(-1)
        else:
            if i % 2 == 0:
                min_ptr = X.pop(0)
            else:
                max_ptr = X.pop(-1)
        cnt += 1

    # コマの移動開始
    counter = 0
    while True:
        if len(X) == 0:
            break
        min_len = abs(min_ptr - X[0])
        max_len = abs(max_ptr - X[-1])
        if min_len <= max_len:
            if min_len == 1:
                X.pop(0)
            min_ptr += 1
        else:
            if max_len == 1:
                X.pop(-1)
            max_ptr -= 1
        counter += 1
    
    return counter

if N >= M:
    print(0)
else: 
    if N == 1:
        print(abs(X[-1] - X[0]))
    else:
        c = hoge(N, M, X)
        print(c)

Submission status: Wrong Answer

Solution code:
line = input().split(" ")
n = int(line[0])
m = int(line[1])
line = input().split(" ")
x = []
s = []

for i in line:
    x.append(int(i))

x.sort()
count = x[m-1] - x[0]

for i in range(len(x)-1):
    s.append(int(x[i+1] - x[i]))

s.sort()
if(count - sum(s[-(n - 1):]) > 0):
    print(count - sum(s[-(n - 1):]))
else:
    print("0")

Submission status: Wrong Answer

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

n = l[0]
m = l[1]

x = list(map(int, input().split()))
y = []
total = 0

del x[m:]
x.sort()

for i in range(1, len(x)):
  y.append(x[i] - x[i - 1])

y.sort()
del y[:n]

for i in range(len(y)):
  total += y[i]

print(total)

Submission status: Wrong Answer

Solution code:
from sys import stdin


############################################
#from sys import stdin
import numpy as np

# read data for n sequences.
input = stdin.readline().split()
m = int(input[0])
n = int(input[1])
#n = int(stdin.readline().split()[1])
data = np.zeros(n)
d = stdin.readline().split()
for i,x in enumerate(d):
    data[i] = int(x)
data = sorted(data)

dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]

out = 0

# split in m data.
#print(data)
dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]
maxdif = sorted(dif, reverse=True)[:m-1]
index=[]
for i, x in enumerate(dif):
    if x in maxdif: 
        index.append(i)

# calclulate out
for i,x in enumerate(index):
    if not x == 0:
        if i==0:
            out += data[x]-data[0]
            if i==len(index)-1:
                out += data[-1]-data[x+1]
        elif x==len(data):
            None
        elif i==len(index)-1:
            out += data[-1]-data[x]
        else:
            out += np.abs(data[x]-data[index[i-1]+1])

print(int(out))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
points=list(map(int,input().split()))
points.sort()
score=0
score=points[-1]-points[0]
sas=[]

for i in range(0,m-1):
  sas.append(points[i+1]-points[i])
sas.sort()

if sas==[]:
  print(0)
  exit()

score-=sum(sas[-n+1:]) 
print(score)

Submission status: Wrong Answer

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

C = [[x] for x in X]

while len(C) > N:
  i = 1
  while i < len(C)-1:
    c = C[i]
    if abs(c[0] - C[i-1][-1]) > abs(c[-1] - C[i+1][0]):
      idx = i+1

    else:
      idx = i-1
    C[idx].extend(c)
    C[idx].sort()
    C.remove(c)
    
    if len(C) <= N:
      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())
X = [0] * M
X[0:M] = map(int, input().split())
if N >= M:
    print(0)
else:
    Li = sorted(X)
    dff = [abs(X[i] - X[i - 1]) for i in range(1, M)]
    d = sorted(dff)
    out = sum(d[:M-N])
    print(out)

Submission status: Wrong Answer

Solution code:
import sys

def split(g, n):
  gl = g[:-1]
  gr = g[1:]
  sub = [[(b - a), i] for a, b, i in zip(gl, gr, range(len(gl)))]
  i = sub.index(max(sub)) + 1
  sub = sorted(sub, key = lambda x: -x[0])
  j = []
  for i in range(n-1):
    j.append(sub[i][1]+1)
  j = sorted(j)
  jl = [0] + j[:-1]
  res = []
  for l, r in zip(jl, j):
    s = g[l:r]
    res.append(s)
  return res


def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  g = [int(a) for a in input().split()]
  g = sorted(g)
  ans = 0
  if N < M:
    sp = split(g, N)
    for s in sp:
      ans += s[-1] - s[0]
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
L = sorted(L)
ans = 0
cnt = 0

if N >= M :
    ans = 0
else :
    left = L[0]
    right = L[-1]
    L = L[1:-1]

    cnt = 2
    #print(L)
    while cnt < N :
        if abs(left - L[0]) < abs(right - L[-1]) :
            right = L[-1]
            L = L[:-1]
        else :
            left = L[0]
            L = L[1:]
        cnt += 1
    #print(L)
    for i in range(len(L)) :
        if abs(left - L[i]) <= abs(right - L[i]) :
            ans += abs(left - L[i])
            left = L[i]
        else :
            ans += abs(right - L[i])
            break

print(ans)
#print(L)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import numpy as np

sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, M = LI()
    X = LI()
    #  print(N, M, X)

    X.sort()
    #  print(X)

    if N > M:
        print(0)
        return
    if N == 1:
        # 端から端までの距離
        print(X[-1] - X[0])
        return


    N_position_list = []
    # 各xの差をとる
    X_diff = [X[i+1] - X[i] for i in range(len(X)-1)]
    #  print(X_diff)
    # 最大値の両脇にコマを置く
    # コマが１つしかおけない場合、外側に置く
    count = 0
    torn_point = []
    while count < N - 1:
        max_idx = np.argmax(X_diff)
        torn_point.append(max_idx)
        X_diff[max_idx] = -10**5-1
        count += 1

   
    #  print('torn_point', torn_point)
    # あとは移動距離の計算
    # listを分断していく
    torn_list = []
    for i in range(len(torn_point)):
        if i == 0:
            torn_list.append(X[0:torn_point[i]+1])
        else:
            torn_list.append(X[torn_point[i-1]+1:torn_point[i]+1])
    else:
        torn_list.append(X[torn_point[-1]+1:])

    #  print('torn_list', torn_list)

    move_count = 0
    for l in torn_list:
        if len(l) >= 2:
            # 端から端までを加算
            move_count += l[-1] - l[0]

    print(move_count)



main()

Submission status: Wrong Answer

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

def m():
    a = [0] * M
    b = [0] * M
    for i in range(1, M):
        a[i] = abs(X[i] - X[i-1])
        b[i] = i

    if M <= N:
        return 0
    if N == 1:
        return sum(a)
    if N == 2:
        return sum(a) - max(a)
    c = list(zip(b,a))
    d = dict(c)
    c.sort(reverse=True,key=lambda x:x[1])
    for k in range(N-2):
        i, v = c[k]
        d[i] = 0
        if d[i-1] == 0:
            continue
        if i != M-1:
            d[i+1] += v

    return sum(d.values()) - max(d.values())


print(m())

Submission status: Wrong Answer

Solution code:
from sys import stdin


############################################
#from sys import stdin
import numpy as np

# read data for n sequences.
input = stdin.readline().split()
m = int(input[0])
n = int(input[1])
#n = int(stdin.readline().split()[1])
data = np.zeros(n)
d = stdin.readline().split()
for i,x in enumerate(d):
    data[i] = int(x)
data = sorted(data)

dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]

out = 0

# split in m data.
#print(data)
dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]
maxdif = sorted(dif, reverse=True)[:m-1]
index=[]
for i, x in enumerate(dif):
    if x in maxdif: 
        index.append(i)

# calclulate out
for i,x in enumerate(index):
    if not x == 0:
        if i==0:
            out += data[x]-data[0]
            if i==len(index)-1:
                out += data[-1]-data[x+1]
        elif x==len(data):
            None
        elif i==len(index):
            out += np.sum(data[x:])
        else:
            out += np.abs(data[x]-data[index[i-1]+1])

print(int(out))

Submission status: Wrong Answer

Solution code:

# ipnut
N, cnt = map(int, input().split(" "))
X = list(map(int, input().split(" ")))

# crending
X.sort()
if min(X) < 0:
    X = list(map(lambda x:x+abs(min(X)), X))

# differencial
X2 = [X[i] - X[i-1] for i in range(1, cnt)] + [0]

# slice
s,r = 0,0
for i in range(N-1):
    if len(X2) == 0:
        break
    key = X2.index(max(X2))
    tmp = X2[s:key]
    r += sum(tmp)
    s = key
    X2 = X2[s+1:]
r += sum(X2)

print(r)

Submission status: Wrong Answer

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

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

if N==1:
	print(sum(Y))
	exit(0)
elif N>=M:
	print(0)
	exit(0)
else:
	Z=[0 for _ in range(M)]
	for i in range(N):
		index=Y.index(max(Y))
		if Z[index]==0 and Z[index+1]==0:
			if index-1<0:
				left=float("inf")
			else:
				left=index-1

			if index+1>=len(Y):
				right=float("inf")
			else:
				right=index+1

			if left>=right:
				tmp=index
			else:
				tmp=index+1

		elif Z[index]==0 and Z[index+1]==1:
			tmp=index
			Y[index]=0

		elif Z[index]==1 and Z[index+1]==0:
			tmp=index+1
			Y[index]=0

		Z[tmp]=1
	ans=0
	for i in range(len(Z)-1):
		if Z[i]==0 or Z[i+1]==0:
			ans+=Y[i]
			Z[i+1]=1
	print(ans)

Submission status: Wrong Answer

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

xList = [int(x) for x in input().split()]
sortedList = sorted(xList)

# 0番目と1番目の間のdiffはdiffcache[0]
diffcache = {}
diff = 0
for i, x in enumerate(sortedList):
    if i == len(sortedList) - 1:
        break

    if sortedList[i+1] < 0 and x < 0:
        diff = abs(sortedList[i+1] - x)
    elif sortedList[i+1] < 0 and -1 < x:
        diff = abs(sortedList[i+1] + x)
    elif -1 < sortedList[i+1] and x < 0:
        diff = abs(sortedList[i+1] + x)
    elif -1 < sortedList[i+1] and -1 < x:
        diff = sortedList[i+1] - x
    diffcache[i] = diff


rangeset = []
counter = 0
for k, v in sorted(diffcache.items(), key=lambda x: -x[1]):
    if counter == N - 1:
        break
    rangeset.append(k)
    counter = counter + 1

rangeset = sorted(rangeset)

result = 0
cache = 0
for i, x in enumerate(rangeset):
    if i == 0:
        st = sortedList[0]
        ed = sortedList[x]
    else:
        st = cache
        ed = sortedList[x]

    if st == ed:
        if x != len(sortedList) - 1:
            cache = sortedList[x+1]
        continue

    if st < 0 and ed < 0:
        diff = abs(st - ed)
    elif -1 < st and ed < 0:
        diff = abs(ed - st)
    elif st < 0 and -1 < ed:
        diff = abs(st - ed)
    elif -1 < st and -1 < ed:
        diff = ed - st
    result = result + diff
    cache = sortedList[x+1]

# 最後の処理
if len(rangeset) > 0:
    if len(sortedList) > rangeset[len(rangeset)-1] + 1:
        st = sortedList[rangeset[len(rangeset)-1]+1]
        ed = sortedList[len(sortedList)-1]
        if st != sortedList[len(sortedList)-2]:
            if st < 0 and ed < 0:
                diff = abs(st - ed)
            elif -1 < st and ed < 0:
                diff = abs(ed - st)
            elif st < 0 and -1 < ed:
                diff = abs(st - ed)
            elif -1 < st and -1 < ed:
                diff = ed - st
            result = result + diff
print(result)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

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

X.sort()

def hoge(N, M, X):

    # 昇順と降順のポインタ
    min_ptr = 0
    max_ptr = 0

    # ポインタ配置
    cnt = 0
    for i in range(N):
        if cnt >= 2:
            if (abs(min_ptr - X[0]) >= abs(max_ptr - X[-1])):
                min_ptr = X.pop(0)
            else:
                max_ptr = X.pop(-1)
        else:
            if i % 2 == 0:
                min_ptr = X.pop(0)
            else:
                max_ptr = X.pop(-1)
        cnt += 1

    # コマの移動開始
    counter = 0
    while True:
        if len(X) == 0:
            break
        min_len = abs(min_ptr - X[0])
        max_len = abs(max_ptr - X[-1])
        if min_len <= max_len:
            if min_len == 1:
                X.pop(0)
            min_ptr += 1
        else:
            if max_len == 1:
                X.pop(-1)
            max_ptr -= 1
        counter += 1
    
    return counter

if N >= M:
    print(0)
else: 
    if N == 1:
        print(X[-1] - X[0])
    else:
        c = hoge(N, M, X)
        print(c)

Submission status: Wrong Answer

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

    if M == 1:
        print(0)
    elif N == 1:
        print(max(X) - min(X))
    else:
        dx = sorted([abs(X[m] - X[m + 1]) for m in range(M - 1)])
        print(sum(dx[:M - N]))


main()

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
tmpX =list(input().split())
X = [int(s) for s in tmpX]
X.sort()
X_difference=[]
for i in range(M-1):
    X_difference.append(X[i+1]-X[i])
print(X_difference)
X_difference.sort(reverse = True)
del X_difference[0:N-1:]
print(sum(X_difference))

Submission status: Wrong Answer

Solution code:
line = input().split(" ")
n = int(line[0])
m = int(line[1])
line = input().split(" ")
x = []
s = []

for i in line:
    x.append(int(i))

x.sort()
count = x[m-1] - x[0]

for i in range(len(x)-1):
    s.append(int(x[i+1] - x[i]))

s.sort()
print(count - sum(s[-(n - 1):]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


from sys import stdin


############################################
#from sys import stdin
import numpy as np

# read data for n sequences.
input = stdin.readline().split()
m = int(input[0])
n = int(input[1])
#n = int(stdin.readline().split()[1])
data = np.zeros(n)
d = stdin.readline().split()
for i,x in enumerate(d):
    data[i] = int(x)
data = sorted(data)

dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]

out = 0

# split in m data.
#print(data)
dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]
maxdif = sorted(dif, reverse=True)[:m-1]
index=[]
for i, x in enumerate(dif):
    if x in maxdif: 
        index.append(i)

# calclulate out
for i,x in enumerate(index):
    if not x == 0:
        if i==0:
            out += np.sum(data[:x])
        elif x==len(data):
            None
        else:
            out += np.abs(data[x]-data[index[i-1]+1])

print(out)
            
        

    
#del data[index]
#print(data)

    


topdif = sorted(dif, reverse=True)[:m]
topkey=[]
for i,x in enumerate(dif):
    if x in topdif:
        topkey.append(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
nm = list(map(int, input().split()))
lis = list(map(int, input().split()))

lis.sort()

sum = 0
diff = []

if nm[1] - nm[0] <= 0:
    print(0)
else:
    for i in range (nm[1] - 1):
        diff.append(lis[i + 1] - lis[i])

    diff.sort()
    print(diff)

    if nm[0] != 1:
        for j in range (nm[0] - 2):
            diff.pop(-1)

    for k in range (nm[1] - nm [0]):
        sum = sum + diff[k]

    print(sum)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
L = sorted(L)
#print(L)
ans = 0
cnt = 0
if N >= M :
    ans = 0
else :
    left = L[0]
    right = L[-1]
    L = L[1:-1]
    cnt = 2

    #print(L)
    while len(L) :
        if abs(left - L[0]) < abs(right - L[-1]) :
            if cnt < N :
                cnt += 1
            else :
                ans += abs(left - L[0])
            left = L[0]
            L = L[1:]
        else :
            if cnt < N :
                cnt += 1
            else :
                ans += abs(right - L[-1])
            right = L[-1]
            L = L[:-1]

print(ans)
#print(cnt)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

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

X.sort()

def hoge(N, M, X):
    min_ptr = 0
    max_ptr = 0
    # ポインタ配置
    FINISH_X = []
    for i in range(N):
        if i % 2 == 0:
            min_ptr = X.pop(0)
        else:
            max_ptr = X.pop(-1)

    # コマの移動開始
    counter = 0
    while True:
        if len(X) == 0:
            break
        min_len = abs(min_ptr - X[0])
        max_len = abs(max_ptr - X[-1])
        if min_len <= max_len:
            if min_len == 1:
                X.pop(0)
            min_ptr += 1
        else:
            if max_len == 1:
                X.pop(-1)
            max_ptr -= 1
        counter += 1
    
    return counter

if N >= M:
    print(0)
else: 
    if N == 1:
        print(X[-1] - X[0])
    else:
        c =hoge(N, M, X)
        print(c)

Submission status: Wrong Answer

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

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

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

out = np.sum(ys_sorted[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
X=sorted(list(map(int,input().split())))
init=[]
tmp=0
i=1

if m<n:
  n=m

while i<len(X):
  init.append([X[i]-X[i-1],i])
  i+=1
init.sort(reverse=True,key=lambda x:x[0])
init=list(map(lambda x:x[1],init))
ans=0
init=sorted(init[:n-1])
tmp=0
for i in init:
  ans+=(X[i-1]-X[tmp])
  tmp=i
try:
  if i!=len(X)-1:
    ans+=X[-1]-X[i]
except IndexError:
  print(0)
  exit()
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()
    y = []
    for i in range(len(x)-1):
        y.append(x[i+1]-x[i])
    y.sort()
    print(y)
    number = 0
    for i in range(len(y)-n+1):
        number = number + y[i]
        print(number)
    print(number)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import combinations, product

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N, M = il()
    X = sorted(il())
    if M == 1:
        return 0
    ans = 999999999999
    for c in combinations(range(1, M), N - 1):
        score = 0
        for i in np.split(X, c):
            score += max(i.max() - i.min(), 0)
        ans = min(ans, score)
    return ans


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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint as pp
N, M = map(int, input().split())
Xm = list(map(int, input().split()))

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

K = N

# center
Xm.sort()
subt = []
for i in range(M-1):
    subt.append(abs(Xm[i+1]-Xm[i]))

subt.sort(reverse=True)

ans = 0
for k in subt:
    if K >= 2:
        K -= 2
    elif K == 1:
        K -= 1
    else:
        ans += k

print(ans)

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 N > M:
  print(0)
elif 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:
N,M =map(int,input().split( ))
X = list(map(int,input().split( )))


if N >= M or M == 1:
	ans = 0
elif N == 1:
	ans = X[-1] -X[0]
else:
	X.sort()
	D = []
	for i in range(M-1):
		d = X[i+1] - X[i]
		D.append([i,d])
	#print(X)

	D.sort(key = lambda x:(x[1]),reverse =True)
	#print(D)
	sikiri = []
	for i in range(N-1):
		sikiri.append(D[i][0])
	sikiri.sort()
	ans = X[sikiri[0]] - X[0]
	for i in range(N-1):
		l = sikiri[i]+1
		if i == N-2:
			r = M-1
		else:
			r = sikiri[i+1]
		ans+= X[r]-X[l]
print(ans)

Submission status: Wrong Answer

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

D = []
for m in range(1, M):
    D.append(X[m] - X[m-1])
D.sort()
print(sum(D[0:M-N]))

Submission status: Wrong Answer

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

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

if N==1:
	print(sum(Y))
	exit(0)
elif N>=M:
	print(0)
	exit(0)
else:
	Z=[0 for _ in range(M)]
	for i in range(N):
		index=Y.index(max(Y))
		if Z[index]==0 and Z[index+1]==0:
			if index-1<0:
				left=0
			else:
				left=index-1
			if index+1>=len(Y):
				right=len(Y)-1
			else:
				right=index+1
			if left>=right:
				tmp=index
			else:
				tmp=index+1
		elif Z[index]==0 and Z[index+1]==1:
			tmp=index
			Y[index]=0
		elif Z[index]==1 and Z[index+1]==0:
			tmp=index+1
			Y[index]=0
		Z[tmp]=1
	ans=0
	for i in range(len(Z)-1):
		if Z[i]==0 or Z[i+1]==0:
			ans+=(X[i+1]-X[i])
			Z[i+1]=1
	print(ans)

Submission status: Wrong Answer

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

if M<=N:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import combinations, product

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N, M = il()
    X = sorted(il())
    if M == 1:
        return 0
    ans = 999999999999
    for c in combinations(range(M), N - 1):
        score = 0
        for i in np.split(X, c):
            if len(i) == 0:
                continue
            score += max(i.max() - i.min(), 0)
        ans = min(ans, score)
    return ans


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

Submission status: Wrong Answer

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