All Tracks Algorithms Sorting Bubble Sort Problem

Kingdom of Dreams
Tag(s):

Medium, Puzzle, Sorting

Problem
Editorial
Analytics

There are N people in city,who wants to visit Kingdom of Dreams. The road of reaching of Kingdom is not so safe.So, they go to kingdom only in a security vehicle which can accomodate atmost 2 people.(There is only one security vehicle available in the town as it is quite costly and unique).People started to hire this vehicle to reach safely by driving it by themselves.Every part of journey from town to kingdom or from kingdom to town has some cost associated with it which is given by an array A[] elements. Array A[] has n elements,where A(i) represents the cost ith person has to pay if they travel alone in the vehicle. If two people travel in vehicle,the cost of travel will be the maximum of cost of two people.Calculate the minimum total cost so that all N people can reach Kingdom safely.

Input Format-

The first line contains, T, denoting the number of test cases. Each test case contains 2 lines each. The first line has an integer N, denoting the number of persons. Next line contains N space separated distinct integers denoting the cost of ith person.

Output Format-

For each test case, print the minimum cost required so that all people can reach kingdom.

Constraints-

1 ≤ T ≤ 10

1 ≤ N ≤ 100000

1 ≤ A[i] ≤ 1000000

SAMPLE INPUT
4 
4
300 400 600 700
2
1321 2450
3
500 123 873
4
600 800 150 700
SAMPLE OUTPUT
2200
2450
1496
2400
Explanation

In the 1st testcase,300 and 400 go together (which costs 400) and 300 comes back (total cost 700 now). Now 600 and 700 go(total cost 1400) and 400 comes back(total 1800) and now 300 and 400 go(total cost 2200)!

In the 2nd testcase,both can reach the kingdom in 2450 as max(1321,2450)=2450.

In the 3rd testcase,second and third will go first.So cost will be max(123,873)=873. Now second person will come back and take the first along with him. So coming back cost is 123. Then taking first one along costs max(123,500)=500.

Therefore,total cost=500+123+873=1496.

Time Limit: 0.5 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

National Institute of Technology Jalandhar

Challenge Name

Sprint Challenge 2

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications

?