Buggy python submission (verdict: Runtime Error):

import sys

N, M = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]

if not nums:
    print(0)
    sys.exit()

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

mem = {}
nums.sort()

def find_max(pivot, places):

    if (pivot, len(places)) in mem:
        return mem[(pivot, len(places))]

    if len(places) == N:
        mem[(pivot, len(places))] = abs(nums[pivot] - nums[-1])
        return mem[(pivot, len(places))]

    local_min = float('inf')
    for i in range(pivot+1, len(nums) - (N - len(places) - 1)):
        tmp = abs(nums[pivot] - nums[i-1]) + find_max(i, places + [i])
        if tmp < local_min:
            local_min = tmp
    mem[(pivot, len(places))] = local_min
    return mem[(pivot, len(places))]

ans = find_max(0, [0])

print(ans)


Fixed submission:

import sys

N, M = [int(x) for x in input().split()]
nums = list(set([int(x) for x in input().split()]))

if not nums:
    print(0)
    sys.exit()

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

nums.sort()

rank = [(abs(nums[i] - nums[i-1]), (i-1, i)) for i in range(1, len(nums))]
rank = sorted(rank, reverse=True)[:N-1]

pos = [0] + [pos[1] for dis, pos in rank]
pos.sort()
ans = 0
for i in range(len(pos)):
    if i == len(pos)-1:
        ans += abs(nums[pos[i]] - nums[-1])
    else:
        ans += abs(nums[pos[i]] - nums[pos[i+1]-1])

print(ans)

Buggy python submission (verdict: Runtime Error):

import sys

N, M = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
nums.sort()

ans = [float('inf')]

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

mem = {}

def find_max(pivot, places):

    if (pivot, len(places)) in mem: return mem[(pivot, len(places))]

    if len(places) == N:
        mem[(pivot, len(places))] = abs(nums[pivot] - nums[-1])
        return mem[(pivot, len(places))]

    local_min = float('inf')
    for i in range(pivot+1, len(nums) - (N - len(places) - 1)):
        tmp = abs(nums[pivot] - nums[i-1]) + find_max(i, places + [i])
        if tmp < local_min:
            local_min = tmp
    mem[(pivot, len(places))] = local_min
    return mem[(pivot, len(places))]

ans = find_max(0, [0])

print(ans)



Fixed submission:

import sys

N, M = [int(x) for x in input().split()]
nums = list(set([int(x) for x in input().split()]))

if not nums:
    print(0)
    sys.exit()

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

nums.sort()

rank = [(abs(nums[i] - nums[i-1]), (i-1, i)) for i in range(1, len(nums))]
rank = sorted(rank, reverse=True)[:N-1]

pos = [0] + [pos[1] for dis, pos in rank]
pos.sort()
ans = 0
for i in range(len(pos)):
    if i == len(pos)-1:
        ans += abs(nums[pos[i]] - nums[-1])
    else:
        ans += abs(nums[pos[i]] - nums[pos[i+1]-1])

print(ans)

Buggy python submission (verdict: Runtime Error):

import sys

N, M = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
nums.sort()

ans = [float('inf')]

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


def find_max(pivot, places, s):
    if len(places) == N:
        s += abs(nums[places[-1]] - nums[-1])
        if s < ans[0]:
            ans[0] = s
        return

    for i in range(pivot+1, len(nums) - (N - len(places) - 1)):
        tmp = s + abs(nums[pivot] - nums[i-1])
        if tmp > ans[0]: continue
        find_max(i, places + [i], tmp)

find_max(0, [0], 0)

print(ans[0])


Fixed submission:

import sys

N, M = [int(x) for x in input().split()]
nums = list(set([int(x) for x in input().split()]))

if not nums:
    print(0)
    sys.exit()

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

nums.sort()

rank = [(abs(nums[i] - nums[i-1]), (i-1, i)) for i in range(1, len(nums))]
rank = sorted(rank, reverse=True)[:N-1]

pos = [0] + [pos[1] for dis, pos in rank]
pos.sort()
ans = 0
for i in range(len(pos)):
    if i == len(pos)-1:
        ans += abs(nums[pos[i]] - nums[-1])
    else:
        ans += abs(nums[pos[i]] - nums[pos[i+1]-1])

print(ans)