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: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
x = list(map(int, input().split()))
ans = 0
for i in range(N-K+1):
    light = x[i:i+K]
    l = light[0]
    r = light[-1]
    time = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    if ans == 0 or time < ans:
        ans = time
print(ans)

Submission status: Time Limit Exceeded

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

ans = 10**8 * 3
for i in range(N-K+1):
    light = x[i:i+K]
    l = light[0]
    r = light[-1]
    time = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    if time < ans:
        ans = time
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**8 * 3

for i in range(N-K+1):
    light = x[i:i+K]
    l = light[0]
    r = light[-1]
    if l*r >= 0:
        time = max(abs(l), abs(r))
    else:
        time = min(abs(l)*2+abs(r), abs(l)+abs(r)*2)
    if time < ans:
        ans = time
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
c = list(map(int, input().split()))
ans = float('Inf')
for i in range(n-k+1):
  temp = c[i:i+k]
  l, r = abs(temp[0]), abs(temp[-1])
  if temp[0] < 0 and temp[-1] > 0:
    ans = min(ans, min(l, r) * 2 + max(l, r))
  else:
    ans = min(ans, max(map(abs, temp)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))

# n, k = 5, 3
# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]
#
# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]



def set_max(ls):
    if len(ls)>0:
        return set([max(ls)])
    else:
        return set()
def set_min(ls):
    if len(ls)>0:
        return set([min(ls)])
    else:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
cnt = 0
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:

        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
        cnt += 1
        if cnt == k + 1:
            break
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []
# for i in range(size):
#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))

# n, k = 5, 3
# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]

def set_max(ls):
    if len(ls)>0:
        return set([max(ls)])
    else:
        return set()
def set_min(ls):
    if len(ls)>0:
        return set([min(ls)])
    else:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []
# for i in range(size):
#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))

# n, k = 5, 3
# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]

def set_max(ls):
    try:
        return set([max(ls)])
    except:
        return set()
def set_min(ls):
    try:
        return set([min(ls)])
    except:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []
# for i in range(size):
#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))

Submission status: Time Limit Exceeded

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

sorted_X = sorted(X, key=abs)

centers = []

if sorted_X[0] == 0:
    # 0がある場合
    centers.append(X.index(0))
else:
    for x in sorted_X:
        if x > 0:
            centers.append(X.index(x))
            break

    for x in sorted_X:
        if x < 0:
            centers.append(X.index(x))
            break


edge_pairs = []
# ひだりエッジ
for center_index in centers:
    left_edge = center_index - (K - 1)
    if left_edge >= 0:
        start_left_edge_index = left_edge
        start_right_edge_index = center_index
    else:
        start_left_edge_index = 0
        start_right_edge_index = K-1

    for move in range(K):
        left_edge = start_left_edge_index + move
        right_edge = start_right_edge_index + move
        if right_edge >= N:
            break
        edge_pairs.append((left_edge, right_edge))
edge_pairs = list(set(edge_pairs))


propose = []
for (left, right) in edge_pairs:
    pos_max = 0
    neg_max = 0
    for i in range(left, right+1):
        target = X[i]
        if target > 0:
            if pos_max < target:
                pos_max = target

        if target < 0:
            if neg_max < abs(target):
                neg_max = abs(target)

    propose.append(pos_max + 2 * neg_max)
    propose.append(neg_max + 2 * pos_max)


print(min(propose))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

# print(N, K)
# print(x)

def calc_time(x):
    x_max = max(x)
    x_min = min(x)
    if x_max * x_min > 0:
        if x_max > 0:
            # print('return abs(x_max)')
            return abs(x_max)
        else:
            # print('return abs(x_min)')
            return abs(x_min)
    else:
        if abs(x_max) > abs(x_min):
            # print('return abs(x_max) + abs(x_min) * 2')
            return abs(x_max) + abs(x_min) * 2
        else:
            # print('return abs(x_max) * 2 + abs(x_min)')
            return abs(x_max) * 2 + abs(x_min)

times = []
x_argmin = np.argmin(list(map(abs, x)))

if x_argmin - K < 0:
    left = 0
else:
    left = x_argmin - K

if x_argmin + K > N - K + 1:
    right = N - K + 1
else:
    right = x_argmin + K

for i in range(left, right):
    # print(x[i: i + K])
    times.append(calc_time(x[i: i + K]))

print(min(times))

Submission status: Time Limit Exceeded

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

# print(N, K)
# print(x)

def calc_time(x):
    x_max = max(x)
    x_min = min(x)
    if x_max * x_min > 0:
        if x_max > 0:
            # print('return abs(x_max)')
            return abs(x_max)
        else:
            # print('return abs(x_min)')
            return abs(x_min)
    else:
        if abs(x_max) > abs(x_min):
            # print('return abs(x_max) + abs(x_min) * 2')
            return abs(x_max) + abs(x_min) * 2
        else:
            # print('return abs(x_max) * 2 + abs(x_min)')
            return abs(x_max) * 2 + abs(x_min)

times = []
for i in range(0, N - K + 1):
    times.append(calc_time(x[i: i + K]))

print(min(times))

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
 
 
def mindist(XLIST):
    cand1 = abs(XLIST[0]) + abs(XLIST[0] - XLIST[-1])
    cand2 = abs(XLIST[-1]) + abs(XLIST[0] - XLIST[-1])
    return(min([cand1, cand2]))
 
dist = float('Inf')
for i in range(N - K + 1):
    dist_ = mindist(X[i:(i+K)])
    if (dist_ < dist):
        dist = dist_
 
print(dist)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

kouho = []
ans_list = []
for i in range(N-K+1):
    li = xi[i:i+K]
    if li[0]*li[-1] < 0:
        a = abs(li[0])*2 + abs(li[-1])
        b = abs(li[-1])*2 + abs(li[0])
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif li[0] < 0:
        ans_list.append(abs(li[0]))
    else: #if li[-1] > 0:
        ans_list.append(abs(li[-1]))

print(min(ans_list))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

kouho = []
ans_list = []
for i in range(N-K+1):
    li = xi[i:i+K]
    if li[0]*li[-1] < 0:
        ans_list.append(abs(li[0])*2 + abs(li[-1]))
        ans_list.append(abs(li[-1])*2 + abs(li[0]))
    elif li[0] < 0:
        ans_list.append(abs(li[0]))
    else: #if li[-1] > 0:
        ans_list.append(abs(li[-1]))

print(min(ans_list))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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


kouho = []
for i in range(N-K+1):
    kouho.append(xi[i:i+K])

# print(kouho)
ans_list = []
for li in kouho:
    if li[0]*li[-1] < 0:
        ans_list.append(abs(li[0])*2 + abs(li[-1]))
        ans_list.append(abs(li[-1])*2 + abs(li[0]))
    elif li[0] < 0:
        ans_list.append(abs(li[0]))
    else: #if li[-1] > 0:
        ans_list.append(abs(li[-1]))

print(min(ans_list))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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


def mindist(XLIST):
    cand1 = abs(XLIST[0]) + abs(XLIST[0] - XLIST[-1])
    cand2 = abs(XLIST[-1]) + abs(XLIST[0] - XLIST[-1])
    return(min([cand1, cand2]))

dist = float('Inf')
for i in range(N - K + 1):
    dist_ = mindist(X[i:(i+K)])
    if (dist_ < dist):
        dist = dist_

print(dist)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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


def mindist(XLIST):
    dist = float('Inf')
    for i in itertools.permutations(XLIST, len(XLIST)):
        dist_ = 0
        curx = 0
        for j in range(len(i)):
            dist_ += abs(i[j]-curx)
            curx = i[j]
        if (dist_ < dist):
            dist = dist_
    return(dist)

dist = float('Inf')
for i in range(N - K + 1):
    dist_ = mindist(X[i:(i+K)])
    if (dist_ < dist):
        dist = dist_

print(dist)

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().strip().split(" ")))
candles = input().strip()
min_cost_index = candles.count("-")
candles = list(map(int, candles.split(" ")))

def update():
    start_index = max(0, min_cost_index - K - 1)
    end_index = min(N, min_cost_index + K + 1)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 

candles = update()

N = len(candles)
min_cost = None
patterns = N - K + 1


for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Time Limit Exceeded

Solution code:
def update():
    cost = list(map(abs, candles))
    min_cost_index = cost.index(min(cost))
    start_index = max(0, min_cost_index - K)
    end_index = min(N, min_cost_index + K)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 

N, K = list(map(int, input().strip().split(" ")))
candles = list(map(int, input().strip().split(" ")))



candles = update()

N = len(candles)
min_cost = None
patterns = N - K + 1


for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Time Limit Exceeded

Solution code:
def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 

N, K = list(map(int, input().strip().split(" ")))
candles = list(map(int, input().strip().split(" ")))

patterns = N - K + 1

min_cost = None

for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Time Limit Exceeded

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

ans = 10**18
for i in range(N - K + 1):
    p = x[i:i+K]
    p1 = x[i]
    p2 = x[i+K-1]
    lr = abs(p1) + abs(p2 - p1)
    rl = abs(p2) + abs(p1 - p2)
    if lr < rl:
        ans = min(ans,lr)
    else:
        ans = min(ans,rl)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 10**18
for i in range(N - K + 1):
    p = x[i:i+K]
    lr = abs(p[0]) + abs(p[-1] - p[0])
    rl = abs(p[-1]) + abs(p[0] - p[-1])
    if lr < rl:
        ans = min(ans,lr)
    else:
        ans = min(ans,rl)
print(ans)

Submission status: Time Limit Exceeded

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

c = []
for i in range(N - K + 1):
    p = x[i:i+K]
    lr = abs(p[0]) + abs(p[-1] - p[0])
    rl = abs(p[-1]) + abs(p[0] - p[-1])
    if lr < rl:
        c.append(lr)
    else:
        c.append(rl)
print(min(c))

Submission status: Time Limit Exceeded

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

c = [1000000000]*(N-K+10)
for i in range(N - K + 1):
    if (len(x[i:i+K]) != K):
        c[i] = 1000000000
        continue
    p = x[i:i+K]
    lr = abs(p[0]) + abs(p[-1] - p[0])
    rl = abs(p[-1]) + abs(p[0] - p[-1])
    if lr < rl:
        c[i] = lr
    else:
        c[i] = rl

print(min(c))

Submission status: Time Limit Exceeded

Solution code:

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

c = [1000000000]*(N-K+10)
for i in range(N - K + 1):
    if (len(x[i:i+K]) != K):
        c[i] = 1000000000
        continue
    p = x[i:i+K]
    if min(p) < 0 and max(p) > 0:
        if p[-1] + p[-1] - p[0] < abs(p[0]) + p[-1] -p[0]:
            c[i] = (p[-1] + (p[-1] - p[0]))
        else:
            c[i] = (abs(p[0]) + p[-1] - p[0])
    elif min(p) >= 0:
        c[i] = (p[-1])
    elif max(p) <= 0:
        c[i] = (abs(p[0]))

print(min(c))

Submission status: Time Limit Exceeded

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

c = []
for i in range(N - K + 1):
    if (len(x[i:i+K]) != K):
        continue
    p = x[i:i+K]
    if min(p) < 0 and max(p) > 0:
        if p[-1] + p[-1] - p[0] < abs(p[0]) + p[-1] -p[0]:
            c.append(p[-1] + (p[-1] - p[0]))
        else:
            c.append(abs(p[0]) + p[-1] - p[0])
    elif min(p) >= 0:
        c.append(p[-1])
    elif max(p) <= 0:
        c.append(abs(p[0]))

print(min(c))

Submission status: Time Limit Exceeded

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

for i in range(n-k+1):
    xtmp = x[i:i+k]
#     print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]), abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

print(MIN)

Submission status: Time Limit Exceeded

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

for i in range(n-k+1):
    xtmp = x[i:i+k]
    if i==0:
        MIN = abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

print(MIN)

Submission status: Time Limit Exceeded

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

if 0 not in x:
    K += 1

minus = []
plus = []
zero = []

for i in x:
    if i < 0:
        minus.insert(0, i)
    elif i == 0:
        zero.append(i)
    else:
        plus.append(i)

p_lis = []
m_lis = []
o_lis = []

p_lis.append(0)
m_lis.append(0)
o_lis.append(0)

for i in plus:
    p_lis.append(i)
    if len(p_lis) == K:
        break

if len(p_lis) < K:
    for i in minus:
        p_lis.append(i)
        if len(p_lis) == K:
            break

p_Mm = []
p_Mm.append(max(p_lis))
p_Mm.append(abs(min(p_lis)))

p_t = 2 * min(p_Mm) + max(p_Mm)

for i in minus:
    m_lis.append(i)
    if len(m_lis) == K:
        break

if len(m_lis) < K:
    for i in plus:
        m_lis.append(i)
        if len(m_lis) == K:
            break

m_Mm = []
m_Mm.append(max(m_lis))
m_Mm.append(abs(min(m_lis)))

m_t = 2 * min(m_Mm) + max(m_Mm)

while len(o_lis) < K:
    pk = 999999999
    mk = -999999999
    if len(plus) != 0:
        pk = plus[0]
    if len(minus) != 0:
        mk = minus[0]
    if pk-max(o_lis) < abs(mk-min(o_lis)):
        o_lis.append(plus.pop(0))
    else:
        o_lis.append(minus.pop(0))

o_Mm = []
o_Mm.append(max(o_lis))
o_Mm.append(abs(min(o_lis)))

o_t = 2 * min(o_Mm) + max(o_Mm)

print(min(p_t, m_t, o_t))

Submission status: Time Limit Exceeded

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

for i in range(n-k+1):
    xtmp = x[i:i+k]
#     print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
    MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))

print(MIN)

Submission status: Time Limit Exceeded

Solution code:
# vim: set fileencoding=utf-8:
import bisect


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

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

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

    print(best)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()

def main():
    n, k = na()
    xs = na()

    m = float('inf')
    for i in range(n-k+1):
        vs = xs[i:i+k]
        if vs[0] * vs[-1] < 0:
            l = abs(vs[0]) + abs(vs[-1]) + min(abs(vs[0]), abs(vs[-1]))
        else:
            l = max(abs(vs[0]), abs(vs[-1]))

        m = min(m, l)

    print(m)
    
main()

Submission status: Time Limit Exceeded

Solution code:
# vim: set fileencoding=utf-8:
import bisect


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

    zenpou = x
    kouhou = x

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

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

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

    print(best)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
x.append(0)
x.sort()

c = []
for i in range(N - K + 2):
    if (0 not in x[i:i+K+1]) or (len(x[i:i+K+1]) != K+1):
        continue
    p = x[i:i+K+1]
    if min(p) < 0 and max(p) > 0:
        if p[-1] + p[-1] - p[0] < abs(p[0]) + p[-1] -p[0]:
            c.append(p[-1] + (p[-1] - p[0]))
        else:
            c.append(abs(p[0]) + p[-1] - p[0])
    elif min(p) >= 0:
        c.append(p[-1])
    elif max(p) <= 0:
        c.append(abs(p[0]))

print(min(c))

Submission status: Time Limit Exceeded

Solution code:
N,K = list(map(int,input().split(" ")))
x = list(map(int,input().split(" ")))
for i in range(N):
    for j in range(N-1):
        if abs(x[j]) > abs(x[j+1]):
            tmp = x[j+1]
            x[j+1] = x[j]
            x[j] = tmp

c = x[0:K]
l = (abs(min(c)) + max(c) - min(c))
r = (abs(max(c)) + max(c) - min(c))
if l < r:
    print(l)
else:
    print(r)

Submission status: Time Limit Exceeded

Solution code:
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()

def main():
    n, k = na()
    xs = na()

    m = float('inf')
    for i in range(n-k+1):
        vs = xs[i:i+k]
        if vs[0] * vs[-1] < 0:
            m = min(m, abs(vs[0]) + abs(vs[-1]) + min(abs(vs[0]), abs(vs[-1])))
        else:
            m = min(m, max(abs(vs[0]), abs(vs[-1])))

    print(m)
    
main()

Submission status: Time Limit Exceeded

Solution code:
# vim: set fileencoding=utf-8:
import bisect


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

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

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

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

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

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

    print(best)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    l1 = abs(min(L))+abs(max(L)-min(L))
    l2 = abs(max(L))+abs(max(L)-min(L))
    ANS.append(min(l1,l2))
print(min(ANS))

Submission status: Time Limit Exceeded

Solution code:
N,K=(int(i) for i in input().split())
x = [int(i) for i in input().split()]
ans=99999999999999999
for i in range(N-K+1):
    c=0
    d=0
    for j in range(K):
        if x[i+j] < 0:
                if c > x[i+j]:
                    c = abs(x[i])
        else:
                if d < x[i+j]:
                    d = x[i+j]
    e = c + d + min(c,d)
    if e < ans:
        ans = e
print(ans)

Submission status: Time Limit Exceeded

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

lefts = []
rights = []
for i in range(n):
    if len(rosokus[i : k + i]) == k:
        lefts.append(rosokus[i])
        rights.append(rosokus[k + i - 1])
    else:
        break

min_dis = 9999999999
for left, right in zip(lefts, rights):
    for start_side in [left, right]:
        if abs(left - right) + abs(start_side) < min_dis:
            min_dis = abs(left - right) + abs(start_side)
print(min_dis)

Submission status: Time Limit Exceeded

Solution code:
# vim: set fileencoding=utf-8:
import bisect


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

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

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

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

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

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

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

    print(sorted(cal)[0])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
xl=list(map(int,input().split()))
ans=float('INF')
for i in range(n-k+1):
    a=xl[i:k+i]
    if min(abs(a[-1]-a[0])+abs(a[-1]),abs(a[-1]-a[0])+abs(a[0]))<ans:
        ans=min(abs(a[-1]-a[0])+abs(a[-1]),abs(a[-1]-a[0])+abs(a[0]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
xl=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
    a=xl[i:k+i]
    ans.append(min(abs(a[-1]-a[0])+abs(a[-1]),abs(a[-1]-a[0])+abs(a[0])))
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    if min(L)*max(L) >= 0 :
        ANS.append(max(abs(min(L)),max(L)))
    else :
        ANS.append(min(abs(min(L))*2+abs(max(L)),abs(max(L))*2+abs(min(L))))
print(min(ANS))
#print(ANS)

Submission status: Time Limit Exceeded

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

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

ans = 0
for i in range(N - K + 1):
    long = []
    for j in range(K):
        long.append(x_list[i + j])

    left = min(long)
    right = max(long)
    total = 0
    if left < 0 and right > 0:
        
        total = abs(left) + right + min(abs(left), right)
        
    else:
        if right < 0:
            total = abs(left)
        else:
            total = right


    if i == 0:   
        ans = total
    elif ans > total:
        ans = total    
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    cplus = 0
    cminus = 0
    czero = 0
    for j in range(len(L)) :

        if L[j] > 0 :
            cplus += 1
        elif L[j] < 0 :
            cminus += 1
        else :
            czero += 1
    if (cplus == K-czero) or (cminus == K-czero):
        ANS.append(max(abs(min(L)),max(L)))
    else :
        ANS.append(min(abs(min(L))*2+abs(max(L)),abs(max(L))*2+abs(min(L))))
print(min(ANS))

Submission status: Time Limit Exceeded

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

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

long_list = []
for i in range(N - K + 1):
    long = []
    for j in range(K):
        long.append(x_list[i + j])

    left = min(long)
    right = max(long)
    if left < 0 and right > 0:
        
        total = abs(left) + right + min(abs(left), right)
        long_list.append(total)
    else:
        if right < 0:
            long_list.append(abs(left))
        else:
            long_list.append(right)
           

ans = min(long_list)
    
print(ans)