#abc353g. G - Merchant Takahashi

    ID: 4162 传统题 1000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>来源atcoder动态规划数据结构高级数据结构线段树

G - Merchant Takahashi

Score: 550550 points

问题陈述

AtCoder王国有NN个城市:城市1122\ldotsNN。要从城市ii移动到城市jj,你必须支付C×ijC \times |i-j|日元的通行费。

商人Takahashi正在考虑参加MM个即将到来的市场之一或更多。

ii个市场(1iM)(1 \leq i \leq M)由一对整数(Ti,Pi)(T_i, P_i)描述,市场在城市TiT_i举行,如果他参加,他将赚取PiP_i日元。

对于所有1i<M1 \leq i < M,第ii个市场在第(i+1)(i+1)个市场开始之前结束。他移动所需的时间可以忽略不计。

他最初有101010010^{10^{100}}日元,并且最初在城市11。确定他通过最优选择参加哪些市场以及如何移动,可以获得的最大利润。

形式上,如果他最大化他在MM个市场之后拥有的金额,那么他最终的金额为1010100+X10^{10^{100}} + X。找出XX

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

The Kingdom of AtCoder has NN towns: towns 11, 22, \ldots, NN. To move from town ii to town jj, you must pay a toll of C×ijC \times |i-j| yen.

Takahashi, a merchant, is considering participating in zero or more of MM upcoming markets.

The ii-th market (1iM)(1 \leq i \leq M) is described by the pair of integers (Ti,Pi)(T_i, P_i), where the market is held in town TiT_i and he will earn PiP_i yen if he participates.

For all 1i<M1 \leq i < M, the ii-th market ends before the (i+1)(i+1)-th market begins. The time it takes for him to move is negligible.

He starts with 101010010^{10^{100}} yen and is initially in town 11. Determine the maximum profit he can make by optimally choosing which markets to participate in and how to move.

Formally, let 1010100+X10^{10^{100}} + X be his final amount of money if he maximizes the amount of money he has after the MM markets. Find XX.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1C1091 \leq C \leq 10^9
  • 1M2×1051 \leq M \leq 2 \times 10^5
  • 1TiN1 \leq T_i \leq N (1iM)(1 \leq i \leq M)
  • 1Pi10131 \leq P_i \leq 10^{13} (1iM)(1 \leq i \leq M)
  • All input values are integers.

Input

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

NN CC

MM

T1T_1 P1P_1

T2T_2 P2P_2

\vdots

TMT_M PMP_M

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 4949 yen by acting as follows:

  • Move to town 55. His money becomes 10101001210^{10^{100}} - 12 yen.
  • Participate in the first market. His money becomes 1010100+1810^{10^{100}} + 18 yen.
  • Move to town 44. His money becomes 1010100+1510^{10^{100}} + 15 yen.
  • Participate in the third market. His money becomes 1010100+4010^{10^{100}} + 40 yen.
  • Move to town 22. His money becomes 1010100+3410^{10^{100}} + 34 yen.
  • Participate in the fourth market. His money becomes 1010100+4910^{10^{100}} + 49 yen.

It is impossible to increase his money to 1010100+5010^{10^{100}} + 50 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 11.

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 3232-bit integer.