Zero tree - MarisaOJ: Marisa Online Judge

Zero tree

Time limit: 1000 ms
Memory limit: 256 MB
Given a tree of $n$ vertices. On vertex $i$ there is an integer $A_i$. You can apply the following operation unlimitedly: - Select a set of connected vertices that contains vertex $1$, increase or decrease all these vertices by $1$. What's the minimum number of operations needed to make all numbers become $0$. ### Input - The first line contains an integer $n$. - The second line contains $n$ integers $A_i$. - The next $n - 1$ lines, each line contains 2 integers $u, v$, there is an edge between $u$ and $v$. ### Output - Print the minimum number of operations. ### Constraints - $1 \le n \le 10^5$. - $0 \le |A_i| \le 10^9$. - $1 \le u, v \le n$. ### Example Input: ``` 3 1 -1 1 1 2 1 3 ``` Output: ``` 3 ```