**Pebbles and Beads solution codeforces** – There are two currencies: pebbles and beads. Initially you have 𝑎a pebbles, 𝑏b beads.

## [Solution] Pebbles and Beads solution codeforces

There are 𝑛n days, each day you can exchange one currency for another at some exchange rate.

On day 𝑖i, you can exchange −𝑝𝑖≤𝑥≤𝑝𝑖−pi≤x≤pi pebbles for −𝑞𝑖≤𝑦≤𝑞𝑖−qi≤y≤qi beads or vice versa. It’s allowed not to perform an exchange at all. Meanwhile, if you perform an exchange, the proportion 𝑥⋅𝑞𝑖=−𝑦⋅𝑝𝑖x⋅qi=−y⋅pi must be fulfilled. Fractional exchanges are allowed.

You can perform no more than one such exchange in one day. The numbers of pebbles and beads you have must always remain non-negative.

Please solve the following 𝑛n problems independently: for each day 𝑖i, output the maximum number of pebbles that you can have at the end of day 𝑖i if you perform exchanges optimally.

## [Solution] Pebbles and Beads solution codeforces

The first line of the input contains a single integer 𝑡t (1≤𝑡≤1031≤t≤103) — the number of test cases. The description of test cases follows.

The first line of each test case contains three integers 𝑛n, 𝑎a and 𝑏b (1≤𝑛≤3000001≤n≤300000, 0≤𝑎,𝑏≤1090≤a,b≤109) — the number of days and the initial number of pebbles and beads respectively.

The second line of each test case contains 𝑛n integers: 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn (1≤𝑝𝑖≤1091≤pi≤109).

The third line of each test case contains 𝑛n integers: 𝑞1,𝑞2,…,𝑞𝑛q1,q2,…,qn (1≤𝑞𝑖≤1091≤qi≤109).

It is guaranteed that the sum of 𝑛n over all test cases doesn’t exceed 300000300000.

## [Solution] Pebbles and Beads solution codeforces

Output 𝑛n numbers — the maximum number of pebbles at the end of each day.

Your answer is considered correct if its absolute or relative error does not exceed 10−610−6.

Formally, let your answer be 𝑎a, and the jury’s answer be 𝑏b. Your answer is accepted if and only if |𝑎−𝑏|max(1,|𝑏|)≤10−6|a−b|max(1,|b|)≤10−6.

## [Solution] Pebbles and Beads solution codeforces

6 8 4 6 9.000000 10.33

In the image below you can see the solutions for the first two test cases. In each line there is an optimal sequence of actions for each day.

## [Solution] Pebbles and Beads solution codeforces

In the first test case, the optimal strategy for the first day is to do no action at all, as we can only decrease the number of pebbles. The optimal strategy for the second day is at first to exchange 11 pebble for 22 beads, which is a correct exchange, since 1⋅4=2⋅21⋅4=2⋅2, and then to exchange 22 beads for 33 pebbles.