IOI '09 - Plovdiv, Bulgaria
The traveling salesman has decided that optimally scheduling his trips
on land is an intractable computational problem, so he is moving his
business to the linear world of the Danube River. He has a very fast
boat that can get him from anywhere to anywhere along the river in no
time, but unfortunately the boat has terrible fuel consumption. It costs
the salesman
There are
Help the traveling salesman choose which trade fairs to attend (if any) and in what order, so that he may maximize his profit at the end of his travels. The traveling salesman's total profit is defined as the sum of the dollars he gained at the fairs he attended, minus the total sum of dollars he spent traveling up and down the river.
Keep in mind that if trade fair
Write a program that, given the date, location and profitability of all fairs, as well as the location of the traveling salesman's home and his costs of traveling, determines the maximum possible profit he can make by the end of his journey.
Input Specification
Your program must read from standard input the following data:
- The first line contains the integers
, , , and , in this order, separated by single spaces. - The next
lines describe the fairs in no particular order. The of these lines describes the fair and contains three integers separated by single spaces: the day of the fair , its location , and its profitability for the salesman .
NOTE: All locations given in the input will be different. That is to say, no two fairs will happen at the same location and no fair will happen at the salesman's home.
Output Specification
Your program must write to standard output a single line containing a single integer: the maximum profit the salesman can possibly make by the end of his journey.
Sample Input
4 5 3 100
2 80 100
20 125 130
10 75 150
5 120 110
Sample Output
50
Explanation
An optimal schedule would visit fairs
- The salesman travels
meters upstream at a cost of dollars. Profit so far: - He attends fair number
and earns . Profit so far: - He travels
meters upstream at a cost of . Profit so far: - He attends fair number
where he earns . Profit so far: - He travels
meters downstream to return home at a cost of . Profit at the end:
Note on grading
For a number of tests, worth a total of
For a number of tests, worth a total of
The tests where both of the above conditions hold are worth
The tests where at least one of the two conditions holds are worth
Comments