B - Balance Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1 から N の番号がついた N 個の重りがあり、番号 i の重りの重さは W_i です。

ある整数 1 \leq T < N に対してこれらの重りを、番号が T 以下の重り と 番号が T より大きい重りの 2 グループに分けることを考え、それぞれのグループの重さの和を S_1, S_2 とします。

このような分け方全てを考えた時、S_1S_2 の差の絶対値の最小値を求めてください。

制約

  • 2 \leq N \leq 100
  • 1 \leq W_i \leq 100
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
W_1 W_2 ... W_{N-1} W_N

出力

S_1S_2 の差の絶対値の最小値を出力せよ。


入力例 1

3
1 2 3

出力例 1

0

T = 2 としたとき、S_1 = 1 + 2 = 3, S_2 = 3 となり、差の絶対値は 0 となります。


入力例 2

4
1 3 1 1

出力例 2

2

T = 2 としたとき、S_1 = 1 + 3 = 4, S_2 = 1 + 1 = 2 となり、差の絶対値は 2 です。これより差の絶対値を小さくすることは出来ません。


入力例 3

8
27 23 76 2 3 5 62 52

出力例 3

2

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_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 S_1 and S_2.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq W_i \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.


Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.


Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.


Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2