Problem: 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.

Submission attempts (in order):
import numpy as np

# N = 3
# M = 7


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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print(first_set)

rem = list(set(x_list) - set(first_set))
print(rem)

cost = 0

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    if cost==0:
        candidate = first_set
    else:
        candidate = [new_set]
    
    for fp in list(candidate):
        for r in rem:
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print(best, best_list)
    cost += best
    
    new_set = best_list[1]
    rem = set(rem) - set([new_set])
print(cost)

import numpy as np

# N = 3
# M = 7


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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print(first_set)

rem = list(set(x_list) - set(first_set))
print(rem)

cost = 0

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    if cost==0:
        candidate = first_set
    else:
        candidate = [new_set]
    
    for fp in list(candidate):
        for r in rem:
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print(best, best_list)
    cost += best
    
    new_set = best_list[1]
    rem = set(rem) - set([new_set])
print(cost)

import numpy as np

# N = 3
# M = 7


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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print(first_set)

rem = list(set(x_list) - set(first_set))
print(rem)

cost = 0

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    if cost==0:
        candidate = first_set
    else:
        candidate = [new_set]
    
    for fp in list(candidate):
        for r in rem:
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print(best, best_list)
    cost += best
    
    new_set = best_list[1]
    rem = set(rem) - set([new_set])
print(cost)

import numpy as np

# N = 3
# M = 7


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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print("First Set", first_set)

rem = list(set(x_list) - set(first_set))
print("First Remain", rem)

cost = 0

current_point = first_set

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    for fp in current_point:
        for r in rem:
            if fp==r:
                continue
                
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print("Next", best, best_list)
    cost += best
    
    bef_point = best_list[0]
    new_point = best_list[1]
    rem = set(rem) - set([new_point])
    
    idx = current_point.index(bef_point)
    current_point[idx] = new_point
    
print(cost)

debug = 0
"""
N: state num
M: city num
"""


if debug:
    N = 2
    m = 10
else:
    (N, M) = list(map(int, input().split()))

if debug:
    p_y_list = [
        [1, 30]
        ,[1, 20]
        ,[1, 10]
        ,[1, 3]
        ,[2, 63]
        ,[2, 33]
        ,[2, 23]
        ,[3, 21]
        ,[3, 7]
    ]

city_id_list = []
for num, z in enumerate(range(M)):
# for num, (P, Y) in enumerate(p_y_list):
    
    if not debug:
        (P, Y) = list(map(int, input().split()))

    city_id_list.append([Y, P, num])
    
city_id_list = sorted( city_id_list, key= lambda x: x[0])

state_cnt = {}
tmp_list = []
for (Y, P, num) in city_id_list:
    
    if P not in state_cnt.keys():
        state_cnt[P] = 1
    elif P in state_cnt.keys():
        state_cnt[P] += 1
        
    tmp_id = str(P).zfill(6) + str(state_cnt[P]).zfill(6)
    
    tmp_list.append([num, tmp_id])
    
for cid in sorted(tmp_list, key=lambda x: x[0]):
    print(cid[1])

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

if N>=M:
    print(0)
else: 
    dist = [b-a for a, b  in zip(X, X[1:])]
    dist.sort()
    if (N-1)==0:
        print(sum(dist[:M-N]))
    else:
        print(sum(dist[:-(N-1)]))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted