Processing math: 100%
Beautiful tree - ReimuOJ: Reimu Online Judge

Beautiful tree

Time limit: 1000 ms
Memory limit: 256 MB
Given a tree of n vertices. On vertex i, you can choose a number ai that li≤ai≤ri and write on it. The beauty of the tree is defined as the sum of |au−av| for every edge (u,v). Find the maximum possible beauty of the tree. ### Input - The first line contains an integer n. - The next n lines, each line contains 2 integers li,ri. - The next n−1 lines, each line contains 2 integers u,v, there is an edge between u and v. ### Output - Print the maximum beauty of the tree. ### Constraints - 1≤n≤105. - 1≤li≤ri≤109. - 1≤u,v≤n. ### Example Input: 31346791223 Output: 8