Two totals for each node
transcript
A freight app has depots in a tree. Each depot holds parcels. A courier cannot empty two linked depots on one day. What is the best total?
Each depot returns two totals to its parent. One total is for taking it. The other is for leaving it. The parent uses only its children's totals.
Leaves first. A leaf is worth its parcels if taken, and zero if left. Depot one: four taken, and four left. Depot two: five taken, and one left. Depot zero: eight taken, nine left. The better one is nine.
Taking a depot adds its parcels. It adds the left totals of its children. Never their taken totals. A taken child would be linked to it.
The better total at the top is nine. All parcels add up to seventeen. That would break the rule. Nine is the answer.
Return two totals from every node. Combine only the children. Full breakdown in the lecture.