Problem:


Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
X_delta=[0 for i in range(len(X))]
r=0
l=0
if X[0]*X[-1]<0:
    Max=min(abs(X[0]),abs(X[-1]))+abs(X[0])+abs(X[-1])
    if abs(X[0]) < abs(X[-1]):
        l=1
    else:
        r=1
    for i in range(N-1):
            if X[i]<0 and X[i+1]>=0:
                X.insert(i+1,0)
                break
    if l==1:
        for i in range(N):
            if X[i+1]<=0:
                X_delta[i]=2*(X[i+1]-X[i])
            else:
                X_delta[i]=X[i+1]-X[i]
    else:
        for i in range(N):
            if X[i+1]>0:
                X_delta[i]=2*(X[i+1]-X[i])
            else:
                X_delta[i]=X[i+1]-X[i]
    for i in range(N-K):
           Max-=max(X_delta[0],X_delta[-1])
           if X_delta[0]>X_delta[-1]:
                del X_delta[0]
           else:
                del X_delta[-1]
    print(Max)
else:
    if X[0]<0:
        print(abs(X[-K]))
    else:
        print(abs(X[K-1]))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
X_delta=[0 for i in range(len(X))]
r=0
l=0
if X[0]*X[-1]<0:
    Max=min(abs(X[0]),abs(X[-1]))+abs(X[0])+abs(X[-1])
    if abs(X[0]) < abs(X[-1]):
        l=1
    else:
        r=1
    for i in range(N-1):
            if X[i]<0 and X[i+1]>=0:
                X.insert(i+1,0)
                break
    if l==1:
        for i in range(N):
            if X[i+1]<=0:
                X_delta[i]=2*(X[i+1]-X[i])
            else:
                X_delta[i]=X[i+1]-X[i]
    else:
        for i in range(N):
            if X[i+1]>0:
                X_delta[i]=2*(X[i+1]-X[i])
            else:
                X_delta[i]=X[i+1]-X[i]
    for i in range(N-K):
           Max-=max(X_delta[0],X_delta[-1])
           if X_delta[0]>=X_delta[-1]:
                del X_delta[0]
           else:
                del X_delta[-1]
    print(Max)
else:
    if X[0]<0:
        print(abs(X[-K]))
    else:
        print(abs(X[K-1]))

Submission status: Wrong Answer

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())


#n = ip()                              #入力整数1つ
x,y= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
s = []
if x == 1:
    print(a[0])
    exit(0)
if x==y:
    c = a[0]
    d = a[x-1]

    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d <= 0:
        s.append(abs(c))
    else: s.append(d)
for i in range(x-y):
    c = a[i]
    d = a[i+y-1]

    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d <= 0:
        s.append(abs(c))
    else: s.append(d)
print(min(s))

Submission status: Wrong Answer

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())


#n = ip()                              #入力整数1つ
x,y= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
s = []
if x == 1:
    print(a[0])
    exit(0)
if x==y:
    c = a[0]
    d = a[x-1]

    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d < 0:
        s.append(abs(c))
    else: s.append(d)
for i in range(x-y):
    c = a[i]
    d = a[i+y-1]

    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d < 0:
        s.append(abs(c))
    else: s.append(d)
print(min(s))

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 100000
if 0 in x:
    K -= 1
    x.remove(0)
if K == 0:
    print(0)
else:
    for i in range(N-K+1):
        if x[i] < 0 and x[i+K-1] > 0:
            cost = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
        elif x[i] < 0:
            cost = abs(x[i])
        else:
            cost = x[i+K-1]
        ans = min(ans, cost)
    print(ans)

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10000
if 0 in x:
    K -= 1
    x.remove(0)
if K == 0:
    print(0)
else:
    for i in range(N-K+1):
        if x[i] < 0 and x[i+K-1] > 0:
            cost = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
        elif x[i] < 0:
            cost = abs(x[i])
        else:
            cost = abs(x[i+K-1])
        ans = min(ans, cost)
    print(ans)

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10000
if 0 in x:
    K -= 1
    x.remove(0)
if K == 0:
    print(0)
else:
    for i in range(N-K):
        if x[i] < 0 and x[i+K-1] > 0:
            cost = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
        elif x[i] < 0:
            cost = abs(x[i])
        else:
            cost = abs(x[i+K-1])
        ans = min(ans, cost)
    print(ans)

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10000
if 0 in x:
    K -= 1
if K == 0:
    print(0)
else:
    for i in range(N-K):
        if x[i] < 0 and x[i+K-1] > 0:
            cost = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
        elif x[i] < 0:
            cost = abs(x[i])
        else:
            cost = abs(x[i+K-1])
        ans = min(ans, cost)
    print(ans)

Submission status: Wrong Answer

Solution code:
n=list(map(int,input().split()))
Y=list(map(int,input().split()))
P=[i for i in Y if i>=0]
M=[i*-1 for i in Y if i<0]
if P==[]:
    P.append(max(M)*1000)
if M==[]:
    M.append(max(P)*1000)
p=sorted(P)
m=sorted(M)
L1=[]
L2=[]
for i in range(n[1]):
    if n[1]-i-2<len(m) and i<len(p):
        L1.append(p[i]+m[n[1]-i-2]*2)
        L2.append(p[i]*2+m[n[1]-i-2])
if len(p)>n[1]:
    L1.append(p[n[1]-1])
if len(m)>n[1]:
    L2.append(m[n[1]-1])
print(min(min(L1),min(L2)))

Submission status: Wrong Answer

Solution code:
n=list(map(int,input().split()))
Y=list(map(int,input().split()))
P=[i for i in Y if i>=0]
M=[i*-1 for i in Y if i<0]
if P==[]:
    P.append(max(M)*1000)
if M==[]:
    M.append(max(P)*1000)
p=sorted(P)
m=sorted(M)
print(p,m)
L1=[]
L2=[]
for i in range(n[1]):
    if n[1]-i-2<len(m) and i<len(p):
        L1.append(p[i]+m[n[1]-i-2]*2)
        L2.append(p[i]*2+m[n[1]-i-2])
if len(p)>n[1]:
    L1.append(p[n[1]-1])
if len(m)>n[1]:
    L2.append(m[n[1]-1])
print(min(min(L1),min(L2)))

Submission status: Wrong Answer

Solution code:
N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
try :
    dis.append(Lright[K-1])
    dis.append(Lleft[K-1])
except:
    pass


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    try :
        d += Lleft[remain-1]
    except:
        pass
    dis.append(d)
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    try:
        d += Lright[remain-1]
    except:
        pass
    dis.append(d)
print(min(dis))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
X = list(map(int,input().split()))
ans = 10**5+10
for l, r in zip(X,X[K-1:]):
  ans = min(ans, r - l + min(abs(l), abs(r)))
print(ans)

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        candidates = []
        for i in range(self.n - self.k + 1):  # 0 .. n-k
            candidates.append((i, i + self.k - 1))

        best_candidate = None
        best_distance = -1
        for c in candidates:
            d = self.distance(c)
            if (best_distance != -1) and (d < best_distance):
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]]
        # X X X 0
        if right <= 0:
            return abs(left)
        # 0 X X X
        elif left >= 0:
            return abs(right)
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        candidates = []
        for i in range(self.n - self.k + 1):  # 0 .. n-k
            candidates.append((i, i + self.k - 1))
            # print(self.x_s[i:i+self.k])
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]]
        # X X X 0
        if right <= 0:
            return left
        # 0 X X X
        elif left >= 0:
            return right
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        # candles = self.filter()
        candles = self.x_s
        candidates = []
        for i in range(len(candles) - self.k + 1):
            candidates.append((i, i+self.k))
            # print(self.x_s[i:i+self.k])
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]-1]
        # X X X 0
        if right <= 0:
            return left
        # 0 X X X
        elif left >= 0:
            return right
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        candles = self.filter()
        candidates = []
        for i in range(len(candles) - self.k + 1):
            candidates.append((i, i+self.k))
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]-1]
        # X X X 0
        if right <= 0:
            return left
        # 0 X X X
        elif left >= 0:
            return right
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        candles = self.filter()
        candidates = []
        for i in range(len(candles) - self.k + 1):
            candidates.append((i, i+self.k))
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]-1]
        # X X X 0
        if right <= 0:
            return left
        # 0 X X X
        elif left > 0:
            return right
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        candles = self.filter()
        candidates = []
        for i in range(len(candles) - self.k + 1):
            candidates.append((i, i+self.k))
            # candidates.append(candles[i:i+self.k])
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = self.x_s[candidate[0]]
        right = self.x_s[candidate[1]-1]
        # X X X 0
        if right < 0:
            return left
        # 0 X X X
        elif left > 0:
            return right
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
class Solver(object):
    def __init__(self):
        self.n, self.k = list(map(int, input().split(" ")))
        self.x_s = sorted(list(map(int, input().split(" "))))

    def solve(self):
        candles = self.filter()
        candidates = []
        for i in range(len(candles) - self.k + 1):
            candidates.append(candles[i:i+self.k])
        best_candidate = candidates[0]
        best_distance = self.distance(best_candidate)
        for c in candidates[1:]:
            d = self.distance(c)
            if d < best_distance:
                best_candidate = c
                best_distance = d
        # print(best_candidate)
        print(best_distance)

    def distance(self, candidate):
        left = candidate[0]
        right = candidate[-1]
        # X X X 0
        if right < 0:
            return left
        # 0 X X X
        elif left > 0:
            return right
        # X 0 X X
        elif abs(left) < abs(right):
            return abs(left) + abs(left) + abs(right)
        # X X 0 X
        else:
            return abs(right) + abs(right) + abs(left)

    def filter(self):
        # Only want -k .. k elements
        right = None
        counter = 0
        for i, v in enumerate(self.x_s):
            if v >= 0:
                counter += 1
                if counter == self.k:
                    right = i + 1
                    break
        if right is None:
            right = len(self.x_s)

        left = None
        counter = 0
        for i, v in reversed(list(enumerate(self.x_s))):
            if v <= 0:
                counter += 1
                if counter == self.k:
                    left = i
                    break
        if left is None:
            left = 0
        return self.x_s[left:right]


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]

    ans = []
    for i in range(n-k+1):
        d = 0
        for j in range(i, i+k):
            if j == i:
                d += abs(x[j])
            else:
                d += x[j] - x[j-1]
        ans.append(d)

    print(min(ans))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(N, K, Xs):
    plus = sorted([x for x in Xs if x >= 0])
    minus = sorted([abs(x) for x in Xs if x < 0])
    ans = 10 ** 9
    for i in range(0, K + 1):
        j = K - i
        if i <= len(plus) and i == K:
            p = plus[-1]
            ans = min(ans, p)
        elif j <= len(minus) and j == K:
            m = minus[-1]
            ans = min(ans, m)
        elif i <= len(plus) and j <= len(minus):
            p = plus[i - 1]
            m = minus[j - 1]
            ans = min(ans, p * 2 + m, m * 2 + p)

    return ans


if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    Xs = list(map(int, input().split(" ")))
    print(solve(N, K, Xs))

Submission status: Wrong Answer

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
x = list(map(int, input().split()))
if 0 in x:
    K -= 1

x_minus = filter(lambda x: x<0, x)
x_plus  = filter(lambda x: x>0, x)

x_minus = sorted(x_minus, reverse=True)
x_plus  = sorted(x_plus , reverse=False)

n_minus = len(x_minus)
n_plus = len(x_plus)

# print(x_minus, x_plus)

ans = 2*10**8

if K == 0:
    print(0)
    exit(0)

# go left only
if n_minus >= K:
    # ans = min(ans, sum( map(lambda x: abs(x)m x_minus[K])))
    ans = min(ans, abs(x_minus[K-1]))

# go right only
if n_plus  >= K:
    # ans = min(ans, sum(x_plus[K-1]))
    ans = min(ans, x_plus[K-1])

# left   and  right
for i in range( min(K-1, n_minus) ):
    remain = K - i - 1
    try:
        ans = min(ans, 2*abs(x_minus[i]) + x_plus[K-i-1-1])
    except IndexError:
        continue
    
# right  and  left
for i in range( min(K-1, n_plus) ):
    remain = K - i - 1
    try: 
        ans = min(ans, 2*x_plus[i] + abs(x_minus[K-i-1-1]))
    except IndexError:
        continue

print(ans)

Submission status: Wrong Answer

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
x = list(map(int, input().split()))
x_minus = filter(lambda x: x<0, x)
x_plus  = filter(lambda x: x>=0, x)

x_minus = sorted(x_minus, reverse=True)
x_plus  = sorted(x_plus , reverse=False)

n_minus = len(x_minus)
n_plus = len(x_plus)

# print(x_minus, x_plus)

ans = 2*10**8

# go left only
if n_minus >= K:
    # ans = min(ans, sum( map(lambda x: abs(x)m x_minus[K])))
    ans = min(ans, abs(x_minus[K-1]))

# go right only
if n_plus  >= K:
    # ans = min(ans, sum(x_plus[K-1]))
    ans = min(ans, x_plus[K-1])

# left   and  right
for i in range( min(K-1, n_minus) ):
    remain = K - i - 1
    try:
        ans = min(ans, 2*abs(x_minus[i]) + x_plus[K-i-1-1])
    except IndexError:
        continue
    
# right  and  left
for i in range( min(K-1, n_plus) ):
    remain = K - i - 1
    try: 
        ans = min(ans, 2*x_plus[i] + abs(x_minus[K-i-1-1]))
    except IndexError:
        continue

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    z = len(m)
    if k-len(m) > 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while z+s+i < n and s+i < z:
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[z+s+i]), abs(x[s+i]) + abs(x[z+s+i])*2))
        i += 1
        
    print(ans)
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
x_arr = list(map(int, input().split()))

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    sys.exit()
    print(minus[k-1])
    
elif minus == []:
    sys.exit()
    print(plus[k-1])

else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  >= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  >= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] < minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] < minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x_arr = list(map(int, input().split()))

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    print(minus[k-1])
elif minus == []:
    print(plus[k-1])
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  >= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  >= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] < minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] < minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x_arr = list(map(int, input().split()))

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    print(minus[k-1])
elif minus == []:
    print(plus[k-1])
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  >= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  >= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] <= minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] <= minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))