atcoder#ARC121D. [ARC121D] 1 or 2

[ARC121D] 1 or 2

Score : 700700 points

Problem Statement

Snuke has a blackboard and NN candies. The tastiness of the ii-th candy is aia_i.

He will repeat the operation below until he has no more candy.

  • Choose one or two of his candies and eat them (of course, they disappear). Then, write on the blackboard the total tastiness of the candies he has just chosen.

Snuke wants to minimize XYX-Y, where XX and YY are the largest and smallest values written on the blackboard, respectively. Find the minimum possible value of XYX-Y.

Constraints

  • All values in input are integers.
  • 1N50001 \leq N \leq 5000
  • 109ai109-10^{9} \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

NN

a1a_{1} a2a_{2} \cdots aNa_N

Output

Print the minimum possible value of XYX-Y, where XX and YY are the largest and smallest values written on the blackboard, respectively.

3
1 2 4
1
  • One optimal sequence of operations is to eat the candies with the tastinesses of 11 and 22 in the first operation, and then eat the candies with the tastiness of 44 in the second operation.
2
-100 -50
0
  • It is optimal to eat both candies with the tastiness of 100-100 and 50-50 in the first operation.
20
-18 31 -16 12 -44 -5 24 17 -37 -31 46 -24 -2 11 32 16 0 -39 35 38
13