Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
score = float('inf')
for x in itertools.combinations(h, k):
    score = min(score, max(x) - min(x))
print(score)

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
score = float('inf')
for x in itertools.combinations(h, k):
    score = min(score, max(x) - min(x))
print(score)

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000000)
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
def score(a):
    return max(a) - min(a)

def dfs(i, a):
    if len(a) == k:
        return score(a)

    s = float('inf')
    for x, y in enumerate(h[i:]):
        a.append(y)
        s = min(s, dfs(i + x + 1, a))
        a.pop()
    return s
print(dfs(0, []))

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

Buggy python submission (verdict: Runtime Error):
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = np.array(sorted(h))
diff = np.diff(h)
diff = np.tile(diff, (n - k + 1, 1))
mask_x = [[i] * (k - 1) for i in range(n - k + 1)]
mask_y = [range(i, i + k - 1) for i in range(n - k + 1)]
cost = np.sum(diff[mask_x, mask_y], axis=1)
print(cost.min())

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = np.array(sorted(h))
diff = np.diff(h)
low_score = np.inf
for i in range(n - k + 1):
    mask = np.arange(i, i + k - 1)
    cost = diff[mask].sum()
    low_score = min(low_score, cost)
print(low_score)

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = np.array(sorted(h))
diff = np.diff(h)
low_score = np.inf
for i in range(n - k + 1):
    cost = diff[i: i + k - 1].sum()
    low_score = min(low_score, cost)
print(low_score)

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k = map(int, input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h = np.array(sorted(h))
diff = np.diff(h)
low_score = np.inf
for i in range(n - k + 1):
    cost = np.sum(diff[i: i + k - 1])
    low_score = min(low_score, cost)
print(low_score)

Fixed submission:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))