Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

L. Laundry Lines
time limit per test
12 s
memory limit per test
1024 megabytes
input
standard input
output
standard output

Image by Mac, from commons.wikimedia.org

As you often need to do, you are about to hang up your laundry. Having recently moved into a tiny hole of an apartment, you have only two laundry lines, both attached to the same flimsy drying rack, which tips over if one line carries much more weight than the other.

If you start by hanging three shirts on the same line, it might just tip over and you have to start all over again. So you must be careful about the order and manner in which you hang the laundry.

This means you pick up the laundry, one piece at a time, in an order of your choosing, and hang that piece on one of the two lines. Once a piece is hung, it stays where it is.

At any moment, the imbalance is the absolute difference between the total weights on the two lines. You want to hang all the laundry so that the largest imbalance occurring during the process is as small as possible.

Trial and error would be a very wasteful use of time, so now it is time to waste time on making an involved algorithm to take care of it instead.

For example, suppose you have four pieces of laundry, of weights $$$100$$$, $$$120$$$, $$$200$$$, and $$$300$$$ grams. Hanging the $$$300$$$ first gives an imbalance of $$$300$$$ straight away, but you can do better: hang the $$$120$$$ on the first line (imbalance $$$120$$$), then the $$$300$$$ on the second line (imbalance $$$180$$$), then the $$$200$$$ on the first line (imbalance $$$20$$$), and finally the $$$100$$$ on the second line (imbalance $$$80$$$). The largest imbalance during this process is $$$180$$$, which is optimal.

Input

The input consists of:

  • One line with an integer $$$n$$$ ($$$1 \leq n \leq 16$$$), the number of pieces of laundry.
  • One line with $$$n$$$ integers $$$w_1, \dots, w_n$$$ ($$$1 \leq w_i \leq 1\,000$$$), the weights of the pieces of laundry in grams.
Output

Output the smallest possible value of the largest imbalance that occurs while hanging up all the laundry.

Examples
Input
4
200 400 200 100
Output
200
Input
7
100 100 900 500 500 1000 400
Output
500