Problem p03137 (python)

Fixed submission:
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()
    print(sum(dist[:M-N]))

Buggy submission (verdict: Runtime Error):
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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

if len(first_set)==0:
    print(0)
else:

    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 new in rem:
                if fp==new:
                    continue
                    
                d = abs(fp-new)
                if d<best:
                    best = d
                    best_list = [fp, new]
        
#         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)

Buggy submission (verdict: Runtime Error):
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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

Buggy submission (verdict: Runtime Error):
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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

Buggy submission (verdict: Runtime Error):
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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

Buggy submission (verdict: Runtime Error):
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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

Buggy submission (verdict: Runtime Error):
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])