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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

 

write a java solution for this ......

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Fundamentals of Computer System
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education