201213: [AtCoder]ARC121 D - 1 or 2

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $700$ points

Problem Statement

Snuke has a blackboard and $N$ candies. The tastiness of the $i$-th candy is $a_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 $X-Y$, where $X$ and $Y$ are the largest and smallest values written on the blackboard, respectively. Find the minimum possible value of $X-Y$.

Constraints

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

Input

Input is given from Standard Input in the following format:

$N$
$a_{1}$ $a_{2}$ $\cdots$ $a_N$

Output

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


Sample Input 1

3
1 2 4

Sample Output 1

1
  • One optimal sequence of operations is to eat the candies with the tastinesses of $1$ and $2$ in the first operation, and then eat the candies with the tastiness of $4$ in the second operation.

Sample Input 2

2
-100 -50

Sample Output 2

0
  • It is optimal to eat both candies with the tastiness of $-100$ and $-50$ in the first operation.

Sample Input 3

20
-18 31 -16 12 -44 -5 24 17 -37 -31 46 -24 -2 11 32 16 0 -39 35 38

Sample Output 3

13

Input

题意翻译

### 题目描述 你有 $n$ 个糖果,第 $i$ 个糖果的美味值为 $a_i$。 你需要吃糖,每次你可以选择吃 $1$ 个或 $2$ 个糖,并将你这一次吃的糖的总和写在黑板上。 你需要求出吃完所有糖果的所有可能的情况中,黑板上数字最大值和最小值之差最小是多少。 ### 输入格式 如原文。 ### 输出格式 如原文。 ### 数据范围与提示 $1\leq n\leq 5\times 10^3,-10^9\leq a_i\leq 10^9$。 --- 样例一: 第一次吃第一和第二个,第二次吃第三个,黑板上的数为 $\{3,4\}$,答案为 $1$。 样例二: 第一次全部吃完,黑板上的数为 $\{150\}$,答案为 $0$。 Translate by Zek3L.

加入题单

算法标签: