#P9689. Pruning the Binary Tree for Maximum Beauty
Pruning the Binary Tree for Maximum Beauty
Pruning the Binary Tree for Maximum Beauty
In this problem, you are given a binary tree constructed using a parameter n. The binary tree is built by the following recursive procedure:
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); }</p>int main(){ build(1, n, 1); return 0; }
It is known that the resulting tree is a full binary tree with exactly 2*n - 1 nodes, and the nodes are labeled consecutively from 1 to 2*n - 1 (in the order they are assigned in a heap‐ordered fashion). The depth of a node is defined as the number of nodes on the path from that node to the root (including both). Thus, the tree's depth D is the maximum depth among all nodes. (For example, when n = 3 the tree has 5 nodes and depth 3.)
You are asked to prune the tree by choosing a depth limit k (with 1 ≤ k ≤ D). In the pruning process, you keep only all the nodes with depth ≤ k and remove the rest. Let the beauty value of the pruned tree be defined as
$$\text{Beauty} = \left\lfloor \frac{\text{(sum of the labels of the kept nodes)}}{k} \right\rfloor. $$However, there is an extra requirement: the number of nodes removed (i.e. pruned away) must be at least m in order to make Little J happy. Your task is to choose a valid depth k such that the number of pruned nodes is at least m, and the beauty value is maximized. If it is impossible to remove at least m nodes by any pruning, output -1
.
Note: The tree is built in a way that if n = 1, then the tree consists of a single node with label 1 and depth 1.
inputFormat
The input consists of two integers n and m separated by a space. Here, n is the tree construction parameter and m is the minimum number of nodes that must be pruned.
Constraints:
- n ≥ 1
- m ≥ 0
outputFormat
Output a single integer, which is the maximum possible beauty value after pruning while satisfying the condition. If no valid pruning exists, output -1
.
sample
3 2
3