report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Picking apples - MarisaOJ: Marisa Online Judge

Picking apples

Time limit: 1000 ms
Memory limit: 256 MB
There is an apple tree growing in Hakurei shrine consisting of $n$ vertices and rooted at $1$, the $i^{th}$ vertex has an apple that value $A_i$. Each subtree rooted at $i$ also has a limit $B_i$, no more than $B_i$ apples in subtree $i$ can be picked. Determine the maximum apple value Marisa can have. ### Input - The first line contains an integer $n$. - The second line contains $n$ integers $A_i$. - The third line contains $n$ integers $B_i$. - The next $n-1$ lines, each line contains two integers $u, v$, there is an edge between $u$ and $v$. ### Output - Print the maximum value. ### Constraints - $1 \le n \le 10^5$. - $1 \le A_i \le 10^9$. - $0 \le B_i \le n$. - $1 \le u, v \le n$. ### Example Input: ``` 4 1 4 2 3 2 1 1 0 1 2 2 3 2 4 ``` Output: ``` 5 ```
Small-to-large
Distinct colors
Picking apples
Frequent color
Path with length k 2
LIS on tree
Topic
Tree
Greedy
DSU
Rating 1700
Solution (0) Solution