#P9689. Bina.
Bina.
Problem Description
There are two trees in front of Xiao J’s house: one is a binary tree, and the other is a ternary tree.
You are asked by Xiao J to prune his binary tree so that its “beauty value” is as large as possible. The beauty value of a tree (the sum of the node labels in the tree) (the depth of the tree), rounded down.
This binary tree has a construction parameter , and it is built as follows:
void build(int s,int t,int p){
if(s==t) return ;
build(s,(s+t)/2,2*p);
build((s+t)/2+1,t,2*p+1);
add_edge(p,2*p),add_edge(p,2*p+1);
}
int main(){
build(1,n,1);
return 0;
}
In the function parameters of build(s,t,p), is the label of the current node. The function add_edge(x,y) means adding a directed edge from the node labeled to the node labeled .
It is easy to see that the root node of this tree is . We define the depth of a node as the number of nodes on the path from it to the root (including itself and the root). The depth of the tree is the maximum depth among all nodes.
For the case , the final constructed tree is shown below. The depth of this tree is . You need to choose a depth and prune (remove) all nodes whose depth is greater than , but must be greater than or equal to and less than or equal to the depth of the tree.

Xiao J also gives you a requirement: the number of nodes you prune must be at least , otherwise he will not be happy. You need to maximize the tree’s beauty value while ensuring that he is happy.
Now Xiao J gives you the parameter for building the tree and the minimum number of nodes to prune . You need to find the maximum possible beauty value after pruning. If no matter what you do, you cannot make Xiao J happy, output .
Input Format
This problem contains multiple test cases.
The first line contains a positive integer , representing the number of test cases.
For each test case:
There is one line containing two integers , representing the construction parameter of the binary tree and the minimum number of nodes you must prune.
Output Format
For each test case, output one line containing one integer, representing the maximum beauty value you can obtain. If it is impossible to make Xiao J happy, output .
6
3 0
3 1
3 2
3 3
3 4
3 5
5
3
3
1
1
-1
10
5 5
10 0
999 155
135 92
1000232 234255
10293845 1239485
123948 1239454
12394 2131094
1000000000 98765432
1000000000 999999999
3
40
52377
1161
27487764480
5864061665280
-1
-1
19215358392218419
4969489234738635
Hint
Sample Explanation #1
For the first group of samples, is , and the constructed tree is the same as shown in the statement.
If we choose to prune all nodes with depth greater than , then we prune , a total of nodes, and the beauty value is .
If we choose to prune all nodes with depth greater than , then we prune , a total of nodes, and the beauty value is .
If we choose to prune all nodes with depth greater than , then we do not prune any nodes, and the beauty value is .
Therefore, when , the answer is ; when , the answer is ; when , the answer is ; otherwise, there is no solution and you should output .
Constraints
For all testdata, it holds that , , .
This problem uses bundled tests: test points with the same constraints are bundled into one .
The additional constraints for each test point are shown in the table below.
| Test Point | Special Constraint | |||
|---|---|---|---|---|
| None | ||||
| None | ||||
| All are the same | ||||
| None |
Translated by ChatGPT 5