Problems > No Two Neighbours Chosen

No Two Neighbours Chosen

Difficulty: 1700 6000 ms 128 MB

dp trees

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

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.