CF BUDDY
← Problems·

1950F · 0, 1, 2, Tree!

1700 · bitmasks, brute force, greedy

Problem: Find the minimum height of a rooted tree^{\dagger} with a+b+ca+b+c vertices that satisfies the following conditions:

  • aa vertices have exactly 22 children,
  • bb vertices have exactly 11 child, and
  • cc vertices have exactly 00 children.

The tree above is rooted at the top vertex, and each vertex is labeled with the number of children it has. Here a=2a=2, b=1b=1, c=3c=3, and the height is 22.

^{\dagger} A rooted tree is a connected graph without cycles, with a special vertex called the root. In a rooted tree, among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child.

The distance between two vertices in a tree is the number of edges in the shortest path between them. The height of a rooted tree is the maximum distance from a vertex to the root.

Input Format: The first line contains an integer tt (1t1041 \leq t \leq 10^4) — the number of test cases.

The only line of each test case contains three integers aa, bb, and cc (0a,b,c1050 \leq a, b, c \leq 10^5; 1a+b+c1 \leq a + b + c).

The sum of a+b+ca + b + c over all test cases does not exceed 31053 \cdot 10^5.

Output Format: For each test case, if no such tree exists, output 1-1. Otherwise, output one integer — the minimum height of a tree satisfying the conditions in the statement.

Note: The first test case is pictured in the statement. It can be proven that you can't get a height smaller than 22.

In the second test case, you can form a tree with a single vertex and no edges. It has height 00, which is clearly optimal.

In the third test case, you can form a tree with two vertices joined by a single edge. It has height 11, which is clearly optimal.

Sample Cases

Case 1

Input

10
2 1 3
0 0 1
0 1 1
1 0 2
1 1 3
3 1 4
8 17 9
24 36 48
1 0 0
0 3 1

Output

2
0
1
1
-1
3
6
-1
-1
3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback