We’re preparing your current view and syncing the latest data.
You are given a tree with N vertices. Each vertex has an associated value. Your goal is to find the minimum possible tax to pay under certain constraints specified in the problem statement for the hard version of Skibidus and Fanum Tax (Codeforces 2065C2). The problem involves advanced tree DP and careful analysis to optimize the solution.
The first line contains an integer N, the number of vertices in the tree. Then follow N-1 lines each containing two integers u and v denoting an edge between vertices u and v. Additional lines describe vertex values and other parameters as per the problem statement.
Output the minimum tax value possible according to the problem constraints.
1 <= N <= 200000; values of vertices and other constraints as defined in the original problem.
Example 1
Input
5 1 2 1 3 3 4 3 5 5 2 3 4 1
Output
7
Explanation
Example shows optimal selection of vertices according to problem constraints leading to the minimum tax 7.