Problem p03274 (python)

Fixed submission:
#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]
if not (0 in ls):
    ls.append(0)
    k+=1
#print (ls)

ls = np.array(ls)
ls.sort()
#print (ls)

idx = np.where(ls==0)[0][0]
#print (idx,k)
#print (idx+k)
start = max(0,idx-k)
goal = min(idx+k,len(ls)-1)

#print (start,goal)
answer = []
for i in range(goal-(k-1)+1):
    #print (i,i+k-1)
    mi = min(abs(ls[i]),abs(ls[i+k-1]))
    ma = max(abs(ls[i]),abs(ls[i+k-1]))
    #print (mi,ma)
    answer.append(2*mi+ma)
print (min(answer))
# 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))

Buggy submission (verdict: Runtime Error):
#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))

Buggy submission (verdict: Runtime Error):
#coding:utf-8

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])
        if sign(ls[i]*ls[i+k-1])<0:
            temp = [abs(ls[i]),abs(ls[i+k-1])]
            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(2*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))

Buggy submission (verdict: Runtime Error):
#coding:utf-8

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])
        temp = [0,ls[i],ls[i+k-1]]
        temp = [abs(min(temp)),abs(max(temp))]
        answer.append((2*min(temp)+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))

Buggy submission (verdict: Runtime Error):
#coding:utf-8

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]

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))

Buggy submission (verdict: Runtime Error):
#coding:utf-8

# 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]

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))

Buggy submission (verdict: Runtime Error):
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))