Buggy python submission (verdict: Runtime Error):
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        print(x[bottom + i:bottom + i + K])
        print(x[bottom + i + K - 1])
        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            best = min(best, 2 * abs(x[bottom + i]) + x[bottom + i + K - 1])
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()


Fixed submission:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    x = x[bottom:top]

    best = 999999999999
    u = len(x)
    for i in xrange(u):
        if u < i + K:
            break

        hani = x[i:i + K]
        if hani[0] < 0 and hani[-1] < 0:
            best = min(best, abs(hani[0]))
        elif hani[0] < 0 <= hani[-1]:
            best = min(best, 2 * abs(hani[0]) + hani[-1])
        elif 0 <= hani[0] and 0 <= hani[-1]:
            best = min(best, hani[-1])

    print(best)


if __name__ == "__main__":
    main()


Fixed submission:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)

    zenpou = x
    kouhou = x

    top = min(o + K, N)
    bottom = max(o - K, 0)
    x = x[bottom:top]

    best = 999999999999
    u = len(x)
    for i in range(u):
        if u < i + K:
            break

        hani = x[i:i + K]
        if hani[0] < 0 and hani[-1] < 0:
            best = min(best, abs(hani[0]))
        elif hani[0] < 0 <= hani[-1]:
            best = min(best, 2 * abs(hani[0]) + hani[-1])
        elif 0 <= hani[0] and 0 <= hani[-1]:
            best = min(best, hani[-1])

    print(best)


if __name__ == "__main__":
    main()


Fixed submission:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)

    zenpou = x
    kouhou = x
    if N <= o + K:  # ただし、もしo+Kやo-Kが超過していたら？
        zenpou = zenpou[::-1][0:K][::-1]
    else:
        zenpou = zenpou[o:o + K]

    if o - K <= 0:
        kouhou = kouhou[0:K]
    else:
        kouhou = kouhou[o - K:o]

    x = list(set(kouhou + zenpou))
    x.sort()

    best = 999999999999
    u = len(x)
    for i in range(u):
        if u < i + K:
            break

        hani = x[i:i + K]
        if hani[0] < 0 and hani[-1] < 0:
            best = min(best, abs(hani[0]))
        elif hani[0] < 0 <= hani[-1]:
            best = min(best, 2 * abs(hani[0]) + hani[-1])
        elif 0 <= hani[0] and 0 <= hani[-1]:
            best = min(best, hani[-1])

    print(best)


if __name__ == "__main__":
    main()


Fixed submission:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    # まず、原点がどこの間に入るのかを探す
    o = bisect.bisect_right(x, 0)
    # ついでに、原点にろうそくがあるかどうか探す。地点aに尽き１本まで。
    if x[o - 1] == 0:
        flag = True
    else:
        flag = False

    # 探索範囲は、x[o:o+K]またはx[o-K:o]の間。

    zenpou = x
    kouhou = x
    if N <= o + K:  # ただし、もしo+Kやo-Kが超過していたら？
        zenpou = zenpou[::-1][0:K][::-1]
    else:
        zenpou = zenpou[o:o + K]

    if o - K <= 0:
        kouhou = kouhou[0:K]
    else:
        kouhou = kouhou[o - K:o]

    x = list(set(kouhou + zenpou))
    x.sort()

    cal = []
    u = len(x)
    for i in range(u):
        if u < i + K:
            break

        hani = x[i:i + K]
        if hani[0] < 0 and hani[-1] < 0:
            ans = abs(hani[0])
            cal.append(ans)
        elif hani[0] < 0 <= hani[-1]:
            ans = 2 * abs(hani[0]) + hani[-1]
            cal.append(abs(ans))
        elif 0 <= hani[0] and 0 <= hani[-1]:
            ans = hani[-1]
            cal.append(hani[-1])

    print(sorted(cal)[0])


if __name__ == "__main__":
    main()


Fixed submission:
# vim: set fileencoding=utf-8:
import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()