Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k + 2)

for i in range(k - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = 20-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999

for i in range(k + 1):
    total = sum(distance[i:i+k-1])
    if minimum > total:
        minimum = total

print(minimum)


Fixed submission:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]
distance[2*k-1] = n-array[k-1] + array[0]
for i in range(k - 1):
    distance[k+i] = array[i+1] - array[i]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k * 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(len(array) + 2)

for i in range(len(array) - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = 20-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999
for i in range(len(distance) - 1):
    if minimum > sum(distance[i:i+k-1]):
        minimum = sum(distance[i:i+k-1])
print(minimum)

Fixed submission:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]
distance[2*k-1] = n-array[k-1] + array[0]
for i in range(k - 1):
    distance[k+i] = array[i+1] - array[i]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k * 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(len(array) + 2)

for i in range(len(array) - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = 20-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999
for i in range(len(distance) - 1):
    if minimum > sum(distance[i:i+k-1]):
        minimum = sum(distance[i:i+k-1])
print(minimum)

Fixed submission:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]
distance[2*k-1] = n-array[k-1] + array[0]
for i in range(k - 1):
    distance[k+i] = array[i+1] - array[i]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k * 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)