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 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)]
    order_idx = np.argsort(X_diff)
    #  print(X_diff)
    # 最大値の両脇にコマを置く
    # コマが１つしかおけない場合、外側に置く
    count = 0
    torn_point = []
    
    while count < N - 1:
        max_idx = max(order_idx)
        order_idx = [i for i in order_idx if i != max_idx]
        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()))
#print(N,M,X)
sort_X=sorted(X)
#print(sort_X)
#print(sort_X[1:],sort_X[0:len(sort_X)-1])
X_sa=[x - y for (x, y) in zip(sort_X[1:], sort_X[0:len(X)-1])]
#print(X_sa)
sort_X_sa=sorted(X_sa)
#print(sort_X_sa)
#print(sort_X_sa[0:M-N])
print(sum(sort_X_sa[0:M-N]))

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=[int(i) for i in input().split()]
if m==1:
    print(0)
else:
    x=sorted(x)

    if n==1:
        print(x[-1]-x[0])
    else:

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

        xsa=sorted(xsa)

        print(int(sum(xsa[0:(m-n)])))

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
if N >= M:
    print(0)
else:
    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
    equalcounter = 0
    equalcache = 0
    sorteddiff = sorted(diffcache.items(), key=lambda x: -x[1])
    for k, v in sorteddiff:
        if counter == N - 1:
            break
        if sorteddiff.count(v) == 1:
            rangeset.append(k)
        elif sorteddiff.count(v) != 1:
            if equalcounter < equalcache // 2:
                if equalcounter == 0:
                    equalcache = sorteddiff.count(v)
                sorteddiff.pop(k)
                equalcounter = equalcounter + 1
                continue
            else:
                equalcounter = 0
                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:
l = list(map(int, input().split()))

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

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

if 1 <= n and n <= 100000:
  if 1 <= m and m <= 100000:
    del x[m:]
    x.sort()

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

    y.sort()

    for i in range(len(y)):
      if y[i] == 0:
        break
    # print(y)
    del y[(len(y) - (n - 1)):]
    # print(y)

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

    print(total)

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)]
    order_idx = np.argsort(X_diff)
    #  print(X_diff)
    # 最大値の両脇にコマを置く
    # コマが１つしかおけない場合、外側に置く
    count = 0
    torn_point = []
    
    while count < N - 1:
        max_idx = order_idx[-1-count]
        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:
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
    for k in range(0, (Xs[-1] - Xs[0]) + 1):
        nans = psolve(k, Xs, N)
        if nans > ans:
            ans = min(ans, nans)
            break
        ans = min(ans, nans)
    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 = list(map(int, input().split()))
point = sorted(list(map(int, input().split())))
res = sorted([abs(point[i]-point[i+1]) for i in range(M-1)])
print(sum(res[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import copy

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

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

Xs.sort()
XsDist = [0]

for i in range(len(Xs)):
    if not i == 0:
        XsDist.append(Xs[i] - Xs[i-1])

# XsDistSorted = []
XsDistSorted = copy.deepcopy(XsDist)
XsDistSorted.sort()
XsDistTopN = XsDistSorted[-n:]

XsTopNYosoS = []
for num in XsDistTopN:
    XsTopNYosoS.append(XsDist.index(num))

XsTopNYosoS.sort()
XsTopNYosoS = XsTopNYosoS[:-1]

ans = 0

for i in range(len(Xs)):
    if not i == 0:        
        if not  i in XsTopNYosoS:
            ans += abs(abs(Xs[i])-abs(Xs[i-1]))

print(ans)

Submission status: Wrong Answer

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

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

if 1 <= n and n <= 100000:
  if 1 <= m and m <= 100000:
    del x[m:]
    x.sort()

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

    y.sort()
    # print(y)
    del y[(len(y) - (n - 1)):]
    # print(y)

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

    print(total)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import random

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

#print(g)

def kmeans(g, n):
        k = [random.randint(0, n-1) for i in range(len(g))]
        #print(k)

        for t in range(100000):
                pre_k = k[:]
                v = [0 for i in range(n)]
                nk = [0 for i in range(n)]
                for nn in range(n):
                        for i in range(len(g)):
                                if k[i] == nn:
                                        nk[nn] += 1
                                        v[nn] += g[i]
                        if nk[nn] != 0:
                                v[nn] /= nk[nn]
                #print(v)

                for i in range(len(g)):
                        min_value = 1000000000000000000000000000000000000000
                        for nn in range(n):
                                if min_value > abs(v[nn] - g[i]):
                                        min_value = abs(v[nn] - g[i])
                                        k[i] = nn
                
                #print(k)
                if pre_k == k:
                        break

        return k


k = kmeans(g, n if n <= m else m)

#print(g)
#print(k)

classes = [[10000000000, -10000000000] for i in range(n if n <= m else m)]
for i in range(len(g)):
        if g[i] < classes[k[i]][0]:
                classes[k[i]][0] = g[i]
        if g[i] > classes[k[i]][1]:
                classes[k[i]][1] = g[i]

#print(classes)

ret = 0
for i in classes:
       ret += abs(i[1] - i[0])
print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# - coding : utf-8 -*-
 
n, m = input().split()
xs = [int(i) for i in input().split()]
xs.sort()
 
ys = []
for x1, x2 in zip(xs[0:-1], xs[1:]):
    ys.append(abs(x2 - x1))
ys.sort()
 
out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

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

n, m = input().split()
xs = [int(i) for i in input().split()]
xs = np.array(xs)
xs_sorted = np.sort(xs)
 
ys = np.abs(np.diff(xs_sorted, n=1))
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())
Xs = list(map(int,input().split()))
if len(Xs) <= N:
    print(0)
else:
    Xs.sort()
    Xdivs = []
    for i in range(M-1):
        Xdivs.append(Xs[i+1]-Xs[i])
    Xdivs.sort()
    print(sum(Xdivs[:M-N+1]))

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 - M == 1:
        return sum(a) - max(a)
    
    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 d.get(i+1, 0) == 0:
            continue
        d[i+1] += v

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


print(m())

Submission status: Wrong Answer

Solution code:
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N >= M:
    print(0)
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

if M / 2 > N:
    for n in range(N-1):
        max_index = diff.index(max(diff))
        diff.pop(max_index)
    print(sum(diff))
    sys.exit()
else:
    count = 0
    for c in range(M-N-1):
        min_index = diff.index(min(diff))
        count += diff.pop(min_index)
    print(count)

Submission status: Wrong Answer

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

if n < m:
    x_far = [abs(x[i + 1] - x[i]) for i in range(m - 1)]
    x_far.sort(reverse = True)
    for i in range (n - 1):
        del x_far[0]
    print(sum(x_far))
else:
    print(m)

Submission status: Wrong Answer

Solution code:
import numpy as np
def dive_position(sa_list):
    position=np.argmax(sa_list)
    return(position)

def dis_count(start,goal):
    dis=goal-start
    return(dis)

N,M = map(int, input().split())
x_list = list(map(int, input().split())) # "1 2 3"と入力

x_list.sort()
#print(x_list)
sa_list=[x_list[i+1]-x_list[i] for i in range(M-1)]
#print(sa_list)

position_list=[-1,M-1]
distance=0
if N==1:
    print(x_list[-1]-x_list[0])
elif N>M:
    print(0)
else:
    for i in range(N-1):
            position_index=dive_position(sa_list)+i
            position_list.append(position_index)
            del sa_list[position_index-i]
    position_list.sort()
    for k in range(N):
        if position_list[k+1]-position_list[k]==1:
            dis=0
        else:
            dis=dis_count(x_list[position_list[k]+1],x_list[position_list[k+1]])
        distance+=abs(dis)
    print(distance)

Submission status: Wrong Answer

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

    if n==1:
        print(x[-1]-x[0])
    else:

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

        xsa=sorted(xsa)

        print(sum(xsa[0:(m-n)]))

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[(len(y) - (n - 1)):]

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

print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

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

if N > M:
    ans = 0
else:
    ans = 0
    X.sort()
    diff = []
    for i in range(len(X) - 1):
        diff.append(X[i + 1] - X[i])
    print(diff)
    diff_orig = copy.copy(diff)
    diff.sort()
    index = []
    for i in range(N - 1):
        index.append(diff_orig.index(diff.pop()))
    index.append(-1)
    index.sort()
    i = len(index) - 1
    index.append(len(X) - 1)
    while i >= 0:
        ans += X[index[i + 1]] - X[index[i] + 1]
        i -= 1
print(ans)

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()
print(y)
del y[(len(y) - (n - 1)):]
print(y)

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

print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    if n==1:
        print(x[-1]-x[0])
    else:

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

        xsa=sorted(xsa)

        print(xsa)

        print(sum(xsa[0:(m-n)]))

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 == 0:
            min_ptr = X.pop(0)
        elif cnt == 1:
            max_ptr = X.pop(-1)
        else:
            if (abs(min_ptr - X[0]) >= abs(max_ptr - X[-1])):
                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:
            min_ptr += 1
            if min_ptr == X[0]:
                X.pop(0)
        else:
            max_ptr -= 1
            if max_ptr == X[-1]:
                X.pop(-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:
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 d.get(i+1, 0) == 0:
            continue
        d[i+1] += v

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


print(m())

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()
print(y)
del y[(len(y) - (n - 1)):]
print(y)

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

print(total)

Submission status: Wrong Answer

Solution code:
import numpy as np

a1 = list(int(i) for i in input().split())  
x = list(int(i) for i in input().split())  
x = np.sort(x)
n = a1[0]

delta = []
for i in range(0,len(x) - 1):
    delta.append(x[i + 1] - x[i])

if n == 1:
    print(np.sum(delta))
else:
    delta = np.sort(delta)
    f = delta.size - (n - 2)
    print(np.sum(delta[0:-(n-1):]))

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)

ys = np.abs(np.diff(xs_sorted, n=1))
ys_sorted = np.sort(ys)
 
out = np.sum(ys_sorted[0:-(int(n)-1)])
print(out)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

def main(inp):
    N,M=map(int,next(inp).split())
    X=np.array(list(map(int,next(inp).split())))
    X=np.sort(X)
    
    if(N>M): return 0
    
#     print(N,M,X)
    
    delta=np.empty([M-1],dtype=int)
    
    for i in range(M-1):
        delta[i]=X[i+1]-X[i]
    
#     print(X)
#     print(delta)
    
    
    #delta=np.split(delta,[1,2])
    #print(delta)
    
    splpoint=np.argpartition(np.array(delta),-N+1)[-N+1:]
    splpoint=np.sort(splpoint)
    for i in range(len(splpoint)):
        splpoint[i]+=1
    
#     delta=np.delete(delta,splpoint)
#     print(delta)    
#     print(splpoint)
    
    spl=np.split(X,splpoint)
#     print(spl)
    
    ans=0
    for i in range(len(spl)):
        ans+=spl[i][len(spl[i])-1]-spl[i][0]
        
    return ans
  
print(main(sys.stdin))

Submission status: Wrong Answer

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

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 = X_diff.index(max(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 = input().split()
N = int(N)
M = int(M)
X = list(map(int, input().split(" ")))

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

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N, M = map(int, input().split())
points = list(map(int, input().split()))
points.sort()
koma = [points[i+1] - points[i] for i in range(M-1)]
sum_ = []
for i in range(M-N):
  koma.append(min(koma))
print(sum(sum_))

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
if N >= M:
    print(0)
else:
    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:
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())
Xs = list(map(int,input().split()))
if len(Xs) <= N:
    print(0)
else:
    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 = sorted(list(map(int,input().split())))
nums =[]
ans = 0

for i in range(m):
  if i==m-1:
    break
  nums.append(abs(x[i]-x[i+1]))
nums = sorted(nums)
sub = nums[n+1::]

for j in range(m):
  if m-1==j:
    break
  a = abs(x[j]-x[j+1])
  if a not in sub:
    ans+=a
  
print(ans)

Submission status: Wrong Answer

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


from sys import stdin

import time


############################################
#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:
            if x == len(data)-2:
                out += np.abs(data[x]-data[index[i-1]+1])
            else:
                out += data[-1]-data[x]
                out += np.abs(data[x]-data[index[i-1]+1])
        else:
            out += np.abs(data[x]-data[index[i-1]+1])

print(int(out))