Lazy Running
Time limit1sMemory limit256 MB
Starting and ending at checkpoint p2 on a 4-cycle, find the shortest closed walk whose swipe-recorded total distance is at least K, given the four edge lengths.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Math, Greedy
- Solved
- No attempts yet
Problem
At HD University, you have to be able to run around the campus 24 times in a row. Otherwise, you will fail the physical education exam and get expelled from the university. According to the rules, you must keep your speed, and your total running distance should be at least meters.
There are four checkpoints on the campus, labeled , , and . Every time you pass a checkpoint, you should swipe your card, and the distance between this checkpoint and the last checkpoint you passed is added to your total distance.
The system regards the four checkpoints as a circle: from checkpoint , you can only run to one of its neighbors, or ; and are also neighbors of each other. You can run along a straight or curved line between neighboring checkpoints, but it makes no difference for the system: only the distance between checkpoints is taken into account.
Checkpoint is the nearest to the dormitory, so Little Q always starts and ends running at this checkpoint. Write a program to help Little Q find the shortest path such that the total running distance taken into account by the system is at least meters.
Input
The first line of the input contains five integers , , , and denoting the required distance and the distances between every pair of neighboring checkpoints (, ).
Output
Print a single line containing a single integer: the length of the shortest path.