atcoder#TOKIOMARINE2020D. Knapsack Queries on a tree
Knapsack Queries on a tree
Score : points
Problem Statement
We have a rooted binary tree with vertices, where the vertices are numbered to . Vertex is the root, and the parent of Vertex () is Vertex .
Each vertex has one item in it. The item in Vertex has a value of and a weight of . Now, process the following query times:
- Given are a vertex of the tree and a positive integer . Let us choose some (possibly none) of the items in and the ancestors of so that their total weight is at most . Find the maximum possible total value of the chosen items.
Here, Vertex is said to be an ancestor of Vertex when is an indirect parent of , that is, there exists a sequence of vertices () where , , and is the parent of for each .
Constraints
- All values in input are integers.
- For the values and given in each query, and .
Input
Let and be the values and given in the -th query. Then, Input is given from Standard Input in the following format:
Output
For each integer from through , the -th line should contain the response to the -th query.
3
1 2
2 3
3 4
3
1 1
2 5
3 5
0
3
3
In the first query, we are given only one choice: the item with . Since , we cannot actually choose it, so our response should be .
In the second query, we are given two choices: the items with and . Since , we can choose both of them, so our response should be .
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
256
255
250
247
255
259
223
253