Bessie rests at grass stops along a trail and must never fall behind Farmer John; maximize total tastiness of eaten grass.
Medium5GreedySortingPrefix sumMathInterviewNo attempts yetTime limit2sMemory limit512 MBFarmer John and his personal trainer Bessie are hiking up Mount Vancowver. For this problem the mountain is one straight trail of length L meters (1≤L≤106). Farmer John hikes at a constant rate of rF seconds per meter (1≤rF≤106). He is working on his stamina, so he takes no rest along the way.
Bessie is allowed to rest, and at a rest stop she can eat tasty grass. Of course she cannot stop just anywhere. There are N rest stops along the trail (1≤N≤105). The i-th stop is xi meters from the start of the trail (0<xi<L), and the grass there has tastiness ci (1≤ci≤106). If Bessie rests at stop i for t seconds, she gains ci⋅t tastiness units.
While she is not resting, Bessie travels at a constant rate of rB seconds per meter (1≤rB≤106). Bessie is young and fit, so rB is strictly less than rF.
The two of them leave the start of the trail at the same moment. Bessie wants to eat as much tasty grass as she can, but she worries about Farmer John. She thinks that if she is behind him on the trail at any moment of the hike, he might lose all motivation to continue.
Find the maximum total tastiness Bessie can obtain while making sure that Farmer John completes the hike.
The first line contains four integers L, N, rF, and rB. The next N lines describe the rest stops. For each i between 1 and N, the i+1-st line contains two integers xi and ci, the position of the i-th rest stop and the tastiness of the grass there.
It is guaranteed that rF>rB and that 0<x1<⋯<xN<L. Note that rF and rB are given in seconds per meter.
Print a single integer, the maximum total tastiness Bessie can obtain.
In the first example it is best for Bessie to rest 7 seconds at the stop at x=7, which gives 14 tastiness units, and then rest 1 more second at the stop at x=8, which gives 1 more unit. The total is 15.