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: Runtime Error

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

X_dif = []

excep = 0

if M == 1:
    excep = M1
    
if not excep = M1:
    for i in range(M-1):
        X_dif.append(X[i+1]-X[i])
    
    X_dif.sort()
    X_dif.reverse()

    if len(X_dif) == 1:
        excep == X_dif1
    
    if not X_dif1:
        for N in range(N-1):
            X_dif.pop(0)

if excep == M1:
    print(1)
elif excep == X_dif1:
    print(2)
else:
    print(sum(X_dif))

Submission status: Runtime Error

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

X_dif = []

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

for N in range(N-1):
    X_dif.pop(0)
    
print(sum(X_dif))

Submission status: Runtime Error

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

if N < M:
    mar = [abs(X[i+1] - X[i]) for i in range(len(M-1))]
    mar.sort()
    print(sum([mar[i] for i in range(M-N)]))
else:
    print(0)

Submission status: Runtime Error

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

if N < M:
    mar = [X[i+1] - X[i] for i in range(len(M-1))]
    mar.sort()
    print(sum([mar[i] for i in range(M-N)]))
else:
    print(0)

Submission status: Runtime Error

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

if N <= M:
    mar = [X[i+1] - X[i] for i in range(len(M-1))]
    mar.sort()
    print(sum([mar[i] for i in range(M-N)]))
else:
    print(0)

Submission status: Runtime Error

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

XList = sorted(XList)
deltaList = []

for i in range(M-1) :
    deltaList.append(XList[i+1] - XList[i])
deltaList = sorted(deltaList, reverse=True)

for i in range(N-1) :
    deltaList[i] = 0
print(sum(deltaList))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
# C
if __name__ == '__main__':
    N, M = map(int, input().split())
    X_list = list(map(int, input().split()))
    if M == 1:
        print(0)
    elif N >= M:
        print(0)
    else:
        X_list = np.array(sorted(X_list))
        delta_list = [0] + [X_list[i] - X_list[i-1] for i in range(1, len(X_list))]
        res = []
        sorted_delta_list = sorted(delta_list, reverse=True)
        them = sorted_delta_list[N-1]
        res2 = []
        for i, delta in enumerate(delta_list):
            if delta > them:
                res.append(i)
            elif delta == them:
                res2.append(i)
        if len(res) < N-1:
            res += res2[:N-1-len(res)]
        res = [0] + sorted(res)
        result = 0
        for i, ii in zip(res[:-1], res[1:]):
            tmp = X_list[int(i):int(ii)]
            result += max(tmp) - min(tmp)
        tmp = X_list[int(ii):]
        result += max(tmp) - min(tmp)
        print(result)

Submission status: Runtime Error

Solution code:
import numpy as np
# C
if __name__ == '__main__':
#     N, M = map(int, input().split())
#     X_list = list(map(int, input().split()))
    if M == 1:
        print(0)
    elif N >= M:
        print(0)
    else:
        X_list = np.array(sorted(X_list))
        delta_list = [0] + [X_list[i] - X_list[i-1] for i in range(1, len(X_list))]
        res = []
        sorted_delta_list = sorted(delta_list, reverse=True)
        them = sorted_delta_list[N-1]
        res2 = []
        for i, delta in enumerate(delta_list):
            if delta > them:
                res.append(i)
            elif delta == them:
                res2.append(i)
        if len(res) < N-1:
            res += res2[:N-1-len(res)]
        res = [0] + sorted(res)
        result = 0
        for i, ii in zip(res[:-1], res[1:]):
            tmp = X_list[int(i):int(ii)]
            result += max(tmp) - min(tmp)
        tmp = X_list[int(ii):]
        result += max(tmp) - min(tmp)
        print(result)

Submission status: Runtime Error

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0
r = range(len(Xs))

mins = [min([abs(Xs[i]-Xs[j]) for j in r if Xs[i] is not Xs[j] and i<j]) for i in r]

mins.sort()
print(mins)
print(mins[:M-N])
count = sum(mins[:M-N])

if M-N > 0:
  print(count)
else:
  print(0)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
X=list(map(int,input().split()))
s=[]
sum=0
if M!=1:
    X.sort()
    for i in range(M-1):
        s.append(X[i+1]-X[i])
    t=[]
    for i in range(M-1):
        t.append(s[i])
    s.sort(reverse=True)
    buf=[[],[]]
    for i in range(N-1):
        buf[0].append(s[i])
        buf[1].append(t.index(s[i]))
    print(buf)
    buf[1].sort()
    for i in range(len(buf[1])):
        if i==0:
            sum+=X[buf[1][i]]-X[0]
        else:
            sum+=X[buf[1][i]]-X[buf[1][i-1]+1]
    sum+=X[M-1]-X[buf[1][-1]+1]
else:
    sum=0
    print(sum)

Submission status: Runtime Error

Solution code:
fl = input()
sl = input()

koma_num, pos_num = [int (x) for x in fl.split(" ")]
poses = [int(x) for x in sl.split(" ")]
poses.sort()

if koma_num >= pos_num:
    print("0")

dists = []
total = 0
for n in range(pos_num-1):
    dist = poses[n+1] - poses[n]
    dists.append(dist)
    total += dist

dists.sort()
dists.reverse()

for n in range(koma_num-1):
    total -= dists[n]

print(total)

Submission status: Runtime Error

Solution code:
fl = input()
sl = input()

koma_num, pos_num = [int (x) for x in fl.split(" ")]
poses = [int(x) for x in sl.split(" ")]
poses.sort()

dists = []
total = 0
for n in range(pos_num-1):
    dist = poses[n+1] - poses[n]
    dists.append(dist)
    total += dist

dists.sort()
dists.reverse()

for n in range(koma_num-1):
    total -= dists[n]

print(total)

Submission status: Runtime Error

Solution code:
fl = input()
sl = input()

koma_num, pos_num = [int (x) for x in fl.split(" ")]
poses = [int(x) for x in sl.split(" ")]
poses.sort()

dists = []
total = 0
for n in range(pos_num-1):
    dists.append(abs(poses[n+1] - poses[n]))
    total += poses[n+1] - poses[n]

dists.sort()
dists.reverse()

for n in range(koma_num-1):
    total -= dists[n]

print(total)

Submission status: Runtime Error

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0
 
#abss = []
maxs = []
for x in Xs:
  tmp = [abs(x-y) for y in Xs]
  #abss.append(tmp)
  maxs.appned(max(tmp))
 
maxs.sort()
sum(maxs[M-N:-1])
 
print(count)

Submission status: Runtime Error

Solution code:
import math
import copy
import bisect








def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input().split())
a=nCast(input().split())


def func():
    if n[1]>=n[0]:
        return 0
    list.sort(a)
    l=list()
    for idx in range(0,len(a)-1):
        l.append(a[idx+1]-a[idx])
    list.sort(l)
    l.reverse()
    result=sum(l)

    for idx in range(0,n[0]-1):
        result-=l[idx]
    return result
    
print(func())

Submission status: Runtime Error

Solution code:
N, M = [int(s) for s in input().split(" ")]
Xs = [int(s) for s in input().split(" ")]
count = 0

#abss = []
maxs = []
for x in Xs:
  tmp = [abs(x-y) for y in Xs]
  #abss.append(tmp)
  maxs.appned(max(tmp))

maxs.sort()
sum(maxs[M-N:-1]

print(count)

Submission status: Runtime Error

Solution code:
import math
import copy
import bisect








def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input().split())
a=nCast(input().split())


def func():
    list.sort(a)
    l=list()
    for idx in range(0,len(a)-1):
        l.append(a[idx+1]-a[idx])
    list.sort(l)
    l.reverse()
    result=sum(l)

    for idx in range(0,n[0]-1):
        result-=l[idx]
    return result
    
print(func())

Submission status: Runtime Error

Solution code:
import math
import copy
import bisect








def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input().split())
a=nCast(input().split())


def func():
    list.sort(a)
    if n[0]==1:
        return a[len(a)-1]-a[0]
    l=list()

    for idx in range(0,len(a)-1):
        l.append(a[idx+1]-a[idx])

    list.sort(l)
    l.reverse()
    result=sum(l)

    for idx in range(0,n[0]-1):
        result-=l[idx]
    return result
    
print(func())

Submission status: Runtime Error

Solution code:
import math
import copy
import bisect








def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input().split())
a=nCast(input().split())


def func():
    list.sort(a)
    if n[1]==1:
        return a[len(a)-1]-a[0]

    l=list()

    for idx in range(0,len(a)-1):
        l.append(a[idx+1]-a[idx])

    list.sort(l)
    l.reverse()
    result=sum(l)

    for idx in range(0,n[0]-1):
        result-=l[idx]
    return result
        
        
    
        
    
    
print(func())

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=[int(i) for i in input().split()]
if m==1 or n>m
    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: Runtime Error

Solution code:
import math
import copy
import bisect








def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input().split())
a=nCast(input().split())




def func():
    list.sort(a)
    if n[1]==1:
        return a[len(a)-1]-a[0]

    l=list()

    for idx in range(0,len(a)-1):
        l.append(a[idx+1]-a[idx])

    list.sort(l)
    l.reverse()
    result=sum(l)

    for idx in range(0,n[0]-1):
        result-=l[idx]
    return result
        
        
    
        
    
    
print(func())

Submission status: Runtime Error

Solution code:
def seek(old_list, current_score, subs):
    new_list = []
    min_index = subs.index(min(subs))
    current_score += min(subs)
    old_list[min_index] = old_list[min_index] | old_list[min_index+1]
    subs.remove(min(subs))
    old_list.remove(old_list[min_index+1])
    
    if(len(old_list) == N):
        return [], current_score
    else:
        return seek(old_list, current_score, subs)

N, M = map(int, input().strip().split())
komas = list(map(int, input().strip().split()))
komas.sort()
komas_set = [set([k]) for k in komas]

if(N >= M):
    print(0)
else:
    subs = [min(komas_set[i+1]) - max(komas_set[i]) for i in range(len(komas_set)-1)]
    print(seek(komas_set, 0, subs)[1])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def seek(old_list, current_score, subs):
    new_list = []
    min_index = subs.index(min(subs))
    current_score += min(subs)
    old_list[min_index] = old_list[min_index] | old_list[min_index+1]
    subs[min_index] = min(min(old_list[min_index+1]) - max(old_list[min_index]), min(old_list[min_index]) - max(old_list[min_index-1]))
    subs.remove(min(subs))
    old_list.remove(old_list[min_index+1])
    
    if(len(old_list) == N):
        return [], current_score
    else:
        return seek(old_list, current_score, subs)

N, M = map(int, input().strip().split())
komas = list(map(int, input().strip().split()))
komas.sort()
komas_set = [set([k]) for k in komas]

if(N >= M):
    print(0)
else:
    subs = [min(komas_set[i+1]) - max(komas_set[i]) for i in range(len(komas_set)-1)]
    print(seek(komas_set, 0, subs)[1])

Submission status: Runtime Error

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

# print(n, m, x)

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


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


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

#%%


#%%

Submission status: Runtime Error

Solution code:
n = list(map(int, input().split())) 
i = list(map(int, input().split())) 
i.sort()
if(n[0] >= n[1]):
  print(0)
  break
else:
	k=1
	kyori=list()
	while k < n[1]: 
	  sa=i[k]-i[k-1]
	  kyori.append(sa)
	  k=k+1
	kyori.sort()
	mm=1
	owari=n[1]-n[0]
	kyori=kyori[0:owari]
	idou=sum(kyori)
	print(idou)

Submission status: Runtime Error

Solution code:
# - coding : utf-8 -*-
 
n, m = input().split()
xs_str = input().split()
xs = list(map(int, xs_str))
sortted(xs)
 
ys = []
for i in range(int(m) - 1):
    ys.append(abs(xs[i+1] - xs[i]))
sorted(ys)
 
out = sum(ys[0:-(int(n)-1)])
print(out)

Submission status: Runtime Error

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

Submission status: Runtime Error

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
    diffcache.pop(k)
    counter = counter + 1

result = 0
for k,v in diffcache:
    result = result + v
print(result)

Submission status: Runtime Error

Solution code:
import sys

def split(g, n):
  gl = g[:-1]
  gr = g[1:]
  sub = [[(b - a), i+1] for a, b, i in zip(gl, gr, range(len(gl)))]
  sub = sorted(sub, key = lambda x: -x[0])
  j = []
  for i in range(n-1):
    j.append(sub[i][1])
  j = sorted(j)
  jl = [0] + j[:-1]
  res = []
  for l, r in zip(jl, j):
    s = g[l:r]
    res.append(s)
  L = j[-1]
  res.append(g[L:])
  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:
      if len(s) <= 0:
        continue
      ans += s[-1] - s[0]
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import copy
import itertools

def funcF(p):
    return p

def funcG(p):
    return funcF(p) + p*p

N, M = (int(i) for i in input().split())
X = list(int(i) for i in input().split())
X.sort()

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

# X[0] ... X[M-1] をN個のグループに分ける
# X[i]とX[i+1]の間でグループを分ける場合Cx=iとする（i = 0 .. M-2の個数(N-1)個のCombinationで求まる）

minS=max(X) - min(X)
for C in list(itertools.combinations(range(M-1), N-1)):
    tmpS = 0
    if C[0] > 0:
        tmpS += X[C[0]] - X[0]
    for i in range(N-2):
        if C[i+1] > C[i]+1:
            tmpS += X[C[i+1]] - X[C[i]+1]
    tmpS += X[M-1] - X[C[N-2]+1]
    if tmpS < minS:
        minS = tmpS

print("{}".format(minS))

Submission status: Runtime Error

Solution code:
nm = [int(i) for i in input().split()]
points = sorted([int(i) for i in input().split()])
n, m = nm[0], nm[1]

if n >= m:
    print(0)
elif n == 1:
    print(max(points) - min(points))
else:
    list_diff = [points[i+1] - points[i] for i in range(m-1)]
    list_th = []
    for i in range(n-1):
        max_index = list_diff.index(max(list_diff))
        list_th.append(max_index)
        list_diff[max_index] = 0
        
    x = points[0]
    th = 0
    count = 0
    print(list_th)
    while x < points[-1]:
        if x == points[list_th[th]]:
            x = points[list_th[th]+1]
            th += 1
        else:
            x += 1
            count += 1
            if x == points[-1]:
                break
    print(count)

Submission status: Runtime Error

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

x_sort = sorted(x_arr)
x_sub = []

if len(x_sort) == 1:
    print(0)
else:
    x_sub = [x_sort[i+1]-x_sort[i] for i in range(m-1) ]
    x_sub = sorted(x_sub)
    
    for i in range(n-1):
        x_sub.remove(x_sub[-1])
    
    print(sum(x_sub))

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = map(int,input().split(" "))
MA = map(int,input().split(" "))
A = list(MA)

arr_x = np.array(range(K+1))
arr_sum = np.zeros((N,K+1),dtype='int32')

for i, a in enumerate(A):
  arr_sum[i] = a ^ arr_x

print(max(arr_sum.sum(axis=0)))

Submission status: Runtime Error

Solution code:
def maxindex(lists):
    max_num = -1
    max_index = 0
    for i in range(len(lists)):
        if max_num <= lists[i]:
            max_num = lists[i]
            max_index = i
    return max_index

n,m = map(int,input().split())
if n >= m:
    print(0)
    exit(0)
lists = list(map(int,input().split()))
lists = sorted(lists)
lists2 = []
sumlists = []
for i in range(m-1):
    lists2.append(abs(int(lists[i]) - int(lists[i+1])))
i
for i in range(n-1):
    if len(lists2) <= 1:
        break
    a = maxindex(lists2)
    sumlists.append(lists[:a+1])
    lists = lists[a+1:]
    lists2.pop(a)
print(sumlists)
sumlists.append(lists)
sumlen = 0
for li in sumlists:
    sumlen += abs(max(li) - min(li))

    print(sumlen)

Submission status: Runtime Error

Solution code:
# -*- coding utf-8 -*-
N, M = (int(_) for _ in input().split())
X = [int(_) for _ in input().split()]
X.sort()
xs = []
for i in range(M-1):
    xs.append(X[i+1] - X[i])
new_xs = sorted(xs)
div_key = []
for i in range(N-1):
    div_key.append(new_xs[M-N+i])

sp = [X[0]]
gp = [X[M-1]]
k = 1
while k < N:
    i = 0
    while i < M:
        if xs[i] == div_key[k-1]:
            gp.append(X[i])
            sp.append(X[i+1])
            k += 1
            break
        i += 1

sp.sort()
gp.sort()

S = 0
for i in range(N):
    S += gp[i] - sp[i]

print(S)

Submission status: Runtime Error

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

x_sort = sorted(x_arr)
x_sub = []

if len(x_sort) == 1:
    print(0)
else:
    for i in range(m-1):
        x_sub.append(x_sort[i+1]-x_sort[i])
    
    x_sub = sorted(x_sub)
    
    for i in range(n-1):
        x_sub.remove(x_sub[-1])
    
    print(sum(x_sub))

Submission status: Runtime Error

Solution code:
i=list(map(int,input().split()))
X=list(map(int,input().split()))
sum=0

def distance(X):
    d=max(X)-min(X)
    return d

for i in range(len(X)-1):
    for j in range(i+1,len(X)):
        if X[i]>X[j]:
            tmp=X[i]
            X[i]=X[j]
            X[j]=tmp
for i in range(len(X)):
    sum=sum+X[i]
X_a=[]
X_b=[]
for i in range(len(X)):
    if X[i]<(sum/len(X)):
        X_a.append(X[i])
    else:
        X_b.append(X[i])

a=distance(X_a)
b=distance(X_b)
print(a+b)

Submission status: Runtime Error

Solution code:
import sys

def split(g, n):
  gl = g[:-1]
  gr = g[1:]
  sub = [[(b - a), i+1] for a, b, i in zip(gl, gr, range(len(gl)))]
  sub = sorted(sub, key = lambda x: -x[0])
  j = []
  for i in range(n-1):
    j.append(sub[i][1])
  j = sorted(j)
  jl = [0] + j[:-1]
  res = []
  for l, r in zip(jl, j):
    s = g[l:r]
    res.append(s)
  R = j[-1]
  res.append(g[R:])
  print(res)
  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:
      if len(s) <= 0:
        continue
      ans += s[-1] - s[0]
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
# - coding : utf-8 -*-
 
n, m = input().split()
xs_str = 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: Runtime Error

Solution code:
import sys

def split(g, n):
  gl = g[:-1]
  gr = g[1:]
  sub = [[(b - a), i+1] for a, b, i in zip(gl, gr, range(len(gl)))]
  sub = sorted(sub, key = lambda x: -x[0])
  j = []
  for i in range(n-1):
    j.append(sub[i][1])
  j = sorted(j)
  jl = [0] + j[:-1]
  res = []
  for l, r in zip(jl, j):
    s = g[l:r]
    res.append(s)
  R = j[-1]
  if R < len(g):
    res.append(g[R:])
  print(res)
  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: Runtime Error

Solution code:
import numpy as np

N,M = map(int,input().split())
Xlist = np.array([int(_) for _ in input().split()])
Xlist = np.sort(Xlist)
answer = 0
max_difflist = np.array([])
salist = np.array([])
ilist = np.array([])
min_n = -1

for i in range(M-1):
    sa = abs(Xlist[i]-Xlist[i+1])
    salist = np.append(salist,sa)

    if len(max_difflist)+1 < N:
        ilist = np.append(ilist,i)
        max_difflist = np.append(max_difflist,sa)

    elif sa > min_n:
        max_difflist = np.sort(max_difflist)[::-1]
        index = np.where(max_difflist == min_n)[0]
        max_difflist[index] = sa
        ilist[index] = i
    min_n = np.min(max_difflist)

for i_ in ilist:
    salist[int(i_)] = 0

print(int(sum(salist)))

Submission status: Runtime Error

Solution code:
def maxindex(lists):
    max_num = -1
    max_index = 0
    for i in range(len(lists)):
        if max_num <= lists[i]:
            max_num = lists[i]
            max_index = i
    return max_index
n,m = map(int,input().split())
lists = list(map(int,input().split()))
lists = sorted(lists)
lists2 = []
sumlists = []
for i in range(m-1):
    lists2.append(abs(int(lists[i]) - int(lists[i+1])))

for i in range(n-1):
    if len(lists2) <= 1:
        break
    a = maxindex(lists2)

    sumlists.append(lists[:a+1])
    lists = lists[a+1:]
    lists2.pop(a)
sumlists.append(lists)
sumlen = 0
for li in sumlists:
    sumlen += abs(max(li) - min(li))
print(sumlen)

Submission status: Runtime Error

Solution code:
n,m = input().split()
n = int(n)
m = int(m)
 
row_x = input().split()
for i in range(m):
    row_x[i] = int(row_x[i])
 
if n >= m:
    print(0)
else:
    difference = 0
    maximun = []
    x = sorted(row_x)
 
    for i in range(m-1):
        diffe = x[i+1]-x[i]
        difference += diffe
        if len(maximun) < (n-1):
            maximun.append(diffe)
        elif min(maximun) < diffe:
            maximun.append(diffe)  
            maximun.remove(min(maximun))
                     

    print(difference-sum(maximun))

Submission status: Runtime Error

Solution code:
n, m = [int(i) for i in input().split()]
x_arr = [int(i) for i in input().split()]
x_n = [[] for i in range(n)]

x_sort = sorted(x_arr)
x_sub = []

for i in range(m-1):
    x_sub.append(x_sort[i+1]-x_sort[i])

x_sub = sorted(x_sub)

for i in range(n-1):
    x_sub.remove(max(x_sub))

print(sum(x_sub))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // 移動方向

int n,m;
int x[100010];

void input(){
    cin >> n >> m;
    for(int i=0;i<m;i++) cin >> x[i];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    if (n >= m) cout << 0 << endl;
    else{
        int sum = 0;
        sort(x,x+m);
        vector<int> margin;
        for(int i=1;i<m;i++) margin.push_back(x[i]-x[i-1]);
        sort(margin.begin(), margin.end());
        for(int i=0;i<(n-1);i++) margin.pop_back();
        for(int i=0;i<margin.size();i++) sum += margin[i];
        cout << sum << endl;
    }
    return 0;
}

Submission status: Runtime Error

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

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

def unko3(tree):
  m = 0
  for arr in tree:
    tmp = unko2(arr)
    if tmp > m:
      a = arr
      m = tmp
  
  tree.remove(a)
  index = unko(a, len(a))
  arr1 = a[0:index+1]
  arr2 = a[index+1:]
  tree.append(arr1)
  tree.append(arr2)    
    

a = list(map(int, input().split()))
N, M = a[0], a[1]
b = list(map(int, input().split()))
b.sort()
ans = 0
tree = []
if M == 1:
  print(0)
elif N == 1:
  print(b[-1] - b[0])
else:
  index = unko(b, M)
  left = b[0:index+1]
  right = b[index+1:]
  tree.append(left)
  tree.append(right)
  N -= 1
  while N != 1:
    N-=1
    unko3(tree)
  for i in tree:
    ans += unko2(i)
  print(ans)