Difference Between Maximum and Minimum Price Sum Problem
Description
LeetCode Problem 2538.
There exists an undirected and initially unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
Each node has an associated price. You are given an integer array price, where price[i] is the price of the i^th node.
The price sum of a given path is the sum of the prices of all nodes lying on that path.
The tree can be rooted at any node root of your choice. The incurred cost after choosing root is the difference between the maximum and minimum price sum amongst all paths starting at root.
Return the maximum possible cost amongst all possible root choices.
Example 1:

Input: n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]
Output: 24
Explanation: The diagram above denotes the tree after rooting it at node 2. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum.
- The first path contains nodes [2,1,3,4]: the prices are [7,8,6,10], and the sum of the prices is 31.
- The second path contains the node [2] with the price [7].
The difference between the maximum and minimum price sum is 24. It can be proved that 24 is the maximum cost.
Example 2:

Input: n = 3, edges = [[0,1],[1,2]], price = [1,1,1]
Output: 2
Explanation: The diagram above denotes the tree after rooting it at node 0. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum.
- The first path contains nodes [0,1,2]: the prices are [1,1,1], and the sum of the prices is 3.
- The second path contains node [0] with a price [1].
The difference between the maximum and minimum price sum is 2. It can be proved that 2 is the maximum cost.
Constraints:
1 <= n <= 10^5edges.length == n - 10 <= a_i, b_i <= n - 1edgesrepresents a valid tree.price.length == n1 <= price[i] <= 10^5
Solution
Approach: Tree DP
Whichever node we pick as the root, a path from it has a price sum equal to the node's own price plus the rest, and the minimum-price path is the single root node. So the cost of a path is its price sum with one end left out. The answer is the best such value over all paths in the tree: the largest path sum that drops one of its two end nodes.
Root the tree at node 0. For each node i, a DFS computes over its subtree:
a: the largest sum of a downward path starting atiand including its last node;b: the largest sum of a downward path starting atiwith its last node left out.
When a child returns (c, d), join it with the best from the children handled earlier. Joining at i gives a + d (earlier path whole, child's path without its end) and b + c (the reverse), and both are answer candidates. Then update a = max(a, price[i] + c) and b = max(b, price[i] + d).
Complexity: O(n) time and O(n) space for the adjacency lists and the recursion.
from collections import defaultdict
class Solution:
def maxOutput(self, n: int, edges: List[List[int]], price: List[int]) -> int:
def dfs(i, fa):
a, b = price[i], 0
for j in g[i]:
if j != fa:
c, d = dfs(j, i)
nonlocal ans
ans = max(ans, a + d, b + c)
a = max(a, price[i] + c)
b = max(b, price[i] + d)
return a, b
g = defaultdict(list)
for a, b in edges:
g[a].append(b)
g[b].append(a)
ans = 0
dfs(0, -1)
return ans