Educational Tree Problem
InterviewTime limit1sMemory limit1024 MB
Given a rooted tree where each child's value is at most its parent's, find the maximum sum of a parent-closed vertex set for each size k from 1 to N.
Problem
The tree has vertices. Each vertex is numbered from to with no duplicates, and vertex is the root of the tree.
For every integer with , the parent of vertex is vertex .
Vertex has the integer written on it, and always holds. Choose some vertices to maximize the sum of the integers written on the chosen vertices. Every chosen vertex must be either vertex or have its parent also chosen.
Find the answer for each possible number of chosen vertices.
Input
The first line gives the integer .
The second line gives the sequence , separated by spaces.
The third line gives the sequence , separated by spaces.
Output
Print lines. On the -th line, print the answer when exactly vertices are chosen.