Easy Scheduling solution codeforces

Easy Scheduling solution codeforces

Eonathan Eostar decided to learn the magic of multiprocessor systems. He has a full binary tree of tasks with height hh. In the beginning, there is only one ready task in the tree — the task in the root. At each moment of time, pp processes choose at most pp ready tasks and perform them. After that, tasks whose parents were performed become ready for the next moment of time. Once the task becomes ready, it stays ready until it is performed.

Easy Scheduling solution codeforces

You shall calculate the smallest number of time moments the system needs to perform all the tasks.

Input Easy Scheduling solution codeforces

The first line of the input contains the number of tests tt (1t51051≤t≤5⋅105). Each of the next tt lines contains the description of a test. A test is described by two integers hh (1h501≤h≤50) and pp (1p1041≤p≤104) — the height of the full binary tree and the number of processes. It is guaranteed that all the tests are different.

Output Easy Scheduling solution codeforces

For each test output one integer on a separate line — the smallest number of time moments the system needs to perform all the tasks.

Example
input

Copy Easy Scheduling solution codeforces

3
3 1
3 2
10 6
output

Copy Easy Scheduling solution codeforces

7
4
173

Note Easy Scheduling solution codeforces

Let us consider the second test from the sample input. There is a full binary tree of height 33 and there are two processes. At the first moment of time, there is only one ready task, 11, and p1p1 performs it. At the second moment of time, there are two ready tasks, 22 and 33, and the processes perform them. At the third moment of time, there are four ready tasks, 445566, and 77, and p1p1 performs 66 and p2p2 performs 55. At the fourth moment of time, there are two ready tasks, 44 and 77, and the processes perform them. Thus, the system spends 44 moments of time to perform all the tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*