F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Lazy Running

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2240    Accepted Submission(s): 909


Problem Description
In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule, you must keep your speed, and your running distance should not be less than $K$ meters.

There are $4$ checkpoints in the campus, indexed as $p_1,p_2,p_3$ and $p_4$. Every time you pass a checkpoint, you should swipe your card, then the distance between this checkpoint and the last checkpoint you passed will be added to your total distance.

The system regards these $4$ checkpoints as a circle. When you are at checkpoint $p_i$, you can just run to $p_{i-1}$ or $p_{i+1}$($p_1$ is also next to $p_4$). You can run more distance between two adjacent checkpoints, but only the distance saved at the system will be counted.





Checkpoint $p_2$ is the nearest to the dormitory, Little Q always starts and ends running at this checkpoint. Please write a program to help Little Q find the shortest path whose total distance is not less than $K$.
 

Input
The first line of the input contains an integer $T(1\leq T\leq15)$, denoting the number of test cases.

In each test case, there are $5$ integers $K,d_{1,2},d_{2,3},d_{3,4},d_{4,1}(1\leq K\leq 10^{18},1\leq d\leq 30000)$, denoting the required distance and the distance between every two adjacent checkpoints.
 

Output
For each test case, print a single line containing an integer, denoting the minimum distance.
 

Sample Input
1 2000 600 650 535 380
 

Sample Output
2165
 

Hint

The best path is 2-1-4-3-2.
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-04-26 13:23:14, Gzip enabled