Suppose the graph(tree) as rooted in node 0. We make a dfs on node 0 and dfs call on a node return two things, 1)number of nodes under its subtree(including itself) and 2)minimum fuel spent for all people under its subtree to come to this node.

```
import math
def solution(A, B):
n = len(A)
adj = [[] for _ in range(n + 1)]
for u, v in zip(A, B):
adj[u].append(v)
adj[v].append(u)
visited = set()
def dfs(node):
visited.add(node)
subtree_nodes_count = 1
subtree_fuel_spent = 0
for neighbor in adj[node]:
if neighbor not in visited:
nodes_count, fuel_spent = dfs(neighbor)
subtree_nodes_count += nodes_count
subtree_fuel_spent += (fuel_spent + math.ceil(nodes_count / 5))
return subtree_nodes_count, subtree_fuel_spent
return dfs(0)[1]
print(
solution(
[1, 1, 1, 9, 9, 9, 9, 7, 8],
[2, 0, 3, 1, 6, 5, 4, 0, 0]
)
)
```