A13636 | Choosing flowers
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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$ .
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$ .
输出格式
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly $n$ flowers optimally.
输入输出样例
输入 #1
2 4 3 5 0 1 4 2 2 5 3 5 2 4 2 3 1
输出 #1
14 16
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted