Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        th = hs[i:i + K]
        r = min(r, th[-1] - th[0])
        i += 1
    print(r)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, max(hs[i:i + K]) - min(hs[i:i + K]))
        i += 1
    print(r)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))


def check(k, hs, mx, mn, r, memo):
    if (k, mx, mn) in memo:
        return memo[(k, mx, mn)]
    if mx - mn > r:
        return r
    if k == 0:
        return min(r, mx - mn)
    for i, h in enumerate(hs):
        hs.remove(h)
        r = min(r, check(k - 1, hs[::], max(mx, h), min(mn, h), r, memo))
        hs.insert(i, h)
    memo[(k, mx, mn)] = r
    return r


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9, 10 ** 9, {}))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))


def check(k, hs, mx, mn, r):
    if mx - mn > r:
        return r
    if k == 0:
        return min(r, mx - mn)
    for i, h in enumerate(hs):
        hs.remove(h)
        r = min(r, check(k - 1, hs[::], max(mx, h), min(mn, h), r))
        hs.insert(i, h)
    return r


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9, 10 ** 9))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))


def check(k, hs, mx, mn, r):
    if mx - mn > r:
        return r
    if k == 0:
        return min(r, mx - mn)
    for i, h in enumerate(hs):
        hs.remove(h)
        r = min(r, check(k - 1, hs, max(mx, h), min(mn, h), r))
        hs.insert(i, h)
    return r


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9, 10 ** 9))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))


def check(k, hs, mx, mn, memo):
    if (k, mx, mn) in memo:
        return memo[(k, mx, mn)]
    if k == 0:
        return mx - mn
    r = []
    for i, h in enumerate(hs):
        hs.remove(h)
        r.append(check(k - 1, hs, max(mx, h), min(mn, h), memo))
        hs.insert(i, h)
    memo[(k, mx, mn)] = min(r)
    return min(r)


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9, {}))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))


def check(k, hs, mx, mn):
    if k == 0:
        return mx - mn
    r = []
    for i, h in enumerate(hs):
        hs.remove(h)
        r.append(check(k - 1, hs, max(mx, h), min(mn, h)))
        hs.insert(i, h)
    return min(r)


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))


def check(k, hs, s):
    if k == 0:
        return max(s) - min(s)
    r = []
    for i, h in enumerate(hs):
        s.append(h)
        hs.remove(h)
        r.append(check(k - 1, hs, s[::]))
        s.pop(-1)
        hs.insert(i, h)
    return min(r)


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, []))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()