To Fish or Not to Fish
Time limit1sMemory limit512 MB
Choose fishing points to visit and bases to sell at along a river, paying fuel only while moving upstream, to maximize profit.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, Prefix sum, Dynamic programming
- Solved
- No attempts yet
Problem
The owners of a fishing vessel working the Kama River decided to optimize their business for the summer season.
They obtained a seasonal permit to fish at points along the river at distances kilometers from the mouth. At point number , at most tons of fish may be caught.
The fish caught can be sold at wholesale bases located along the river bank at distances kilometers from the mouth. The base at point number is willing to buy at most tons of fish this season at a price of rubles per ton.
Distances from the mouth to the fishing points and wholesale bases are measured along the river channel.
The vessel sets out to fish from the river mouth and must return there after the season ends. During the season, the vessel may travel freely up and down the river, stopping to fish or to sell fish. The vessel's carrying capacity is sufficient to transport any amount of caught fish. When moving away from the mouth, the vessel travels against the current and spends fuel worth rubles per kilometer. When moving toward the mouth, the vessel travels with the current and therefore spends no fuel.
At the end of the season, the profit from the catch equals the total value of the fish sold minus the total cost of the fuel spent.
Write a program that determines the maximum profit that can be obtained during the season.
Input
The first line of the input contains three integers , , and : the number of fishing points, the number of wholesale bases, and the fuel price (; ).
The next lines contain two integers each, and : the distance from the mouth and the maximum catch for each fishing point (; ).
The next lines contain three integers each, , , and : the distance from the mouth, the maximum amount of fish bought in tons, and the purchase price per ton for each wholesale base (; ).
Output
The output must contain a single integer: the maximum possible profit.
Hint
In the second example, the following actions are optimal. The vessel should reach the point at a distance of 6 kilometers from the mouth, spending 600 rubles on fuel, and catch 5 tons of fish there. After that, it should go 1 kilometer down the river to the base at a distance of 5 kilometers from the mouth and sell the caught fish at a price of 2000 rubles per ton. Then it should return to the river mouth. The total profit will be 9400 rubles.