4개 체크포인트가 이루는 고리에서 p2에서 출발해 p2로 돌아오며 총 이동 거리가 K 이상이 되는 가장 짧은 경로를 구한다.
보통7그래프최단 경로수학아직 제출이 없습니다시간 제한1초메모리 제한256 MBIn 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 K meters.
There are four checkpoints in the campus, labeled as p_1, p_2, p_3 and p_4. Every time you pass a checkpoint, you should swipe your card, and the distance between this checkpoint and the last checkpoint you passed will be added to your total distance.
The system regards the four checkpoints as a circle: from checkpoint p_i, you can only run to one of its neighbors, p_i−1 or p_i+1; p_1 and p_4 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 p_2 is the nearest to the dormitory, so Little Q always starts and ends running at this checkpoint. Please 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 K meters.
The first line of the input contains five integers K, d_1,2, d_2,3, d_3,4 and d_4,1 denoting the required distance and the distances between every pair of neighboring checkpoints (1≤K≤1018, 1≤d≤3⋅104).
Print a single line containing a single integer: the length of the shortest path.