QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 16 MB Total points: 100

#11878. The Bridge

Statistics

In the middle of the night a group of tourists want to cross an old, ruined bridge. They have just one torch. The light of the torch enables two tourists at the most to cross the bridge simultaneously. The tourists cannot cross the bridge without the torch nor in groups larger than two, unless they want to fall into the river. Each tourist requires a certain amount of time to cross the bridge. Two tourists crossing the bridge together need as much time as the slower of them. What is the shortest time, which all of the tourist can cross the bridge in?

Example

Suppose that the group numbers 4 people. The first one of them needs 6 minutes to cross the bridge, the second 7 minutes, the third 10 minutes, the fourth 15 minutes. The following image shows how they can cross the bridge in 44 minutes. However, they can do it faster. How?

problem_11878_1.gif

A hypothetical method of crossing the bridge in 44 minutes. The numbers in circles denote time (in minutes) required by each tourist to cross the bridge.

Task

Write a programme which:

  • reads from the standard input a description of the group of tourists,
  • finds the shortest time required to cross the bridge,
  • writes the result to the standard output.

Input

In the first line of the standard input there is a single positive integer $n$ - the number of tourists, $1 ≤ n ≤ 100\,000$. In the following $n$ lines there is a non-decreasing sequence of integers not greater than $1\,000\,000\,000$, a single number in a line. The number in the $(i+1)$’st line ($1 ≤ i ≤ n$) represents the time needed by the $i$’th tourist to cross the bridge. The sum of these numbers does not exceed $1\,000\,000\,000$.

Output

Your programme should write to the standard output, in the first line, one integer - the shortest time required for all of the tourist to cross the bridge.

Example

Input

4
6
7
10
15

Output

42
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.