#P5902. [IOI 2009] Salesman

[IOI 2009] Salesman

Description

The traveling salesman has found that the best overland travel plan is a hard computational problem to solve, so he moved his business to the linear world of the Danube River. He has a very fast boat that can take him from anywhere along the river to anywhere else in a short time, but unfortunately, this boat consumes a lot of fuel. The cost for the salesman to move upstream (toward the river source) by every meter is UU dollars, and the cost to move downstream (away from the river source) by every meter is DD dollars.

There are NN fairs along the river that the salesman wants to attend. Each fair lasts only one day. For each fair XX, the salesman knows its day TXT_X (day 00 is the day he bought the boat), the fair location LXL_X, and the profit MXM_X he can earn at this fair. The location is the distance from the river source, in meters. He must start and end his trip at his home, located at position SS.

Help the salesman choose which fairs to attend (if any) and in what order, so that he can maximize his profit when the trip ends. The salesman’s total profit is the sum of dollars earned from attended fairs minus the dollars spent traveling upstream and downstream.

Note that if fair AA is held earlier than fair BB, then the salesman can only visit them in this order (i.e., he cannot visit fair BB first and then fair AA). However, if two fairs are held on the same day, he may visit them in any order. There is no limit on how many fairs he can visit in one day, but he cannot earn profit from the same fair twice. He may pass through fairs he has already visited without earning anything.

Task: Write a program that, given the day, location, and profit of each fair, as well as the location of the salesman’s home and his movement costs, computes the maximum profit he can have at the end of his trip.

Input Format

The first line contains four integers N,U,D,SN, U, D, S, separated by single spaces, representing the number of fairs, the per-meter cost of moving upstream (UU) or downstream (DD), and the position of the salesman’s home.

The next NN lines describe the NN fairs. The kk-th line contains three integers, separated by single spaces, representing the fair day TkT_k, its location LkL_k, and the profit MkM_k the salesman can earn at this fair.

Output Format

Output one integer: the maximum profit the salesman can have at the end of the trip.

4 5 3 100
2 80 100
20 125 130
10 75 150
5 120 110

50

Hint

Sample Explanation

In one optimal plan, the salesman attends fairs 11 and 33 (at positions 8080 and 7575, respectively). The sequence of events and the corresponding profit are as follows:

  • The salesman leaves home and moves upstream by 2020 meters, costing 100100 dollars. Current profit: 100-100.
  • The salesman attends fair 11 and earns 100100 dollars. Current profit: 00.
  • The salesman moves upstream by 55 meters, costing 2525 dollars. Current profit: 25-25.
  • The salesman attends fair 33 and earns 150150 dollars. Current profit: 125125.
  • The salesman moves downstream by 2525 meters to return home, costing 7575 dollars. Final profit: 5050.

Constraints

  • For 60%60\% of the testdata, no two fairs are held on the same day.
  • For 40%40\% of the testdata, all input numbers are at most 50005000.
  • 15%15\% of the testdata satisfies both conditions above, and 85%85\% satisfies at least one of them.
  • For 100%100\% of the testdata, 1N,Tk5×1051 \le N, T_k \le 5\times 10^5, 1DU101 \le D \le U \le 10, 1S,Lk5×105+11 \le S, L_k \le 5 \times 10^5 +1, 1Mk40001 \le M_k \le 4000.

Translated by ChatGPT 5