This page is still under construction.

Parts of this page are still being built. What you see may change.

Merchant

Time limit1sMemory limit128 MB

Summary
Choose a subset of markets to visit in nondecreasing opening-day order along a river, starting and ending at home, to maximize profits minus asymmetric upstream/downstream fuel costs.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Binary search, Prefix sum
Solved
No attempts yet

Problem

A merchant found planning an optimal trip over land far too hard, so he decided to travel along a straight river, selling goods as he goes. He owns a very fast boat that can move instantly from any point on the river to any other point, but the boat burns a lot of fuel. Moving upstream (toward where the river begins) costs UU dollars per meter, and moving downstream (with the current) costs DD dollars per meter.

There are NN markets the merchant wants to visit, and each market is open for exactly one day. For each market kk you are given, counted from the day he bought the boat, the day TkT_k on which it is open, its distance LkL_k (in meters) from the source of the river, and the profit MkM_k (in dollars) he gains by visiting it. The merchant's home is at distance SS from the source of the river. He starts at home, visits any markets he chooses, and returns home to finish the trip.

You must decide which markets to visit and in what order so that his total profit is maximized. (He may visit no market at all.) His total profit equals the sum of the profits of the markets he visits minus the fuel cost of moving up and down the river.

If he visits two markets, he must visit them in the order of the days on which they open. For example, if market AA opens before market BB, he cannot visit market BB first and then market AA. However, if two markets open on the same day, he may visit them in either order. There is no limit on how many markets he can visit in one day. He cannot visit the same market twice to double its profit, but he may pass through an already-visited market without gaining its profit again.

Given the boat's per-meter fuel costs, the merchant's home position, and each market's day, position, and profit, write a program that computes the maximum profit he can obtain after finishing the trip.

Input

The first line contains the integers NN, UU, DD, and SS, separated by single spaces.

Each of the next NN lines describes one market, in no particular order. The kk-th of these lines contains three integers TkT_k, LkL_k, and MkM_k, separated by single spaces: the day the market is open, the market's position, and the profit gained by visiting it.

All market positions are distinct, and no market is located at the merchant's home. That is, no two markets share a position and Lk≠SL_k \ne S.

  • 1≤N≤500,0001 \le N \le 500{,}000 (number of markets)
  • 1≤D≤U≤101 \le D \le U \le 10 (cost per meter is UU going upstream and DD going downstream)
  • 1≤S≤500,0011 \le S \le 500{,}001 (position of the merchant's home)
  • 1≤Tk≤500,0001 \le T_k \le 500{,}000 (day market kk is open)
  • 1≤Lk≤500,0011 \le L_k \le 500{,}001 (position of market kk)
  • 1≤Mk≤4,0001 \le M_k \le 4{,}000 (profit from visiting market kk)

Output

Print a single integer on one line: the maximum profit the merchant can obtain after finishing the trip.

Note

Consider the example. The home is at position 100100, and the optimal schedule is to visit the market at position 8080 (open on day 22) and the market at position 7575 (open on day 1010). The order of visits and the running profit are as follows.

  • Move 2020 meters upstream to position 8080, costing 5×20=1005 \times 20 = 100 dollars. (running profit −100-100)
  • Visit the market at position 8080 and gain 100100 dollars. (running profit 00)
  • Move 55 more meters upstream to position 7575, costing 5×5=255 \times 5 = 25 dollars. (running profit −25-25)
  • Visit the market at position 7575 and gain 150150 dollars. (running profit 125125)
  • Move 2525 meters downstream back home to position 100100, costing 3×25=753 \times 25 = 75 dollars. (final profit 5050)

Therefore the maximum profit is 5050 dollars.

Examples3

  1. Example 1

    Input
    4 5 3 100
    2 80 100
    20 125 130
    10 75 150
    5 120 110
    
    Expected output
    50
    
  2. Example 2

    Input
    1 1 1 5
    1 10 100
    
    Expected output
    90
    
  3. Example 3

    Input
    1 10 10 1
    3 500 5
    
    Expected output
    0