cswork / OAOpen practice workspace

JPMorgan ChaseOA #23

Get Minimum Operations

Estimated MediumJudge-ready

Topics (AI-labelled)Bits & MathGreedy

Start from 0; each step either adds 1 or multiplies by 2. Find the minimum number of operations to reach each target value. The first part of the original text is missing; the set of operations is restored from the original description and the ADD_1 and MULTIPLY_2 examples.

Input

The first line contains n, followed by n values of k; full bounds from the original text: 1≤n≤10000, 0≤k≤10000000000000000.

Output

Output n lines with the minimum number of operations, in input order.

Example 1

Input:

2
5 3

Output:

4
3

Example 2

Input:

3
0 1 8

Output:

0
1
4

Example 3

Input:

3
7 16 31

Output:

5
5
9

Example notes

The outputs of the three public examples are, in order: 4 / 3; 0 / 1 / 4; 5 / 5 / 9. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…