Buggy python submission (verdict: Runtime Error):
def search_left(ind):
  doors = []
  for j in range(ind - 1, -1, -1):
    if alst[j] < 0:
     doors.append((j, -alst[j]))
    if alst[j] == 0:
      ret = alst[ind]
      for pos, lim in doors:
        ret = min(ret, lim - (ind - pos))
      ret = max(0, ret)
      return ret
  return 0

def search_right(ind):
  doors = []
  for j in range(ind + 1, w):
    if alst[j] < 0:
      doors.append((j, -alst[j]))
    if alst[j] == 0:
      ret = alst[ind]
      for pos, lim in doors:
        ret = min(ret, lim - (pos - ind))
      ret = max(0, ret)
      return ret
  return 0

def main():
  w = int(input())
  alst = list(map(int, input().split()))
  ans = 0
  for ind, a in enumerate(alst):
    if a > 0:
      ans += max(search_left(ind), search_right(ind))
  print(ans)
main() 


Fixed submission:
w = int(input())
alst = list(map(int, input().split()))
INF = 10 ** 20
acc = -INF
left = []
for i in range(w):
  a = alst[i]
  if a == 0:
    acc = INF
  elif a > 0:
    acc -= 1
  elif a < 0:
    acc = min(acc - 1, -a)
  left.append(acc)

acc = -INF
right = []
for i in range(w):
  a = alst[w - i - 1]
  if a == 0:
    acc = INF
  elif a > 0:
    acc -= 1
  elif a < 0:
    acc = min(acc - 1, -a)
  right.append(acc)
right.reverse()

print(sum([max(0, min(alst[i], max(left[i], right[i]))) for i in range(w)]))

Buggy python submission (verdict: Time Limit Exceeded):
def search_left(ind, alst, a, w):
  doors = []
  for j in range(ind - 1, -1, -1):
    if alst[j] < 0:
     doors.append((j, -alst[j]))
    if alst[j] == 0:
      ret = a
      for pos, lim in doors:
        ret = min(ret, lim - (ind - pos))
      ret = max(0, ret)
      return ret
  return 0

def search_right(ind, alst, a, w):
  doors = []
  for j in range(ind + 1, w):
    if alst[j] < 0:
      doors.append((j, -alst[j]))
    if alst[j] == 0:
      ret = a
      for pos, lim in doors:
        ret = min(ret, lim - (pos - ind))
      ret = max(0, ret)
      return ret
  return 0

def main():
  w = int(input())
  alst = list(map(int, input().split()))
  ans = 0
  for ind, a in enumerate(alst):
    if a > 0:
      ans += max(search_left(ind, alst, a, w), search_right(ind, alst, a, w))
  print(ans)
main() 


Fixed submission:
w = int(input())
alst = list(map(int, input().split()))
INF = 10 ** 20
acc = -INF
left = []
for i in range(w):
  a = alst[i]
  if a == 0:
    acc = INF
  elif a > 0:
    acc -= 1
  elif a < 0:
    acc = min(acc - 1, -a)
  left.append(acc)

acc = -INF
right = []
for i in range(w):
  a = alst[w - i - 1]
  if a == 0:
    acc = INF
  elif a > 0:
    acc -= 1
  elif a < 0:
    acc = min(acc - 1, -a)
  right.append(acc)
right.reverse()

print(sum([max(0, min(alst[i], max(left[i], right[i]))) for i in range(w)]))

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left as bl

def search_left(in_ind, alst, outs, doors):
  tmp = bl(outs, in_ind)
  if tmp == 0:
    return 0
  out_ind = outs[tmp - 1]

  right_door = bl(doors, in_ind) - 1
  if right_door < 0:
    return alst[in_ind]
  left_door = bl(doors, out_ind)
  
  ret = alst[in_ind]
  for door_ind in doors[left_door:right_door + 1]:
    if out_ind < door_ind < in_ind:
      ret = min(ret, -alst[door_ind] - (in_ind - door_ind))
  ret = max(0, ret)
  return ret

def search_right(in_ind, alst, outs, doors):
  tmp = bl(outs, in_ind)
  if tmp == len(outs):
    return 0
  out_ind = outs[tmp]

  right_door = bl(doors, out_ind) - 1
  if right_door < 0:
    return alst[in_ind]
  left_door = bl(doors, in_ind)

  ret = alst[in_ind]
  for door_ind in doors[left_door:right_door + 1]:
    if in_ind < door_ind < out_ind:
      ret = min(ret, -alst[door_ind] - (door_ind - in_ind))
  ret = max(0, ret)
  return ret

def main():
  w = int(input())
  alst = list(map(int, input().split()))
  outs = []
  ins = []
  doors = []
  for ind, a in enumerate(alst):
    if a > 0:
      ins.append(ind)
    elif a == 0:
      outs.append(ind)
    else:
      doors.append(ind)
  ans = 0
  for in_ind in ins:
    ans += max(search_left(in_ind, alst, outs, doors), search_right(in_ind, alst, outs, doors))
  print(ans)
main() 


Fixed submission:
w = int(input())
alst = list(map(int, input().split()))
INF = 10 ** 20
acc = -INF
left = []
for i in range(w):
  a = alst[i]
  if a == 0:
    acc = INF
  elif a > 0:
    acc -= 1
  elif a < 0:
    acc = min(acc - 1, -a)
  left.append(acc)

acc = -INF
right = []
for i in range(w):
  a = alst[w - i - 1]
  if a == 0:
    acc = INF
  elif a > 0:
    acc -= 1
  elif a < 0:
    acc = min(acc - 1, -a)
  right.append(acc)
right.reverse()

print(sum([max(0, min(alst[i], max(left[i], right[i]))) for i in range(w)]))