WAYHOME - The Bridge to Home
Late in the night, a group of people are approaching home after an exciting adventure in the wilderness. However, as a last challenge on their long and tiresome fare, they have to cross a small bridge over a raging river.
The bridge is so narrow and slippery that the group decides only to let a maximum of two people walk on it at any given time. Also, the person or pair who is walking on the bridge, must carry the single light that the group possesses.
The group asks you to determine, given the amounts of time that each person takes to cross the bridge, the minimum total amount of time it'll take the entire group to cross. Notice that, when two people walk on the bridge at the same time, their crossing time is determined by the slower walker.
Input
First line: The number of test cases C.
Next C lines: The number of people in the group N, followed by N sorted integers Ai. Ai is the crossing time of the i-th person.
0 ≤ C ≤ 100; 1 ≤ N ≤ 1000; 0 ≤ Ai ≤ 1000.
Output
For each testcase, write the minimum total amount of time it'll take the group to pass.
Example
Input: 4 1 1 2 1 2 3 1 1 1 4 1 2 4 8 Output: 1 2 3 15
In the third test case, the people can cross the bridge in the following way:
- The first two people walk over the bridge, taking 1 time unit.
- One person walks back over the bridge, with the light, taking 1 time unit.
- The two people, now on the left, cross the bridge, taking 1 time unit.
hide comments
rainy jain :
2016-06-02 14:10:51
Nice Question :) Last edit: 2016-06-03 01:02:30 |
|
r0bo_dart:
2015-06-19 07:44:52
AC at second go. Costed me a WA for not considering the intuitive case as well. I thought that it was ruled out of being suboptimal, but instead it is optimal for some cases depending upon the values of the numbers in seq.
|
|
fitcat:
2013-12-28 06:12:07
@Thomas: The main 2 purposes for that account are:
|
|
Thomas Dybdahl Ahle:
2013-12-28 03:04:51
@fitcat what is the point of such a user, other than screwing the spoj stats? |
|
fitcat:
2013-12-27 11:24:33
@Thomas: (test) is my other account (it is for testing and other purposes). That's why I disqualified the AC solution of (test).
|
|
Thomas Dybdahl Ahle:
2013-12-27 10:19:38
@fitcat: Your solution appears to be a copy of one earlier submitted by @(test). @Swarna, your solution seems to be a copy of @john dykes'.
|
|
fitcat:
2013-12-27 05:23:26
@Thomas: Please tell me why you disqualified my solution? |
|
Apoorv Jindal:
2013-12-24 08:13:13
@:D You could actually prove the optimality of the solution, the way its done in greedy approach, where the solution always stays ahead or stays at level with any other alternate path. |
|
:D:
2013-12-23 13:12:22
A very good problem. It's always very informative see a solution actually being counter-intuitive.
|
Added by: | Thomas Dybdahl Ahle |
Date: | 2013-12-21 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |