This is the hardest rerooting problem so far. Think carefully:
Downward pass: What do you store for each node? (Hint: Max balance in subtree rooted at u, taking only parts with positive contribution)
Upward pass: When moving root from u to v, what 'up' value do you pass to v? (Hint: Contribution from u and everything except v's subtree)
Final answer at node u: How do you combine down and up? (Hint: u always contributes its color, then add positive parts only) Core idea: You can choose to exclude negative-balance subtrees. So you take max(, child_contribution). Spend minutes thinking.