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 testcases C.
Next C lines: The number of people in the group N, followed by N sorted integers A_{i}. A_{i} is the crossing time of the ith person.
0 ≤ C ≤ 100; 1 ≤ N ≤ 1000; 0 ≤ A_{i} ≤ 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 :
20160602 14:10:51
Nice Question :) Last edit: 20160603 01:02:30 

r0bo_dart:
20150619 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:
20131228 06:12:07
@Thomas: The main 2 purposes for that account are:


Thomas Dybdahl Ahle:
20131228 03:04:51
@fitcat what is the point of such a user, other than screwing the spoj stats? 

fitcat:
20131227 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:
20131227 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:
20131227 05:23:26
@Thomas: Please tell me why you disqualified my solution? 

Apoorv Jindal:
20131224 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:
20131223 13:12:22
A very good problem. It's always very informative see a solution actually being counterintuitive.

Added by:  Thomas Dybdahl Ahle 
Date:  20131221 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ASM64 