No Two Neighbours Chosen
Each node of a tree has a weight. Choose a set of nodes with NO two adjacent, maximizing the total weight.
Input: the first line has N; the second line has the N weights; the next N−1 lines have the edges.
Output: the maximum weight of an independent set.
Constraints:
- 1 ≤ N ≤ 105
- 0 ≤ wi ≤ 1000
Sample Tests
Input 1
4 3 1 4 1 1 2 1 3 3 4
Output 1
5
Input 2
1 9
Output 2
9
Discussion (opens after you solve — no spoilers before)
No comments yet.