Choosing flowers
Contest: Codeforces Round 657 (Div. 2)
Difficulty rating: 2000
Tags: binary search, brute force, data structures, dfs and similar, dp, greedy, sortings, two pointers

Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly $$$n$$$ flowers.

Vladimir went to a flower shop, and he was amazed to see that there are $$$m$$$ types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the $$$i$$$-th type happiness of his wife increases by $$$a_i$$$ and after receiving each consecutive flower of this type her happiness increases by $$$b_i$$$. That is, if among the chosen flowers there are $$$x_i > 0$$$ flowers of type $$$i$$$, his wife gets $$$a_i + (x_i - 1) \cdot b_i$$$ additional happiness (and if there are no flowers of type $$$i$$$, she gets nothing for this particular type).

Please help Vladimir to choose exactly $$$n$$$ flowers to maximize the total happiness of his wife.

Input format:
The first line contains the only integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$), the number of test cases. It is followed by $$$t$$$ descriptions of the test cases.

Each test case description starts with two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^9$$$, $$$1 \le m \le 100\,000$$$), the number of flowers Vladimir needs to choose and the number of types of available flowers.

The following $$$m$$$ lines describe the types of flowers: each line contains integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \le a_i, b_i \le 10^9$$$) for $$$i$$$-th available type of flowers.

The test cases are separated by a blank line. It is guaranteed that the sum of values $$$m$$$ among all test cases does not exceed $$$100\,000$$$.

Output format:
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly $$$n$$$ flowers optimally.

Constraints:
Time limit: 1.0 s
Memory limit: 512.0 MB

Note:
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals $$$5 + (1 + 2 \cdot 4) = 14$$$.

In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals $$$(5 + 1 \cdot 2) + (4 + 1 \cdot 2) + 3 = 16$$$.

Example input:
2
4 3
5 0
1 4
2 2

5 3
5 2
4 2
3 1

Example output:
14
16

Editorial:
Let's look at sorts, for which there is more than one flower in the bouquet. We can prove, that there can be only one such sort. Let's assume that there are sorts $$$x$$$ and $$$y$$$ with $$$b_x \ge b_y$$$, such that for both sorts there are at least 2 flowers. Let's exclude flower of sort $$$y$$$ from bouquet and add flower of sort $$$x$$$. The total happiness will change by $$$b_x - b_y$$$, so it will not decrease. Repeating this, we can leave bouquet with only one flower of sort $$$y$$$ without decreasing the total happiness. So, there always exists an answer where there is only one sort with multiple flowers.
Using this fact, we can solve the problem the following way. Let's iterate over each sort of flowers and for every $$$x$$$ suppose that there are multiple flowers of sort $$$x$$$. It is obvious, that if some sort $$$y$$$ has $$$a_y > b_x$$$, then we should take one flower of such sort, and if $$$a_y \le b_x$$$, then we would better take another flower of sort $$$x$$$. So, we need to know sum of $$$a_i$$$ for all flowers that have $$$a_i > b_x$$$. To do it in $$$O(\log m)$$$, we can sort all flowers by $$$a_y$$$ in decreasing order, calculate prefix sums on that array, and using binary search find maximum $$$k$$$, such that $$$a_k > b_x$$$ and get prefix sum of $$$a_i$$$ for first $$$k$$$ sorts. All the rest $$$n - k$$$ flowers will be of the sort $$$x$$$.
Also we should take into account the case when for each sort only one flower of that sort is taken. In that case we should take $$$n$$$ sorts with largest $$$a_i$$$.
That way we can get the answer in $$$O(m \log m)$$$ time.