日本レジストリサービス(JPRS)プログラミングコンテスト2025#2(AtCoder Beginner Contest 415)
G
Get Many Cola
AtCoder
⌀ 2180
原题
翻译中…

Problem Statement

翻译中…

The setting of this problem is similar to Problem D. It differs from Problem D in the target of maximization and some constraints.

翻译中…

There is a mysterious cola shop. This shop does not sell cola directly, but provides an exchange service between empty cola bottles and new bottled cola.

翻译中…

Initially, Takahashi has $N$ bottled colas, and from now on he can choose and take one of the following actions any number of times (possibly zero):

翻译中…
  • Drink $1$ bottled cola that he has. The number of bottled colas he has decreases by $1$, and the number of empty bottles increases by $1$. (If he has no bottled cola before the action, he cannot take this action.)
  • Choose an integer $i$ between $1$ and $M$, inclusive. Give $A_i$ empty bottles to the cola shop, and receive $B_i$ bottled colas in return. (He cannot choose $i$ such that the number of empty bottles he has before the action is less than $A_i$. If there is no $i$ that can be chosen, he cannot take this action.)
翻译中…

Takahashi loves cola. When he acts optimally, what is the maximum number of colas he can drink?

翻译中…

He initially has no empty bottles.

翻译中…

Constraints

翻译中…
  • $1\leq N \leq 10^{15}$
  • $1\leq M \leq 2\times 10^5$
  • $1\leq B_i < A_i \leq 300$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

翻译中…

Output

翻译中…

Output the answer as an integer.

翻译中…

样例

Input #1
5 3
5 4
5 1
4 2
Output #1
11
Consider the following actions: - Initially, Takahashi has $5$ bottled colas. - Repeat drinking $1$ bottled cola $5$ times. Takahashi has $5$ empty bottles. - Make an exchange with $i=1$, giving $5$ empty bottles in exchange for $4$ bottled colas. Takahashi has $4$ bottled colas. - Repeat drinking $1$ bottled cola $4$ times. Takahashi has $4$ empty bottles. - Make an exchange with $i=3$, giving $4$ empty bottles in exchange for $2$ bottled colas. Takahashi has $2$ bottled colas. - Repeat drinking $1$ bottled cola $2$ times. Takahashi has $2$ empty bottles. At this point, Takahashi has drunk a total of $5+4+2=11$ colas. No matter how he acts, he cannot drink $12$ or more colas, so the answer is $11$.
Input #2
3 3
5 1
4 2
5 1
Output #2
3
He can only drink the bottled colas he originally had.
Input #3
925532735634776 8
91 40
273 265
286 153
155 126
92 83
291 228
216 90
234 9
Output #3
31583804603529464

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。