Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search_naive(cur, k, x):
        if k == K:
            return 0
        costs = []
        for i in range(len(x)):
            xi = x[i]
            dist = abs(xi-cur)
            costs.append(dist + search_naive(xi, k+1, x[:i]+x[i+1:]))
        return min(costs)
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            conds = x[i:i+window_size]
            right_end = max(filter(lambda x: x>=0, conds), default=0)
            left_end = abs(min(filter(lambda x: x<0, conds), default=0))
            if right_end > left_end:
                costs.append(right_end + 2*left_end)
            else:
                costs.append(2*right_end + left_end)
        return min(costs)
    #print(search_naive(0, 0, x)) 
    print(search(N, K, x))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, x)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            right_end = x[i+window_size-1]
            left_end = x[i]
            dist = abs(right_end-left_end)
            costs.append(min(abs(right_end)+dist, abs(left_end)+dist))
        return min(costs)
    print(search(N, K, x))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, x)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search_naive(cur, k, x):
        if k == K:
            return 0
        costs = []
        for i in range(len(x)):
            xi = x[i]
            dist = abs(xi-cur)
            costs.append(dist + search_naive(xi, k+1, x[:i]+x[i+1:]))
        return min(costs)
    print(search_naive(0, 0, x)) 
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, x)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            right_end = x[i+window_size-1]
            left_end = x[i]
            dist = abs(right_end-left_end)
            costs.append(min(abs(right_end)+dist, abs(left_end)+dist))
        return min(costs)
    print(search(N, K, x))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, x)

if __name__ == '__main__':
    main()