Silk Road
InterviewTime limit1sMemory limit256 MB
Choose N of M days in order for the eastward moves so the sum of distance times daily badness is smallest.
- Level
Medium4 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
Kazakhstan once had a trade route called the Silk Road.
The Silk Road has cities. From west to east they are city , city , ..., city . The distance between city and city () is .
JOI, a trader, starts at city and travels through the cities from west to east until reaching city . The trip must finish within days. Each day JOI picks one of two actions.
- Move: spend the day going to the city immediately to the east. From city (), JOI arrives at city .
- Wait: stay in the current city for the day.
Moving builds up fatigue. The weather on day () has badness , and moving from city to city on day adds fatigue. A day spent waiting adds no fatigue.
Find the smallest total fatigue with which JOI can reach city within days.
Input
The first line has two integers and (), separated by a space. The Silk Road has cities, and JOI must get from city to city within days.
Each of the next lines has one integer. The -th of these lines has (), the distance between city and city .
Each of the next lines has one integer. The -th of these lines has (), the weather badness on day .
Output
Print the smallest total fatigue with which JOI can reach city within days, on one line.
Hint
In the first example, this schedule reaches the smallest total fatigue.
- Day 1: wait.
- Day 2: move from city to city . The fatigue added is .
- Day 3: move from city to city . The fatigue added is .
- Day 4: wait.
- Day 5: move from city to city . The fatigue added is .
The total fatigue is , and no schedule does better.