Problems
There is a folding stick made up of
You are to find the minimum wrapping length under the condition that the stick is folded in the following way: First, place the segments of the stick along a horizontal line. Then, fold the stick clockwise from left to right. During folding, the segment attached to the left side of each hinge rotates 180 degrees clockwise or not at all.
For example, the figure below shows a four-segment stick with a sum of segment lengths of
In this example, the stick cannot be folded at both hinges ① and ②. This is because if the stick is folded at hinge ① and then at hinge ②, the segment with length 3 passing over the hinge ② will be broken. If it is folded only at hinge ②, the wrapping length is 5. If it is folded at hinges ① and ③ in order, the wrapping length is 4 as shown in the figure below.
Given a sequence of segments lengths of a folding stick, write a program to output the minimum wrapping length of the stick.
Input
Your program is to read from standard input. The input starts with a line containing an integer,
Output
Your program is to write to standard output. Print exactly one line. The line should contain the positive integer representing the minimum wrapping length.
Example #1
4
3 2 2 3
4
Example #2
5
1 1 1 1 1
1
Example #3
7
1 3 2 3 4 2 2
6