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()))
l, r = -1, n
while r - l > 1:
    p = (l + r) // 2
    if X[p] < 0:
        l = p
    else:
        r = p

if l == -1:
    print(X[k - 1])
elif r == n:
    print(abs(X[n - k + 1]))
else:
    best_score = 10 ** 9
    for i in range(max(l - k + 1, 0), n - k):
        print(i, i + k - 1, X[i], X[i + k - 1])
        score = 0
        if i > r:
            break
        if X[i + k] < 0:
            score = abs(X[i])
        elif X[i] > 0:
            score = X[i + k - 1]
        elif abs(X[i]) < abs(X[i + k - 1]):
            score = 2 * abs(X[i]) + X[i + k - 1]
        else:
            score = abs(X[i]) + 2 * X[i + k - 1]
        if score < best_score:
            best_score = score
    print(best_score)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
X = list(map(int, input().split()))
l, r = -1, n
while r - l > 1:
    p = (l + r) // 2
    if X[p] < 0:
        l = p
    else:
        r = p

if l == -1:
    print(X[k - 1])
elif r == n:
    print(abs(X[n - k]))
else:
    best_score = 10 ** 9
    for i in range(max(l - k + 1, 0), n - k):
        score = 0
        if i > r:
            break
        if X[i + k] < 0:
            score = abs(X[i])
        elif X[i] > 0:
            score = X[i + k - 1]
        elif abs(X[i]) < abs(X[i + k - 1]):
            score = 2 * abs(X[i]) + X[i + k - 1]
        else:
            score = abs(X[i]) + 2 * X[i + k - 1]
        if score < best_score:
            best_score = score
    print(best_score)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
X = list(map(int, input().split()))
l, r = -1, n
while r - l > 1:
    p = (l + r) // 2
    if X[p] < 0:
        l = p
    else:
        r = p

if l == -1:
    print(X[k - 1])
elif r == n:
    print(abs(X[n - k]))
else:
    best_score = 10 ** 9
    for i in range(max(l - k - 1, 0), n - k):
        score = 0
        if i > r:
            break
        if X[i + k] < 0:
            score = abs(X[i])
        elif X[i] > 0:
            score = X[i + k - 1]
        elif abs(X[i]) < abs(X[i + k - 1]):
            score = 2 * abs(X[i]) + X[i + k - 1]
        else:
            score = abs(X[i]) + 2 * X[i + k - 1]
        if score < best_score:
            best_score = score
    print(best_score)

Submission status: Wrong Answer

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

#全探索
Ans=10**9
for i in range(N-K+1):
    XL,XR=X[i],X[i+K-1]
    if XL*XR>0:
        Ans=min(Ans,abs(XR-XL))
    else:
        Ans=min(Ans,abs(XR)*2+abs(XL),abs(XL)*2+abs(XR))
print(Ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
##x.sort()
ans = 1e12
if N == K:
    ans = abs(x[-1]-x[0])
    
for i in range(N-K+1):
    a = abs(x[i])+abs(x[i]-x[i+K-1])
    b = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans,min(a,b))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
##x.sort()
ans = 1e12
if N == K:
    ans = abs(x[-1]-x[0])
    
for i in range(N-K):
    a = abs(x[i])+abs(x[i]-x[i+K-1])
    b = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans,min(a,b))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
ans = 1e9
if N == K:
    ans = abs(x[-1]-x[0])
    
for i in range(N-K):
    tmp = x[i:i+K]
    a = abs(tmp[0])+abs(tmp[0]-tmp[-1])
    b = abs(tmp[-1]) + abs(tmp[0]-tmp[-1])
    ans = min(ans,min(a,b))

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    s = N * (10 ** 8)
    for i in range(N-K):
        if x[i] * x[K - 1 + i] < 0:
            s = min(s, min(x[K - 1 + i], abs(x[i])) * 2 + max(x[K - 1 + i], abs(x[i])))
        else:
            s = min(s, max(abs(x[K - 1 + i]), abs(x[i])))
    if N == K:
        if x[0] * x[N - 1] < 0:
            s = min(x[N - 1], abs(x[0])) * 2 + max(x[N - 1], abs(x[0]))
        else:
            s = max(abs(x[N - 1]), abs(x[0]))
    print(s)
main()

Submission status: Wrong Answer

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

Xl, Xr = [0], [0]
for x in X:
    if x < 0:
        Xl.append(abs(x))
    else:
        Xr.append(x)
while len(Xl) <= k:
    Xl.append(1e10)
while len(Xr) <= k:
    Xr.append(1e10)
Xl.sort()

ans = 1e10

for i in range(k):
    ans = min(ans, Xl[i] + 2*Xr[k-i], 2*Xl[i] + Xr[k-i])

print(ans)

Submission status: Wrong Answer

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

Xl, Xr = [0], [0]
for x in X:
    if x < 0:
        Xl.append(abs(x))
    else:
        Xr.append(x)
while len(Xl) <= k:
    Xl.append(10**9)
while len(Xr) <= k:
    Xr.append(10**9)
Xl.sort()

ans = 10**9

for i in range(k):
    ans = min(ans, Xl[i] + 2*Xr[k-i], 2*Xl[i] + Xr[k-i])

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N,K = map(int,input().split())
    x = list(map(int,input().split()))
    diffx = [x[i]-x[i-1] for i in range(1,N)]
    qcost = sum(diffx[:K-1])
    indsd,inded = 0,K-1
    addcost = min(abs(0-x[indsd]),abs(0-x[inded]))
    indsd += 1
    inded += 1
    ans = qcost + addcost
    for i in range(K-1,N-1):
        tmp = ans - addcost + diffx[i] - diffx[i-K+1]
        addcost = min(abs(0-x[indsd]),abs(0-x[inded]))
        indsd += 1
        inded += 1
        ans = min(ans,tmp+addcost)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cost = 1000000000
l = -1
r = -1
for i in range(n - k + 1):
  temp = sum(map(abs, x[i:i + k]))
  if temp < cost:
    cost = temp
    l = i
    r = i + k - 1
ans = min(abs(x[l]) + abs(x[r] - x[l]), abs(x[r]) + abs(x[r] - x[l]))
print(ans)

Submission status: Wrong Answer

Solution code:
def binary_search(list):
    lower = 0
    upper = len(list)
    while upper - lower > 1:
        if list[(lower + upper) // 2] < 0:
            lower = (lower + upper) // 2
        else:
            upper = (lower + upper) // 2
    return lower


def main():
    N, K = map(int, input().split(" "))
    coodinates = list(map(int, input().split(" ")))
    result = 10 ** 9 + 7
    if N > K:
        for i in range(0, N - K + 1):
            if coodinates[i] < 0 and coodinates[i + K - 1] > 0:
                start = binary_search(coodinates[i:i + K])
                round_steps = min(0 - coodinates[i], coodinates[i + K - 1])
                result = min(result, coodinates[i + K - 1] - coodinates[i] + abs(round_steps))
            else:
                result = min(result, coodinates[i + K - 1])
    else:
        result = abs(coodinates[0]) + coodinates[-1] - coodinates[0]
    print(result)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 10**10

for i in range(K):
  if i + K - 1 > N - 1:
    break
  
  left = li[i]
  right = li[i + K - 1]
  ans = min(ans, min(abs(left), abs(right)) + right - left)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, input().split())
li = list(map(int, input().split()))

ans = 10**9+7

for i in range(K):
  if i + K - 1 > N - 1:
    break
  
  left = li[i]
  right = li[i + K - 1]
  ans = min(ans, min(abs(left), abs(right)) + right - left)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9 + 7
for i in range(N - K + 1):
	if ans > abs(x[i]) + abs(x[i+K-1]) :
		ans   = x[K-1+i] - x[i] + min( abs(x[K-1+i]) , abs(x[i]) )
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9 + 7
for i in range(N - K + 1):
	print(i)
	print(x[i],x[i+K-1])
	if ans > abs(x[i]) + abs(x[i+K-1]) :
		ans   = x[K-1+i] - x[i] + min( abs(x[K-1+i]) , abs(x[i]) )
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9 + 7
for i in range(N - K + 1):
	if ans > abs(x[i]) + abs(x[i+K-1]) :
		ans = min(abs(x[i]) + abs(x[i+K-1] - x[i]) , abs(x[i+K-1]) + abs(x[i] - x[i+K-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = 10**9 + 7
for i in range(N - K + 1):
#	print(i)
#	print(x[i],x[i+K-1])
	if ans > abs(x[i]) + abs(x[i+K-1]) :
		ans = min(2 * abs(x[i]) + abs(x[i+K-1]),abs(x[i]) + 2 * abs(x[i+K-1]))
print(ans)

Submission status: Wrong Answer

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

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

xm=[]
xp=[]
x0=[]
ans=10**9

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if (K+ajst)==1 and ajst==1:
    ans=0

print(ans)

Submission status: Wrong Answer

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

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

xm=[]
xp=[]
x0=[]
ans=10**9

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if K==1 and x0!=[]:
    ans=0

print(ans)

Submission status: Wrong Answer

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

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

xm=[]
xp=[]
x0=[]
ans=10**8

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if K==1 and x0!=[]:
    ans=0

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=[int(i) for i in input().split()]
X=[int(i) for i in input().split()]
m=10**5
for i in range(N-K+1):
    m=min(m,X[K-1+i]-X[i]+min(abs(X[K-1+i]),abs(X[i])))

print(m)

Submission status: Wrong Answer

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

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

xm=[]
xp=[]
x0=[]
ans=10**5+1

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if K==1 and x0!=[]:
    ans=0

print(ans)

Submission status: Wrong Answer

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

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

xm=[]
xp=[]
x0=[]
ans=10**5+1

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if K==0 and x0!=0:
    ans=0

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)


cnt = 10000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    #リストがK以下の場合不具合が起きる
    #for i in range(1,K):   
    #len(P)=4 len(M)=3 K=5の場合、IndexErrorとなる。rangeをうまく設定する必要があるが実装できない。
    #正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
    s = max(0,K-len(M)+1)
    e = min(K+1, len(P))
    for i in range(s, e):
        cnt =min(cnt,P[i]*2 + M[K-i])
    #負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
    s1 = max(0,K-len(P)+1)
    e1 = min(K+1, len(M))
    for i in range(s1, e1):
        cnt =min(cnt,M[i]*2 + P[K-i])

print(cnt)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

print(P,M)
cnt = 10000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    #リストがK以下の場合不具合が起きる
    #for i in range(1,K):   
    #len(P)=4 len(M)=3 K=5の場合、IndexErrorとなる。rangeをうまく設定する必要があるが実装できない。
    #正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
    s = max(0,K-len(M)+1)
    e = min(K+1, len(P))
    for i in range(s, e):
        cnt =min(cnt,P[i]*2 + M[K-i])
    #負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
    s1 = max(0,K-len(P)+1)
    e1 = min(K+1, len(M))
    for i in range(s1, e1):
        cnt =min(cnt,M[i]*2 + P[K-i])

print(cnt)

Submission status: Wrong Answer

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

min_value = 10**6

for i in range(N-K+1):
    left = abs(candle[i])+abs(candle[i]-candle[i+K-1])
    right = abs(candle[i+K-1])+abs(candle[i]-candle[i+K-1])
    if min_value > min(left,right):
        min_value = min(left,right)
        
        
print(min_value)

Submission status: Wrong Answer

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

min_value = 10*6

for i in range(N-K+1):
    left = abs(candle[i])+abs(candle[i]-candle[i+K-1])
    right = abs(candle[i+K-1])+abs(candle[i]-candle[i+K-1])
    if min_value > min(left,right):
        min_value = min(left,right)
        
        
print(min_value)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

PP = []
for i in range(len(P)-1):
    PP.append(P[i+1] - P[i])

MM = []
for i in range(len(M)-1):
    MM.append(M[i+1] - M[i])

cnt = 10000000
#正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
for i in range(1,len(P)):
    cnt =min(cnt,P[i]*2 + sum(MM[0:K-i]))
    #print(cnt)
#負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
for i in range(1,len(M)):
    cnt =min(cnt,M[i]*2 + sum(PP[0:K-i]))

print(cnt)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)


PP = []
for i in range(len(P)-1):
    PP.append(P[i+1] - P[i])

MM = []
for i in range(len(M)-1):
    MM.append(M[i+1] - M[i])




cnt = 10000000
#正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
for i in range(1,len(P)):
    cnt =min(cnt,P[i]*2 + sum(MM[0:K-1]))
    #print(cnt)
#負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
for i in range(1,len(M)):
    cnt =min(cnt,M[i]*2 + sum(PP[0:K-1]))

print(cnt)

Submission status: Wrong Answer

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


for i in range(n-k+1):
  l = X[i]
  r = X[i+k-1]
  
  ans = min(ans, (abs(r-l)+min(abs(l), abs(r))))
  
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):
  l = X[i]
  r = X[i+k-1]
  
  ans = min(ans, (abs(r-l)+min(abs(l), abs(r))))
  
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):
  l = X[i]
  r = X[i+k-1]
  
  ans = min(ans, (r-l+min(abs(l), abs(r))))
  
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):
  l = X[i]
  r = X[i+k-1]
  
  ans = min(ans, r-l+min(abs(l), abs(r)))
  
print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

N,K = map(int, input().split())

# 複数個の数値を、intの配列として取得
X = list(map(int, input().split()))

ans = 10**18
for i in range(N-K+1):
    ans = min(ans, abs(X[i])+abs(X[i+K-1]-X[i]), abs(X[i+K-1])*abs(X[i+K-1]-X[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

N,K = map(int, input().split())

# 複数個の数値を、intの配列として取得
X = list(map(int, input().split()))

ans = 2*(10**8)
for i in range(N-K+1):
    tmp = min(abs(X[i])+abs(X[i+K-1]-X[i]), abs(X[i+K-1])*abs(X[i+K-1]-X[i]))
    ans = min(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

N,K = map(int, input().split())

# 複数個の数値を、intの配列として取得
X = list(map(int, input().split()))

ans = 2*(10**8)
for i in range(N-K+1):
    ans = min(ans, abs(X[i])+abs(X[i+K-1]-X[i]), abs(X[i+K-1])*abs(X[i+K-1]-X[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

N,K = map(int, input().split())

# 複数個の数値を、intの配列として取得
X = list(map(int, input().split()))

ans = 2*(10**8)
for i in range(N-K+1):
    if X[i]*X[i+K-1] >= 0:
        ans = min(ans,max(abs(X[i]),abs(X[i+K-1])))
    else:
        ans = min(ans, 2*abs(X[i])+abs(X[i+K-1]), abs(X[i])+2*abs(X[i+K-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    if N == K:
        print(0)
    else:
        print(min(fullsearch()))

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            if abs(l) > abs(r):
                distance += abs(r)
            else:
                distance += abs(l)
            yield distance

    if N == K:
        print(0)
    else:
        print(min(fullsearch()))

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            if abs(l) > abs(r):
                distance += r
            else:
                distance += l
            yield distance

    a = list(fullsearch())
    if not a:
        print(0)
    else:
        print(min(a))


main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(200000)

def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))
# import sys
# input = sys.stdin.readline

import bisect

N,K=two_int()
X=many_int()

def dfs(left_index, right_index, now_index, score, count):
#     print(left_index, right_index, now_index, score, count)

    if count==K:
#         print(left_index, right_index, now_index, score, count)
        return score
    
    right =10**5+2
    left  =10**5+2
    
    if right_index+1<len(X):
        right = dfs(left_index, right_index+1, right_index+1, 
                   score+abs(X[right_index+1]-X[now_index]), count+1)
        
    if left_index-1>=0:
        left = dfs(left_index-1, right_index, left_index-1, 
                    score+abs(X[now_index]-X[left_index-1]), count+1)
    

    return min(right,left)

zero_index = bisect.bisect_left(X,0)



count=0
if  bisect.bisect_right(X,0)==zero_index:
    bisect.insort_left(X,0)


# print(zero_index, count)

# X

print(dfs(zero_index, zero_index, zero_index, 0, count))

Submission status: Wrong Answer

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

m=[]
p=[]
ans=10**18
for xx in x:
    if xx<0:
        m.append(xx)
    elif xx>=0:
        p.append(xx)

if len(p)==0:
    print(m[-k])
    exit()
elif len(m)==0:
    print(p[k-1])
    exit()

p_idx=n+1
for i in range(n):
    if x[i]>0:
        p_idx=i
        break

if k==1:
    print(min(x[p_idx-1],x[p_idx]))
    exit()

for i in range(max(0,p_idx-k+1),min(p_idx+k-3,n-k+1)):
    ans=min(ans,2*x[i+k-1]+abs(x[i]),2*abs(x[i])+x[i+k-1])
    # print('1:',2*x[i+k-1]+abs(x[i]))
    # print('2:',2*abs(x[i])+x[i+k-1])
    # print(max(0,p_idx-k+1))
    # print(min(p_idx+k-2,n-k+1))

print(ans)

Submission status: Wrong Answer

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

m=[]
p=[]
ans=10**18
for xx in x:
    if xx<0:
        m.append(xx)
    elif xx>=0:
        p.append(xx)

if len(p)==0:
    print(m[-k])
    exit()
elif len(m)==0:
    print(p[k-1])
    exit()

p_idx=n+1
for i in range(n):
    if x[i]>0:
        p_idx=i
        break

for i in range(max(0,p_idx-k+1),min(p_idx+k-1,n-k+1)):
    ans=min(ans,2*x[i+k-1]+abs(x[i]),2*abs(x[i])+x[i+k-1])
    # print('1:',2*x[i+k-1]+abs(x[i]))
    # print('2:',2*abs(x[i])+x[i+k-1])
    # print(max(0,p_idx-k+1))
    # print(min(p_idx+k-1,n-k+1))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

N, K, *X = map(int, open(0).read().split())

# 0 を除外
X = [x for x in X if x != 0]

zero_index = bisect_left(X, 0)
# 0 にロウソクがある場合、Kを減らしてよい
K -= N - len(X)

# 座標 0 にロウソクがあるとき、開始時点でクリアしている場合がある
if K <= 0:
  print(0)
  sys.exit()

ret = float("inf")

# しゃくとり法で解く 
# 0 がどこに入るかでループの開始位置が変わる
if zero_index > K:
  head_start = zero_index - K
  tail_start = zero_index
else:
  head_start = 0
  tail_start = K - 1
  
# 必要な時間は両端のロウソクの近い方を2倍して長い方を足したもの
# ループは O(K) = O(10^5) なので間に合う
for head, tail in zip(X[head_start:], X[tail_start:]):
  if head < 0 and tail < 0:
    ret = min(ret, -head)
  elif head < 0 and tail > 0:
    tmp = min(2 * tail - head, tail - 2 * head) 
    ret = min(ret, tmp)
  else: # head > 0 and tail > 0
    ret = min(ret, tail)

print(ret)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

N, K, *X = map(int, open(0).read().split())

# 0 を除外
X = [x for x in X if x != 0]

zero_index = bisect_left(X, 0)
# 0 にロウソクがある場合、Kを減らしてよい
K -= N - len(X)

# 座標 0 にロウソクがあるとき、開始時点でクリアしている場合がある
if K <= 0:
  print(0)
  sys.exit()

ret = float("inf")

# しゃくとり法で解く 
# 0 がどこに入るかでループの開始位置が変わる
if zero_index >= K:
  head_start = zero_index - K
  tail_start = zero_index
else:
  head_start = 0
  tail_start = K - 1
  
# 必要な時間は両端のロウソクの近い方を2倍して長い方を足したもの
# ループは O(K) = O(10^5) なので間に合う
for head, tail in zip(X[head_start:], X[tail_start:]):
  if head < 0 and tail < 0:
    ret = min(ret, -head)
  elif head < 0 and tail > 0:
    tmp = min(2 * tail - head, tail - 2 * head) 
    ret = min(ret, tmp)
  else: # head > 0 and tail > 0
    ret = min(ret, tail)

print(ret)

Submission status: Wrong Answer

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

Sn = [0] * N
for i in range(N-1):
    Sn[i+1] = abs(Xn[i+1] - Xn[i]) + Sn[i]

ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, abs(Xn[i]) + Sn[i+K-1] - Sn[i])

print(ans)

Submission status: Wrong Answer

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

dist = [abs(b-a) for a, b in zip(Xn[:-1], Xn[1:])]

ans = float("inf")
for i in range(N-K+1):
    res = abs(Xn[i]) + sum(dist[i:i+K-1])
    ans = min(res, ans)

print(ans)

Submission status: Wrong Answer

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

ans = 10**9
for i in range(N-K+1):
    res = abs(Xn[i])
    for a, b in zip(Xn[i:(K+i)-1], Xn[i+1:(K+i)]):
        res += abs(b - a)
    ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

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

distans_list = [10000000] * N
for i in range(N - K + 1):
    if X[i] < 0:
        if X[i+K-1] < 0:
            distans_list[i] = abs(X[i])
        else:
            if abs(X[i]) < abs(X[i+K-1]):
                distans_list[i] = 2 * abs(X[i]) + X[i+K-1]
            else:
                distans_list[i] = abs(X[i]) + 2 * X[i+K-1]
    else:
        distans_list[i] = X[i+K-1]
##print(distans_list)
print(min(distans_list))

Submission status: Wrong Answer

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

count_0 = X.count(0)

if k <= count_0:
    print(0)
    exit()

k -= count_0

neg = [x for x in X if x < 0]
pos = [x for x in X if x > 0]

neg_count = len(neg)
pos_count = len(pos)

if not neg:
    print(pos[k-1])
    exit()

res = []
for left in range(1, min(neg_count, k)):
    time1 = neg[-left]
    right = k - left - 1
    if right >= pos_count:
        continue
    elif right < 0:
        res.append(abs(time1))
    else:
        time2 = pos[right]
        res.append(time2-time1*2)

for right in range(min(pos_count, k)):
    time1 = pos[right]
    left = k - right - 1
    if left >= neg_count:
        continue
    elif left <= 0:
        res.append(abs(time1))
    else:
        time2 = neg[-left]
        res.append(time1*2-time2)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    ruiseki = [0]
    j = 0
    for i in range(N):
        if x[i] >= 0:
            ruiseki.append(x[i])
            j += 1

    j = 0
    ruiseki2 = [0]
    for i in range(N - 1, -1, -1):
        if x[i] < 0:
            ruiseki2.append(x[i])
            j += 1

    ans = float("inf")
    for i in range(1, K + 1):
        if len(ruiseki) - 1 < i:
            continue

        if len(ruiseki2) - 1 < K - i:
            continue


        rsum = ruiseki[i]
        lsum = abs(ruiseki2[K - i])

        if rsum >= lsum:
            ans = min(ans, rsum + lsum * 2)
        else:
            ans = min(ans, rsum * 2 + lsum)

    print(ans)

    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    ruiseki = [0]
    j = 0
    for i in range(N):
        if x[i] >= 0:
            ruiseki.append(x[i])
            j += 1

    j = 0
    ruiseki2 = [0]
    for i in range(N - 1, -1, -1):
        if x[i] < 0:
            ruiseki2.append(x[i])
            j += 1

    ans = float("inf")
    for i in range(1, K + 1):
        rsum = 0
        if len(ruiseki) - 1 >= i:
            rsum = ruiseki[i]
        else:
            continue

        lsum = 0
        if len(ruiseki2) - 1 >= K - i:
            lsum = abs(ruiseki2[K - i])
        else:
            continue

        if rsum > lsum:
            ans = min(ans, rsum + lsum * 2)
        else:
            ans = min(ans, rsum * 2 + lsum)

    print(ans)

    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    ruiseki = [0]
    j = 0
    for i in range(N):
        if x[i] >= 0:
            ruiseki.append(x[i])
            j += 1

    j = 0
    ruiseki2 = [0]
    for i in range(N - 1, -1, -1):
        if x[i] < 0:
            ruiseki2.append(x[i])
            j += 1

    ans = float("inf")
    for i in range(1, K + 1):
        rsum = float("inf")
        if len(ruiseki) - 1 >= i:
            rsum = ruiseki[i]

        lsum = float("inf")
        if len(ruiseki2) - 1 >= K - i:
            lsum = abs(ruiseki2[K - i])

        if rsum > lsum:
            ans = min(ans, rsum + lsum * 2)
        else:
            ans = min(ans, rsum * 2 + lsum)

    print(ans)

    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 並んでるやつから取ったほうがいい + 折返しのぶんは左右どっちにいってから折り返すのがいいかminで判定
# cur, cur + kで調べる
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 10 ** 10
for i in range(n - k + 1):
    if arr[i] < 0 and arr[i + k - 1] < 0:
        ans = min(ans, abs(arr[i]))
    elif arr[i] < 0 and 0 <= arr[i + k - 1]:
        ans = min(
            ans, abs(arr[i]) * 2 + arr[i + k - 1], abs(arr[i]) + arr[i + k - 1] * 2
        )
    elif 0 <= arr[i] < arr[i + k - 1]:
        ans = min(ans, arr[i + k - 1])
    # print("taken from {}, ans = {}".format(arr[i : i + k], ans))
print(ans)