#ABC129B. Balance

Balance

题目描述

We have NN weights indexed 11 to NN. The mass of the weight indexed ii is WiW_i.

We will divide these weights into two groups: the weights with indices not greater than TT, and those with indices greater than TT, for some integer 1T<N1 \leq T \lt N. Let S1S_1 be the sum of the masses of the weights in the former group, and S2S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S1S_1 and S2S_2.

我们有 NN 个砝码,索引号为 11NN 。索引为 ii 的砝码的质量为 WiW_i

我们将把这些权重分为两组:索引不大于 TT 的权重和索引大于 TT 的权重,其整数为 1T<N1 \leq T \lt N 。设 S1S_1 为前一组砝码的质量总和, S2S_2 为后一组砝码的质量总和。

考虑所有可能的分割,求出 S1S_1S2S_2 的最小绝对差。

输入格式

输入内容按以下格式标准输入:

NN
W1W_1 W2W_2 ...... WN1W_{N-1} WNW_N

输出格式

打印 S1S_1S2S_2 的最小绝对差值。

样例 #1

样例输入 #1

3
1 2 3

样例输出 #1

0

样例 #2

样例输入 #2

4
1 3 1 1

样例输出 #2

2

样例 #3

样例输入 #3

8
27 23 76 2 3 5 62 52

样例输出 #3

2

说明

数据规模与约定

  • 2N1002 \leq N \leq 100
  • 1Wi1001 \leq W_i \leq 100
  • 所有输入值均为整数。

样例 11 解释

如果 T=2T = 2S1=1+2=3S_1 = 1 + 2 = 3S2=3S_2 = 3 ,绝对差值为 00

样例 22 解释

如果 T=2T = 2S1=1+3=4S_1 = 1 + 3 = 4S2=1+1=2S_2 = 1 + 1 = 2 ,绝对差为 22 。我们不可能有更小的绝对差值。