Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        k = K - abs(i - a)
        d = abs(ary[i])
        if 0 < k < len(ary[a:]):
            d += abs(ary[i] - ary[k + a])
            if ans > d:
                ans = d

print(0 if ans == float("Inf") else ans)


Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        try:
            k = K - abs(i - a)
            d = abs(ary[i])
        except:print(1)
        if k > 0:
                d += abs(ary[i] - ary[k + a])
                if ans > d:
                    ans = d

print(0 if ans == float("Inf") else ans)


Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N+1):
        if i == a:continue
        k = abs(i - a)
        d = abs(ary[i])
        if k >= K and ans > d:
            ans = d
        if i <= a:
            k = K - abs(i-a)
            d = abs(ary[i])
            if k > 0:
                d += abs(ary[i] - ary[k+a])
                if ans > d:
                    ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
dp = [[0]*(N+1) for _ in range(N+1)]
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N+1):
    for j in range(N+1):
        dp[i][j] = abs(ary[i]-ary[j])
for i in range(N+1):
        if i == a:continue
        k = abs(i - a)
        d = abs(ary[i])
        if k >= K and ans > d:
            ans = d
        if i <= a:
            k = K - abs(i-a)
            d = abs(ary[i])
            if k > 0:
                d += dp[i][a+k]
                if ans > d:
                    ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
dp = [[0]*N for _ in range(N)]
check = [[False]*N for _ in range(N)]
ary = sorted(ary)
a = ary.index(0)
ary.pop(a)
ans = float("Inf")
for i in range(N):
    for j in range(N):
        dp[i][j] = [abs(ary[i]-ary[j]),abs(j-i)]
for i in range(N):
    for j in range(i,N):
        if i <= j <= a-1:continue
        if i == j:
            k = abs(dp[i][i][1] - a)
            d = abs(ary[i])
            if k >= K and ans > d:
                ans = d
            continue

        k = 1 + dp[i][j][1]
        d = abs(ary[i]) + dp[i][j][0]
        if k >= K and ans > d:
            ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
dp = [[0]*N for _ in range(N)]
ary = sorted(ary)
a = ary.index(0)
ary.pop(a)
ans = float("Inf")
for i in range(N):
    for j in range(N):
        dp[i][j] = [abs(ary[i]-ary[j]),abs(j-i)]
for i in range(N):
    for j in range(i,N):
        if i == j:
            k = abs(dp[i][i][1] - a)
            d = abs(ary[i])
            if k >= K and ans > d:
                ans = d
            continue
        k = 1 + dp[i][j][1]
        d = abs(ary[i]) + dp[i][j][0]
        if k >= K and ans > d:
            ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
dp = [[0]*N for _ in range(N)]
ary = sorted(ary)
a = ary.index(0)
ary.pop(a)
ans = float("Inf")
for i in range(N):
    for j in range(N):
        dp[i][j] = [abs(ary[i]-ary[j]),abs(j-i)]
for i in range(N):
    for j in range(i,N):
        if i == j:
            k = abs(dp[i][i][1] - a)
            d = abs(ary[i])
            if k >= K and ans > d:
                ans = d
            continue
        k = 1 + dp[i][j][1]
        d = abs(ary[i]) + dp[i][j][0]
        if k >= K and ans > d:
            ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
dp = [[0]*N for _ in range(N)]
ary = sorted(ary)
a = ary.index(0)
ary.pop(a)
ans = float("Inf")
for i in range(N):
    for j in range(N):
        dp[i][j] = [abs(ary[i]-ary[j]),abs(j-i)]
for i in range(N):
    for j in range(N):
        if i == j:
            k = abs(dp[i][i][1] - a)
            d = abs(ary[i])
            if k >= K and ans > d:
                ans = d
            continue
        k = 1 + dp[i][j][1]
        d = abs(ary[i]) + dp[i][j][0]
        if k >= K and ans > d:
            ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
ary = [0]+list(map(int,input().split()))
dp = [[0]*N for _ in range(N)]
ary = sorted(ary)
a = ary.index(0)
ary.pop(a)
ans = float("Inf")
for i in range(N):
    for j in range(N):
        dp[i][j] = [abs(ary[i]-ary[j]),abs(j-i)]
for i in range(N):
    for j in range(N):
        if i == j:
            k = abs(dp[i][i][1] - a)
            d = abs(ary[i])
            if k >= K and ans > d:
                ans = d
            continue
        k = 1 + dp[i][j][1]
        d = abs(ary[i]) + dp[i][j][0]
        if k >= K and ans > d:
            ans = d
print(0 if ans == float("Inf") else ans)

Fixed submission:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)