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=list(range(1,n+1))
xp=[xi for xi in x if xi>=0]
xm=[xi for xi in x if xi<0]
xm.sort(reverse=True)
ans=2*10**8
for i in range(k+1):# xpからi個のろうそくをつける
    if i>len(xp) or k-i>len(xm):
        continue
    a=xp[i-1] if len(xp)>0 else 0
    b=xm[k-i-1] if len(xm)>0 else 0
    ans=min(ans,2*a-b,a-2*b)
print(ans)

Submission status: Wrong Answer

Solution code:

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

xmin=(x[n-1]-x[0])*2

for i in range(n-k):
    x1=abs(x[i])+abs(x[i+k-1]-x[i])
    x2=abs(x[i+k-1])+abs(x[i+k-1]-x[i])
    xmin=min(xmin,x1,x2)

print(xmin)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, m):
    if l < m:
        return l * 2 + m
    else:
        return l + m * 2

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))
    right_x = [i for i in x if i >= 0][:K]
    left_x = [-i for i in x if i < 0][-K:]

    left_len = len(left_x)
    right_len = len(right_x)
    if right_len < K:
        right_x += [10 ** 8 + 1] * (K - right_len)
    if left_len < K:
        left_x = [10 ** 8 + 1] * (K - left_len) + left_x

    right_x = [0] + right_x
    left_x += [0]

    ans = 10 ** 9
    for i in range(K + 1):
        tmp_ans = solve(left_x[i], right_x[i])
        if tmp_ans < ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, m):
    print(l, m)
    if l < m:
        return l * 2 + m
    else:
        return l + m * 2

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))
    right_x = [i for i in x if i >= 0][:K]
    left_x = [-i for i in x if i < 0][-K:]

    left_len = len(left_x)
    right_len = len(right_x)
    if right_len < K:
        right_x += [10 ** 8 + 1] * (K - right_len)
    if left_len < K:
        left_x = [10 ** 8 + 1] * (K - left_len) + left_x

    right_x = [0] + right_x
    left_x += [0]

    ans = 10 ** 9
    for i in range(K + 1):
        tmp_ans = solve(left_x[i], right_x[i])
        if tmp_ans < ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
#from numpy import sign
#import numpy as np
out = float('Inf')
for i in range(0, n - k + 1):
    ans = abs(a[i] - a[i + k - 1]) + abs(min(a[i], a[i + k - 1]))
    out = min(out, ans)
print(out)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
xlist = list(map(int,input().split()))

def f(min1,max1):
  #min1 = list1[0]
  #max1 = list1[-1]
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<=0 and max1 <=0:
    return abs(min1)
  else:
    return min([-2*min1 + max1,-min1 + 2*max1])

ans = 2*10**8 + 2
for i in range(N-K+1):
  min1 = xlist[i]
  max1 = xlist[i+K-1]
  dist =f (min1,max1) 
  if dist < ans :
    ans = dist

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
xlist = list(map(int,input().split()))

def f(list1):
  min1 = list1[0]
  max1 = list1[-1]
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<=0 and max1 <=0:
    return abs(min1)
  else:
    return min([-2*min1 + max1,-min1 + 2*max1])

ans = 2*10**8 + 2
for i in range(N-K+1):
  dist =f (xlist[i:i+K]) 
  if dist < ans :
    ans = dist

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
xlist = list(map(int,input().split()))

def f(list1):
  min1 = list1[0]
  max1 = list1[-1]
  total = abs(min1) + abs(max1)
  if min1*max1>0:
    return total
  else:
    return total+min([abs(min1),abs(max1)])

ans = 2*10**8 + 2
for i in range(N-K+1):
  dist =f (xlist[i:i+K]) 
  if dist < ans :
    ans = dist

print(ans)

Submission status: Wrong Answer

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

cur = 10**8
i = 0

while(i+k <= n):
    if(A[i+k-1] <= 0):
        tmp = -A[i]
    elif(A[i] <= 0):
        B = [-A[i], A[i+k-1]]
        B.sort()
        tmp = B[1] + 2 * B[0]
    else:
        tmp = A[i+k-1]
    cur = min(cur, tmp)
    i = i + 1

print(cur)

Submission status: Wrong Answer

Solution code:
import sys

n, k = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

cur = 10**8
i = 0

if(n == 1):
    print(abs(A[0]))
    sys.exit()

while(i+k < n):
    if(A[i+k-1] <= 0):
        tmp = -A[i]
    elif(A[i] <= 0):
        B = [-A[i], A[i+k-1]]
        B.sort()
        tmp = B[1] + 2 * B[0]
    else:
        tmp = A[i+k-1]
    cur = min(cur, tmp)
    i = i + 1

print(cur)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
X=list(map(int,input().split()))
for i in range(n):
    if X[i]>=0:
        break
if i==0:
    M=[]
else:
    M=X[i-1::-1]
if i==n:
    P=[]
else:
    P=X[i:]

ans=10**10
for j in range(k+1):
    if len(M)<j or len(P)<k-j:
        continue
    if j==0:
        ans=min(P[k-1],ans)
    elif j==k:
        ans=min(-M[k-1],ans)
    else:
        l=max(-M[j-1],P[k-j-1])
        s=min(-M[j-1],P[k-j-1])
        ans=min(s*2+l,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    plus = [0]
    minus = []
    for n in input().split():
        n = int(n)
        if n < 0:
            minus.append(-n)
        elif n > 0:
            plus.append(n)
        else:
            K -= 1
    minus.sort()
    for i in range(1, len(minus)):
        minus[i] += minus[i-1]
    ans = 10 ** 18
    for i in range(K+1):
        if i < len(plus) and K-i-1 >= 0 and K-i-1 < len(minus):
            ans = min(ans, plus[i] + minus[K-i-1] * 2)
            ans = min(ans, plus[i] * 2 + minus[K-i-1])
        elif K-i-1 < len(minus) and i < len(plus):
            ans = min(ans, plus[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10 ** 8
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if r <= 0:
        a = abs(l)
    elif l >= 0:
        a = r
    else:
        a = min(abs(l), abs(r)) * 2 + max(abs(l), abs(r))
    if ans > a: ans = a
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10 ** 8
for i in range(n-k+1):
    if x[i] <= 0:
        if x[i+k-1] > 0: 
            a = min(abs(x[i]), abs(x[i+k-1])) * 2 + max(abs(x[i]), abs(x[i+k-1]))
        else: 
            a = abs(x[i])
    else:
        a = abs(x[i+k-1])
    if ans > a: ans = a
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x =list(map(int,input().split())) 
sakai = 0
for i in range(N):
  if x[i]>= 0:
    sakai = i
    break
  sakai = N
sakai += K  

x = [float("inf") for i in range(K)] + x + [float("inf") for i in range(K)]

def main(sakai):
  ans = float("inf")
  ans = min(ans,x[sakai-K])
  ans = min(ans,x[sakai+K-1])
  if sakai == 0:
    ans = min(ans,x[sakai-K+1])
    ans = min(ans,x[sakai+K-1])    

  for i in range(1,K+1):
    ans = min(ans,2*abs(x[sakai-i])+x[sakai+(K-i-1)])
    ans = min(ans,abs(x[sakai-i])+2*x[sakai+(K-i-1)])

  return print(ans)

main(sakai)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10 ** 8
for i in range(n-k+1):
    if x[i] <= 0:
        if x[i+k-1] > 0: 
            a = min(abs(x[i]), x[i+k-1]) * 2 + max(abs(x[i]), x[i+k-1])
        else: 
            a = abs(x[i])
    else:
        a = x[i+k-1]
    if ans > a: ans = a
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x =list(map(int,input().split())) 
sakai = 0
for i in range(N):
  if x[i]>= 0:
    sakai = i
    break
  sakai = N
sakai += K  

x = [float("inf") for i in range(K)] + x + [float("inf") for i in range(K)]

def main(sakai):
  ans = float("inf")
  ans = min(ans,x[sakai-K])
  ans = min(ans,x[sakai+K-1])
  for i in range(1,K+1):
    ans = min(ans,2*abs(x[sakai-i])+x[sakai+(K-i-1)])
    ans = min(ans,abs(x[sakai-i])+2*x[sakai+(K-i-1)])
  if sakai == K:
    ans = x[2*K-1]
  return print(ans)

main(sakai)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x =list(map(int,input().split())) 
sakai = 0
for i in range(N):
  if x[i]>= 0:
    sakai = i
    break
  sakai = N
sakai += K  

x = [float("inf") for i in range(K)] + x + [float("inf") for i in range(K)]

def main(sakai):
  ans = float("inf")
  ans = min(ans,x[sakai-K+1])
  ans = min(ans,x[sakai+K-1])
  for i in range(1,K+1):
    ans = min(ans,2*abs(x[sakai-i])+x[sakai+(K-i-1)])
    ans = min(ans,abs(x[sakai-i])+2*x[sakai+(K-i-1)])
  return print(ans)

main(sakai)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x =list(map(int,input().split())) 
sakai = 0
for i in range(N):
  if x[i]>= 0:
    sakai = i
    break
  sakai = N
sakai += K  

x = [float("inf") for i in range(K)] + x + [float("inf") for i in range(K)]

def main(sakai):
  ans = float("inf")
  ans = min(ans,x[sakai-K])
  ans = min(ans,x[sakai+K-1])
  for i in range(1,K+1):
    ans = min(ans,2*abs(x[sakai-i])+x[sakai+(K-i-1)])
    ans = min(ans,abs(x[sakai-i])+2*x[sakai+(K-i-1)])
  return print(ans)

main(sakai)

Submission status: Wrong Answer

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

for i in range(n-k+1):
    a = 0
    if x[i] < 0:
        if x[i+k-1] > 0: 
            a += min(abs(x[i]), x[i+k-1]) * 2 + max(abs(x[i]), x[i+k-1])
        else: 
            a += abs(x[i])
    else:
        a += x[i+k-1]
    if ans > a: ans = a
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())

lst1 = list(map(int,readline().split()))
plus = []
minus = []
for i in lst1:
    if i < 0:
        minus.append(-i)
    else:
        plus.append(i)
minus.reverse() #[1,2,3]
ans = 10**18
if len(minus) >= k:
    ans = min(ans,minus[k-1])
if len(plus) >= k:
    ans = min(ans,plus[k-1])

if len(plus) == 0 or len(minus) == 0:
    print(ans)
    exit()
for i in range(1,k):
    if i > len(plus) or k-i > len(minus):
        break
    p = plus[i-1]
    m = minus[k-i-1]
    res = min(p*2+m,p+m*2)
    ans = min(ans,res)

print(ans)

Submission status: Wrong Answer

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

for i in range(n-k+1):
    a = 0
    if x[i] < 0:
        if x[i+k-1] >= 0: 
            a += min(abs(x[i]), x[i+k-1]) * 2 + max(abs(x[i]), x[i+k-1])
        else: 
            a += abs(x[i]) + abs(x[i] - x[i+k-1])
    else:
        a += x[i+k-1]
    if ans > a: ans = a

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())

lst1 = list(map(int,readline().split()))
plus = []
minus = []
for i in lst1:
    if i < 0:
        minus.append(-i)
    else:
        plus.append(i)
minus.reverse() #[1,2,3]
ans = 10**18
if len(minus) >= k:
    ans = min(ans,minus[k-1])
if len(plus) >= k:
    ans = min(ans,plus[k-1])

if len(plus) == 0 or len(minus) == 0:
    print(ans)
    exit()
for i in range(1,k+1):
    if i > len(plus) or k-i > len(minus):
        break
    p = plus[i-1]
    m = minus[k-i-1]
    res = min(p*2+m,p+m*2)
    ans = min(ans,res)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())

lst1 = list(map(int,readline().split()))
plus = []
minus = []
for i in lst1:
    if i < 0:
        minus.append(-i)
    else:
        plus.append(i)
minus.reverse() #[1,2,3]
ans = 10**18
if len(minus) >= k:
    ans = min(ans,minus[k-1])
if len(plus) >= k:
    ans = min(ans,plus[k-1])

if len(plus) == 0 or len(minus) == 0:
    print(ans)
    exit()
for i in range(min(k,len(plus))):
    if len(minus) < k-i-2:
        break
    res = min(plus[i]*2+minus[k-i-2],plus[i]+minus[k-i-2]*2)
    ans = min(ans,res)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())

lst1 = list(map(int,readline().split()))
plus = []
minus = []
for i in lst1:
    if i < 0:
        minus.append(-i)
    else:
        plus.append(i)
minus.reverse() #[1,2,3]
ans = 10**18
if len(minus) >= k:
    ans = min(ans,minus[k-1])
if len(plus) >= k:
    ans = min(ans,plus[k-1])

if len(plus) == 0 or len(minus) == 0:
    print(ans)
    exit()
for i in range(min(k,len(plus))):
    if len(minus) < k-i-1:
        break
    res = min(plus[i]*2+minus[k-i-2],plus[i]+minus[k-i-2]*2)
    ans = min(ans,res)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10 ** 8
ok = True
for i in range(n-k+1):
    a = 0
    for j in range(k):
        if not ok and x[i+j] < 0: continue
        elif not ok and x[i+j] > 0: 
            a *= 2
            ok =True
        if x[i] < 0 and a == 0:
            ok = False
            a += abs(x[i])
        if ok and j == k-1:
            a += x[i+j]
    if ans > a: ans = a
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
id = bisect.bisect_left(x, 0)
ID = bisect.bisect_right(x, 0)
ans = 10**10
if id >= K:
    ans = min(ans, x[id-K])
if id + K - 1 < N:
    ans = min(ans, x[id+K-1])


for i in range(max(0, id - K + 1), ID):
    if i+K-1 < N:
        ans = min(ans, -x[i]*2 + x[i+K-1], -x[i]+x[i+K-1]*2)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
INF = float("inf")


def main():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))
    if n == 1 and k == 1:
        print(0)
        exit()
    ans = INF
    for i in range(n - k):
        l, r = X[i], X[i + k - 1]
        tmp = r - l
        if l * r < 0:
            tmp += min(abs(l), abs(r))
        else:
            tmp += min(abs(l), abs(r))
        ans = min(tmp, ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    X=tupin()
    Y=[]
    temp=X[0]
    ans=100000000
    for i in range(N-K+1):
        temp=abs(X[i+K-1]-X[i])+min(abs(X[i+K-1]),abs(X[i]))
        #print((X[i+K-1],X[i]))
        ans=min(ans,temp)
    print(ans)    
#%%submit!
resolve()

Submission status: Wrong Answer

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
id = bisect.bisect_left(x, 0)
ans = 10**10
if id >= K:
    ans = min(ans, x[id-K])
if id + K - 1 < N:
    ans = min(ans, x[id+K-1])

for i in range(max(0, id - K + 1), id):
    if i+K-1 < N:
        ans = min(ans, -x[i]*2 + x[i+K-1], -x[i]+x[i+K-1]*2)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
id = bisect.bisect_left(x, 0)
ans = max(x) - min(0, min(x))
if id >= K:
    ans = min(ans, x[id-K])
if id + K - 1 < N:
    ans = min(ans, x[id+K-1])

for i in range(max(0, id - K + 1), id):
    if i+K-1 < N:
        ans = min(ans, -x[i]*2 + x[i+K-1], -x[i]+x[i+K-1]*2)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    ans = 10**12
    for i in range(N-K+1):
        u = X[i]
        v = X[i+K-1]
        if u*v >= 0:
            ans = min(ans, max(abs(u), abs(v)))
        else:
            ans = min(ans, min(abs(u), v))*2+max(abs(u), v)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]
    x.sort()
    answer = float("inf")
    if n == k:
        if 0 <= x[0]:
            answer = x[-1]
        elif x[-1] <= 0:
            answer = abs(x[0])
        else:
            answer = x[-1] - x[0] + min(abs(x[-1]), abs(x[0]))
    else:
        for i in range(n - k):
            left = x[i]
            right = x[i + k - 1]
            if left * right >= 0:
                answer = min(answer, max(abs(left), abs(right)))
            else:
                answer = min(answer, right - left + min(abs(left), abs(right)))
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
res = []
for i in range(n):
    if x[i] >= 0:
        xplus = x[i:]
        xminus = x[:i]
        break
else:
    xplus = []
    xminus = x[::]
if len(xplus) >= k:
    res.append(xplus[k-1])
elif len(xminus) >= k:
    res.append(-xminus[-k])
from collections import deque
a = x
anslist = deque(a)
while len(anslist) > k:
    if abs(anslist[0]) > abs(anslist[-1]):
        anslist.popleft()
    else:
        anslist.pop()
#print(anslist)
if anslist[0] >= 0 and anslist[-1] >= 0:
    res.append(anslist[-1])

elif anslist[0] <= 0 and anslist[-1] <= 0:
    res.append(-anslist[0])

else:
    if anslist[-1] > -anslist[0]:
        res.append(-2*anslist[0] + anslist[-1])
    else:
        res.append(2*anslist[-1] - anslist[0])
print(min(res))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k = map(int,input().split())
a = list(map(int,input().split()))
anslist = deque(a)
while len(anslist) > k:
    if abs(anslist[0]) > abs(anslist[-1]):
        anslist.popleft()
    else:
        anslist.pop()
if anslist[0] >= 0 and anslist[-1] >= 0:
    print(anslist[-1])

elif anslist[0] <= 0 and anslist[-1] <= 0:
    print(-anslist[0])

else:
    if anslist[-1] > -anslist[0]:
        print(-2*anslist[0] + anslist[-1])
    else:
        print(2*anslist[-1] - anslist[0])

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())

A = list(map(int, input().split()))

B = []
C = []
for i in A:
    if i < 0:
        B.append(- 2 * i)
        C.append(0)
    else:
        B.append(i)
        C.append(1)

D = zip(B, C)
D = sorted(D)
B, C = zip(*D)

left = 0
right  = 0

for i in range(b):
    if C[i] == 0:
        left = B[i]
    else:
        right = B[i]

if right == 0:
    print(left//2)
else:
    print(left + right)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())

A = list(map(int, input().split()))

B = []
C = []
for i in A:
    if i < 0:
        B.append(- 2 * i)
        C.append(0)
    else:
        B.append(i)
        C.append(1)

D = zip(B, C)
D = sorted(D)
B, C = zip(*D)

left = 0
right  = 0

for i in range(b):
    if C[i] == 0:
        left = B[i]
    else:
        right = B[i]
print(left + right)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from itertools import combinations # (string,3) 3回
#from collections import deque
#import collections.defaultdict(list)
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    X = readInts()
    m = float('inf')
    # 端だけ保存してあげればよい
    left = []
    right = []
    for i in range(n-k+1):
        l = X[i]
        r = X[k+i-1]
        m = min(m,abs(min(l,r)) + r - l)
    print(m)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = X[-1]-X[0]+min(X[-1],abs(X[0]))
    for i in range(N-K+1):
        if X[i+K-1]<=0:
            p = -X[i]
        elif X[i]>=0:
            p = X[i+K-1]
        else:
            p = X[i+K-1]-X[i]+min(X[i+K-1],abs(X[i]))
        ans = min(ans,p)
    return ans
print(solve())

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from collections import deque
def binary_search(A,p):
    ind = bisect_left(A,p)
    if A[0]<=p and p<=A[-1] and p == A[ind]:
        return True, ind
    return False,ind
    

def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    boo, ind = binary_search(X,0)
    if boo:
        K -= 1
        N -= 1
        X.remove(0)
    if K == 0:
        return 0
    minus = X[:ind]
    plus = X[ind:]
    ans = []
    if len(minus)>=K:
        ans.append(abs(minus[-K]))
    if len(plus)>=K:
        ans.append(abs(plus[K-1]))
    d_plus = deque(plus)
    d_minus = deque(minus)
    if len(d_plus)==0:
        return ans[0]
    else:
        p = d_plus.popleft()
    if len(d_minus)==0:
        return ans[0]
    else:
        m = d_minus.pop()
    for _ in range(K):
        if p<=abs(m):
            p_prev = p
            if len(d_plus)>0:
                p = d_plus.popleft()
            else:
                p = 10**9
        else:
            m_prev = m
            if len(d_minus)>0:
                m = d_minus.pop()
            else:
                m = -10**9
    ans.append(p_prev+abs(m_prev)+min(p_prev,abs(m_prev)))
    return min(ans)
print(solve())

Submission status: Wrong Answer

Solution code:
import sys

def main(lines):
  N, K = map(int, lines[0].split())
  a = list(map(int, lines[1].split()))
  ans = 1000000000000
  for i in range(N):
    if i + K - 1 >= N:
      break
    b = a[i]
    c = a[i + K - 1]
    if b < 0 and c >= 0:
      ans = min(ans, max(abs(b), abs(c)) + 2*min(abs(b), abs(c)))
    else:
      ans = min(ans, min(abs(b), abs(c)))
  print(ans)


if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Wrong Answer

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

res_lst = []
for i in range(n-k+1):
  lst = xs[i:i+k]
  res = 0
  for i, elem in enumerate(lst):
    if i > 0 and lst[i] * elem < 0:
      res += min(abs(lst[i]), elem) + elem
    else:
      res += abs(elem)
  res_lst.append(res)
print(min(res_lst))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))

zeroi = 0
while zeroi < N:
    if X[zeroi] >= 0:
        break
    zeroi += 1
if zeroi < N and X[zeroi] == 0:
    K -= 1
    del X[zeroi]
    N -= 1


def solve():
    if not X:
        return 0
    minval = 2**60
    for i in range(N):
        if i < zeroi - K or i > zeroi + K:
            continue
        if X[i] < 0:
            r = K + i
            if r >= len(X):
                continue
            d = -X[i]
            if r >= zeroi:
                d += 2 * X[r]
        else:
            r = i - K + 1
            if r < 0:
                continue
            d = X[i]
            if r < zeroi:
                d += -2 * X[r]
        minval = min(minval, d)
    return minval

print(solve())

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))

zeroi = 0
while zeroi < N:
    if X[zeroi] >= 0:
        break
    zeroi += 1
if zeroi < N and X[zeroi] == 0:
    K -= 1
    del X[zeroi]


def solve():
    if not X:
        return 0
    minval = 2**60
    for i in range(max(zeroi-K, 0), min(zeroi+K+1, len(X))):
        if X[i] < 0:
            r = K + i
            if r >= len(X):
                continue
            d = -X[i]
            if r >= zeroi:
                d += 2 * X[r]
        else:
            r = i - K + 1
            if r < 0:
                continue
            d = X[i]
            if r < zeroi:
                d += -2 * X[r]
        minval = min(minval, d)
    return minval

print(solve())

Submission status: Wrong Answer

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

mi = sys.maxsize
start = 0

p = 0
for i in range(len(x)):
    if x[i] >= 0:
        p = i
        break
if p-k > 0:
    start = p-k
end = p+1
if end+k > len(x):
    end = len(x)-k+1

for i in range(start,end):
    l = x[i]
    r = x[i+k-1]
    #print(l,r)
    vall = abs(l) + abs(r - l)
    valr = abs(r) + abs(r - l)
    mi = min(mi,min(vall,valr))

print(mi)

Submission status: Wrong Answer

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

mi = 99999
start = 0

p = 0
for i in range(len(x)):
    if x[i] >= 0:
        p = i
        break
if p-k > 0:
    start = p-k
end = p+1
if end+k > len(x):
    end = len(x)-k+1

for i in range(start,end):
    l = x[i]
    r = x[i+k-1]
    #print(l,r)
    vall = abs(l) + abs(r - l)
    valr = abs(r) + abs(r - l)
    mi = min(mi,min(vall,valr))

print(mi)

Submission status: Wrong Answer

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

mi = 99999

for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    vall = abs(l) + abs(r - l)
    valr = abs(r) + abs(r - l)
    mi = min(mi,vall,valr)

print(mi)

Submission status: Wrong Answer

Solution code:
def val(lst):
    m = list([lst[i] for i in range(len(lst)) if lst[i] < 0])
    p = list([lst[i] for i in range(len(lst)) if lst[i] >= 0])
    if (len(m) == 0):
        return max(p)
    elif(len(p) == 0):
        return abs(min(m))
    elif abs(min(m)) > max(p):
        return abs(min(m))+max(p)*2
    else:
        return abs(min(m))*2 + max(p)

n,k = map(int,input().split())
x = list(map(int,input().split()))
sa = n-k
p = 0
for i in range(len(x)):
    if x[i] >= 0:
        p = i
        break

start = 0
if p-k > 0:
    start = p-k
    
end = p+1
if end+k > len(x):
    end = len(x)-k+1

value = []
for i in range(start,end):
    #lst = x[i:i-sa if i-sa < 0 else None]
    lst = x[i:i+k if i+k < len(x) else None]
    value.append(val(lst))

print(min(value))

Submission status: Wrong Answer

Solution code:
def val(lst):
    m = list([lst[i] for i in range(len(lst)) if lst[i] < 0])
    p = list([lst[i] for i in range(len(lst)) if lst[i] >= 0])
    if (len(m) == 0):
        return max(p)
    elif(len(p) == 0):
        return abs(min(m))
    elif abs(min(m)) > max(p):
        return abs(min(m))+max(p)*2
    else:
        return abs(min(m))*2 + max(p)

n,k = map(int,input().split())
x = list(map(int,input().split()))
sa = n-k
p = 0
for i in range(len(x)):
    if x[i] > 0:
        p = i
        break

start = 0
if p-k > 0:
    start = p-k
    
end = p+1
if end+k > len(x):
    end = len(x)-k+1

value = []
for i in range(start,end):
    lst = x[i:i-sa if i-sa < 0 else None]
    value.append(val(lst))
print(min(value))

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
X = LIST()

# dist = 0
s = []
l = []
s = deque(s)
l = deque(l)
for x in X:
    if x < 0:
        s.appendleft(-1 * x)
    else:
        l.append(x)
# s.sort()
# print(s, l)

left = 0
right = 0
q = []
while len(q) != K:
    if len(s) != 0 and len(l) != 0:
        if s[0] <= l[0]:
            left = s[0]
            q.append(s.popleft())
        else:
            right = l[0]
            q.append(l.popleft())
    elif len(s) == 0 and len(l) != 0:
        right = l.popleft()
        q.append(right)
    else:
        left = s.popleft()
        q.append(left)

print(min(left, right) + left + right)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
X = LIST()

# dist = 0
s = []
l = []
s = deque(s)
l = deque(l)
for x in X:
    if x < 0:
        s.appendleft(-1 * x)
    else:
        l.append(x)
# s.sort()
# print(s, l)

left = 0
right = 0
q = []
while len(q) != K:
    if len(s) != 0 and len(l) != 0:
        if s[0] <= l[0]:
            left = s[0]
            q.append(s.popleft())
        else:
            right = l[0]
            q.append(l.popleft())
    elif len(s) == 0:
        right = l.popleft()
        q.append(right)
    else:
        left = s.popleft()
        q.append(left)

print(min(left, right) + left + right)

Submission status: Wrong Answer

Solution code:
n,k,*x=map(int,open(0).read().split())
if n==k:
  l=abs(x[0])
  r=abs(x[-1])
  ans=min(l+l+r,r+r+l)
elif x[0]>=0:
  ans=x[k-1]
elif x[-1]<=0:
  ans=abs(x[~(k-1)])
else:
  ans=10**10
  for i in range(n-k):
    l=x[i]
    r=x[i+k-1]
    if l<=0<=r:
      ll=abs(l)
      rr=abs(r)
      tmp=min(ll+2*rr,2*ll+rr)
      ans=min(ans,tmp)
      
print(ans)