Students Vasya and Petya are studying at the BSU (Byteland State University). At one of the breaks they decided to order a pizza. In this problem pizza is a circle of some radius. The pizza was delivered already cut into n pieces. The i-th piece is a sector of angle equal to a_{i}. Vasya and Petya want to divide all pieces of pizza into two continuous sectors in such way that the difference between angles of these sectors is minimal. Sector angle is sum of angles of all pieces in it. Pay attention, that one of sectors can be empty.


-----Input-----

The first line contains one integer n (1 ≤ n ≤ 360)  — the number of pieces into which the delivered pizza was cut.

The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 360)  — the angles of the sectors into which the pizza was cut. The sum of all a_{i} is 360.


-----Output-----

Print one integer  — the minimal difference between angles of sectors that will go to Vasya and Petya.


-----Examples-----
Input
4
90 90 90 90

Output
0

Input
3
100 100 160

Output
40

Input
1
360

Output
360

Input
4
170 30 150 10

Output
0



-----Note-----

In first sample Vasya can take 1 and 2 pieces, Petya can take 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0.

In third sample there is only one piece of pizza that can be taken by only one from Vasya and Petya. So the answer is |360 - 0| = 360.

In fourth sample Vasya can take 1 and 4 pieces, then Petya will take 2 and 3 pieces. So the answer is |(170 + 10) - (30 + 150)| = 0.

Picture explaning fourth sample:

[Image]

Both red and green sectors consist of two adjacent pieces of pizza. So Vasya can take green sector, then Petya will take red sector.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
a = list(map(int, input().split()))
mn = 360
for i in range(n):
    x = 0
    for j in range(i, n):
        x += a[j]
        mn = min(mn, abs(x - (360 - x)))
print(mn)

n = int(input().strip())

kosi = list(map(int, input().strip().split()))

mini = 400

for a in range(len(kosi)):
    for b in range(a, len(kosi)):
        first = sum(kosi[a:b])
        second = sum(kosi[:a]) + sum(kosi[b:])
        if abs(first - second) < mini:
            mini = abs(first - second)

print(mini)
        



import sys
sys.setrecursionlimit(100000000)
# def input(): return sys.stdin.readline()[:-1]
def iin(): return int(input())
def impin(): return list(map(int, input().split()))
def irrin(): return [int(x) for x in input().split()]
def imrin(n): return [int(input()) for _ in range(n)]


n = iin()
arr = irrin()
sa = sum(arr)
mn = 100000000
for i in range(n):
    for j in range(i, n+1):
        s = sum(arr[i:j])
        # print(s)
        k = sa-s
        mn = min(mn, abs(s-k))
print(mn)


n = int(input())

nums = list(map(int, input().split()))

answer = float('inf')

for l in range(n):
    for s in range(n):
        if(s + l - 1 >= n):continue
        current = sum(nums[s:s + l])
        answer = min(answer, abs(360 - 2 * current))


print(answer)



def main():
    n = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    res = 100 ** 10
    for l in range(n):
        for r in range(l, n):
            s1 = sum(a[l:r])
            res = min(res, abs(s1 - (s - s1)))

    print(res)




def __starting_point():
    main()
__starting_point()

from collections import deque

n = int(input())
a = deque(map(int, input().split()))
ps = [0] * n
mindif = 2e9
for i in range(n):
    for i in range(n):
        ps[i] = ps[i - 1] if i > 0 else 0
        ps[i] += a[i]
    for i in range(n):
        mindif = min(mindif, abs((ps[i - 1] if i > 0 else 0) - (ps[n - 1] - (ps[i - 1] if i > 0 else 0))))
    a.append(a.popleft())

print(mindif)

n = int(input())
a = [int(i) for i in input().split()]
pref = []
s = 0
for i in range(n):
    s += a[i]
    pref.append(s)
mins = 1000
for i in range(n):
    for j in range(i, n):
        s = abs((pref[j] - pref[i]) - (pref[i] + (pref[n-1] - pref[j])))
        if s < mins:
            mins = s
print(mins)

n = int(input())

best = 360

tab = list(map(int, input().split()))

pre_sum = [0]

for i in range(1, len(tab) + 1):
    pre_sum += [tab[i - 1] + pre_sum[-1]]

for i in range(n + 1):
    for j in range(i, n + 1):
        best = min([best, 2 * abs(180 - (pre_sum[j] - pre_sum[i - 1]))])

print(best);


n = int(input())
a = list(map(int, input().split()))
a += a
ans = 360
for i in range(n):
  cur = 0
  j = i
  while cur < 180:
    cur += a[j]
    j += 1
  ans = min(ans, 2*cur - 360)
print(ans)

n = int(input())
pieces = [int(x) for x in input().split()]
sm = 360
curs = 0
ans = []
for i in range(n):
    for elem in pieces:
        ans.append(abs(sm - 2 * curs))
        curs += elem
        if curs >= 360:
            curs = 0
    pieces.append(pieces.pop(0))
print(min(ans))

n = int(input())
slices = list(map(int, input().split()))
answer = 360
for i in range(n):
    for j in range(i + 1, n):
        cur_s = abs(sum(slices[:i]) + sum(slices[j:]) - sum(slices[i:j]))
        answer = min(cur_s, answer)
print(answer)


n = int(input())
a = [int(i) for i in input().split()]
a = a + a
pref_sum = [0] * (2 * n)
for i in range(2 * n):
    pref_sum[i] = pref_sum[i - 1] + a[i]
dif = int(1e9)
for i in range(n):
    for j in range(n):
        dif = min(dif, abs(pref_sum[n - 1] - 2 * (pref_sum[j + i] - pref_sum[j])))
print(dif)

n = int(input())
arr = [int(num) for num in input().strip().split()]
ans = 360
for i in range(n):
    l = 0
    r = 360
    for j in range(n):
        ans = min(ans, abs(l - r))
        l += arr[j]
        r -= arr[j]
    if ans == 0:
        break
    val = arr[0]
    arr.remove(arr[0])
    arr.append(val)
print(ans)

#from math import gcd, factorial as f
#list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))
x = sum(a)
a += a
an = 0
ann = 1000000
for j in range(n):
    an = 0
    for i in range(j, 2 * n):
        an += a[i]
        if an >= x // 2:
            if abs(x - 2 * an) < abs(x - 2 * ann):
                ann = an
            an = 0
print(abs(x - 2 * ann))


#python3
#utf-8

pieces_nr = int(input())
piece_idx___deg = [int(x) for x in input().split()]
ans = 360
for left_cl in range(pieces_nr):
    for right_op in range(left_cl + 1, pieces_nr + 1):
        curr_split = sum(piece_idx___deg[left_cl:right_op])
        rest = 360 - curr_split
        # print(curr_split, rest)
        curr_ans = rest - curr_split
        if curr_ans < 0:
            curr_ans *= -1
        ans = min(curr_ans, ans)
        # print(curr_ans)
print(ans)


n = int(input())
ar = [int(i) for i in input().split(' ')]

sum = 0
s_a = 1000
for i in range(n):
    k = 0
    sum = 0
    while sum < 180:
        sum += ar[i-k]
        k += 1
    if s_a > sum-(360-sum):
        s_a = sum-(360-sum)

print(s_a)

n = int(input())
angles = list(map(int, input().split()))
angles = angles+angles

sum_ = 0
from_ = 0
best = 360
for to in range(n):
    sum_ += angles[to]
    while sum_ >= 180:
        best = min(best, abs(360-2*sum_))
        sum_ -= angles[from_]
        from_ += 1
    best = min(best, abs(360-2*sum_))
print(best)


kolichestvoKuskov = int(input())
spisokUglov = list(map(int, input().split()))
raznica = 180
for a in range(0, kolichestvoKuskov):
    sum = 0
    for i in range(a, kolichestvoKuskov):
        sum += spisokUglov[i]
        if raznica > abs(180 - sum):
            raznica = abs(180 - sum)
print(raznica * 2)


n = int(input())
sectors = list(map(int, input().split()))

ans = 360
for _ in range(n):
    for i in range(n):
        temp = abs(sum(sectors[:i]) - sum(sectors[i:]))
        if temp < ans:
            ans = temp
    sectors[:] = sectors[-1:] + sectors[:-1]
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
ans = 1e9
for j in range(n):
    pref = [0]*(n+1)
    for i in range(1, n+1):
        pref[i] = pref[i-1] + a[i-1]
    for i in range(n):
        ans = min(ans, abs(pref[-1]-2*pref[i]))
    a = [a[-1]] + a[:-1]
print(ans)


import sys
data = sys.stdin.readlines()

n = int(data[0])
m = [int(x) for x in data[1].split()]

res = []

for k in range(n):
    for i in range(n):
        a = abs(sum(m[:i]) - sum(m[i:]))
        res.append(a)
    m.append(m[0])
    m = m[1:]
print(min(res))
        


n=int(input())
l=[]
l=input().split()
for i in range(n):
    l[i]=int(l[i])
l.extend(l)

otv=400
for i in range(n):
    s=0
    j=i
    while s<180:
        s+=l[j]
        j+=1

    if abs(s-180)*2<otv:
        otv=abs(s-180)*2
print(otv)

n = int(input())
a = list(map(int, input().split()))
sumspref = [a[0]]
for i in range(1, n):
    sumspref.append(sumspref[-1] + a[i])
sums = []
for i in range(n):
    for j in range(i, n):
        sums.append(abs(180 - (sumspref[j] - sumspref[i])))
print(min(sums) * 2)
    



n = int( input() )

a = list( map( int, input().split() ) )

a = a + a

#print( a )

mid = n//2

best = 99999999999999999999999

for i in range( n ):
    sub = a[i:i+n]
    #print( sub )
    d = sum(sub)
    best = min( best, d )
    s = 0

    #print( d, s )
    for j in range( n ):
        s += sub[j]
        d -= sub[j]

        diff = abs( s - d)
        best = min( best, diff )

print( best )


def solution(sectors):
    n = len(sectors)
    if n == 1:
        return sectors[0]
    else:
        min_diff = 360
        for i in range(n):
            j = i
            sum_sector = 0
            prev = sum_sector
            while(sum_sector <= 180):
                prev = sum_sector
                sum_sector += sectors[j]
                j += 1
                j %= n
            #print(prev, sum_sector)
            if abs(360 - 2 * sum_sector) < min_diff:
                min_diff = abs(360 - 2 * sum_sector)
            if abs(360 - 2 * prev) < min_diff:
                min_diff = abs(360 - 2 * prev)
        return min_diff


n = int(input())
sectors = []
sectors_str = input().split()
for s in sectors_str:
    sectors.append(int(s))

print(solution(sectors))