#FT0000091. Thương nhân Takahashi (Merchant Takahashi)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thương nhân Takahashi (Merchant Takahashi)
Merchant Takahashi
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
The Kingdom of AtCoder has towns numbered from through along a line. Moving from town to town costs the merchant Takahashi yen.
There are upcoming markets, numbered from to in chronological order. The -th market is held in town , and attending it earns Takahashi yen. Market ends before market starts; travel time can be ignored.
Takahashi initially stays in town and owns a tremendously large amount of money, yen. He may skip any markets, including all of them, or attend selected markets in chronological order. Each move incurs a toll, and attending a market earns exactly its associated reward. He does not need to return to town after the last market.
Let be the difference between the maximum money he can own after all markets and the initial yen. Find . The initial amount only guarantees that he can afford the tolls; you do not need to represent this huge number in your program. By skipping every market and never moving, he can always achieve a non-negative profit.
Input
- The first line contains two integers : the number of towns and the toll per unit of distance between town indices.
- The second line contains the integer , the number of markets.
- Each of the next lines describes one market. Line contains integers : the town where market is held and the reward for attending it. Markets are listed in chronological order.
Output
Print one integer : the maximum profit relative to the initial money, which may be if attending no market is optimal.
Subtasks
- Subtask 1 (20 points): ; ; ; and for every .
- Subtask 2 (30 points): ; ; ; and for every .
- Subtask 3 (50 points): ; ; ; and for every .
Examples
Example 1
Input
6 3
4
5 30
2 10
4 25
2 15
Output
49
Explanation
An optimal plan is: move from town to town , pay , then attend market for , yielding profit . Skip market . Move from to , pay , and receive at market , yielding . Finally move from to , pay , and earn at market , yielding . No chronological selection of markets can produce a larger net profit.
Example 2
Input
6 1000000000
4
5 30
2 10
4 25
2 15
Output
0
Explanation
Moving from town to even town costs yen, while the sum of all four rewards is only . Every market is outside town . Thus visiting any market costs more than all rewards combined. Staying at town and skipping all markets yields the optimal profit 0.
Example 3
Input
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
Output
5000
Explanation
Attending only market in town would give , a negative intermediate state that may still be useful later. After market , the best profit ending in town is . Processing the markets chronologically, the best profits after markets are . Market in town increases the best profit achievable upon reaching that town by its reward , giving 5000.
Example 4
Input
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
Output
606214471001
Explanation
For market , travelling from town to town costs , so the profit after collecting is . After market at town , the best ending profit is . The maximum profits after markets , , and are , , and . Both final markets are in town : after market the profit is ; market adds without additional travel, reaching 606214471001. The answer exceeds 32-bit integer range.