PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000091. Thương nhân Takahashi (Merchant Takahashi)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thương nhân Takahashi (Merchant Takahashi)

Merchant Takahashi

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

The Kingdom of AtCoder has nn towns numbered from 11 through nn along a line. Moving from town uu to town vv costs the merchant Takahashi C⋅∣u−v∣C\cdot|u-v| yen.

There are mm upcoming markets, numbered from 11 to mm in chronological order. The ii-th market is held in town TiT_i, and attending it earns Takahashi PiP_i yen. Market ii ends before market i+1i+1 starts; travel time can be ignored.

Takahashi initially stays in town 11 and owns a tremendously large amount of money, 101010010^{10^{100}} 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 11 after the last market.

Let XX be the difference between the maximum money he can own after all markets and the initial 101010010^{10^{100}} yen. Find XX. 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 n,Cn,C: the number of towns and the toll per unit of distance between town indices.
  • The second line contains the integer mm, the number of markets.
  • Each of the next mm lines describes one market. Line ii contains integers Ti,PiT_i,P_i: the town where market ii is held and the reward for attending it. Markets are listed in chronological order.

Output

Print one integer XX: the maximum profit relative to the initial money, which may be 00 if attending no market is optimal.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 1≤m≤101\le m\le 10; 1≤C≤1091\le C\le 10^9; 1≤Ti≤n1\le T_i\le n and 1≤Pi≤10131\le P_i\le 10^{13} for every 1≤i≤m1\le i\le m.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; 1≤m≤30001\le m\le 3000; 1≤C≤1091\le C\le 10^9; 1≤Ti≤n1\le T_i\le n and 1≤Pi≤10131\le P_i\le 10^{13} for every 1≤i≤m1\le i\le m.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤m≤2000001\le m\le 200000; 1≤C≤1091\le C\le 10^9; 1≤Ti≤n1\le T_i\le n and 1≤Pi≤10131\le P_i\le 10^{13} for every 1≤i≤m1\le i\le m.

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 11 to town 55, pay 3⋅4=123\cdot4=12, then attend market 11 for 3030, yielding profit 1818. Skip market 22. Move from 55 to 44, pay 33, and receive 2525 at market 33, yielding 18−3+25=4018-3+25=40. Finally move from 44 to 22, pay 66, and earn 1515 at market 44, yielding 40−6+15=4940-6+15=49. 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 11 to even town 22 costs 10910^9 yen, while the sum of all four rewards is only 30+10+25+15=8030+10+25+15=80. Every market is outside town 11. Thus visiting any market costs more than all rewards combined. Staying at town 11 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 11 in town 3737 would give 261−10⋅36=−99261-10\cdot36=-99, a negative intermediate state that may still be useful later. After market 22, the best profit ending in town 2828 is 215215. Processing the markets chronologically, the best profits after markets 5,10,12,155,10,12,15 are 1483,3016,3948,50001483,3016,3948,5000. Market 1515 in town 2626 increases the best profit achievable upon reaching that town by its reward 453453, 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 11, travelling from town 11 to town 3030 costs 29⋅10929\cdot10^9, so the profit after collecting 6054120975660541209756 is 3154120975631541209756. After market 22 at town 4848, the best ending profit is 6277991826762779918267. The maximum profits after markets 33, 88, and 1212 are 8956726327389567263273, 224215544192224215544192, and 413712788326413712788326. Both final markets are in town 4040: after market 1414 the profit is 538319848483538319848483; market 1515 adds 6789462251867894622518 without additional travel, reaching 606214471001. The answer exceeds 32-bit integer range.