#abc353g. G - Merchant Takahashi
G - Merchant Takahashi
Score: points
问题陈述
AtCoder王国有个城市:城市、、、。要从城市移动到城市,你必须支付日元的通行费。
商人Takahashi正在考虑参加个即将到来的市场之一或更多。
第个市场由一对整数描述,市场在城市举行,如果他参加,他将赚取日元。
对于所有,第个市场在第个市场开始之前结束。他移动所需的时间可以忽略不计。
他最初有日元,并且最初在城市。确定他通过最优选择参加哪些市场以及如何移动,可以获得的最大利润。
形式上,如果他最大化他在个市场之后拥有的金额,那么他最终的金额为。找出。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
The Kingdom of AtCoder has towns: towns , , , . To move from town to town , you must pay a toll of yen.
Takahashi, a merchant, is considering participating in zero or more of upcoming markets.
The -th market is described by the pair of integers , where the market is held in town and he will earn yen if he participates.
For all , the -th market ends before the -th market begins. The time it takes for him to move is negligible.
He starts with yen and is initially in town . Determine the maximum profit he can make by optimally choosing which markets to participate in and how to move.
Formally, let be his final amount of money if he maximizes the amount of money he has after the markets. Find .
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
6 3
4
5 30
2 10
4 25
2 15
Sample Output 1
49
For example, Takahashi can increase his money by yen by acting as follows:
- Move to town . His money becomes yen.
- Participate in the first market. His money becomes yen.
- Move to town . His money becomes yen.
- Participate in the third market. His money becomes yen.
- Move to town . His money becomes yen.
- Participate in the fourth market. His money becomes yen.
It is impossible to increase his money to yen or more, so print 49
.
Sample Input 2
6 1000000000
4
5 30
2 10
4 25
2 15
Sample Output 2
0
The toll fee is so high that it is optimal for him not to move from town .
Sample Input 3
50 10
15
37 261
28 404
49 582
19 573
18 633
3 332
31 213
30 377
50 783
17 798
4 561
41 871
15 525
16 444
26 453
Sample Output 3
5000
Sample Input 4
50 1000000000
15
30 60541209756
48 49238708511
1 73787345006
24 47221018887
9 20218773368
34 40025202486
14 28286410866
24 82115648680
37 62913240066
14 92020110916
24 20965327730
32 67598565422
39 79828753874
40 52778306283
40 67894622518
Sample Output 4
606214471001
Note that the output value may exceed the range of a -bit integer.