404388: GYM101492 E Teamwork
Description
Teamwork is highly valued in the ACM ICPC. Since the beginning, the ICPC has distinguished itself from other programming contests in that teamwork is a key factor.
The University of Beijing, host of next year's World Finals, is planning recreational activities for the participants. They want to prepare games that show the importance of teamwork and enable new acquaintances among the contestants from all over the world. One of the staff members has been thinking about the following game.
In a court we have several N-person teams. The goal of each team is roughly as follows: starting from a corner, each team must take all of its members to the other corner of the court. The winning team is the one that finishes this in the shortest time.
Now we explain the game more formally. Consider the team that starts at corner A and must take all of its members to corner B. The following procedure is repeated:
- While there are team members at corner A,
- If there is only one team member, he is the only one that goes to corner B.
- Otherwise, two team members must tie one leg of each with a rope and go to B.
- Once arriving at B, they untie their legs, if applicable.
- If there are still team members at corner A, some team member at B must return to A with the rope.
The organization wants to form the teams so that no team has an advantage. They entrusted you with the following task. Given the time in seconds that the members of a team take to go from a corner to the other, find the minimum time it takes for the team to take all its members from corner A to corner B. When two team members cross the court with their legs tied, the time for the pair to cross is the maximum between the times of the two members.
InputThe first line of input contains an integer N, the number of team members. The next line contains N integers ti, the time in seconds that the i-th team member takes to go from one corner to the other.
- 1 ≤ N ≤ 105
- 1 ≤ ti ≤ 109
Print a single integer, the minimum time required for the whole team to reach corner B.
ExamplesInput3Output
30 40 50
120Input
4Output
10 20 50 100
170Note
In the first example, the process can be completed in 120 seconds as follows:
- The first and second members go from A to B in 40 seconds.
- The first member returns to corner A in 30 seconds.
- The first and third team members go from A to B in 50 seconds.
This is not the only way to complete the process in this total time, but no other way does it in strictly less than 120 seconds.