Mizuyokan

Given a bar divided by N-1 score lines into segments of given lengths, cut along some lines so the longest and shortest resulting pieces differ as little as possible.

Medium7Dynamic programmingBinary searchPrefix sumGreedyInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Mizuyokan is a Japanese sweet made by pouring a paste of red beans into a mould and setting it with agar. JOI has one bar of mizuyokan shaped like a long rectangular block. He plans to eat it as today's snack.

The bar carries N1N-1 score lines that run across it. The whole bar has length L1+L2++LNL_1 + L_2 + \dots + L_N, and the ii-th score line (1iN1)(1 \le i \le N-1) sits at distance L1+L2++LiL_1 + L_2 + \dots + L_i from the left end.

The bar is too large to eat whole, so JOI picks at least one of the score lines and cuts the bar along every picked line, splitting it into several pieces. Pieces of uneven size look bad, so he wants the difference between the longest piece and the shortest piece to be as small as possible.

Find the smallest possible difference between the length of the longest piece and the length of the shortest piece.

Input

The input is given from standard input in the following format.

N
L_1
L_2
...
L_N

The first line has the number of blocks NN. Each of the next NN lines has LiL_i on the ii-th of them.

Output

Print the smallest possible difference between the longest piece and the shortest piece on one line.

Constraints

  • 2N502 \le N \le 50
  • 1Li1000 (1iN)1 \le L_i \le 1000 \ (1 \le i \le N)
  • Every input value is an integer.