Driving Lanes
Time limit2sMemory limit512 MB
Given n straightaways separated by n-1 curves whose cost grows by c per lane, find the minimum total distance from lane 1 back to lane 1, where lane changes cost k+r and must fit within a straightaway.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Implementation, Math
- Solved
- No attempts yet
Problem
While driving around a curve on the highway, Sam realizes that using the inside lane means traveling a shorter distance. Sam wonders what the minimum distance needed to reach the destination is.
A multilane highway consists of a sequence of straightaways connected by curves. When going around a curve, the distance traveled depends on which lane you are in. Each curve has a curvature and a stretch . Specifically, if Sam is in lane , they travel meters while going around this curve.
Whenever Sam is on a straightaway, they may change from one lane into an adjacent lane. When changing to an adjacent lane, Sam moves forward meters, but travels a total of meters. Each lane change must be completed before the car reaches the end of the current straightaway. Sam may change lanes multiple times on the same straightaway. For safety reasons, changing lanes is not possible on curves.

Sam starts in lane and wishes to end in lane . What is the minimum distance they must travel?
Input
The first line of input contains two integers (), which is the number of straightaways, and (), which is the number of lanes on the highway. The lanes are numbered .
The second line of input contains two integers () and (), which are the lane changing parameters.
The next lines describe the straightaways in order. Each of these lines contains a single integer (), which is the length of this straightaway.
The next lines describe the curves in order. Each of these lines contains two integers (), which is the stretch of this curve, and (), which is the curvature of this curve. It is guaranteed that .
The th curve connects the th and th straightaway.
Output
Display the minimum distance Sam must travel.