Moving the Light Stone
InterviewTime limit1sMemory limit256 MB
Pick drag or carry for each of N segments, paying a switch cost K whenever the choice changes, to minimize the total.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy, Array, Implementation
- Solved
- No attempts yet
Problem
At the center of the kingdom of Polymath stands the Light Stone, which supplies light to the people. The Light Stone can supply light to places up to a distance away. Because the Light Stone is growing weaker, the people want to move it so that every house can be supplied with light.
The destination of the Light Stone is already decided. The problem is that moving the Light Stone costs a lot. To minimize the cost, they must decide for each part of the way whether to drag the Light Stone or carry it.
Divide the route over which the Light Stone is moved into segments. In segment , dragging the Light Stone costs , and carrying it costs . Also, each time the way of moving the Light Stone changes, an extra cost is incurred. (There is no extra cost at the very beginning or the very end.)
For example, let , , , . Dragging the stone over the entire route costs . On the other hand, dragging it in the first segment and carrying it in the second and third segments costs , so the stone can be moved for less.
Input
The first line gives the number of segments and the cost of changing the way of moving.
The second line gives the costs of dragging the Light Stone.
The third line gives the costs of carrying the Light Stone.
Output
Print the minimum cost of moving the Light Stone.