Bouquet (Hard Version)
Contest: Codeforces Round 961 (Div. 2)
Difficulty rating: 1700
Tags: binary search, data structures, greedy, math, sortings, two pointers

This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers.

A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $$$n$$$ different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with $$$k$$$ petals costs $$$k$$$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $$$m$$$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?

Input format:
Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases. This is followed by descriptions of the test cases.

The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18}$$$) — the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $$$n$$$ different integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the number of petals of the $$$i$$$-th flower type in the store (for different indexes $$$i \neq j$$$, it must be $$$a_i \neq a_j$$$). The third line of each test case contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 10^9$$$), where $$$c_i$$$ is the quantity of the $$$i$$$-th flower type in the store.

The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot {10}^5$$$.

Output format:
For each test case, print one integer — the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above.

Constraints:
Time limit: 1.5 s
Memory limit: 256.0 MB

Note:
In the first test case, some valid bouquets are $$$(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$$$. The maximum over all valid bouquets not greater than $$$10$$$ is $$$7$$$ for $$$(2, 2, 3)$$$. In the second test case, you can assemble a valid bouquet with $$$(206, 206, 207, 207, 207)$$$ with a sum of $$$1033$$$, which is the maximum number of petals the girl can buy. In the third test case, you can assemble a valid bouquet with $$$(5, 5, 5, 4)$$$ with a sum of $$$19$$$. It can be seen that no valid bouquet can have $$$20$$$ petals.

Example input:
7
3 10
1 2 3
2 2 1
3 1033
206 207 1000
3 4 1
6 20
4 2 7 5 6 1
1 2 1 3 1 7
8 100000
239 30 610 122 24 40 8 2
12 13123 112 1456 124 100 123 10982
6 13
2 4 11 1 3 5
2 2 1 2 2 1
8 10330
206 210 200 201 198 199 222 1000
9 10 11 12 13 14 15 16
2 10000000000
11 12
87312315 753297050

Example output:
7
1033
19
99990
13
10000
9999999999

Editorial:
We already have a list of $$$c_x$$$. We can use hashmap to be able to check for any $$$c_x$$$ by $$$x$$$.
We again will try to assemble the bouquet only with flowers with $$$x, x + 1$$$ petals. We set $$$k1 = min(c_{x}, \left\lfloor\frac{m}{x}\right\rfloor)$$$. Then we have $$$coins = m - k1 * x$$$. Let's set $$$k2 = min(c_{x + 1}, \left\lfloor\frac{coins}{x + 1}\right\rfloor)$$$. Then we have $$$coins = m - (k1 * x + k2 * (x + 1))$$$. Let's substitute flower with $$$x$$$ petals with flower with $$$x + 1$$$ petals as many times as we can. This can be done $$$r = min(k1, c_{x + 1} - k2, coins)$$$ times, as each operation will require us 1 coin, 1 flower in the bouquet with $$$x$$$ petals and one 1 flower with $$$x + 1$$$ petals not in the bouquet. In total we can get $$$(k1 - r) * x + (k2 + r) * (x + 1)$$$ petals.
This assembling is optimal. Here is why. Suppose that we have $$$0 \le b1 \le c_{x}$$$ flowers with $$$x$$$ petals and $$$0 \le b2 \le c_{x + 1}$$$ flowers with $$$x + 1$$$ petals and greater total value of $$$b1 * x + b2 * (x + 1)$$$. We already know that $$$b1 \le k1$$$ by choosing of $$$k1$$$. If $$$k1 - r < b1$$$, then we can "undo" our operation $$$r + b1 - k1$$$ times, sum is still not greater than $$$m$$$, and we know that now there can't be more than $$$k2 + r + b1 - k1$$$ flowers with $$$x + 1$$$ petals, as otherwise we didn't chose optimal $$$k2$$$. If $$$k2 + r < b2$$$, then $$$r \ne c_{x + 1} - k2$$$, if $$$r = k1$$$ then it is just the case when we have only flowers with $$$x + 1$$$ petals which will be considered in case $$$x + 1, x + 2$$$, if $$$r = coins$$$ then $$$m = (k1 - r) * x + (k2 + r) * (x + 1)$$$ and we already found the maximum. So $$$b2 \le k2 + r$$$ and $$$b1 \le k1 - r$$$ and $$$b1 * x + b2 * (x + 1)$$$ is not better than optimal.
Total time complexity is $$$O(n)$$$.