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: EASY

Solutions:
n = int(input())
a = list(map(int, input().split()))
ans = 360
for i in range(n):
	for j in range(i, n):
		ans = min(ans, abs(180 - sum(a[i:j])) * 2)
print(ans)


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


import math
a = int(input())
b = list(map(int, input().split()))
e = 361
if b[0] == 360:
	print(360)
else:
	for c in range(a - 1):
		for d in range(a - 1):
			if math.fabs(180 - sum(b[c:-(a - 1 - d)])) * 2 < e:
				e = math.fabs(180 - sum(b[c:-(a - 1 - d)])) * 2
	print(int(e))


(z, zz, dgraphs) = (input, lambda : list(map(int, z().split())), {})
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
n = int(z())
ll = zz()
lx = [360]
for l in range(n):
	for r in range(l + 1, n + 1):
		ans = abs(2 * sum(ll[l:r]) - 360)
		lx.append(ans)
print(min(lx))


input()
angs = [int(x) for x in input().split()]
result = 99999
for i in range(len(angs)):
	curr = 0
	for j in range(i, i + len(angs)):
		curr += angs[j % len(angs)]
		result = abs(180 - curr) if abs(180 - curr) < result else result
print(2 * result)


n = int(input())
from itertools import permutations
a = list(map(int, input().split()))
b = 361
for i in range(n):
	for j in range(i, n):
		b = min(b, abs(sum(a[:]) - sum(a[i:j]) * 2))
print(b)


class CodeforcesTask895ASolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.sectors = []

	def read_input(self):
		self.n = int(input())
		self.sectors = [int(x) for x in input().split(' ')]

	def process_task(self):
		mn = 360
		for a in range(self.n):
			for b in range(a, self.n):
				mn = min(mn, abs(2 * sum(self.sectors[a:b]) - 360))
		self.result = str(mn)

	def get_result(self):
		return self.result
Solution = CodeforcesTask895ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


n = int(input())
e = input()
a = e.split(' ')
(sum2, sum1) = (0, 0)
k = 0
for t in range(n):
	i = k
	k += 1
	while sum2 < 180:
		sum2 += int(a[i % n])
		i += 1
	if sum2 < sum1 or sum1 == 0:
		sum1 = sum2
	sum2 = 0
print(sum1 - (360 - sum1))


a = int(input())
l1 = [int(j) for j in input().split()]
r = 180
l2 = []
for j in range(len(l1)):
	l2.append(l1[j:] + l1[:j])
o1 = []
for j in l2:
	o = []
	r1 = 0
	for i in j:
		r1 += i
		if r1 > 180:
			o.append(r1)
			o.append(r1 - i)
			break
	u = [abs(o[0] - 180), abs(o[1] - 180)]
	o1.append(2 * min(u))
print(min(o1))


n = int(input())
a = list(map(int, input().split()))
m = abs(2 * a[0] - 360)
for i in range(n):
	for j in range(i + 1, n + 1):
		s = 0
		if i == 0 and j == n:
			continue
		for k in range(i, j):
			s += a[k]
		m = min(m, abs(2 * s - 360))
print(m)


n = int(input())
a = [int(i) for i in input().split()]
best = 360
for i in range(n):
	for j in range(i + 1, n + 1):
		if abs(sum(a[i:j]) - (360 - sum(a[i:j]))) < best:
			best = abs(sum(a[i:j]) - (360 - sum(a[i:j])))
print(best)


nP = int(input())
secs = [int(k) for k in input().split()]
total = sum(secs)
result = total
for left in range(nP):
	curS = 0
	for right in range(left, nP):
		curS += secs[right]
		result = min(result, abs(total - curS - curS))
print(result)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	a = a + a
	ans = 360
	cnt = 0
	for i in range(2 * n):
		cnt += a[i]
		a[i] = cnt
	for i in range(n):
		for j in range(n):
			x = a[i + j + 1] - a[i]
			ans = min(ans, abs(360 - 2 * x))
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
a = list(map(int, input().split()))
ans = 360
for l in range(n):
	for r in range(l + 1, n + 1):
		ans = min(ans, abs(2 * sum(a[l:r]) - 360))
print(ans)


n = int(input())
l = list(map(int, input().split()))
ans = 180
for i in range(0, n):
	sum = 0
	for j in range(i, n):
		sum += l[j]
		if ans > abs(180 - sum):
			ans = abs(180 - sum)
print(ans * 2)


a = int(input())
b = list(map(int, input().split())) * 2
pizza = 360
for i in range(2 * a):
	t = 0
	for j in range(i, min(i + a, 2 * a)):
		t += b[j]
		pizza = min(pizza, abs(2 * t - 360))
print(pizza)


n = int(input())
A = list(map(int, input().split()))
if n == 1:
	print(A[0])
	exit()
t = float('inf')
gs = sum(A)
(fs, ss) = (A[0], abs(A[0] - gs))
for l in range(-n, n):
	for r in range(l, n):
		fs = sum(A[l:r])
		ss = gs - fs
		if abs(ss - fs) < t:
			t = abs(ss - fs)
print(t)


n = int(input())
sectors = list(map(int, input().split()))
sectors.extend(sectors)
min_diff = 360
for i in range(n):
	total = 0
	for j in range(i, i + n):
		total += sectors[j]
		min_diff = min(min_diff, abs(total - (360 - total)))
print(min_diff)


R = lambda : map(int, input().split())
n = int(input())
a = list(R())
ans = abs(2 * a[0] - 360)
for i in range(n):
	sum = a[i]
	if abs(2 * sum - 360) < ans:
		ans = sum
	for j in range(1, n):
		sum += a[(i + j) % n]
		temp = abs(2 * sum - 360)
		if temp < ans:
			ans = temp
print(ans)


import sys
sys.setrecursionlimit(100000000)

def iin():
	return int(input())

def impin():
	return 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])
		k = sa - s
		mn = min(mn, abs(s - k))
print(mn)


n = int(input())
ai = list(map(int, input().split()))
ai2 = [0] * n
summ = sum(ai)
ai2[0] = ai[0]
minim = 360
for i in range(1, n):
	ai2[i] = ai2[i - 1] + ai[i]
	minim = min(minim, abs(180 - ai2[i]) * 2)
for i in range(n - 1, 0, -1):
	for j in range(i):
		minim = min(minim, abs((180 - (ai2[i] - ai2[j])) * 2))
print(minim)


N = int(input())
pizzaList = list(map(int, input().split()))

def checkMinSum(minDiff, currDiff):
	if abs(currDiff) < minDiff:
		return abs(currDiff)
	return minDiff
sum0 = 0
minDiff = 180
currDiff = 180
for i in range(0, N):
	sum0 = 0
	for j in range(0, N):
		sum0 += pizzaList[(i + j) % N]
		currDiff = 180 - sum0
		minDiff = checkMinSum(minDiff, currDiff)
		if currDiff <= 0:
			break
print(minDiff * 2)


N = int(input())
raw = input()
numbers = [int(token) for token in raw.split(' ')]
total = sum(numbers)
smallest = 1000
for i in range(N):
	for j in range(i, N):
		sum1 = sum(numbers[i:j])
		sum2 = total - sum1
		smallest = min(smallest, abs(sum1 - sum2))
print(smallest)


n = int(input())
a = list(map(int, input().split()))
(j, s, m) = (0, 0, 360)
for ai in a:
	while s < 180:
		s += a[j]
		j = (j + 1) % n
	m = min(m, s)
	s -= ai
print(2 * m - 360)


n = int(input())
a = list(map(int, input().split()))
res = 360
for qq in range(n):
	sm = 0
	for i in range(n - 1):
		sm += a[i]
		q = 360 - sm
		res = min(res, abs(sm - q))
	a = [a[-1]] + a[:-1]
print(res)


n = int(input())
a = [int(i) for i in input().split()]
ans = 360
for i in range(-len(a), len(a)):
	s = 0
	for j in range(i, len(a)):
		s += a[j]
		if j - i + 1 > len(a):
			break
		elif j < 0:
			continue
		elif j >= 0 and ans > 2 * abs(180 - s):
			ans = 2 * abs(180 - s)
print(ans)


n = int(input())
lst = list(map(int, input().strip().split(' ')))
if n == 1:
	print(lst[0])
elif n == 2:
	print(abs(lst[1] - lst[0]))
else:
	s = 360
	m = 360
	lst = lst * 2
	for i in range(n):
		p = 0
		for j in range(i, i + n - 1):
			p += lst[j]
			if abs(s - 2 * p) < m:
				m = abs(s - 2 * p)
	print(m)


n = int(input())
l = [int(i) for i in input().split()]
o = []
if n == 1:
	print('360')
elif n == 2:
	print(abs(l[0] - l[1]))
else:
	for i in range(n):
		for j in range(i, n):
			k = sum(l[i:j + 1])
			o.append(abs(k - 180))
	print(2 * min(o))


n = int(input())
lst = [int(i) for i in input().split()]
print(min([abs(360 - 2 * sum(lst[j:j + i])) for i in range(n + 1) for j in range(n)]))


input()
a = list(map(int, input().split(' '))) * 2
min = 512
for i in range(len(a)):
	sum = 0
	for j in range(i, len(a)):
		sum = a[j] + sum
		if abs(sum - 180) < min:
			min = abs(sum - 180)
print(2 * min)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
N = int(input())
A = [int(x) for x in input().split()]
ans = float('inf')
total = sum(A or [0])
for i in range(N):
	for j in range(i, N):
		a = sum(A[i:j] or [0])
		b = total - a
		ans = min(ans, abs(a - b))
print(ans)


n = int(input())
s = [int(i) for i in input().split()]
ans = 10 ** 9 + 7
for i in range(n):
	a = 0
	ans = min(ans, abs(180 - a))
	for j in range(i, n):
		a += s[j]
		ans = min(ans, abs(180 - a))
print(ans * 2)


n = int(input())
mas = list(map(int, input().split()))
delta = 100000000000000000000
for a in range(n):
	for b in range(a + 1, n):
		s1 = sum(mas[a:b])
		s2 = sum(mas) - s1
		if abs(s1 - s2) < delta:
			delta = abs(s1 - s2)
if delta == 100000000000000000000:
	delta = sum(mas)
print(delta)


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

def pizza(i, summ, n):
	if i == n:
		return abs(2 * summ - 360)
	return min(abs(2 * summ - 360), pizza(i + 1, summ + a[i], n))
minn = float('INF')
for i in range(n):
	tmp = pizza(i, 0, n)
	if tmp < minn:
		minn = tmp
print(minn)


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)


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())
a = list(map(int, input().split()))
l = []
for i in range(n):
	for j in range(n):
		l.append(abs(180 - sum(a[i:j + 1])))
print(min(l) * 2)


n = int(input())
arr = list(map(int, input().split()))
sum = 0
l = 0
i = 0
ans = 360
while i < n:
	sum += arr[i]
	while sum >= 180:
		ans = min(ans, 2 * abs(sum - 180))
		sum -= arr[l]
		l += 1
	i += 1
	ans = min(ans, 2 * abs(sum - 180))
print(ans)


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


n = int(input())
p = list(map(int, input().split()))
r = [0] * n
mn = 360
for i in range(n):
	q = []
	q.extend(p[i:])
	if i > 0:
		q.extend(p[0:i])
	sm = 0
	for i in range(len(q)):
		sm += q[i]
		r[i] = sm
	for i in r:
		if abs(360 - 2 * i) < mn:
			mn = abs(360 - 2 * i)
print(mn)


n = int(input())
a = [int(i) for i in input().split()]
c = sum(a)
m = c
for i in range(n):
	for k in range(i, n + 1):
		m = min(m, abs(c - 2 * sum(a[i:k + 1])))
print(m)


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()))
l = 0
ans = 360
sum1 = 0
for k in a:
	sum1 = sum1 + k
	while sum1 >= 180:
		ans = min(ans, 2 * abs(180 - sum1))
		sum1 = sum1 - a[l]
		l = l + 1
	ans = min(ans, 2 * abs(180 - sum1))
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
ans = 180
for i in range(n):
	t = 0
	for j in range(i, n):
		t += a[j]
		c = abs(t - 180)
		ans = min(ans, c)
print(2 * ans)


n = int(input())
a = list(map(int, input().split()))
res = float('inf')
for i in range(len(a)):
	(l, r) = (0, 360)
	for i in a[i:] + a[:i]:
		res = min(res, abs(l - r))
		l += i
		r -= i
	res = min(res, abs(l - r))
print(res)


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(360)
	exit(0)
r = 360
for i in range(len(a)):
	for j in range(1, len(a) + 1):
		k = (i + j) % n
		if i == k:
			s1 = sum(a)
		elif i < k:
			s1 = sum(a[i:k])
		else:
			s1 = sum(a[k:i])
		ss = abs(360 - s1 - s1)
		if ss < r:
			r = ss
print(r)


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
			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))


N = int(input())
angles = list(map(int, input().split()))
total = sum(angles)
min_diff = total
start = total
for i in range(N, -1, -1):
	if i < N:
		start -= angles[i]
	count = start
	if count > total // 2:
		for j in range(0, i):
			count -= angles[j]
			min_diff = min(min_diff, abs(total - count * 2))
print(min_diff)


n = int(input())
l = list(map(int, input().split()))
mi = 360
f = 0
for i in range(n):
	f = l[i]
	f1 = f
	mi = min(mi, max(f, 360 - f))
	for j in range(i + 1, n):
		f1 += l[j]
		mi = min(mi, max(f1, 360 - f1))
print(abs(180 - mi) * 2)


n = int(input())
a = list(map(int, input().split()))
print(2 * min((abs(180 - sum(a[l:r])) for l in range(n) for r in range(l, n))))


n = int(input())
a = list(map(int, input().split()))
a = a + a
mid = n // 2
best = 99999999999999999999999
for i in range(n):
	sub = a[i:i + n]
	d = sum(sub)
	best = min(best, d)
	s = 0
	for j in range(n):
		s += sub[j]
		d -= sub[j]
		diff = abs(s - d)
		best = min(best, diff)
print(best)


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)


def read():
	return tuple((int(x) for x in input().split()))

def main():
	n = int(input())
	ns = list(read())
	ss = []
	s = []
	ss.append(s)
	for i in ns:
		if len(s) == 0:
			s.append(i)
		else:
			s.append(i + s[-1])
	for i in range(n):
		s[i] = s[i] * 2 - s[-1]
	for i in range(n - 1):
		s = []
		ss.append(s)
		for j in range(n):
			s.append(ss[-2][j] - 2 * ns[i])
	m = 9999999
	for s in ss:
		for n in s:
			if abs(n) < m:
				m = abs(n)
	return m
print(main())


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(n,) = I()
a = I() * 2
ans = INF
for i in range(n):
	cur = 0
	for j in range(i, i + n):
		cur += a[j]
		ans = min(ans, abs(360 - 2 * cur))
print(ans)


p = input().strip().split()
arr = list(map(int, input().strip().split()))
if len(arr) == 1:
	print(360)
else:
	minimum = 360
	for i in range(len(arr)):
		(a, b) = (0, 360)
		before = 0
		for j in range(i, len(arr)):
			if i == j:
				before = arr[i]
			else:
				before += arr[j]
			minimum = min(abs(360 - 2 * before), minimum)
	print(minimum)


from math import inf

def pizza(lst):
	result = inf
	for i in range(len(lst)):
		count = 0
		for j in range(i, len(lst)):
			count += lst[j]
			result = min(result, abs(360 - 2 * count))
	return result
n = int(input())
a = [int(x) for x in input().split()]
print(pizza(a))


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)


n = int(input())
l = list(map(int, input().split()))
s = [0]
r_s = [0]
for i in range(n):
	s.append(s[-1] + l[i])
	r_s.append(r_s[-1] + l[n - 1 - i])
m = float('inf')
for right_end in range(n):
	for left_end in range(n - right_end + 1):
		if abs(s[left_end] + r_s[right_end] - (r_s[-left_end - 1] - r_s[right_end])) < m:
			m = abs(s[left_end] + r_s[right_end] - (r_s[-left_end - 1] - r_s[right_end]))
print(m)


n = int(input())
B = list(map(int, input().split()))
sum = 0
A = []
for w in range(len(B)):
	sum = 0
	for q in range(w, len(B)):
		sum = sum + B[q]
		if sum >= 180:
			A.append(min(2 * abs(180 - sum), 2 * abs(180 - sum - B[q])))
			break
	B.append(B[w])
print(min(A))


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


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)


n = int(input())
a = list(map(int, input().split()))
delta = sum(a)
for i in range(len(a)):
	for j in range(len(a)):
		if delta == 0:
			break
		if delta > abs(sum(a[j:]) + sum(a[:i]) - sum(a[i:j])):
			delta = abs(sum(a[j:]) + sum(a[:i]) - sum(a[i:j]))
print(delta)


from collections import deque
n = int(input())
a = deque(map(int, input().split()))
piece = 0
ans = []
for cnt in range(n):
	for p in a:
		piece += p
		if piece >= 180:
			ans.append(piece - (360 - piece))
			piece = 0
			a.appendleft(a.pop())
			break
print(min(ans))


n = int(input())
a = list(map(int, input().split()))
mina = None
flag = 0
for i in range(0, len(a)):
	for j in range(i, len(a)):
		x = sum(a[i:j + 1])
		y = sum(a[:i]) + sum(a[j + 1:])
		if x - y == 0:
			print(0)
			flag = 1
			break
		if mina == None or abs(x - y) < mina:
			mina = abs(x - y)
	if flag == 1:
		break
if flag == 0:
	print(mina)


n = int(input())
a = [int(i) for i in input().split()]
min_dif = 180
for i in range(n + 1):
	for j in range(i, n + 1):
		dif = abs(sum(a[i:j + 1]) - 180)
		if dif < min_dif:
			min_dif = dif
	if min_dif == 0:
		break
print(min_dif * 2)


from itertools import product, accumulate
n = int(input())
arr = list(map(int, input().split()))
arr_sum = sum(arr)
arr_cum = list(accumulate(arr))
arr_cum_rev = list(reversed(list(accumulate(reversed(arr)))))
m = sum(arr)
for (left, right) in product(range(n), range(n)):
	(a, b) = (min(left, right), max(left, right))
	x = sum(arr[:a]) + sum(arr[b:])
	y = arr_sum - x
	m = min(m, abs(x - y))
print(m)


n = int(input())
pices = list(map(int, input().split()))
scores = []
suma2 = 0
for i in range(n):
	suma = 0
	k = i
	while suma < 180:
		suma2 = suma
		suma += pices[k]
		if k + 1 == n:
			k = -1
		k += 1
	scores.append(min(suma - (360 - suma), 360 - suma2 - suma2))
print(min(scores))


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(1000000000.0)
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)


from __future__ import print_function
import sys
try:
	range = xrange
	input = raw_input
except NameError:
	pass
MOD = int(1000000000.0 + 7)
MAXN = int(10000.0 + 8)
N = int(input().strip())
angs = list(map(int, input().strip().split()))
angs.extend(angs)
su = [0] * (N * 2 + 1)
for i in range(1, 2 * N + 1):
	su[i] = su[i - 1] + angs[i - 1]
ans = 360
for i in range(1, 2 * N + 1):
	for j in range(i, 2 * N + 1):
		c = su[j] - su[i - 1]
		ans = min(ans, abs(360 - 2 * c))
print(ans)


n = int(input())
A = list(map(int, input().split()))
print(2 * min((abs(180 - sum(A[i:j])) for i in range(n) for j in range(i, n))))


n = int(input())
a = [int(x) for x in input().split()] + [0]
Tot = sum(a)
B = 360
lena = len(a) - 1
for i in range(0, lena):
	for j in range(i + 1, lena):
		P = abs(Tot - 2 * sum(a[i:j]))
		if P < B:
			B = P
print(B)


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 = 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))


i1 = int(input())
ar = list(map(int, input().split()))
lii = []
if i1 == 1:
	print(360)
else:
	for j in range(i1 + 1):
		ar.insert(0, ar.pop())
		for x in range(i1 - 1):
			if sum(ar[0:x + 1]) >= sum(ar[x + 1:]):
				lii.append(abs(sum(ar[x + 1:]) - sum(ar[0:x + 1])))
				lii.append(abs(sum(ar[x:]) - sum(ar[0:x])))
				break
	print(min(lii))


n = int(input())
A = list(map(int, input().split()))
mndiff = 10000
for st in range(n):
	for en in range(st, n):
		if abs(180 - sum(A[st:en])) < mndiff:
			mndiff = abs(180 - sum(A[st:en]))
print(2 * mndiff)


n = int(input())
inp = list(map(int, input().split()))
sol = []
if n == 1:
	print(inp[0])
else:
	for i in range(n):
		for j in range(i + 1, n):
			sol.append(abs(360 - 2 * sum(inp[i:j])))
	print(min(sol))


def findAngel(ang, n):
	summ = 0
	i = -1
	while summ < 180:
		i += 1
		summ += ang[i]
	return summ
n = int(input())
a = [int(i) for i in input().split()]
i = 0
summ = 0
minSumm = 360
while abs(360 - 2 * summ) != 0 and i < n:
	summ = findAngel([a[i]] + a[i + 1:len(a)] + a[0:i], n)
	i += 1
	if summ < minSumm:
		minSumm = summ
print(abs(360 - 2 * minSumm))


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)


import sys
sys.setrecursionlimit(100000000)
N = int(input())
arr = [int(x) for x in input().split()]
result = 360
for i in range(N):
	vasya = 0
	petya = sum(arr)
	for j in range(i, i + N):
		vasya += arr[j % N]
		petya = sum(arr) - vasya
		check = abs(vasya - petya)
		result = min(result, check)
print(result)


from math import *
n = int(input())
l = list(map(int, input().split()))
ka = inf
for i in range(n):
	s = 0
	for j in range(i, n):
		s = s + l[j]
		ka = min(ka, abs(360 - 2 * s))
print(ka)


m = 1000000000
n = int(input())
a = list(map(int, input().split()))
for _ in range(n):
	for x in range(n):
		m = min(m, abs(sum(a[:x]) - sum(a[x:])))
	a.append(a.pop(0))
print(m)


from collections import deque
n = int(input())
a = deque(map(int, input().split()))
ps = [0] * n
mindif = 2000000000.0
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())
line = input()
line = 2 * [int(i) for i in line.split()]
ans = 360
vasya = 0
if n == 1:
	print(line[-1])
else:
	for i in range(n):
		j = i + 1
		vasya = line[i]
		ans = min(ans, abs(2 * vasya - 360))
		while vasya <= 180:
			vasya += line[j]
			j += 1
			ans = min(ans, abs(2 * vasya - 360))
	print(ans)


n = int(input())
a = list(map(int, input().split()))
maxh = 360
for i in range(n):
	res1 = 0
	for j in range(i, n):
		res1 += a[j]
		maxh = min(abs(360 - 2 * res1), maxh)
print(maxh)


n = int(input())
s = [int(s) for s in input().split()]
m = s * 2
d = 360
t = sum(s)
for i in range(n):
	a = 0
	for j in range(i, i + n):
		a += m[j]
		if abs(2 * a - t) < d:
			d = abs(2 * a - t)
print(d)


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 circular(l, start):
	while True:
		for j in range(start, l.__len__()):
			yield l[j]
		start = 0
count = int(input())
pieces = list(map(int, input().split()))
result = 360
for i in range(count):
	gen = circular(pieces, i)
	s = 0
	while s < 180:
		s += next(gen)
	result = min(result, 2 * abs(180 - s))
print(result)


n = int(input())

def get(l, r):
	if not l:
		return pr[r]
	return pr[r] - pr[l - 1]
arr = list(map(int, input().split()))
gg = 360
ans = 360
pr = [0] * n
pr[0] = arr[0]
for i in range(1, n):
	pr[i] = arr[i] + pr[i - 1]
for i in range(n):
	for j in range(i, n):
		if abs(i - j) == n - 1:
			continue
		s = get(i, j)
		ans = min(ans, abs(360 - s - s))
print(ans)


n = int(input())
l = [int(i) for i in input().split()]
result = 360
for i in range(n):
	for j in range(i, n):
		result = min(result, abs(sum(l[i:j]) - 180) * 2)
print(result)


def main(lst):
	l = len(lst)
	my_ind = [i for i in range(l)]
	res = []
	for outer in range(l):
		summ = 0
		prev_sum = 0
		for ind in range(outer, outer + l):
			cur = ind % l
			if summ > 180:
				break
			prev_sum = summ
			summ += lst[cur]
		res.append(summ - 180)
		res.append(180 - prev_sum)
	return min(res) * 2
a = int(input())
lst = [int(el) for el in input().split()]
print(main(lst))


n = int(input())
a = list(map(int, input().split()))
sectorSize = [sum(a[l:r]) for l in range(n) for r in range(l + 1, n)]
differenceBetweenSectorAndRest = [360] + [2 * abs(180 - sectorSize[i]) for i in range(len(sectorSize))]
print(min(differenceBetweenSectorAndRest))


n = int(input())
a = [int(k) for k in input().split()]
maxx = 360
for i in range(n):
	for j in range(i + 1, n):
		maxx = min(maxx, abs(180 - sum(a[i:j])) * 2)
print(maxx)


test = int(input())
num = list(map(int, input().split()))
num += num
ans = 360
for i in range(test):
	for j in range(test):
		pias = sum(num[i:i + j + 1])
		ans = min(ans, abs(360 - 2 * pias))
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
m = s
for i in range(n):
	st = 0
	sp = s
	for j in range(i, n):
		st += a[j]
		if abs(s - st - st) > sp:
			break
		sp = abs(s - st - st)
	if abs(s - st - st) < m:
		m = abs(s - st - st)
	if sp < m:
		m = sp
print(m)


n = int(input())
a = list(map(int, input().split()))
maxv = 0
minv = 360
for i in range(0, n):
	sum = 0
	for j in range(i, n):
		sum = sum + a[j]
		if sum > maxv and sum <= 180:
			maxv = sum
		if sum < minv and sum >= 180:
			minv = sum
print(min(360 - 2 * maxv, 2 * minv - 360))


import math
n = int(input())
a = list(map(int, input().split()))
rs = 360
for i in range(n):
	t = 0
	for j in range(i, n):
		t = t + a[j]
		rs = min(rs, abs(360 - 2 * t))
print(rs)


import sys
N = int(sys.stdin.readline())
p = [int(x) for x in sys.stdin.readline().split()]
ans = 360
for i in range(0, N, 1):
	sum = 0
	for j in range(i, N, 1):
		sum = sum + p[j]
		ans = min(ans, abs(360 - 2 * sum))
print(ans)


n = int(input())
lst = []
for x in input().split():
	lst.append(int(x))
k = 360
for x in range(len(lst) - 1):
	for y in range(x + 1, len(lst)):
		s1 = sum(lst[:x] + lst[y:])
		s2 = sum(lst[x:y])
		k = min(k, abs(s1 - s2))
print(k)


from math import fabs
n = int(input())
m = list(map(int, input().split()))
a = 0
d = 360
for i in range(n):
	a = 0
	x = 360
	j = i
	while j < n:
		if fabs(180 - a - m[j]) > x:
			break
		a += m[j]
		x = fabs(180 - a)
		j += 1
	if x < d:
		d = x
print(int(d) * 2)


n = int(input())
a = list(map(int, input().split()))
a.extend(a)
mn = 360
for pea in range(1, n):
	l = 0
	r = pea
	full = 0
	for i in range(r - l):
		full += a[i]
	mn = min(abs(360 - full - full), mn)
	for i in range(n):
		full = full - a[l] + a[r]
		l += 1
		r += 1
		mn = min(abs(360 - full - full), mn)
print(mn)


n = int(input())
arr = list(map(int, input().split()))
min_list = set()
for i in range(n):
	s = 0
	l = []
	for j in range(i, n):
		s += arr[j]
		l.append(abs(360 - (s << 1)))
	min_list.add(min(l))
	l.clear()
print(min(min_list))


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
		curr_ans = rest - curr_split
		if curr_ans < 0:
			curr_ans *= -1
		ans = min(curr_ans, ans)
print(ans)


n = int(input())
ls1 = input().split()
ls1 = [int(i) for i in ls1]
mangle = 180

def through(j):
	if j < n - 1:
		j = j + 1
	else:
		j = 0
	return j
for i in range(n):
	j = i
	angle = ls1[i]
	while True:
		j = through(j)
		angle += ls1[j]
		if angle >= 180:
			break
	mangle = min(abs(180 - angle), abs(180 - angle + ls1[j]), mangle)
	if mangle == 0:
		break
print(2 * mangle)


n = int(input())
a = list(map(int, input().split(' ')))
mi = 360
for i in range(1, n):
	a[i] = a[i - 1] + a[i]
for i in range(n):
	for j in range(i, n):
		sa = a[j] - (a[i - 1] if i > 0 else 0)
		sb = a[-1] - a[j] + (a[i - 1] if i > 0 else 0)
		if abs(sa - sb) < mi:
			mi = abs(sa - sb)
print(mi)