#P6023. 走路

走路

Description

Xiao W plans to work out over the next mm days. Since he cannot walk too much and get exhausted (how could that happen), over these mm days he can walk at most nn steps in total.
To encourage Xiao W to walk, the app provides kk incentive rules. Each rule is of the form: “If on day pp you finish walking qq steps, then on day pp every subsequent step will give you an additional 11 point.” Incentive rules can stack, meaning that for one step you may gain more than 11 point.
Now Xiao W wants to know: what is the maximum total number of points he can get?

Input Format

The first line contains three integers n,m,kn,m,k, with meanings as above.
The next kk lines each contain two integers p,qp,q, describing an incentive rule as above.

Output Format

Output one integer, the maximum points that can be obtained after mm days.

5 1 3
1 0
1 2
1 4

9

Hint

Explanation for the sample:
There is only one plan: walk 55 steps on the first day. The first and second steps each gain 11 point, the third and fourth steps each gain 22 points, and the fifth step gains 33 points, for a total of 99 points.

Constraints:
For 10%10\% of the testdata, n,m,k10n,m,k \le 10.
For 40%40\% of the testdata, n,m,k103n,m,k \le 10^3.
For 100%100\% of the testdata, 1n10121 \le n \le 10^{12}, 1m,k1051 \le m,k \le 10^5, 1pm1 \le p \le m, 0qn0 \le q \le n.

Translated by ChatGPT 5