← 返回 twosigma 的题目列表Split Drainage Tree
类型:online_judge
Split Drainage Tree
A drainage tree has n nodes numbered from 0 to n - 1, with node 0 as the root.
You are given:
an array parent, where parent[0] = -1 and parent[i] is the parent of node i;
an array flow, where flow[i] is the flow directly associated with node i.
You must cut exactly one parent-child edge. This splits the tree into two connected components. The flow of a component is the sum of flow over all nodes in that component.
Return the minimum possible absolute difference between the total flows of the two components.
Input Format
n
parent[0] parent[1] ... parent[n-1]
flow[0] flow[1] ... flow[n-1]
Output Format
Print the minimum possible flow difference.
Example
Input:
5
-1 0 0 1 1
4 2 3 5 1
Output:
1
Constraints
2 <= n <= 2 * 10^5
parent[0] = -1
0 <= parent[i] < n for every i > 0
0 <= flow[i] <= 10^9
parent represents a valid rooted tree with root 0.
Example
Input
5
-1 0 0 1 1
4 2 3 5 1
Output
1