report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
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 ```
DP on tree
Tree coloring
Tree coloring 2
Maximum sum path
Path with length k
Subgraph
Beautiful tree
Company
Zero tree
Selfie
Topic
Graph
Tree
Dynamic Programming
Rating 1700
Source Codeforces
Solution (1) Solution