Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  if N == 0 and A[0] != 1:
    print(-1)
    return True
  min_maxs = []
  min_maxs.insert(0, [A[-1], A[-1]])

  for a in reversed(A[:-1]):
    min_maxs.insert(0, [min_maxs[0][0] // 2 + a, min_maxs[0][1] + a])
  # print(min_maxs)
  # max_node_in_depth = [sum(A[i:]) for i in range(len(A))]

  # # 各 depth における最小値
  # min_node_in_depth = [0] * (len(A))
  # min_node_in_depth[-1] = max_node_in_depth[-1]
  # index = -1
  # for i in range(1, len(A)):
  #   min_node_in_depth[index] = -(-min_node_in_depth[index + 1] // 2) + A[index]
  #   index = index - 1

  # for min_max in min_maxs:
  #   print(min_max)

  result = 1
  recent = 1
  for min_max, a in zip(min_maxs[1:], A):
    num_of_node = (recent - a) * 2
    # print(min_max, num_of_node)
    if num_of_node < min_max[0]:
      print(-1)
      return True
    
    result += min(num_of_node, min_max[1])
    recent = min(num_of_node, min_max[1])

  print(result)

if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  if N == 0 and A[0] != 1:
    print(-1)
    return True
  min_maxs = {}
  min_maxs[len(A)-1] = [A[-1], A[-1]]

  for index in reversed(range(len(A)-1)):
    min_maxs[index] = [min_maxs[index+1][0] // 2 + A[index], min_maxs[index+1][1] + A[index]]
  # print(min_maxs)
    # min_maxs.insert(0, [min_maxs[0][0] // 2 + a, min_maxs[0][1] + a])
  # print(min_maxs)
  # max_node_in_depth = [sum(A[i:]) for i in range(len(A))]

  # # 各 depth における最小値
  # min_node_in_depth = [0] * (len(A))
  # min_node_in_depth[-1] = max_node_in_depth[-1]
  # index = -1
  # for i in range(1, len(A)):
  #   min_node_in_depth[index] = -(-min_node_in_depth[index + 1] // 2) + A[index]
  #   index = index - 1

  # for min_max in min_maxs:
  #   print(min_max)

  result = 1
  recent = 1
  for index in range(1, len(A)):
    num_of_node = (recent - A[index-1]) * 2
    # print(min_max, num_of_node)
    if num_of_node < min_maxs[index][0]:
      print(-1)
      return True
    
    result += min(num_of_node, min_maxs[index][1])
    recent = min(num_of_node, min_maxs[index][1])

  print(result)

if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  if N == 0 and A[0] != 1:
    print(-1)
    return True
  max_node_in_depth = [sum(A[i:]) for i in range(len(A))]

  # 各 depth における最小値
  min_node_in_depth = [0] * (len(A))
  min_node_in_depth[-1] = max_node_in_depth[-1]
  index = -1
  for i in range(1, len(A)):
    min_node_in_depth[index] = -(-min_node_in_depth[index + 1] // 2) + A[index]
    index = index - 1

  result = 1
  recent = 1
  for min_node, max_mode, a in zip(min_node_in_depth[1:], max_node_in_depth[1:], A):
    # print(min_node, max_mode, a)
    num_of_node = (recent - a) * 2
    if num_of_node < min_node:
      print(-1)
      return True
    
    result += min(num_of_node, max_mode)
    recent = min(num_of_node, max_mode)

  print(result)


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  if N == 0 and A[0] != 1:
    print(-1)
    return True
  min_maxs = {}
  min_maxs[len(A)-1] = [A[-1], A[-1]]

  for index in reversed(range(len(A)-1)):
    min_maxs[index] = [min_maxs[index+1][0] // 2 + A[index], min_maxs[index+1][1] + A[index]]
  # print(min_maxs)
    # min_maxs.insert(0, [min_maxs[0][0] // 2 + a, min_maxs[0][1] + a])
  # print(min_maxs)
  # max_node_in_depth = [sum(A[i:]) for i in range(len(A))]

  # # 各 depth における最小値
  # min_node_in_depth = [0] * (len(A))
  # min_node_in_depth[-1] = max_node_in_depth[-1]
  # index = -1
  # for i in range(1, len(A)):
  #   min_node_in_depth[index] = -(-min_node_in_depth[index + 1] // 2) + A[index]
  #   index = index - 1

  # for min_max in min_maxs:
  #   print(min_max)

  result = 1
  recent = 1
  for index in range(1, len(A)):
    num_of_node = (recent - A[index-1]) * 2
    # print(min_max, num_of_node)
    if num_of_node < min_maxs[index][0]:
      print(-1)
      return True
    
    result += min(num_of_node, min_maxs[index][1])
    recent = min(num_of_node, min_maxs[index][1])

  print(result)

if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  if N == 0 and A[0] != 1:
    print(-1)
    return True
  max_node_in_depth = [sum(A[i:]) for i in range(len(A))]

  # 各 depth における最小値
  min_node_in_depth = [0] * (len(A))
  min_node_in_depth[-1] = max_node_in_depth[-1]
  index = -1
  for i in range(1, len(A)):
    min_node_in_depth[index] = -(-min_node_in_depth[index + 1] // 2) + A[index]
    index = index - 1

  result = 1
  recent = 1
  for i in range(1, len(A)):
    num_of_node = (recent - A[i - 1]) * 2
    if num_of_node < min_node_in_depth[i]:
      print(-1)
      return True

    if num_of_node <= max_node_in_depth[i]:
      result += num_of_node
      recent = num_of_node
    else:
      result += max_node_in_depth[i]
      recent = max_node_in_depth[i]

  print(result)


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  if N == 0 and A[0] != 1:
    print(-1)
    return True
  min_maxs = {}
  min_maxs[len(A)-1] = [A[-1], A[-1]]

  for index in reversed(range(len(A)-1)):
    min_maxs[index] = [min_maxs[index+1][0] // 2 + A[index], min_maxs[index+1][1] + A[index]]
  # print(min_maxs)
    # min_maxs.insert(0, [min_maxs[0][0] // 2 + a, min_maxs[0][1] + a])
  # print(min_maxs)
  # max_node_in_depth = [sum(A[i:]) for i in range(len(A))]

  # # 各 depth における最小値
  # min_node_in_depth = [0] * (len(A))
  # min_node_in_depth[-1] = max_node_in_depth[-1]
  # index = -1
  # for i in range(1, len(A)):
  #   min_node_in_depth[index] = -(-min_node_in_depth[index + 1] // 2) + A[index]
  #   index = index - 1

  # for min_max in min_maxs:
  #   print(min_max)

  result = 1
  recent = 1
  for index in range(1, len(A)):
    num_of_node = (recent - A[index-1]) * 2
    # print(min_max, num_of_node)
    if num_of_node < min_maxs[index][0]:
      print(-1)
      return True
    
    result += min(num_of_node, min_maxs[index][1])
    recent = min(num_of_node, min_maxs[index][1])

  print(result)

if __name__ == "__main__":
    resolve()