Train Travel
InterviewTime limit1sMemory limit256 MB
Count how many times each rail is crossed and pay the cheaper of single tickets or a discount card plus cheap fares for every rail.
- Level
Medium4 of 10
- Topics
- Prefix sum, Greedy
- Solved
- No attempts yet
Problem
There are cities on a line numbered to . Rail connects cities and bidirectionally.
For each rail you may pay ticket fare per ride, or buy a rail-specific IC card for once and pay per ride on that rail (). You start with no cards.
You visit cities in order, moving from to on day . Minimize total spending on card purchases plus rides.
Input
Line 1: , . Line 2: . Next lines: , , for rail .
Output
Print the minimum total cost.
Constraints
, , , , and .