Two Sawmills
Time limit1sMemory limit128 MB
Place two extra sawmills along a road so that every tree's downhill haul to the first mill at or below it is minimized.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Divide and conquer, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
old trees stand along a road that runs from the top of a hill down to its foot. All of them will be cut down, and to avoid wasting wood every felled tree must be carried to a sawmill.
Wood can be moved in one direction only: downhill. There is already a sawmill at the lower end of the road. You may build two more sawmills at points along the road, and you must choose their locations so that the total transportation cost is as small as possible. Each felled tree travels downhill to the first sawmill at or below its own position. Transportation costs one cent per kilogram of wood per meter.
Given the number of trees together with their weights and positions on the standard input, write a program that computes the minimum possible total transportation cost and prints it to the standard output.
Input
The first line contains the number of trees (). The trees are numbered from the top of the hill downwards. Each of the next lines contains two integers separated by a single space. Line contains , the weight in kilograms of tree (), and , the distance in meters between tree and tree (). The last of these distances, , is the distance from tree to the sawmill at the lower end of the road. It is guaranteed that the total cost of carrying every tree to the sawmill at the end of the road is less than 2,000,000,000 cents.
Output
Print a single integer: the minimum total transportation cost.
Hint
The two extra sawmills may be placed at tree positions. Every tree is carried to the nearest sawmill at or below its own position. The figure below shows an optimal placement of the sawmills for the first test case; trees are drawn as circles labeled with their weights, and sawmills are marked in black. The resulting minimum cost is
