Iahub got bored, so he invented a game to be played on paper. 

He writes n integers a_1, a_2, ..., a_{n}. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values a_{k} for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 - x.

The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.


-----Input-----

The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there are n integers: a_1, a_2, ..., a_{n}. It is guaranteed that each of those n values is either 0 or 1.


-----Output-----

Print an integer — the maximal number of 1s that can be obtained after exactly one move. 


-----Examples-----
Input
5
1 0 0 1 0

Output
4

Input
4
1 0 0 1

Output
4



-----Note-----

In the first case, flip the segment from 2 to 5 (i = 2, j = 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].

In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.

Difficulty: EASY

Solutions:
def main():
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	for i in range(n):
		k = arr.count(1)
		for j in arr[i:]:
			if j == 1:
				k -= 1
			else:
				k += 1
			count = max(count, k)
	return count
print(main())


def f(x):
	return 1 - x
n = int(input())
q = list(map(int, input().split()))
if n == 1:
	print(1 - q[0])
else:
	s = []
	for i in range(n - 1):
		for j in range(i, n):
			a = list(map(f, q))
			t = 0
			for k in range(i, j + 1):
				t += a[k]
			for m in range(0, i):
				t += q[m]
			for h in range(j + 1, n):
				t += q[h]
			s.append(t)
	print(max(s))


n = int(input())
coins = [int(x) for x in input().split()]
s = sum(coins)
maxim = n - s
for i in range(len(coins)):
	for j in range(i, len(coins)):
		new_sum = s + len(coins[i:j + 1]) - 2 * sum(coins[i:j + 1])
		if new_sum > maxim:
			maxim = new_sum
print(maxim)


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
n = iinput()
l = list(map(int, input().split()))
big = 0
s = 0
c1 = 0
for i in l:
	if i == 1:
		c1 += 1
		s = max(0, s - 1)
	else:
		s += 1
		big = max(big, s)
if c1 == n:
	print(n - 1)
else:
	print(big + c1)


n = int(input())
a = [int(i) for i in input().split()]
if n == 1 and a[0] == 1:
	print(0)
elif n == 1 and a[0] == 0:
	print(1)
elif n == sum(a):
	print(n - 1)
else:
	max = sum(a)
	for i in range(n):
		for j in range(i + 1, n + 1):
			x = sum(a[0:i]) + (j - i) - sum(a[i:j]) + sum(a[j:])
			if x > max:
				max = x
	print(max)


n = int(input())
a = list(map(int, input().split()))
(count_1, extra_0, extra_0_max) = (0, 0, -1)
for i in range(len(a)):
	if a[i] == 1:
		count_1 += 1
		if extra_0 > 0:
			extra_0 -= 1
	else:
		extra_0 += 1
		if extra_0 > extra_0_max:
			extra_0_max = extra_0
print(count_1 + extra_0_max)


n = int(input())
nn = list(map(int, input().rstrip().split()))
count0 = 0
count1 = 0
maxx = -float('inf')
if n > 1:
	for i in range(n):
		for j in range(i, n):
			if nn[j] == 0:
				count0 += 1
			else:
				count1 += 1
			if count0 - count1 > maxx:
				maxx = count0 - count1
				tempp = count1
				left = i
				right = j
		count0 = 0
		count1 = 0
	left = sum([1 for i in range(left) if nn[i] == 1])
	right = sum([1 for i in range(right + 1, n) if nn[i] == 1])
	if maxx > 0:
		print(maxx + left + right + tempp)
	else:
		print(left + right)
elif nn[0] == 0:
	print(1)
else:
	print(0)


x = int(input())
a = list(map(int, input().split()))
s = []
for i in range(x):
	for j in range(i, x):
		l = 0
		for k in range(x):
			if k >= i and k <= j:
				l += 1 - a[k]
			else:
				l += a[k]
		s.append(l)
print(max(s))


input()
a = input().split()
c = 0
for i in range(len(a)):
	for j in range(len(a) - 1, i - 1, -1):
		m = a[i:j + 1].count('0') + (a[:i] + a[j + 1:]).count('1')
		if m > c:
			c = m
print(c)


n = int(input())
x = [int(i) for i in input().split()]
y = []
for i in range(n):
	for j in range(i, n):
		z = x[:]
		for k in range(i, j + 1):
			z[k] = 1 - z[k]
		y.append(z.count(1))
max = y[0]
for i in range(len(y)):
	if y[i] > max:
		max = y[i]
print(max)


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

def f(x, y):
	t = x - 1
	s = 0
	while t < x + y - 1:
		if line[t] == 1:
			s = s - 1
		elif line[t] == 0:
			s = s + 1
		t = t + 1
	if s < 0:
		s = 0
	return s
a = 1
z = 0
total = 0

def g(x):
	b = 1
	c = 0
	while b <= n - x + 1:
		if f(x, b) > c:
			c = f(x, b)
		b = b + 1
	return c
while a < n + 1:
	if g(a) > z:
		z = g(a)
	if line[a - 1] == 1:
		total = total + 1
	a = a + 1
if total == n:
	print(z + total - 1)
else:
	print(z + total)


a = int(input())
b = input().replace(' ', '')
(i1, i2) = (0, 0)
s1 = 0
for i in range(a):
	s = 0
	for j in range(i, a):
		if b[j] == '1':
			s -= 1
		else:
			s += 1
		if s1 < s:
			s1 = s
			i1 = i
			i2 = j
print(b.count('1') - b[i1:i2 + 1].count('1') + i2 - i1 + 1 - b[i1:i2 + 1].count('1'))


n = int(input())
a = input().split()
max = a.count('1') + 1
if max == n + 1:
	max = n - 1
for i in range(2, n + 1):
	for j in range(0, n - i + 1):
		b = (a[:j] + a[j + i:]).count('1')
		buf = a[j:j + i].count('0')
		if b + buf > max:
			max = b + buf
print(max)


k = int(input())
a = list(map(int, input().split()))
b = []
for i in range(len(a)):
	(c, d, e, f) = (0, 0, 0, 0)
	for j in range(i + 1, len(a) + 1):
		c = a[:i]
		c = sum(c)
		d = a[i:j]
		d = d.count(0)
		e = a[j:len(a)]
		f = c + d + sum(e)
		b.append(f)
print(max(b))


def invert(s):
	return str(1 - int(s))
input()
a = input().split()
c = 0
for i in range(len(a)):
	for j in range(len(a) - 1, i - 1, -1):
		m = (a[:i] + list(map(invert, a[i:j + 1])) + a[j + 1:]).count('1')
		if m > c:
			c = m
print(c)


import math
n = int(input())
maxi = math.inf * -1
arr = list(map(int, input().split()))
for i in range(n):
	for j in range(i, n):
		add = 0
		for k in range(n):
			if i <= k <= j:
				add += 1 - arr[k]
			else:
				add += arr[k]
		if add > maxi:
			maxi = add
print(f'{maxi}')


n = int(input())
a = input()
if n == 1:
	A = int(a)
	if A == 0:
		print('1')
	else:
		print('0')
else:
	line = a.split()
	ASD = []
	for i in range(1, n + 1):
		for j in range(i, n + 1):
			s = 0
			for k in range(i, j + 1):
				s = s + 1 - int(line[k - 1])
			for c in range(0, i - 1):
				s = s + int(line[c])
			for d in range(j, n):
				s = s + int(line[d])
			ASD.append(s)
	print(max(ASD))


n = int(input())
a = list(map(int, input().split()))
s = [-1 if e == 1 else 1 for e in a]
ans = 0
ending = 0
for e in s:
	ending = max(0, ending + e)
	ans = max(ans, ending)
if ans == 0:
	print(sum(a) - 1)
else:
	print(sum(a) + ans)


n = int(input())
a = list(map(int, input().split()))
(count1, countCurrentChange, maxChange) = (0, 0, 0)
for i in a:
	if i == 1:
		count1 += 1
		countCurrentChange -= 1
	else:
		if countCurrentChange < 0:
			countCurrentChange = 0
		countCurrentChange += 1
		if countCurrentChange > maxChange:
			maxChange = countCurrentChange
if count1 == n:
	print(n - 1)
else:
	print(count1 + maxChange)


def kadane(a):
	currMax = globalMax = a[0]
	for i in range(1, len(a)):
		currMax = max(a[i], currMax + a[i])
		globalMax = max(currMax, globalMax)
	return globalMax
n = int(input().strip())
arr = list(map(int, input().strip().split()))
s = sum(arr)
b = [-1 if i else 1 for i in arr]
print(s + kadane(b))


n = int(input())
arr = list(map(int, input().split()))
cnt = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		tmp = 0
		for k in range(n):
			if k >= i and k <= j:
				tmp += 1 - arr[k]
			else:
				tmp += arr[k]
		cnt = max(cnt, tmp)
print(cnt)


N = int(input())
A = list(map(int, input().split()))
lc = 0 - A[0]
an = lc
for a in A:
	if a == 0:
		lc = max(1, lc + 1)
		an = max(lc, an)
	else:
		lc = max(-1, lc - 1)
print(A.count(1) + an)


import sys
sys.setrecursionlimit(10000)
n = int(sys.stdin.readline())
arr = [int(x) for x in sys.stdin.readline().strip().split()]
su = [0] + arr
for i in range(1, n + 1):
	su[i] += su[i - 1]
index = []
m = su[-1]
for j in range(n):
	for i in range(j + 1):
		k = su[j + 1] - su[i]
		alpha = su[-1] - su[j + 1] + su[i]
		if m <= j - i + 1 - k + alpha:
			m = j - i + 1 - k + alpha
			index = [i, j]
print(m if su[-1] != n else n - 1)


n = int(input())
nums = [int(x) for x in input().split()]
if nums.count(1) == n:
	print(n - 1)
	exit()
count = nums.count(1)
for i in range(n):
	for j in range(i, n):
		count = max(count, nums[i:j + 1].count(0) - nums[i:j + 1].count(1) + nums.count(1))
print(count)


n = int(input())
lst = list(map(int, input().split()))
lst2 = []
mega_cnt = 0
for i in lst:
	if i == 1:
		mega_cnt += 1
for i in range(n):
	for j in range(i + 1, n + 1):
		a = lst[i:j]
		cnt = 0
		cnt2 = 0
		for b in a:
			if b == 1:
				cnt += 1
			else:
				cnt2 += 1
		yo = cnt2 - cnt
		lst2.append(yo)
print(mega_cnt + max(lst2))


n = int(input())
l = list(map(int, input().split()))
a = []
for i in range(n):
	for j in range(i, n):
		a.append(l[i:j + 1].count(0) - l[i:j + 1].count(1))
print(max(a) + l.count(1))


import copy
n = int(input())
s = [int(x) for x in input().split()]
l = []
s1 = copy.deepcopy(s)
if s == [0]:
	print(1)
elif s == [1]:
	print(0)
else:
	for i in range(len(s)):
		for j in range(i, len(s)):
			s[j] = 1 - s[j]
			Sum = sum(s)
			l.append(Sum)
		s = copy.deepcopy(s1)
	print(max(l))


def main():
	from sys import stdin, stdout
	write = stdout.write
	tokens = map(int, stdin.read().split())
	N = next(tokens)
	A = list(tokens)
	S = sum(filter(lambda x: x == 1, A))
	B = [-1 if x else 1 for x in A]
	max_number_of_ones_after_flip = 0
	max_ending_in_i = max_number_of_ones_after_flip = B[0]
	for i in range(1, N):
		max_ending_in_i = max(B[i], max_ending_in_i + B[i])
		max_number_of_ones_after_flip = max(max_number_of_ones_after_flip, max_ending_in_i)
	write(str(S + max_number_of_ones_after_flip))
main()


a = int(input())
h = input()
c = 0
d = 1
f = 0
g = 0
k = 0
while c < a:
	k = 0
	b = h.split()
	for i in range(c, c + d):
		if int(b[i]) == 0:
			b[i] = 1
		elif int(b[i]) == 1:
			b[i] = 0
	for j in range(0, a):
		f = f + int(b[j])
	if f > g:
		g = f
	f = 0
	if c + d + 1 <= a:
		d = d + 1
		k = 1
	if c < a and k == 0:
		c = c + 1
		d = 1
print(g)


n = int(input())
a = list(map(int, input().split()))
maximum_count = 0
for start in range(0, n):
	for end in range(0, n):
		if start <= end:
			b = list(a)
			for i in range(start, end + 1):
				b[i] = 1 - b[i]
			count = sum(b)
			maximum_count = max(maximum_count, count)
print(maximum_count)


import heapq as hp
import collections
import bisect

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int):
	return sorted(map(func, input().split()))

def range_n():
	return range(int(input()))

def counter(a):
	d = {}
	for x in a:
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	return d

def setbits(x):
	count = 0
	while x:
		x = x & x - 1
		count += 1
	return count

def getint():
	return int(input())

def kadens(arr):
	ans = 0
	ans = arr[0]
	for i in range(1, len(arr)):
		arr[i] = max(arr[i - 1] + arr[i], arr[i])
		ans = max(ans, arr[i])
	return ans

def main():
	n = getint()
	a = []
	ss = 0
	for i in unpack():
		a.append(-1 if i else 1)
		ss += i
	ans = ss + kadens(a)
	print(ans)
main()


n = int(input())
x = list(map(int, input().split()))
y = []
if 0 not in x:
	print(int(n - 1))
else:
	for i in range(n):
		for j in range(i, n):
			t = x[i:j + 1].count(0) - x[i:j + 1].count(1)
			s = x.count(1) + t
			y.append(s)
	print(int(max(y)))


n = int(input())
a = list(map(int, input().split()))
o = a.count(1)
ans = 0
for i in range(n):
	c = o
	for j in range(i, n):
		if a[j] == 0:
			c += 1
		else:
			c -= 1
		ans = max(ans, c)
print(ans)


import sys
n = int(input())
A = list(map(int, input().split()))
t = [0] * n
for i in range(0, n):
	if A[i] == 1:
		t[i] = -1
	else:
		t[i] = 1
ms = me = k = 0
st = e = sys.maxsize
temp = []
for i in range(len(t)):
	if me + t[i] < 0:
		k = i + 1
		me = 0
	else:
		me += t[i]
	if me > ms:
		ms = me
		st = k
		e = i
if A.count(1) == n:
	print(n - 1)
else:
	print(t[st:e + 1].count(1) + t[0:st].count(-1) + t[e + 1:n].count(-1))


n = int(input())
s = input().split()
u = {}
y = 0
w = 0
r = 0
e = 0
for i in range(0, n):
	u[i] = int(s[i])
	if u[i] == 1:
		r = r + 1
for j in range(0, n):
	for i in range(0, j + 1):
		for k in range(i, j + 1):
			if u[k] == 1:
				y = y + 1
			elif u[k] == 0:
				e = e + 1
		t = e - y
		e = 0
		y = 0
		if w <= t:
			w = t
if r == n:
	w = w - 1
print(r + w)


n = int(input())
array = list(map(int, input().split()))
maxOnes = sum(array)
newArray = []
for i in array:
	newArray.append(1 if i == 0 else -1)
curMaxSum = 0
totalMaxSum = 0
for i in newArray:
	curMaxSum = max(0, curMaxSum + i)
	totalMaxSum = max(totalMaxSum, curMaxSum)
print(maxOnes + totalMaxSum if maxOnes != n else n - 1)


n = int(input())
vals = list(map(int, input().split()[:n]))
n_ones = sum(vals)
deltas = [1 - vals[0] * 2]
for i in range(1, n):
	if vals[i - 1] == 0:
		if vals[i] == 0:
			deltas[-1] += 1
		else:
			deltas.append(-1)
	elif vals[i] == 1:
		deltas[-1] -= 1
	else:
		deltas.append(1)
if deltas[0] < 0:
	deltas = deltas[1:]
if deltas and deltas[-1] < 0:
	deltas = deltas[:-1]
if deltas:
	d = deltas[0]
	for i in range(0, len(deltas), 2):
		s = deltas[i]
		d = max(d, s)
		for j in range(i + 2, len(deltas), 2):
			s += deltas[j - 1] + deltas[j]
			d = max(d, s)
	print(d + n_ones)
else:
	print(n_ones - 1)


def scan(type):
	return list(map(type, input().split()))

def kadane(arr):
	size = len(arr)
	ans = 0
	for l in range(size):
		for r in range(l, size):
			cnt = 0
			for i in range(size):
				if i >= l and i <= r:
					cnt += 1 - arr[i]
				else:
					cnt += arr[i]
			ans = max(ans, cnt)
	return ans
(n,) = scan(int)
arr = scan(int)
ans = kadane(arr)
print(ans)


n = int(input())
list1 = input()
sum1 = list1.count('1')
list1 = list1.split()
ans = 0
for a in range(1, n + 1):
	for i in range(n):
		list2 = list1[i:a + i]
		suma = sum1 - int(list2.count('1')) + int(list2.count('0'))
		ans = max(sum1, suma, ans)
if sum1 == n:
	print(ans - 1)
else:
	print(ans)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	l = [0] + list(map(int, input().split()))
	p = [0] * (n + 1)
	p[1] = l[1]
	for i in range(1, n + 1):
		p[i] = l[i] + p[i - 1]
	r = 0
	for i in range(1, n + 1):
		for j in range(i, n + 1):
			r = max(r, p[n] + j - i + 1 - 2 * (p[j] - p[i - 1]))
	print(r)
main()


n = int(input())
numbers = [int(i) for i in input().split()]
current_sum = sum(numbers)
part_add = 0
max_add = -1
for j in range(n):
	for i in range(j + 1):
		part_add = j + 1 - i - 2 * sum(numbers[i:j + 1])
		max_add = max([max_add, part_add])
print(current_sum + max_add)


n = int(input())
a = [int(i) for i in input().split()]
num0 = 0
num1 = 0
d = -9
for i in range(n):
	for j in range(i, n):
		for k in range(i, j + 1):
			if a[k] == 0:
				num0 += 1
			else:
				num1 += 1
		dd = num0 - num1
		if dd > d:
			d = dd
		(num0, num1) = (0, 0)
d0 = 0
for i in a:
	if i == 1:
		d0 += 1
print(d0 + d)


N = int(input())
line = input()
list = line.split(' ')
list1 = []
list3 = []
if N > 1:
	for i in list:
		list1.append(int(i))
	for i in range(N):
		for j in range(i, N):
			list2 = []
			for q in list1:
				list2.append(q)
			for m in range(i, j + 1):
				list2[m] = 1 - list2[m]
			n = sum(list2)
			list3.append(n)
	print(max(list3))
if N == 1:
	if int(line) == 1:
		print(0)
	if int(line) == 0:
		print(1)


n = int(input())
s = input()
c = s.split()
a = 0
p = 0
if c.count('1') == n:
	p = n - 1
else:
	for i in range(n):
		for t in range(n - i):
			if c[t + i] == '0':
				a += 1
				if a > p:
					p = a
			else:
				a -= 1
		a = 0
	o = c.count('1')
	p = p + o
print(p)


n = int(input())
l = list(map(int, input().split()))
ones = sum(l)
arr = [-1 if item == 1 else 1 for item in l]
maxSoFar = arr[0]
maxEndingHere = arr[0]
for i in range(1, n):
	maxEndingHere = max(maxEndingHere + arr[i], arr[i])
	maxSoFar = max(maxSoFar, maxEndingHere)
print(maxSoFar + ones)


n = int(input())
a = list(map(int, input().split()))
ans = -n
for i in range(n):
	for j in range(i, n):
		u = j - i + 1 - 2 * a[i:j + 1].count(1)
		if u > ans:
			ans = u
print(a.count(1) + ans)


n = int(input())
arr = list(map(int, input().split()))
presum = [0 for i in range(n)]
presum[0] = arr[0]
for i in range(1, n):
	presum[i] = presum[i - 1] + arr[i]
ans = 0
for i in range(n):
	for j in range(i, n):
		mid = j - i + 1 - (presum[j] - presum[i] + arr[i])
		lef = presum[i] - arr[i]
		rig = presum[n - 1] - presum[j]
		tot = mid + rig + lef
		ans = max(ans, tot)
print(ans)


def get_ones(arr):
	ones = 0
	for val in arr:
		if val == 1:
			ones += 1
	return ones

def max_subarray_sum(arr):
	(max_so_far, curr_max) = (arr[0], arr[0])
	for i in range(1, len(arr)):
		curr_max = max(arr[i], curr_max + arr[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far

def maximum_ones(arr, N, count_ones):
	if len(arr) == 1:
		return 1 - arr[0]
	new_arr = [0] * N
	for (i, _) in enumerate(new_arr):
		if arr[i] == 1:
			new_arr[i] = -1
		elif arr[i] == 0:
			new_arr[i] = 1
	sub_sum = max_subarray_sum(new_arr)
	return count_ones + sub_sum
N = int(input())
arr = list(map(int, input().split()))
count_ones = get_ones(arr)
print(maximum_ones(arr, N, count_ones))


n = input()
line = [int(i) for i in input().split()]
line_1 = line[:]
s = 0
for i in range(0, len(line)):
	for k in range(i, len(line)):
		line_1[k] = 1 - line_1[k]
		if sum(line_1) >= s:
			s = sum(line_1)
	line_1 = line[:]
print(s)


from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = [0] * n
l = 0
for i in range(n):
	if a[i] == 1:
		b[i] = -1
		l += 1
	else:
		b[i] = 1
dp = [0] * n
dp[0] = b[0]
for i in range(1, n):
	dp[i] = max(dp[i - 1] + b[i], b[i])
print(l + max(dp))


def maxSubarraySum(l):
	(globalMax, localMax) = (l[0], l[0])
	for i in range(1, len(l)):
		localMax = max(l[i], localMax + l[i])
		globalMax = max(localMax, globalMax)
	return globalMax
n = int(input())
l = input().split()
a = []
maxOnes = l.count('1')
if maxOnes == len(l):
	print(maxOnes - 1)
	exit()
for i in l:
	if i == '1':
		a.append(-1)
	else:
		a.append(1)
print(maxOnes + maxSubarraySum(a))


n = int(input())
v = [int(i) for i in input().split()]
dp = [[-10 ** 9 for i in range(n + 1)] for j in range(3)]
dp[0][0] = 0
for j in range(n):
	dp[2][j + 1] = max(dp[2][j] + (v[j] == 1), dp[1][j] + (v[j] == 1))
	dp[1][j + 1] = max(dp[1][j], dp[0][j]) + (v[j] == 0)
	dp[0][j + 1] = dp[0][j] + (v[j] == 1)
print(max(dp[1][n], dp[2][n]))


n = int(input())
a = [int(i) for i in input().split()]
d = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		t = a[:i].count(1)
		t = t + a[j:].count(1)
		t = t + a[i:j].count(0)
		if t > d:
			d = t
print(d)


n = int(input())
arr = list(map(int, input().split()))
count = 0
for i in range(n):
	for j in range(i, n):
		arr2 = [x for x in arr]
		for k in range(i, j + 1):
			arr2[k] = 1 - arr2[k]
		count = max(count, sum(arr2))
print(count)


n = int(input())
s = input()
a = s.split()
for i in range(n):
	a[i] = int(a[i])
ans = 0
tot = 0
for i in a:
	tot += i
for i in range(n):
	xtot = 0
	for j in range(i, n):
		xtot += a[j]
		if tot + j - i + 1 - 2 * xtot > ans:
			ans = tot + j - i + 1 - 2 * xtot
print(ans)


n = int(input())
items = [int(item) for item in input().split(' ')]
running_value = 0
max_run = 0
ones = 0
for i in range(n):
	item = items[i]
	if item == 1:
		ones += 1
		if running_value:
			running_value -= 1
	else:
		running_value += 1
	max_run = max(max_run, running_value)
if not max_run:
	ones -= 1
print(max_run + ones)


n = int(input())
a = [int(i) for i in input().split()]
max_0 = 0
c_0 = 0
c_1 = 0
for v in a:
	if v == 0:
		c_0 += 1
	else:
		c_1 += 1
		c_0 -= 1
		c_0 = max(0, c_0)
	max_0 = max(max_0, c_0)
if max_0 == 0:
	print(c_1 - 1)
else:
	print(c_1 + max_0)


from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from sys import *
from re import *

def fast():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))

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

def output(answer):
	stdout.write(str(answer))
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
n = int(z())
arr = zzz()
mx = arr.count(1)
ans = -1
mi = 0
for i in arr:
	if i == 0:
		mi += 1
	else:
		mi -= 1
	ans = max(mi, ans)
	if mi < 0:
		mi = 0
print(ans + mx)


def game(a):
	for i in range(len(a)):
		if a[i] == 1:
			a[i] = -1
		else:
			a[i] = 1
	j = a.count(-1)
	max1 = 0
	max2 = j
	for i in a:
		max2 = max(max2 + i, j)
		max1 = max(max2, max1)
	if max1 == j:
		return max1 - 1
	else:
		return max1
input()
a = list(map(int, input().split(' ')))
print(game(a))


n = int(input())
a = list(map(int, input().split()))
cnt = sum(a)
maxsum = 0
for i in range(n):
	m = cnt
	for j in range(i, n):
		if a[j] == 0:
			m += 1
		else:
			m -= 1
		if m > maxsum:
			maxsum = m
print(maxsum)


def flip(l):
	n = len(l)
	for i in range(0, n):
		if l[i] == 0:
			l[i] = 1
		elif l[i] == 1:
			l[i] = 0
	return l
n = int(input())
l = list(map(int, input().split()))
l2 = []
for x in range(1, n + 1):
	for i in range(0, n - x + 1):
		a = l[i:i + x]
		flip(a)
		li = l.copy()
		li[i:i + x] = a
		s = sum(li)
		l2.append(s)
ma = max(l2)
print(ma)


n = int(input())
a = [0] * n
s = [0] * n
a = [int(x) for x in input().split()]
ts = 0
large = -1 * n
for i in range(0, n):
	su = 0
	ts = ts + a[i]
	for j in range(i, n):
		if a[j] == 0:
			value = 1
		else:
			value = -1
		su = su + value
		s[j] = su
		if su > large:
			large = su
print(ts + large)


n = int(input())
a = list(map(int, input().split()))
x = 0
y = -1
for i in a:
	if i == 1:
		x -= 1
	else:
		x += 1
	if x > y:
		y = x
	if x < 0:
		x = 0
print(y + sum(a))


n = int(input())
s = list(map(int, input().split()))
c = 0
m = []
if (len(s) == 2 and (s.count(1) == 1 and s.count(0) == 1 or s.count(1) == 2)) and (len(s) == 1 and s.count(0) == 1):
	print(1)
elif len(s) == 1 and s.count(1) == 1:
	print(0)
else:
	for i in range(n):
		v = s.count(1)
		for j in s[i:len(s) + 1]:
			if j == 1:
				v -= 1
			else:
				v += 1
			m.append(v)
	print(max(m))


n = int(input())
a = [int(x) for x in input().split(' ')]
if a.count(0) == 0:
	print(len(a) - 1)
else:
	c = a.count(1)
	ml = mg = 0
	for i in a:
		if i == 0:
			ml += 1
		else:
			ml -= 1
		if ml < 0:
			ml = 0
		if ml > mg:
			mg = ml
	print(c + mg)


n = int(input())
a = [int(i) for i in input().split()]
max = 0
k = 0
sum = (n + 1) * [0]
sum[0] = 0
for i in range(1, n + 1):
	sum[i] = sum[i - 1] + a[i - 1]
for i in range(1, n + 1):
	for j in range(i, n + 1):
		k = sum[n] - sum[j] + sum[i - 1] + j - i + 1 - sum[j] + sum[i - 1]
		if k > max:
			max = k
print(max)


n = int(input())
s = list(map(int, input().split(' ')))
(maxs, maxed) = (0, 0)
for (kk, vv) in enumerate(s):
	(cmaxs, i, j, eds) = (0, 0, 0, 0)
	for x in range(kk, len(s)):
		if s[x] == 0:
			cmaxs = cmaxs + 1
			if cmaxs > maxs:
				maxs = cmaxs
				j = x
		else:
			eds = eds + 1
			if eds > maxed:
				maxed = eds
			cmaxs = cmaxs - 1
if maxed == n:
	print(n - 1)
else:
	print(maxs + maxed)


n = int(input())
L = input().split()
L = [int(i) for i in L]
ans = [0] * n
orig = L.count(1) - L.count(0)
for i in range(0, n):
	temp = [0] * (n - i)
	if L[i] == 0:
		temp[0] = -1
	else:
		temp[0] = 1
	for j in range(i + 1, n):
		if L[j] == 1:
			temp[j - i] = temp[j - i - 1] + 1
		else:
			temp[j - i] = temp[j - i - 1] - 1
	for j in range(0, n - i):
		temp[j] = orig - 2 * temp[j]
	ans[i] = max(temp)
ans = [(i + n) // 2 for i in ans]
print(max(ans))


t = int(input())
num = [int(x) for x in input().split()]
ones = num.count(1)
delta = []
for i in range(t):
	for j in range(i + 1, t + 1):
		delta.append(2 * num[i:j].count(0) - (j - i))
delta.sort()
print(ones + delta[-1])


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	if a[i] == 1:
		b.append(-1)
	else:
		b.append(1)
meh = 0
msf = -1
for i in range(n):
	meh = max(meh + b[i], b[i])
	msf = max(msf, meh)
print(msf + sum(a))


n = int(input())
a = list(map(int, input().split()))
max_a = -100
for i in range(n):
	for j in range(i, n):
		max_a = max(max_a, a[i:j + 1].count(0) - a[i:j + 1].count(1))
print(sum(a) + max_a)


n = int(input())
arr = list(map(int, input().split()))
gain = [0] * n
cnt = 0
for i in range(0, n):
	if arr[i] == 0:
		gain[i] = 1
	else:
		cnt += 1
		gain[i] = -1
maxsum = float('-inf')
cursum = 0
for i in range(0, n):
	cursum += gain[i]
	if gain[i] > cursum:
		cursum = gain[i]
	maxsum = max(maxsum, cursum)
print(maxsum + cnt)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
n = inp()
A = inlt()

def maxSubArraySum(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far
B = []
for i in range(n):
	if A[i] == 1:
		B.append(-1)
	else:
		B.append(1)
a = sum(A)
b = maxSubArraySum(B, n)
if a == n:
	print(n - 1)
else:
	print(a + b)


(n, t) = (int(input()), list(map(int, input().split())))
k = t.count(1)
t = [1 - 2 * i for i in t]
(m, s) = (-1, 0)
for i in range(n):
	s = max(s, 0)
	s += t[i]
	m = max(m, s)
print(k + m)


import sys
n = int(sys.stdin.readline())
a = sys.stdin.readline()

def doTest():
	assert findMaxReverse(5, '1 0 0 1 0') == 4
	assert findMaxReverse(4, '1 0 0 1') == 4

def findMaxReverse(n, a):
	a = a.strip().split()
	mx = 0
	combo = []
	last_digit = a[0]
	last_count = 1
	zeroExists = False
	for position in range(1, len(a)):
		if a[position] == last_digit:
			last_count += 1
		else:
			combo.append([last_digit, last_count])
			last_count = 1
			last_digit = a[position]
			if last_digit == '0':
				zeroExists = True
	if last_digit == '0':
		zeroExists = True
	combo.append([last_digit, last_count])
	for size_block in range(1, len(combo) + 1):
		for position_to_change_start in range(0, len(combo) - size_block + 1):
			d1 = 0
			d0 = 0
			for position_pre_change in range(0, position_to_change_start):
				count = combo[position_pre_change][1]
				if combo[position_pre_change][0] == '1':
					d1 += count
				else:
					d0 += count
			position_to_change_end = position_to_change_start + size_block
			for position_to_change in range(position_to_change_start, position_to_change_end):
				count = combo[position_to_change][1]
				if combo[position_to_change][0] == '1':
					d0 += count
				else:
					d1 += count
			for position_after_change in range(position_to_change_end, len(combo)):
				count = combo[position_after_change][1]
				if combo[position_after_change][0] == '1':
					d1 += count
				else:
					d0 += count
			if d1 > mx:
				mx = d1
			False
	if not zeroExists:
		mx = n - 1
	return mx
print(findMaxReverse(n, a))


n = int(input())
L = list(map(int, input().split()))
sumL = []
s = sum(L)
for i in range(0, len(L)):
	if L[i] == 0:
		s += 1
	elif L[i] == 1:
		s -= 1
	sumL.append(s)
maxL = []
cmax = 0
s = 0
for i in range(0, len(L)):
	if L[i] == 0:
		s -= 1
	elif L[i] == 1:
		s += 1
	cmax = max(cmax, s)
	maxL.append(cmax)
for i in range(0, len(L)):
	sumL[i] += maxL[i]
if L.count(1) == n:
	print(n - 1)
else:
	print(max(sumL))


def flip(n, array):
	s = 0
	max_s = 0
	start = 0
	end = 0
	max_start = 0
	max_end = 0
	for i in range(n):
		if array[i] == 0:
			s += 1
			if s == 1:
				start = i
			end = i
		elif array[i] == 1:
			s -= 1
			if s == 0:
				continue
			if s < 0:
				s += 1
				continue
		if s > max_s:
			max_s = s
			max_start = start
			max_end = end
	for x in range(max_start, max_end + 1):
		if array[x] == 1:
			array[x] = 0
		else:
			array[x] = 1
	return array.count(1)
n = int(input())
array = list(map(int, input().split(' ')))
print(flip(n, array))


n = int(input())
l = list(map(int, input().split()))
ones = [0 for i in range(n + 2)]
zeroes = [0 for i in range(n + 2)]
for i in range(n):
	if l[i]:
		ones[i] = ones[i - 1] + 1
		zeroes[i] = zeroes[i - 1]
	else:
		ones[i] = ones[i - 1]
		zeroes[i] = zeroes[i - 1] + 1
mx = -1000
for i in range(n):
	for j in range(i + 1):
		a = zeroes[i] - zeroes[j - 1] - (ones[i] - ones[j - 1])
		if a > mx:
			mx = a
print(ones[n - 1] + mx)


n = int(input())
an = [int(x) for x in input().split()]
count = sum(an)
if count == len(an):
	print(count - 1)
else:
	for i in range(n):
		for j in range(i, n):
			result = sum(an[:i]) + j - i + 1 - sum(an[i + 1:j + 1]) + sum(an[j + 1:])
			count = max(count, result)
	print(count)


n = int(input())
line = input().split()
list = []
for i in line:
	list.append(int(i))
b = 0
for i in range(len(list)):
	for j in range(i, len(list)):
		list1 = list.copy()
		for k in range(len(list1[i:j + 1])):
			if list1[k + i] == 1:
				list1[k + i] = 0
			else:
				list1[k + i] = 1
		a = sum(list1)
		if a > b:
			b = a
print(b)


import os, sys
from io import BytesIO, IOBase
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))
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *
from collections import deque as dq
n = ii1()
arr = iia()
one = arr.count(1)
zero = n - one
leftone = 0
ans = 0
for i in range(n):
	currone = 0
	currzero = 0
	for j in range(i, n):
		currone += arr[j]
		currzero += arr[j] ^ 1
		ans = max(ans, leftone + currzero + one - leftone - currone)
	leftone += arr[i]
print(ans)


n = int(input())
a = input().split()
for (i, o) in enumerate(a):
	a[i] = int(o)
a = tuple(a)
maxa = -1
for i in range(0, n):
	b = list(a)
	for j in range(i, n):
		b[j] = 1 - a[j]
		cn = b.count(1)
		if maxa < cn:
			maxa = cn
print(maxa)


n = int(input())
a = input().split()
list(a)
a = [bool(int(i)) for i in a]
ans = 0
m = 0
s = 0
for i in range(0, n):
	if a[i] == False:
		ans += 1
	else:
		ans = max(0, ans - 1)
		s += 1
	if ans > m:
		m = ans
if m == 0:
	m = -1
print(s + m)


n = int(input())
m = n
l = []
l = input().split()
lenth = len(l)
p = 1
sum2 = 0
q = 0
l2 = []
if lenth == 1:
	if l[0] == '1':
		summ = 0
	else:
		summ = 1
else:
	for i in l:
		if i == '1':
			sum2 += 1
	while p < lenth and p > q:
		q = 0
		while q < lenth - 1 and q < p:
			sum1 = 0
			for i in range(q, p + 1):
				num1 = l[i]
				if num1 == '0':
					sum1 += 1
				else:
					sum1 -= 1
			l2.append(sum1)
			q += 1
		p += 1
	if max(l2) >= 0:
		max1 = max(l2)
	else:
		max1 = -1
	summ = max1 + sum2
print(summ)


result = 0
n = int(input())
line = input()
line = line.split(sep=' ')
for k in range(n):
	line[k] = int(line[k])
for i in range(n):
	a = line[:]
	for j in range(i, n):
		a[j] = 1 - line[j]
		temp = sum(a)
		if temp > result:
			result = temp
print(result)


n = int(input())
l1 = [int(i) for i in input().split()]
max1 = 0
for i in range(n):
	cnt1 = 0
	for j in range(i, n):
		if l1[j] == 0:
			cnt1 += 1
		else:
			cnt1 -= 1
		cnt = l1.count(1) + cnt1
		if max1 < cnt:
			max1 = cnt
print(max1)


n = int(input())
l = list(map(int, input().split()))
c = 0
v = 0
ma = -1
for i in range(n):
	if l[i] == 1:
		v += 1
		c -= 1
	else:
		c += 1
	if c > ma:
		ma = c
	if c < 0:
		c = 0
print(v + ma)


n = int(input())
arr = [int(x) for x in input().split(' ')]
dp = [0] * n
dp[0] = -1 if arr[0] else 1
s = sum(arr)
for i in range(1, len(arr)):
	if arr[i] == 0:
		dp[i] = max(dp[i - 1] + 1, 1)
	else:
		dp[i] = max(dp[i - 1] - 1, -1)
print(s + max(dp))


n = int(input())
vec = list(map(int, input().split(' ')))
s = [0]
cnt = 0
ans = 0
for x in vec:
	s.append(x + s[-1])
	if x == 1:
		cnt = cnt + 1
for i in range(1, n + 1):
	for j in range(1, i + 1):
		one = s[i] - s[j - 1]
		tot = i - j + 1
		ans = max(ans, cnt - one + tot - one)
print(ans)


n = int(input())
nums = list(map(int, input().split()))
max_score = 0
max_start_i = -1
max_end_i = -1
curr_score = 0
curr_start_i = 0
curr_end_i = 0
for i in range(n):
	if nums[i] == 0:
		curr_score += 1
	else:
		curr_score -= 1
	if curr_score < 0:
		curr_score = 0
		curr_zeroes_freq = 0
		curr_start_i = i + 1
		curr_end_i = i + 1
	else:
		curr_end_i = i
		if curr_score > max_score:
			max_score = curr_score
			max_start_i = curr_start_i
			max_end_i = curr_end_i
num_zeroes_max = 0
num_ones_outside = 0
for i in range(n):
	if i < max_start_i or max_end_i < i:
		if nums[i] == 1:
			num_ones_outside += 1
	elif nums[i] == 0:
		num_zeroes_max += 1
if all(nums) and len(nums) > 1:
	print(len(nums) - 1)
elif max_start_i != -1:
	print(num_zeroes_max + num_ones_outside)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
s = -1
for i in range(n):
	for j in range(i, n):
		t = j - i - sum(a[i:j + 1]) + sum(a[:i]) + sum(a[j + 1:])
		if t > s:
			s = t
print(s + 1)


import itertools
import sys

def solve_bruteforce(xs):
	n = len(xs)
	if n == 1:
		return len(xs) - sum(xs)
	return max((sum(xs[:i]) + sum(xs[j:]) - sum(xs[i:j]) + (j - i) for (i, j) in itertools.combinations(n, 2)))

def solve0(xs):
	assert len(xs) > 0
	if set(xs) == {1}:
		return sum(xs) - 1
	acc = list(itertools.accumulate((1 if x == 0 else -1 for x in xs)))
	return sum(xs) + max((max(acc[i:]) - y for (i, y) in enumerate(acc)))

def solve(xs):
	ys = [1 if x == 0 else -1 for x in xs]
	best_so_far = 0
	min_so_far = 0
	for (i, acc) in enumerate(itertools.accumulate(ys)):
		best_so_far = max(best_so_far, acc - min_so_far)
		min_so_far = min(min_so_far, acc)
		print(xs, (best_so_far, i, min_so_far), file=sys.stderr)
	if best_so_far == 0:
		return sum(xs) - 1
	else:
		return sum(xs) + best_so_far

def test():
	xs = [0]
	assert solve(xs) == 1
	xs = [1]
	assert solve(xs) == 0
	xs = [0, 1, 0]
	assert solve(xs) == 2
	xs = [0, 0, 1, 0, 0]
	assert solve(xs) == 4
	xs = [0, 0, 0, 0]
	assert solve(xs) == 4
	xs = [1, 1, 1]
	assert solve(xs) == 2

def main():
	n = int(input())
	xs = [int(i) for i in input().strip().split()]
	assert len(xs) == n
	result = solve(xs)
	print(result)
main()


n = int(input())
a = list(map(int, input().split()))
one = sum(a)
tot = 0
ans = -1
zero = 0
for i in range(len(a)):
	if a[i] == 0:
		zero += 1
	elif zero > 0:
		zero -= 1
	ans = max(ans, zero)
if one == n:
	print(n - 1)
else:
	print(one + ans)


n = input('')
a = input('').split()
maxCount = -100
for i in range(len(a)):
	count = 0
	for j in range(i, len(a), 1):
		if a[j] == '0':
			count += 1
		else:
			count -= 1
		if count > maxCount:
			maxCount = count
out = 0
for i in range(len(a)):
	if a[i] == '1':
		out += 1
print(out + maxCount)


n = int(input())
a = list(map(int, input().split()))
num = 0
for i in range(n):
	for j in range(i, n):
		tmp = 0
		for k in range(n):
			if i <= k <= j:
				if a[k] == 0:
					tmp += 1
			elif a[k] == 1:
				tmp += 1
		num = max(num, tmp)
print(num)


n = int(input())
s = input().split()
maxmax = -1
for i in range(n):
	for j in range(i + 1):
		temp = s[0:j].count('1') + s[j:i + 1].count('0') + s[i + 1:].count('1')
		maxmax = max(maxmax, temp)
print(maxmax)


n = int(input())
A = list(map(int, input().split()))
s = sum(A)
s = -1
t = -1
for i in range(n):
	if A[i] == 0:
		s = i
		break
for i in range(n - 1, -1, -1):
	if A[i] == 0:
		t = i
		break
if t == -1 or s == -1:
	print(n - 1)
	exit(0)
for i in range(s, t + 1):
	for j in range(i, t + 1):
		B = A[:i] + [1 - A[k] for k in range(i, j + 1)] + A[j + 1:]
		b = sum(B)
		if b > s:
			s = b
print(s)


n = int(input())
l = []
w = []
q = []
p = input().rstrip().split(' ')
for i in range(0, len(p)):
	for j in range(i, len(p)):
		t = p[i:j + 1]
		r = []
		for k in range(0, len(p)):
			if k > i and k > j or (k < i and k < j):
				r.append(p[k])
		d = r.count('1')
		e = t.count('0')
		l.append(d + e)
		q.append(i)
		w.append(j)
print(max(l))


in_n = input()
in_nums = input()
n = int(in_n)
nums = [int(x) for x in in_nums.split()]
stored = [[] for x in range(n)]

def getScore(i, j):
	score = 0
	for x in range(0, i):
		score += nums[x]
	for x in range(i, j + 1):
		score += 1 if nums[x] == 0 else 0
	for x in range(j + 1, n):
		score += nums[x]
	return score
max_val = 0
for i in range(n):
	for j in range(i, n):
		max_val = max(max_val, getScore(i, j))
print(max_val)


n = int(input())
a = input().split()
s = 0
S = []
x = 0
for i in range(0, n):
	if a[i] == '1':
		x = x + 1
	for j in range(0, i + 1):
		s = i - j
		for k in range(j, i + 1):
			s = s - 2 * int(a[k])
		S.append(s + 1)
	s = 0
print(x + max(S))


n = int(input())
l = [int(x) for x in input().split()]
zeroes = 0
ones = 0
flip = []
for i in range(n):
	if ones > zeroes:
		zeroes = 0
		ones = 0
	if l[i] == 0:
		zeroes += 1
	if l[i] == 1:
		ones += 1
	flip.append(zeroes - ones)
result = l.count(1) + max(flip)
print(result)


n = int(input())
line = [int(x) for x in input().split()]
v = sum(line)
if n == 1:
	v = 1 - v
elif n == v:
	v -= 1
else:
	for i in range(n):
		for j in range(i + 1, n):
			nline = line.copy()
			for y in range(i, j + 1):
				nline[y] = 1 - nline[y]
				v = max(v, sum(nline))
print(v)


n = int(input())
s = input().split()
M = [[0] * n for _ in range(n)]
Max = -1000
num = 0
for i in range(n):
	if s[i] == '0':
		M[i][i] = 1
	else:
		M[i][i] = -1
		num += 1
	Max = max(M[i][i], Max)
for j in range(n - 1, -1, -1):
	for i in range(j - 1, -1, -1):
		if s[i] == '1':
			M[i][j] = M[i + 1][j] - 1
		else:
			M[i][j] = M[i + 1][j] + 1
		Max = max(M[i][j], Max)
print(num + Max)


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
s = 0
temp = a[:]
for i in range(n):
	if a[i] == 0:
		a[i] -= 1
	else:
		s += 1
start = 0
end = 0
fs = 0
fe = 0
ts = a[0]
fsu = a[0]
for i in range(1, n):
	if ts + a[i] < a[i]:
		ts = ts + a[i]
		end = i
	else:
		start = i
		end = i
		ts = a[i]
	if ts < fsu:
		fsu = ts
		fs = start
		fe = end
print(max(s - fsu, 0))


n = int(input())
a = [int(i) for i in input().split()]
cnt1 = sum(a)
mx = 0
cnt0 = 0
for i in a:
	if i == 0:
		cnt0 += 1
		if cnt0 > mx:
			mx = cnt0
	elif cnt0 > 0:
		cnt0 -= 1
if mx == 0:
	mx -= 1
print(cnt1 + mx)


n = int(input())
nums = list(map(int, input().split()))
ones = nums.count(1)

def flipp(num):
	if num == 0:
		return 1
	return 0

def getflippTotal(i, j):
	total = 0
	for k in range(len(nums)):
		if i <= k <= j:
			total += flipp(nums[k])
		else:
			total += nums[k]
	return total
size = len(nums)
if ones != len(nums):
	if size != 1:
		flippTotal = 0
		for i in range(len(nums)):
			for j in range(len(nums) - 1, 0, -1):
				temp = getflippTotal(i, j)
				if temp > flippTotal:
					flippTotal = temp
		print(flippTotal)
	elif nums[0] == 0:
		print(1)
	else:
		print(0)
else:
	print(size - 1)


n = int(input())
arr = [int(x) for x in input().split()]
sumarr = sum(arr)
totmax = sumarr
min_count = 0
for i in range(0, n):
	for j in range(0, i + 1):
		ar = arr[j:i + 1]
		t = sum(ar)
		if t < len(ar) - t:
			curmax = sumarr - t + len(ar) - t
			if curmax > totmax:
				if min_count == 0:
					min_count = 1
				totmax = curmax
if min_count == 1:
	print(totmax)
else:
	print(sumarr - 1)


n = int(input())
arr = list(map(int, input().split()))
nrOneInitial = 0
D = [0] * n
if arr[0] == 0:
	D[0] = 1
else:
	nrOneInitial += 1
for i in range(1, n):
	D[i] = D[i - 1] + (1 if arr[i] == 0 else 0)
	if arr[i] == 1:
		nrOneInitial += 1
maxPossible = 0
for i in range(n):
	profit = 2 * D[i] - (i + 1)
	maxPossible = max(maxPossible, nrOneInitial + profit)
	for j in range(i):
		profit = 2 * (D[i] - D[j]) - (i - j)
		maxPossible = max(maxPossible, nrOneInitial + profit)
print(maxPossible)


n = int(input())
(*s,) = map(int, input().split())
print(max((sum(s) + j - i - 2 * sum(s[i:j]) for i in range(n) for j in range(i + 1, n + 1))))


def max_subarray_sum(a):
	max_till_now = 0
	max_ending_here = 0
	for i in range(len(a)):
		max_ending_here = max_ending_here + a[i]
		if max_ending_here < 0:
			max_ending_here = 0
		if max_ending_here > max_till_now:
			max_till_now = max_ending_here
	return max_till_now
n = int(input())
l = list(map(int, input().strip().split(' ')))
num_ones = 0
b = []
for i in range(n):
	if l[i] == 0:
		b.append(1)
	else:
		num_ones += 1
		b.append(-1)
if num_ones == n:
	print(n - 1)
else:
	print(num_ones + max_subarray_sum(b))


n = int(input())
list = input().strip().split()
list = [int(i) for i in list]
list_2 = [1 - x for x in list]
list_3 = []
for a in range(0, n):
	for b in range(a, n):
		list_3.append(sum(list[:a]) + sum(list_2[a:b + 1]) + sum(list[b + 1:]))
print(max(list_3))


n = int(input())
arr = list(map(int, input().strip().split()))
res = 0
for i in arr:
	if i == 1:
		res += 1
c1 = 0
c2 = float('-inf')
for i in range(len(arr)):
	if arr[i] == 0:
		c1 += 1
	else:
		c1 -= 1
	if c1 <= 0:
		c1 = 0
	c2 = max(c1, c2)
if c2 == 0:
	print(len(arr) - 1)
else:
	print(res + c2)


n = int(input())
arr = [int(p) for p in input().split()]
counter = 0
curr = 0
if len(arr) == 1:
	if arr[0] == 0:
		print(1)
	else:
		print(0)
	exit()
for i in range(len(arr)):
	for j in range(i + 1, len(arr) + 1):
		arr1 = arr.copy()
		for k in range(i, j):
			if arr1[k] == 0:
				arr1[k] = 1
			else:
				arr1[k] = 0
		curr = max(curr, arr1.count(1))
print(curr)


n = int(input())
a = list(map(int, input().split()))
s = 0
v = []
for i in a:
	s += i
	if i:
		v.append(-1)
	else:
		v.append(1)
x = v[0]
ans = x
mn = x
for i in range(len(v)):
	x += v[i]
	ans = max(ans, x - mn)
	mn = min(mn, x)
print(s + ans)


n = int(input())
a = list(map(int, input().split()))
ans = a.count(0) - a.count(1)
for i in range(len(a)):
	for j in range(i, len(a)):
		ans = max(ans, a.count(1) - a[i:j + 1].count(1) + a[i:j + 1].count(0))
print(ans)


n = int(input())
a = list(map(int, input().split()))
w = sum(a)
if w == n:
	print(w - 1)
else:
	ans = w
	for i in range(n):
		(zeros, one) = (0, 0)
		for j in range(i, n):
			if a[j] == 0:
				zeros += 1
			else:
				one += 1
			ans = max(ans, w - one + zeros)
	print(ans)


N = int(input())
ls = input().split(' ')
maxsum = 0
for i in range(0, N):
	for j in range(i, N):
		tls = ls[:]
		for k in range(i, j + 1):
			if tls[k] == '1':
				tls[k] = '0'
			else:
				tls[k] = '1'
		if tls.count('1') > maxsum:
			maxsum = tls.count('1')
print(maxsum)


n = int(input())
l = [-1 if x == 1 else 1 for x in list(map(int, input().split()))]
tot = 0
for x in l:
	if x == -1:
		tot += 1
max_so_far = float('-inf')
max_ending_here = 0
for i in range(0, n):
	max_ending_here = max_ending_here + l[i]
	if max_so_far < max_ending_here:
		max_so_far = max_ending_here
	if max_ending_here < 0:
		max_ending_here = 0
print(max_so_far + tot)


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

def Flp(w, m):
	global l
	p = 0
	for i in range(w, m):
		if l[i] == 0:
			p += 1
		else:
			p -= 1
	return p
m = 0
o = l.count(1)
for i in range(len(l)):
	for j in range(i + 1, len(l) + 1):
		m = max(m, o + Flp(i, j))
print(m)


n = int(input())
arr = [int(i) for i in input().split()]
print(max((sum(arr) - 2 * sum(arr[i:j]) + j - i for i in range(n) for j in range(i + 1, n + 1))))


n = int(input())
s = [int(s) for s in input().split()]
t = []
for i in range(n):
	p = []
	for r in range(n):
		p.append(s[r])
	for j in range(i, n):
		p[j] = 1 - p[j]
		t.append(sum(p))
print(max(t))


def kadsum(b):
	currsum = b[0]
	globalsum = b[0]
	for i in range(1, len(b)):
		currsum = max(b[i], currsum + b[i])
		if currsum > globalsum:
			globalsum = currsum
	return globalsum
n = int(input())
a = list(map(int, input().split()))
count = 0
b = []
for k in a:
	if k == 1:
		count += 1
		b.append(-1)
	else:
		b.append(1)
print(count + kadsum(b))


num = input()
arr = list(map(int, input().split()))

def zero(arr):
	return arr.count(0) - arr.count(1)

def move(arr):
	i = 0
	j = i + 1
	mx = 0
	while j != len(arr) + 1:
		total = arr.count(1) + zero(arr[i:j])
		if zero(arr[i:j]) >= 0:
			j += 1
		else:
			i += 1
			j = i + 1
		if mx < total:
			mx = total
	return mx
print(move(arr))


import sys
n = int(sys.stdin.readline())
nums = list(map(int, sys.stdin.readline().split(' ')))
arr = []
ones = 0
for i in range(0, n):
	if nums[i] == 1:
		ones += 1
		arr.append(-1)
	else:
		arr.append(1)
dp = [arr[0]]
for i in range(1, n):
	dp.append(max(arr[i] + dp[i - 1], arr[i]))
sys.stdout.write(str(max(dp) + ones) + '\n')


import sys
import math
import bisect

def solve(A):
	n = len(A)
	max_val = 0
	for i in range(n):
		for j in range(i, n):
			for k in range(i, j + 1):
				A[k] = 1 - A[k]
			val = sum(A)
			max_val = max(max_val, val)
			for k in range(i, j + 1):
				A[k] = 1 - A[k]
	return max_val

def main():
	n = int(input())
	A = list(map(int, input().split()))
	ans = solve(A)
	print(ans)
main()


n = int(input())
arr = [int(x) for x in input().split()]
value = 1 - arr[0] * 2
current = 1 - arr[0] * 2
temp = arr[0]
for i in arr[1:]:
	current = max(1 - i * 2, current + 1 - i * 2)
	value = max(value, current)
	temp += i
print(temp + value)


_ = input()
numbers = [int(n) for n in input().split()]

def get_zero_data(numbers):
	zero_data = []
	i = 0
	while i < len(numbers):
		if numbers[i] == 0:
			data = [i, 1, 0]
			i += 1
			while i < len(numbers) and numbers[i] == 0:
				data[1] += 1
				i += 1
			zero_data.append(data)
		i += 1
	return zero_data

def get_best_solution(numbers, zero_data):
	if not zero_data:
		return len(numbers) - 1
	(best_solution, solution) = ([*zero_data[0]], [*zero_data[0]])
	for z_data in zero_data[1:]:
		cur_ones_between = z_data[0] - solution[0] - solution[1] - solution[-1]
		if solution[-1] + cur_ones_between < solution[1]:
			best_solution = max(best_solution, solution, key=lambda s: s[1] - s[-1])[:]
			solution[1] += z_data[1]
			solution[-1] += cur_ones_between
		else:
			best_solution = max(best_solution, solution, key=lambda s: s[1] - s[-1])
			solution = [*z_data]
	best_solution = max(best_solution, solution, key=lambda s: s[1] - s[-1])
	return sum(numbers) + best_solution[1] - best_solution[2]
zero_data = get_zero_data(numbers)
print(get_best_solution(numbers, zero_data))


n = int(input())
arr = list(map(lambda x: -1 if int(x) > 0 else 1, input().split()))
cn_1 = 1 if arr[0] == -1 else 0
(local_optimal, global_optimal) = (arr[0], arr[0])
for idx in range(1, n):
	num = arr[idx]
	local_optimal = max(num, local_optimal + num)
	global_optimal = max(local_optimal, global_optimal)
	if num == -1:
		cn_1 += 1
print(global_optimal + cn_1)


n = int(input())
a = [int(i) for i in input().split()]
print(max((sum(a[:i]) + (j - i) - sum(a[i:j]) + sum(a[j:]) for i in range(n) for j in range(i + 1, n + 1))))


n = int(input())
a = list(map(int, input().split()))
one_count = [0]
one_count.extend(a)
for i in range(1, n + 1):
	one_count[i] += one_count[i - 1]
ans = 0
for i in range(0, n):
	for j in range(i + 1, n + 1):
		one = one_count[j] - one_count[i]
		zero = j - i - one
		ans = max(ans, one_count[-1] + zero - one)
print(ans)


__author__ = 'Walnut'
import math
import copy

def score(s):
	r = int(0)
	for i in s:
		if i == 1:
			r += 1
	return r

def flip(s, i, j):
	t = copy.copy(s)
	for k in range(i, j + 1):
		t[k] = 1 - t[k]
	return t
n = int(input())
line = input().split()
data = []
m = int(0)
for i in line:
	data.append(int(i))
for i in range(n):
	for j in range(i, n):
		tem = score(flip(data, i, j))
		if tem > m:
			m = tem
print(m)


n = int(input())
(x, c, ma) = (0, 0, 0)
l = list(map(int, input().split()))
for i in l:
	if i:
		c += 1
	if not i:
		x += 1
		if x > ma:
			ma = x
	elif x > 0:
		x -= 1
if not ma:
	ma -= 1
print(ma + c)


n = int(input())
a = [int(x) for x in input().split()]
cnt = 0
total_cnt = a[0]
if a[0] == 0:
	cnt = 1
else:
	cnt = -1
max_cnt = cnt
for i in range(1, n):
	if a[i] == 0:
		cnt = max(1, cnt + 1)
	else:
		cnt -= 1
	max_cnt = max(max_cnt, cnt)
	total_cnt += a[i]
print(total_cnt + max_cnt)


n = int(input())
a = list(map(int, input().split()))
print(max((sum(a) - 2 * sum(a[i:j]) + j - i for i in range(n) for j in range(i + 1, n + 1))))


n = int(input())
arr = list(map(int, input().split()))
if n == 1:
	if arr[0] == 0:
		print(1)
	else:
		print(0)
else:
	(one, zero, m) = (0, 0, 0)
	for i in arr:
		if i == 1:
			one += 1
			if zero > 0:
				zero -= 1
		else:
			zero += 1
			if zero > m:
				m = zero
	if one == n:
		print(n - 1)
	else:
		print(m + one)


n = int(input())
I = map(int, input().split())
d = []
for val in I:
	d.append(val)
res = 0
for i in range(0, n):
	for j in range(i, n):
		cnt = 0
		for k in range(0, n):
			if k >= i and k <= j:
				cnt = cnt + 1 - d[k]
			else:
				cnt = cnt + d[k]
		if cnt > res:
			res = cnt
print(res)


n = int(input())
num = [int(n) for n in input().split()]
count = 0
for i in range(n):
	list = num[:]
	for j in range(i, n):
		list[j] = 1 - list[j]
		count = max(sum(list), count)
if n == 1:
	count = 1 - num[0]
print(count)


def suma(i, j):
	if i < 0:
		return l[j]
	else:
		return l[j] - l[i - 1]
n = int(input())
a = input().split()
l = [0]
zbr = 0
for i in range(n):
	if a[i] == '1':
		zbr += 1
	l.append(zbr)
r = []
for i in range(n):
	for j in range(i, n):
		p = l[i]
		s = j - i + 1 - (l[j + 1] - l[i])
		k = l[-1] - l[j + 1]
		r.append(p + s + k)
print(max(r))


n = int(input())
maxm = 0
x = list(map(int, input().split(' ')))
for i in range(n):
	for j in range(i, n, 1):
		y = x.copy()
		for k in range(i, j + 1, 1):
			y[k] = 1 - y[k]
			cnt = y.count(1)
			if maxm < cnt:
				maxm = cnt
print(maxm)


from __future__ import division, print_function
from collections import *
from math import *
from itertools import *
from time import time
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	n = int(input())
	ar = list(map(int, input().split()))
	if n == 1:
		print(1 - ar[0])
		return
	dp = [0] * n
	for i in range(n):
		for j in range(i, n):
			dp[i] = max(dp[i], ar[:i].count(1) + ar[i:j + 1].count(0) + ar[j + 1:].count(1))
	print(max(dp))
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


_ = input()
original = input().split()
l = list(map(lambda x: 1 if x == '0' else -1, original))
max_current = l[0]
max_global = l[0]
for idx in range(1, len(l)):
	max_current = max(l[idx], l[idx] + max_current)
	if max_current > max_global:
		max_global = max_current
print(max_global + original.count('1'))


def kadane(ls):
	n = len(ls)
	sm = 0
	i = 0
	max_so_far = -100000000000000
	sm = 0
	s = 0
	cnt = 0
	e = 0
	ss = 0
	while i < n:
		sm += ls[i]
		if max_so_far <= sm:
			max_so_far = sm
			e = i
			s = ss
		if sm < 0:
			cnt += 1
			sm = 0
			ss = i + 1
		i += 1
	return (s, e)
n = int(input())
ls = list(map(int, input().split()))
ar = []
for i in range(n):
	if ls[i] == 1:
		ar.append(-1)
	else:
		ar.append(1)
(a, b) = kadane(ar)
for i in range(a, b + 1):
	if ls[i] == 1:
		ls[i] = 0
	else:
		ls[i] = 1
cnt = 0
for i in range(n):
	if ls[i]:
		cnt += 1
print(cnt)


n = int(input())
a = [int(x) for x in input().split()]
maximum = 0
ones = 0
if a.count(1) == 0:
	ones = len(a)
elif a.count(0) == 0:
	ones = len(a) - 1
else:
	for i in range(n):
		for j in range(n):
			if a[i:j + 1].count(0) - a[i:j + 1].count(1) > maximum:
				maximum = a[i:j + 1].count(0) - a[i:j + 1].count(1)
				ones = a.count(1) + a[i:j + 1].count(0) - a[i:j + 1].count(1)
print(ones)


a = input()
n = input().split(' ')
n = [int(n[i]) for i in range(len(n))]
max_const = 0
for i in range(len(n)):
	for j in range(i + 1, len(n) + 1):
		arr = n[i:j]
		counting = arr.count(0)
		arr = n[0:i] + n[j:]
		counting += arr.count(1)
		if counting > max_const:
			max_const = counting
print(max_const)


import sys
n = int(input())
alist = list(map(int, sys.stdin.readline().split()))
blist = []
b = 0
for i in alist:
	if i == 1:
		if b >= 0:
			b += 1
		else:
			blist.append(b)
			b = 1
	elif b <= 0:
		b -= 1
	else:
		blist.append(b)
		b = -1
if b:
	blist.append(b)
s = sum([a for a in blist if a > 0])
l = len(blist)
extra = 1
for i in range(l):
	for j in range(i + 1, l + 1):
		ps = sum(blist[i:j])
		extra = min(extra, ps)
print(s - extra)


import sys
input = sys.stdin.readline

def gint():
	return int(input())

def gint_arr():
	return list(map(int, input().split()))

def gfloat():
	return float(input())

def gfloat_arr():
	return list(map(float, input().split()))

def pair_int():
	return map(int, input().split())
INF = 1 << 31
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
n = gint()
a = gint_arr()
s = sum(a)
if s == n:
	print(s - 1)
	exit()
res = s
for i in range(n):
	for j in range(i + 1):
		ones = sum(a[j:i + 1])
		zeroes = i - j + 1 - ones
		res = max(res, s - ones + zeroes)
print(res)


import sys
input = sys.stdin.readline
n = int(input())
l = list(map(int, input().split()))
big = 0
s = 0
c1 = 0
for i in l:
	if i == 1:
		c1 += 1
		s = max(0, s - 1)
	else:
		s += 1
		big = max(big, s)
if c1 == n:
	print(n - 1)
else:
	print(big + c1)


from copy import copy

def change(i, j, arr):
	if i == j:
		arr[i] = 1 - arr[i]
		return sum(arr)
	for p in range(i, j + 1):
		arr[p] = 1 - arr[p]
	return sum(arr)
n = int(input())
arr = list(map(int, input().split()))
mx = 0
for i in range(n):
	for j in range(i, n):
		x = change(i, j, copy(arr))
		if x > mx:
			mx = x
print(mx)


n = int(input())
line = input()
list1 = [int(i) for i in line.split()]
list2 = []
list3 = []
for x in range(0, n):
	for y in range(x, n):
		list3 = list1[x:y + 1]
		b = list3.count(0)
		a = list1.count(1) - list3.count(1) + b
		list2.append(a)
print(max(list2))


n = int(input())
A = [x for x in input().split()]
s = '0' + ''.join(A)
B = []
for i in range(1, n + 1):
	for j in range(i + 1, n + 2):
		p = s[i:j]
		C1 = p.count('1')
		C0 = p.count('0')
		B.append(C0 - C1)
print(s.count('1') + max(B))


n = int(input())
a = [int(i) for i in input().split()]
t = 0
g = 1
s = -1
for i in range(len(a)):
	for j in range(i + 1, len(a) + 1):
		if a[i:j].count(0) - a[i:j].count(1) > s:
			s = a[i:j].count(0) - a[i:j].count(1)
			t = i
			g = j
print(a[t:g].count(0) + a[:t].count(1) + a[g:].count(1))


a = int(input())
a = int(a)
b = input().split(' ')
c = 0
d = 0
e = 0
for i in range(a):
	if b[i] == '0':
		c += 1
	else:
		c -= 1
		c = max(0, c)
		e += 1
	d = max(d, c)
if d == 0:
	print(e - 1)
else:
	print(d + e)


n = int(input())
line = input().split()
num1 = line.count('1')
num = -1
for i in range(n):
	for j in range(i, n):
		a = line[i:j + 1].count('0')
		b = line[i:j + 1].count('1')
		if num < a - b:
			num = a - b
print(num1 + num)


n = int(input())
arr = list(map(int, input().split()))
c = 0
maxc = 0
ic = 0
for e in arr:
	if e == 1:
		ic += 1
for i in range(len(arr)):
	temp = list(arr)
	for j in range(i, len(arr)):
		c = ic
		for k in range(i, j + 1):
			if temp[k] == 0:
				c += 1
			else:
				c -= 1
		maxc = max(maxc, c)
print(maxc)


n = int(input())
l = list(map(int, input().split()))
ll = []
l1 = []
we = 0
cnt = 0
for i in range(n):
	if l[i] == 1:
		cnt -= 1
	else:
		cnt += 1
	ll.append(cnt)
	if cnt < 0:
		cnt = 0
	if l[i] == 1:
		we += 1
print(max(ll) + we)


n = int(input())
nums = list(map(int, input().split()))
b = []
ones = 0
for num in nums:
	if num == 1:
		b.append(-1)
		ones += 1
	else:
		b.append(1)
current_max = ret_max = b[0]
for i in range(1, n):
	current_max = max(b[i], current_max + b[i])
	if current_max > ret_max:
		ret_max = current_max
print(ret_max + ones)


def maxSubArraySum(a, size):
	max_so_far = a[0]
	curr_max = a[0]
	for i in range(1, size):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far
n = int(input())
l = list(map(int, input().split()))
d = [0] * n
for i in range(n):
	if l[i] == 0:
		d[i] = 1
	else:
		d[i] = -1
print(maxSubArraySum(d, len(d)) + sum(l))


import copy
c = 0
a = int(input())
b = [int(i) for i in input().split()]
for i in range(a):
	for j in range(i, a):
		c = max(c, b[i:j + 1].count(0) + b[:i].count(1) + b[j + 1:a].count(1))
print(c)


n = int(input())
a = [int(i) for i in input().split()]
b = []
s = 0
s1 = 0
for i in range(n):
	for j in range(i, n):
		s1 = sum((a[p] for p in range(i)))
		s1 += j - i + 1 - sum((a[p] for p in range(i, j + 1)))
		s1 += sum((a[p] for p in range(j + 1, n)))
		b.append(s1)
print(max(b))


n = int(input())
l = list(map(int, input().split(' ')))
b = list()
count = 0
for i in range(0, n):
	if l[i] == 1:
		count = count + 1
		b.append(-1)
	else:
		b.append(1)
max_ending_here = 0
max = 0
for i in b:
	max_ending_here = max_ending_here + i
	if max_ending_here < 0:
		max_ending_here = 0
	if max_ending_here > max:
		max = max_ending_here
while 1 > 0:
	if n != 1 and count != n:
		print(max + count)
		break
	if n == 1:
		if l[0] == 1:
			print('0')
			break
		else:
			print('1')
			break
	if count == n:
		print(n - 1)
		break


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

def flipping_comprehension(ls):
	return max([sum(ls[:i] + [0 if n == 1 else 1 for n in ls[i:j + i + 1]] + ls[j + i + 1:]) for i in range(len(ls)) for j in range(len(ls[i:]))])
print(flipping_comprehension(ls))


n = int(input())
a = list(map(int, input().split()))
b = []
mx = -1
c = 0
for k in range(0, n):
	b.append(a[k])
for i in range(0, n):
	for j in range(i, n):
		for k in range(i, j + 1):
			if b[k] == 1:
				b[k] = 0
			else:
				b[k] = 1
		c = 0
		for k in range(0, n):
			if b[k] == 1:
				c += 1
		mx = max(mx, c)
		for k in range(0, n):
			b[k] = a[k]
print(mx)


n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
D = [[0] * 3 for i in range(n + 1)]
if n == 1:
	print(1 - a[1])
else:
	for i in range(1, n + 1):
		D[i][0] = D[i - 1][0] + a[i]
		D[i][1] = max(D[i - 1][0], D[i - 1][1]) + (1 - a[i])
		D[i][2] = max(D[i - 1][0], D[i - 1][1], D[i - 1][2]) + a[i]
	if D[n][0] == max(D[n][0], D[n][1], D[n][2]):
		if sum(a) - D[n][0] == 0:
			print(D[n][0] - 1)
	else:
		print(max(D[n][0], D[n][1], D[n][2]))


n = int(input())
sequence = list(map(int, input().split()))
ones = sum(sequence)
if ones == len(sequence):
	print(len(sequence) - 1)
	exit(0)
sequence = [i - 0.5 for i in sequence]
minsum = 0
begin = end = 0
for i in range(0, n):
	s = 0
	for j in range(i, n):
		s += sequence[j]
		if s > 0:
			break
		if s < minsum:
			minsum = s
			(begin, end) = (i, j)
newones = -int(minsum / 0.5)
ones += newones
print(ones)


n = int(input())
m = input().split()
l = []
s = [0]
a = 0
if n == 1:
	if int(m[0]) == 1:
		f = 0
	else:
		f = 1
else:
	for i in range(n):
		if int(m[i]) == 0:
			li = 1
		else:
			a += 1
			li = -1
		l.append(li)
		s0 = sum(l)
		s.append(s0)
	s1 = []
	for j in range(n + 1):
		for k in range(j + 1, n + 1):
			ds = s[k] - s[j]
			s1.append(ds)
	f = max(s1) + a
print(f)


n = int(input())
a = [int(i) for i in input().split()]
f = [[0] * n for i in range(n)]
m = [0] * n
for i in range(n):
	f[i][n - 1] = sum(a[0:i]) + n - i - sum(a[i:n])
	for j in range(n - 2, i - 1, -1):
		f[i][j] += f[i][j + 1] + 1 if a[j + 1] == 1 else f[i][j + 1] - 1
	m[i] = max(f[i])
print(max(m))


n = int(input())
l = list(map(int, input().split()))
j = l.copy()
c = 0
for i in range(len(l)):
	if l[i] == 0:
		l[i] = 1
	else:
		l[i] = -1
max_so_far = l[0]
curr_max = l[0]
for i in range(1, n):
	curr_max = max(l[i], curr_max + l[i])
	max_so_far = max(max_so_far, curr_max)
for i in j:
	if i == 1:
		c += 1
print(c + max_so_far)


def main():
	input()
	s = list(map(int, input().split()))
	m = 0
	for i in range(len(s) + 1):
		for j in range(i + 1, len(s) + 1):
			m = max(m, s[i:j].count(0) + s[0:i].count(1) + s[j:len(s)].count(1))
	print(m)
main()


n = int(input())
list = [int(i) for i in input().split()]
i = 0
ans = []
while i < n:
	j = i
	while j < n:
		temp = list.copy()
		for l in range(i, j + 1):
			temp[l] = 1 - temp[l]
		ans.append(temp.count(1))
		j += 1
	i += 1
print(max(ans))


N = int(input())
n = input().split()
gg = []
for i in range(N):
	for j in range(i, N):
		l = 0
		p = 0
		for s in range(i, j + 1):
			if int(n[s]) == 0:
				l = l + 1
			if int(n[s]) == 1:
				p = p + 1
		gg.append(l - p)
j = max(gg)
f = 0
for i in range(N):
	if int(n[i]) == 1:
		f = f + 1
print(f + j)


from sys import stdin
stdin.readline()
arr = [int(x) for x in stdin.readline().rstrip().split()]
m = [[0 for j in range(len(arr))] for i in range(len(arr))]
s = [[0 for j in range(len(arr))] for i in range(len(arr))]
theMax = -1
for i in range(len(arr)):
	m[i][i] = 1 - arr[i]
	s[i][i] = sum(arr[0:i]) + m[i][i] + sum(arr[i + 1:len(arr)])
	theMax = max(s[i][i], theMax)
for size in range(2, len(arr) + 1):
	for i in range(0, len(arr) - size + 1):
		j = i + size - 1
		m[i][j] = m[i][j - 1] + m[j][j]
		s[i][j] = sum(arr[0:i]) + m[i][j] + sum(arr[j + 1:len(arr)])
		theMax = max(s[i][j], theMax)
print(theMax)


def go():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	m = 0
	for i in range(n):
		for j in range(i, n):
			c = 0
			for k in range(n):
				if i <= k and k <= j:
					x = 1 - a[k]
				else:
					x = a[k]
				if x == 1:
					c += 1
			m = max(m, c)
	return m
print(go())


n = int(input())
l = list(map(int, input().split()))
ma = -9999999999999999999
dp = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
	if l[i] == 0:
		dp[i][i] = 1
	ma = max(ma, dp[i][i] - (1 - dp[i][i]))
	for j in range(i + 1, n):
		dp[i][j] = dp[i][j - 1] + (1 - l[j])
		ma = max(ma, dp[i][j] - (j - i + 1 - dp[i][j]))
ans = l.count(1) + ma
print(ans)


def co(A, a, b):
	j = 0
	for i in range(a, b + 1):
		j += A[i]
	return b - a + 1 - j - j
n = int(input())
A = [int(x) for x in input().split()]
diff = 0
bi = 0
bj = 0
for i in range(n):
	for j in range(i, n):
		X = co(A, i, j)
		if X > diff:
			diff = X
			bi = i
			bj = j
for i in range(bi, bj + 1):
	A[i] = 1 - A[i]
print(sum(A))


from sys import stdin
n = int(stdin.readline())
a = [-1 if int(i) == 1 else 1 for i in stdin.readline().split()]
dp = [0] * (n + 1)
s = 0
for i in range(n):
	s += a[i]
	s = max(s, 0)
	dp[i + 1] = s
x = max(dp)
print(a.count(-1) + x if x > 0 else a.count(-1) - 1)


(n, lst, k, max_0) = (int(input()), list(map(int, input().split())), 0, 0)
if lst.count(1) == n:
	print(n - 1)
else:
	for i in lst:
		k = max(k + (i == 0) - (i == 1), 0)
		max_0 = max(k, max_0)
	else:
		print(lst.count(1) + max_0)


n = int(input())
s = list(map(int, input().split()))
ans = -1
x = 0
for i in range(n):
	if s[i] == 1:
		x -= 1
	else:
		x += 1
	if x > ans:
		ans = x
	if x < 0:
		x = 0
print(ans + s.count(1))


def solve1(array, n):

	def count_ones():
		ret = 0
		for k in range(n):
			if array[k] == 1:
				ret += 1
		return ret

	def generate_subarrays():
		max_num = 0
		for i in range(n):
			for j in range(i, n):
				for k in range(i, j + 1):
					array[k] = 1 - array[k]
				max_num = max(max_num, count_ones())
				for k in range(i, j + 1):
					array[k] = 1 - array[k]
		return max_num
	return generate_subarrays()

def solve2(array, n):
	dp = [[0 for x in range(n)] for y in range(n)]
	(max_ones, num1_i_j) = (0, 0)
	for elm in array:
		num1_i_j += 1 if elm == 1 else 0
	for i in range(n):
		if array[i] == 0:
			dp[i][i] = num1_i_j + 1
		else:
			dp[i][i] = num1_i_j - 1
		max_ones = dp[i][i] if dp[i][i] > max_ones else max_ones
		for j in range(i + 1, n):
			if j > 0 and array[j] == 0:
				dp[i][j] = dp[i][j - 1] + 1
			else:
				dp[i][j] = dp[i][j - 1] - 1
			max_ones = dp[i][j] if dp[i][j] > max_ones else max_ones
	return max_ones
n = int(input())
array = list(map(int, input().split(' ')))
print(solve2(array, n))


__author__ = 'Utena'
n = int(input())
s = list(map(int, input().split()))
t = -1
for i in range(n):
	for j in range(i, n):
		x = len(s[i:j + 1]) - 2 * sum(s[i:j + 1])
		if x > t:
			t = x
print(sum(s) + t)


n = int(input())
l = list(map(int, input().split()))
mx = l.count(1)
mx1 = mx
mx2 = mx
for i in range(0, n):
	a = 0
	b = 0
	mx1 = mx2
	for j in range(i, n):
		if l[j] == 0:
			a = a + 1
		else:
			b = b + 1
			mx1 = mx1 - 1
		mx = max(a + mx1, mx)
if n == 1:
	if l[0] == 1:
		mx = 0
	else:
		mx = 1
if mx == mx2:
	mx = mx - 1
print(mx)


n = int(input())
A = list(map(int, input().split()))
B = A[:]
max = 0
for i in range(n):
	for k in range(n):
		for z in range(i, k + 1):
			if A[z] == 1:
				A[z] = 0
			else:
				A[z] = 1
			if A.count(1) > max:
				max = A.count(1)
		A = B[:]
print(max)


n = int(input())
a = list(map(int, input().split()))
inverse_dp = [0 for _ in range(n)]
inverse_dp[0] = a[0]
for i in range(n):
	inverse_dp[i] = inverse_dp[i - 1] + (a[i] == 1)
dp = [[0 for _ in range(n)] for _ in range(n)]
ldp = 0
for i in range(n):
	dp[i][i] = int(a[i] == 0)
for start_idex in range(n):
	for end_index in range(start_idex + 1, n):
		dp[start_idex][end_index] = dp[start_idex][end_index - 1] + (a[end_index] == 0)
for start_idex in range(n):
	for end_index in range(start_idex, n):
		temp_dp = dp[start_idex][end_index]
		if start_idex >= 1:
			temp_dp += inverse_dp[start_idex - 1]
		if end_index < n - 1:
			temp_dp += inverse_dp[n - 1] - inverse_dp[end_index]
		ldp = max(ldp, temp_dp)
print(ldp)


from sys import maxsize
n = int(input())
a = [int(ai) for ai in input().split()]
ones_count = 0
for i in range(n):
	if a[i] == 0:
		a[i] = 1
	else:
		a[i] = -1
		ones_count += 1
max_so_far = -maxsize - 1
max_ending_here = 0
start = 0
end = 0
S = 0
for i in range(n):
	max_ending_here += a[i]
	if max_so_far < max_ending_here:
		max_so_far = max_ending_here
		start = S
		end = i
	if max_ending_here < 0:
		max_ending_here = 0
		S = i + 1
print(ones_count + max_so_far)


n = int(input())
array = list(map(int, input().split()))
numOnes = sum(array)
if numOnes == n:
	print(n - 1)
else:
	one = 0
	zero = 0
	maxi = 0
	for i in range(n):
		if array[i] == 0:
			zero += 1
			if one >= zero:
				zero = 1
				one = 0
			else:
				diff = zero - one
				if maxi < diff:
					maxi = diff
		else:
			one += 1
	print(numOnes + maxi)


n = int(input())
a = input().split()
b = [0] * n
for i in range(len(a)):
	a[i] = int(a[i])
	if i > 0:
		b[i] = b[i - 1] + a[i]
	else:
		b[i] = a[i]
maxs = 0
for i in range(n):
	for j in range(i, n):
		s = 0
		for k in range(i):
			s += a[k]
		for k in range(i, j + 1):
			s += 1 - a[k]
		for k in range(j + 1, n):
			s += a[k]
		if s > maxs:
			maxs = s
print(maxs)


n = int(input())
vls = [int(x) for x in input().split(' ')]
longest = 0
for i in range(n):
	for j in range(i, n):
		a = [1 - vls[x] if j >= x >= i else vls[x] for x in range(n)]
		longest = max(longest, len(list(filter(lambda x: x == 1, a))))
print(longest)


a = int(input())
s = 0
arr = [int(i) for i in input().split()]
for i in range(a):
	for k in range(i, a):
		test = arr[:i] + list(map(lambda x: 1 - x, arr[i:k + 1])) + arr[k + 1:]
		s = max(s, test.count(1))
print(s)


t = int(input())
a = list(map(int, input().split()))
final = 0
if len(a) == 1:
	print(0) if a[0] == 1 else print(1)
else:
	pre = 0
	for (k, i) in enumerate(a):
		pre = a[:k].count(1)
		for j in range(k + 1, len(a)):
			suff = a[j + 1:].count(1)
			result = a[k:j + 1].count(0) + pre + suff
			if final is None or final < result:
				final = result
	if pre > final:
		final = pre
	print(final)


n = int(input())
nums = list(map(int, input().split()))
max_sum = 0
one = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		nums2 = nums[:]
		for z in range(i, j):
			nums2[z] = 1 - nums[z]
		if sum(nums2) > max_sum:
			max_sum = sum(nums2)
print(max_sum)


n = int(input())
s = [int(i) for i in input().split()]
li = []
if n == 1 and int(s[0]) == 1:
	print(0)
	exit()
elif n == 1 and int(s[0]) == 0:
	print(1)
	exit()
c = s.count(1)
d = s.count(0)
for m in range(1, n + 1):
	for i in range(1, n - m + 2):
		q = [s[r - 1] for r in range(m, m + i)]
		a = q.count(1)
		b = q.count(0)
		p = c - a + b
		li.append(p)
t = max(li)
print(t)


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	n = int(input())
	arr = array()
	ans = 0
	o = arr.count(1)
	z = arr.count(0)
	ans = 0
	for i in range(n):
		one = 0
		zero = 0
		for j in range(i, n):
			if arr[j] == 0:
				zero += 1
			else:
				one += 1
			rem = o - one
			if rem + zero > ans:
				ans = rem + zero
	print(ans)
t = 1
for _ in range(t):
	main()


n = int(input())
i = 0
o = 0
b = False
van = False
m = 0
inp = 0
e = 0
t = list(map(int, input().split()))
for i in range(0, n):
	if t[i] == 1:
		e += 1
	if t[i] == 0:
		van = True
		o += 1
		if b == False:
			b = True
	elif b == True and t[i] == 1:
		o -= 1
	if b == True and o < 0:
		o = 0
		b = False
	if o > m:
		m = o
if van == False:
	m = -1
print(m + e)


n = int(input())
x = list(map(int, input().split()))
mx = 0
cunt = x.count(1)
if cunt == n:
	print(n - 1)
elif cunt == 0:
	print(n)
else:
	for i in range(2, n):
		for j in range(0, n - i + 1):
			meh = x[j:j + i].count(0) * 2 - i
			if meh > mx:
				mx = meh
	print(mx + cunt)


n = int(input())
kamene = list(map(int, input().split()))
(prefixy, suffixy) = ([0], [0])
for i in range(n):
	prefixy.append(prefixy[-1] + (-1) ** kamene[i])
	suffixy.append(suffixy[-1] + (-1) ** kamene[-i - 1])
suffixy.reverse()
(nul, jednotiek) = (kamene.count(0), kamene.count(1))
c_delta = nul - jednotiek
maxi = 0
for i in range(n):
	for j in range(n, i, -1):
		delta = c_delta - (prefixy[i] + suffixy[j])
		maxi = max(maxi, jednotiek + delta)
print(maxi)


N = int(input())
a = list(map(int, input().split()))
num = a.count(1)
ans = 0
for i in range(N):
	nums = num
	if a[i] == 0:
		nums += 1
	else:
		nums -= 1
	ans = max(ans, nums)
	for j in range(i + 1, N):
		if a[j] == 0:
			nums += 1
		else:
			nums -= 1
		ans = max(ans, nums)
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = 0
for i in range(n):
	p = a[:]
	for j in range(i, n):
		if p[j] == 1:
			p[j] = 0
		else:
			p[j] = 1
		q = p.count(1)
		s = max(s, q)
print(s)


n = int(input())
L = [int(X) for X in input().split()]
previous = 0
for i in range(n):
	for j in range(i, n):
		ones = 0
		for X in L[i:j + 1]:
			if X == 0:
				ones += 1
		for X in L[:i] + L[j + 1:]:
			if X == 1:
				ones += 1
		if previous < ones:
			previous = ones
print(previous)


n = int(input())
pieces = input().split()
A = []
for i in range(n):
	A.append(int(pieces[i]))
B = []
for i in range(n):
	for j in range(i + 1):
		S = 0
		for m in range(j, i + 1):
			if A[m] == 0:
				S += 1
			if A[m] == 1:
				S -= 1
		B.append(S)
flip = sum(A) + max(B)
print(flip)


p = int(input())
n = list(map(int, input().split()))
a = n.count(1)
m = [(-1) ** i for i in n]
s = -100
for i in range(p):
	for j in range(i, p):
		q = sum(m[i:j + 1])
		if q > s:
			s = q
print(s + a)


n = int(input(''))
arr = list(map(int, input().split()))
sol = 0
ini1 = sol
dp = [0] * n
dp1 = [0] * n
dp[0] = arr[0]
dp1[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
	dp1[i] = dp1[i + 1] + arr[i]
for i in range(1, n):
	dp[i] = dp[i - 1] + arr[i]
for i in range(n):
	for j in range(i, n):
		cnt = 0
		tst = arr.copy()
		for k in range(i, j + 1):
			tst[k] = 1 - tst[k]
			if tst[k]:
				cnt += 1
		if i > 0:
			cnt += dp[i - 1]
		if j < n - 1:
			cnt += dp1[j + 1]
		sol = max(sol, cnt)
print(sol)


n = int(input())
a = []
list1 = [int(i) for i in input().split()]
for i in range(n):
	for k in range(n - i):
		list2 = list1[i:i + k + 1]
		one = sum(list2)
		zero = len(list2) - one
		p = zero - one
		a.append(p)
print(sum(list1) + max(a))


import math
import sys
import collections
input = sys.stdin.readline

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = prime[1] = False
	r = [p for p in range(n + 1) if prime[p]]
	return r

def pow(x, y, mod):
	r = 1
	x = x % mod
	while y > 0:
		if y & 1:
			r = r * x % mod
		y = y >> 1
		x = x * x % mod
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))
n = ii()
d = li()
ps = [0]
for i in d:
	ps.append(ps[-1] + i)
res = 0
for i in range(n):
	for j in range(i, n):
		res = max(res, 2 * (ps[i] - ps[j + 1]) + ps[n] + j - i + 1)
print(res)


n = int(input())
a = list(map(int, input().split(' ')))
s = 0
for i in range(0, n):
	for j in range(i, n):
		t = 0
		for k in range(0, n):
			t += a[k] ^ (k >= i and k <= j)
		s = max(s, t)
print(s)


from sys import stdin
n = int(stdin.readline())
nlist = list(map(int, stdin.readline().split()))
templist = nlist
n = len(nlist)

def count(l):
	n = len(l)
	counter = 0
	for i in range(n):
		if l[i] == 1:
			counter += 1
	return counter

def flip(l, a, b):
	temp = l.copy()
	for i in range(a, b):
		temp[i] = (l[i] + 1) % 2
	return temp
maxones = 0
onesct = 0
for i in range(n + 1):
	for j in range(i):
		templist = flip(nlist, j, i).copy()
		onesct = count(templist)
		if onesct > maxones:
			maxones = onesct
print(maxones)


class CodeforcesTask327ASolution:

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

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

	def process_task(self):
		mx = 0
		base = sum(self.a)
		for x in range(self.n):
			for y in range(x + 1, self.n + 1):
				n = sum(self.a[x:y])
				nb = base - n + (y - x - n)
				mx = max(mx, nb)
		self.result = str(mx)

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


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
n = inp()
A = inlt()
pre = []
prev = 0
for i in range(n):
	if A[i] == 1:
		prev -= 1
	else:
		prev += 1
	pre.append(prev)
best_j = 0
best_k = 0
maxx = -2
for j in range(n):
	for k in range(j, n, 1):
		if j == 0:
			if pre[k] > maxx:
				best_k = k
				best_j = j
				maxx = pre[k]
		elif pre[k] - pre[j - 1] > maxx:
			best_k = k
			best_j = j
			maxx = pre[k] - pre[j - 1]
out = sum(A)
c = sum(A[best_j:best_k + 1])
out += best_k - best_j + 1 - c - c
print(out)


n = int(input())
f = list(map(int, input().split()))
sums = [0]
for i in range(n):
	sums.append(sums[i] + f[i])
max_p = 0
del sums[0]
for i in range(n):
	for j in range(i, n):
		len_p = j - i + 1
		if i == 0:
			k = abs(j + 1 - sums[j]) + sums[-1] - sums[j]
			u = 0
		else:
			u = sums[i - 1] + sums[-1] - sums[j]
			k = abs(len_p - sums[j] + sums[i - 1])
		max_p = max(max_p, k + u)
print(max_p)


n = int(input())
a = list(map(int, input().split()))
one = 0
for x in a:
	one += x
ans = 0
for i in range(n):
	for j in range(i + 1):
		(cnt0, cnt1) = (0, 0)
		for t in range(j, i + 1):
			cnt0 += a[t] == 0
			cnt1 += a[t]
		ans = max(ans, one + cnt0 - cnt1)
print(ans)


def get_gain(s):
	gain = s.count('0')
	gain -= s.count('1')
	return gain

def revert(s, startIndex, endIndex):
	arr = list(s)
	for i in range(startIndex, endIndex + 1):
		c = arr[i]
		if c == '1':
			arr[i] = '0'
		else:
			arr[i] = '1'
	return arr
input()
inp = input()
inp = inp.replace(' ', '')
if inp.count('0') > 0:
	fz_i = inp.index('0')
	gs = {}

	def calc(inp, fz_i):
		for i in range(fz_i, len(inp)):
			cut = inp[fz_i:i + 1]
			g = get_gain(cut)
			if g not in gs.keys():
				gs[g] = []
			gs[g].append(revert(inp, fz_i, i))
	calc(inp, fz_i)
	while fz_i < len(inp):
		fz_i += 1
		calc(inp, fz_i)
	mx = max(gs.keys())
	max_ones = -19
	for s in gs[mx]:
		max_ones = max(max_ones, s.count('1'))
	print(max_ones)
else:
	print(inp.count('1') - 1)


n = int(input())
li = [int(x) for x in input().split()]
zeros = [0 for _ in range(n)]

def subsum(li):
	s = 0
	m_s = float('-inf')
	for i in range(len(li)):
		s += li[i]
		if s < 0:
			s = 0
		if s > m_s:
			m_s = s
	return m_s
if all(li):
	print(n - 1)
else:
	for i in range(len(li)):
		if li[i] == 1:
			zeros[i] = -1
		else:
			zeros[i] = 1
	print(len(list(filter(lambda x: x == 1, li))) + subsum(zeros))


n = int(input())
a = [int(i) for i in input().split()]
m = 0
for i in range(n):
	for j in range(i, n):
		b = a[:]
		for k in range(i, j + 1):
			b[k] = 1 - a[k]
		c = sum(b)
		if c > m:
			m = c
print(m)


n = int(input())
a = [int(i) for i in input().split()]
s = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		s = max(s, sum(a[:i] + a[j:]) + sum(map(lambda x: 1 - x, a[i:j])))
print(s)


n = int(input())
arr = list(map(int, input().split()))
count1 = 0
for i in range(len(arr)):
	if arr[i] == 1:
		count1 += 1
		arr[i] = -1
	else:
		arr[i] = 1
maxdiff = arr[0]
currdiff = 0
for i in range(len(arr)):
	currdiff += arr[i]
	if currdiff > maxdiff:
		maxdiff = currdiff
	if currdiff < 0:
		currdiff = 0
print(count1 + maxdiff)


n = int(input())
num_lst = [int(i) for i in input().split()]
output = []
if num_lst == [1]:
	print(0)
elif num_lst == [0]:
	print(1)
else:
	for i in range(n):
		for j in range(i + 1, n + 1):
			total = 0
			lst1 = num_lst[:i]
			lst2 = num_lst[i:j]
			lst3 = num_lst[j:]
			total += lst1.count(1) + lst2.count(0) + lst3.count(1)
			output.append(total)
	print(max(output))


def fun(t, i, j, temp):
	for k in range(i, j + 1):
		temp += -1 if 1 - t[k] == 0 else 1
	return temp
n = int(input())
t = [int(x) for x in input().split()]
slon = -100
temp = t.count(1)
for i in range(n):
	for j in range(i, n):
		slon = max(slon, fun(t, i, j, temp))
print(slon)


n = int(input())
arr = input().split()
Max = 0
for i in range(len(arr)):
	for j in range(i, len(arr)):
		newArr = []
		for k in range(len(arr)):
			if i <= k <= j:
				newArr.append(1 - int(arr[k]))
			else:
				newArr.append(int(arr[k]))
		Max = max(sum(newArr), Max)
print(Max)


n = int(input())
a = map(int, input().split())
b = list(a)
c = b[:]
out = b.count(1)
if n == 1:
	print(b.count(0))
elif out == n:
	print(n - 1)
else:
	for i in range(n):
		for j in range(i, n):
			if b[j] == 0:
				b[j] = 1
			else:
				b[j] = 0
			e = b.count(1)
			if e > out:
				out = e
		b = c[:]
	print(out)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = LI()
	r = 0
	for i in range(n):
		for j in range(i, n):
			t = 0
			for k in range(n):
				if i <= k <= j:
					t += 1 - a[k]
				else:
					t += a[k]
			if r < t:
				r = t
	return r
print(main())


import sys
n = int(input())
x = [int(x) for x in input().split()]
q = []

def check(l, i, j):
	count1 = 0
	count0 = 0
	count = 0
	for k in range(i, j + 1):
		if l[k] == 1:
			count1 = count1 + 1
		else:
			count0 = count0 + 1
	for k in range(i):
		if l[k] == 1:
			count = count + 1
	for k in range(j + 1, len(l)):
		if l[k] == 1:
			count = count + 1
	return count0 + count
if x == [1] * n:
	print(n - 1)
	sys.exit(0)
if len(x) != 1:
	for i in range(len(x)):
		for j in range(i + 1, len(x)):
			q.append(check(x, i, j))
	print(max(q))
else:
	print(1 - x[0])


n = int(input())
a = list(map(int, input().split()))
st = 0
b = []
for i in range(0, n):
	if a[i] == 0:
		b.append(1)
	else:
		b.append(-1)
		st += 1
c = [0]
for i in range(0, n):
	c.append(c[-1] + b[i])
min = c[0]
d = [min]
for i in range(1, n + 1):
	if c[i] < min:
		min = c[i]
	d.append(min)
max = c[1] - d[1]
for i in range(1, n + 1):
	if c[i] - d[i] > max:
		max = c[i] - d[i]
if st == n:
	print(n - 1)
else:
	print(st + max)


n = int(input())
arr = input().split()
score = arr.count('1')
if '1' not in arr:
	print(n)
elif '0' in arr:
	start = arr.index('0')
	end = n - 1 - arr[::-1].index('0')
	diff = 0
	for i in range(start, end):
		for j in range(start + 1, end + 1):
			temp = arr[i:j + 1]
			zeroes = temp.count('0')
			ones = temp.count('1')
			if zeroes > ones and zeroes - ones > diff:
				diff = zeroes - ones
	print(diff + score)
else:
	print(n - 1)


n = int(input())
arr = [int(x) for x in input().split(' ')]
arr2 = []
for item in arr:
	if item:
		arr2.append(-1)
	else:
		arr2.append(1)
(t, ans) = (-99999, -1)
for x in range(n):
	t = max(arr2[x], arr2[x] + t)
	ans = max(ans, t)
print(arr.count(1) + ans)


I0 = lambda : map(int, input().split())
I1 = lambda : int(input())
I2 = lambda : list(map(int, input().split()))
n = I1()
a = I2()
M = -1
one = a.count(1)
for i in range(n):
	for j in range(i, n):
		zero = a[i:j + 1].count(0)
		one1 = j + 1 - i - zero
		M = max(M, one - one1 + zero)
print(M)


n = int(input())
a = list(map(int, input().split()))
b = [0]
for i in range(n):
	b.append(b[i] + a[i])
result = -1
for l in range(n):
	for r in range(l + 1, n + 1):
		result = max(result, b[n] - b[r] + r - l - (b[r] - b[l]) + b[l])
print(result)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nlargest, nsmallest, _heapify_max, _heapreplace_max
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
n = int(data())
arr = l()
answer = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		temp = arr[:i].count(1) + arr[i:j].count(0) + arr[j:].count(1)
		answer = max(answer, temp)
out(answer)


a = int(input())
b = [int(x) for x in input().split()]
count = 0
answer = 0
highest = 0
if 0 in b:
	for x in range(len(b)):
		if b[x] == 0:
			count += 1
		else:
			count -= 1
			if count < 0:
				count = 0
		if count > answer:
			answer = int(count)
			highest = x
	count = 0
	for x in range(len(b) - 1, -1, -1):
		if answer > 0 and x <= highest:
			if b[x] == 0:
				count += 1
				answer -= 1
		if b[x] == 1:
			count += 1
	print(count)
else:
	print(len(b) - 1)


n = int(input())
q = input().split(' ')
m = 0
for x in range(n):
	q[x] = int(q[x])
for i in range(n):
	for j in range(i, n):
		l = q[:]
		for k in range(i, j + 1):
			if l[k] == 0:
				l[k] = 1
			else:
				l[k] = 0
			if l.count(1) > m:
				m = l.count(1)
print(m)


n = int(input())
a = input().split()
b = {}

def skaicius(i, j):
	return a[i:j].count('0') - a[i:j].count('1')

def maksimumas(i, j):
	if (i, j) in b:
		return b[i, j]
	if j == i + 1 and a[i] == '0':
		return 1
	elif j == i + 1 and a[i] == '1':
		return 0
	else:
		b[i, j - 1] = maksimumas(i, j - 1)
		b[i + 1, j] = maksimumas(i + 1, j)
		return max(maksimumas(i + 1, j), maksimumas(i, j - 1), skaicius(i, j))
if a.count('0') != 0:
	print(a.count('1') + maksimumas(0, n))
else:
	print(a.count('1') - 1)


n = int(input())
A = [int(i) for i in input().split()]
ones = 0
zeros = 0
max_zeros = -1
for num in A:
	if num == 1:
		ones += 1
		if zeros > 0:
			zeros -= 1
	else:
		zeros += 1
		max_zeros = max(max_zeros, zeros)
print(ones + max_zeros)


n = int(input())
l = [int(i) for i in input().split()]
a = l[:]
r = 0
if n == 1 and l == [1]:
	print(0)
else:
	m = [1] * n ** 2
	for k in range(0, n):
		for t in range(k, n):
			for s in range(k, t + 1):
				a[s] = 1 - l[s]
			m[r] = a.count(1)
			r = r + 1
		a = l[:]
	print(max(m))


n = int(input())
list1 = list(map(int, input().split()))
max = 0
if 0 not in list1:
	print(len(list1) - 1)
else:
	for i in range(n):
		for j in range(i, n):
			if list1[0:i].count(1) + list1[i:j].count(0) + list1[j:].count(1) >= max:
				max = list1[0:i].count(1) + list1[i:j].count(0) + list1[j:].count(1)
			if list1[0:i].count(1) + list1[i:].count(0) >= max:
				max = list1[0:i].count(1) + list1[i:].count(0)
	print(max)


def reverse_game(A):
	change_max_value = float('-inf')
	for i in range(len(A)):
		try_max = 0
		for j in range(i, len(A)):
			try_max += (-1) ** A[j]
			change_max_value = try_max if try_max > change_max_value else change_max_value
	return change_max_value
n = int(input())
A = [int(i) for i in input().split()]
value = 0
for i in A:
	value += i
print(value + reverse_game(A))


from sys import stdin
n = int(stdin.readline())
arr = [int(x) for x in stdin.readline().split()]
new_arr = [1 - x * 2 for x in arr]
ans = -2
ma = 0
for i in new_arr:
	ma += i
	if ma > ans:
		ans = ma
	if ma < 0:
		ma = 0
print(arr.count(1) + ans)


def kad(a):
	curr = gb = a[0]
	for i in range(1, len(a)):
		curr = max(a[i], a[i] + curr)
		gb = max(curr, gb)
	return gb
n = int(input())
arr = list(map(int, input().split()))
b = [-1 if i else 1 for i in arr]
print(sum(arr) + kad(b))


def hey():
	n = int(input())
	ls = [-1 if int(x) == 1 else 1 for x in input().split()]
	init = ls.count(-1)
	if init == n:
		print(n - 1)
		return
	maxContr = 0
	curr = 0
	for i in range(n):
		curr = ls[i]
		maxContr = max(maxContr, curr)
		for j in range(i + 1, n):
			curr += ls[j]
			maxContr = max(maxContr, curr)
	print(maxContr + init)
hey()


n = int(input())
a = [int(i) for i in input().split()]
max = 0
for i in range(0, n):
	for b in range(0, n - i):
		SUM = sum(a[0:i]) + (b + 1) - sum(a[i:i + b + 1]) + sum(a[i + b + 1:])
		if SUM >= max:
			max = SUM
print(max)


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))

def max_subarray():
	summ = 0
	best_start = best_end = start = end = 0
	current_sum = 0
	for i in range(n):
		if current_sum <= 0:
			start = i
			current_sum = d[a[i]]
		else:
			current_sum += d[a[i]]
		if current_sum > summ:
			summ = current_sum
			best_start = start
			best_end = i
	return (best_start, best_end)
(n,) = mp()
a = mp()
d = {0: 1, 1: -1}
if sum(a) == n:
	cout(str(n - 1))
	exit(0)
(i, j) = max_subarray()
ans = a[:i].count(1) + a[j + 1:].count(1) + a[i:j + 1].count(0)
cout(str(ans))


n = int(input())
a = input()
A = list(map(int, list(a.split())))
c = 0
for i in range(n):
	if A[i] == 1:
		A[i] = -1
		c = c + 1
	else:
		A[i] = 1
table = [0] * n
table[0] = A[0]
for i in range(1, n):
	table[i] = max(A[i], table[i - 1] + A[i])
print(max(table) + c)


n = int(input())
a = [int(j) for j in input().split()]
b = []
count = 0
for i in range(n):
	if a[i] == 0:
		b.append(1)
	else:
		count += 1
		b.append(-1)
(sum, max) = (0, -2)
i = 0
while i < n:
	sum = sum + b[i]
	if sum > max:
		max = sum
	if sum < 0:
		sum = 0
	i += 1
print(max + count)


n = int(input())
nums = list(map(int, input().split()))
sim = [None] * n
k = sum(nums)
max_sim = 0
for (ind, num) in enumerate(nums):
	if ind == 0:
		sim[0] = k - num + (1 - num)
	else:
		sim[ind] = max(k - num + (1 - num), sim[ind - 1] - num + (1 - num))
	max_sim = max(max_sim, sim[ind])
print(max_sim)


n = int(input())
lst = map(int, input().split(' '))
a = 0
b = -1
t = 0
for i in lst:
	if i == 1:
		t += 1
		if a > 0:
			a -= 1
	else:
		a += 1
		if a > b:
			b = a
print(t + b)


n = int(input())
arr = list(map(int, input().split()))
left = [0 for x in range(0, n + 1)]
total = 0
for i in range(n):
	left[i + 1] = left[i] + arr[i]
	total = total + arr[i]
dp = [[0 for x in range(n)] for y in range(n)]
ans = 0
for i in range(n):
	for j in range(i, n):
		local = total + j - i + 1 - 2 * (left[j + 1] - left[i])
		if local > ans:
			ans = local
print(ans)


num_integers = int(input())
numbers = list(map(int, input().split()))
num_ones = 0
max_num_zeros_to_change = 0
num_zeros_to_change = 0
for number in numbers:
	if number == 1:
		num_ones += 1
		num_zeros_to_change = max(0, num_zeros_to_change - 1)
	else:
		num_zeros_to_change += 1
		max_num_zeros_to_change = max(max_num_zeros_to_change, num_zeros_to_change)
if max_num_zeros_to_change != 0:
	print(num_ones + max_num_zeros_to_change)
else:
	print(num_integers - 1)


num = int(input())
li = [int(i) for i in input().split()]
maxi = 0
for i in range(num):
	count = li.count(1)
	for j in range(i, num):
		if li[j] == 1:
			count -= 1
		else:
			count += 1
		if count > maxi:
			maxi = count
print(maxi)


n = int(input())
a = [int(i) for i in input().split()]
h = k = x = 0
p = q = 0
for i in range(n):
	if a[i] == 0:
		for j in range(i, n):
			if a[j]:
				k += 1
			else:
				h += 1
			if h - k > x:
				x = h - k
				p = i
				q = j
	h = k = 0
for i in range(p, q + 1):
	if a[i]:
		a[i] = 0
	else:
		a[i] = 1
x = 0
for i in range(n):
	if a[i]:
		x += 1
print(x)


n = int(input())
coins = input()
coins = coins.split()

def flip(x):
	if x == '0':
		x = '1'
	else:
		x = '0'
	return x
L = []
for i in range(n):
	for j in range(i + 1, n + 1):
		for k in range(i, j):
			coins[k] = flip(coins[k])
		L.append(coins.count('1'))
		for k in range(i, j):
			coins[k] = flip(coins[k])
print(max(L))


import copy
n = int(input())
ip = list(map(int, input().split()))
a = []
for i in range(n):
	for j in range(i, n):
		d = copy.deepcopy(ip)
		flag = False
		for k in range(i, j + 1):
			flag = True
			if d[k] == 0:
				d[k] = 1
			else:
				d[k] = 0
		if flag == True:
			res = d.count(1)
			a.append(res)
print(max(a))


n = int(input())
a = input().split()
for i in range(len(a)):
	a[i] = int(a[i])
ss = a.count(1)
st = 0
sm = 0
ch1 = 0
ch0 = 0
for i in range(len(a)):
	if a[i] == 0:
		ch0 += 1
	else:
		ch1 += 1
	st = ch0 + ss - ch1
	sm = max(sm, st)
	if ch0 < ch1:
		ch0 = 0
		ch1 = 0
print(sm)


n = int(input())
sta = [int(i) for i in input().split()]
ms = []
base = 0
for i in sta:
	base += i
for a in range(n):
	m = 0
	ml = 0
	con = 0
	for i in sta[a:n]:
		if i == 0:
			m += 1
		else:
			m -= 1
		if con == 0:
			ml = m
			con = 1
			continue
		if m > ml:
			ml = m
	ms.append(ml + base)
print(max(ms))


__author__ = 'Bian'
n = int(input())
cards = [int(x) for x in input().split()]
if sum(cards) == n:
	print(n - 1)
	exit()
last = [0] * n
last[0] = max(0, (0.5 - cards[0]) * 2)
for i in range(1, n):
	last[i] = max(0, last[i - 1] + (0.5 - cards[i]) * 2)
print(int(sum(cards) + max(last)))


n = int(input())
num = list(map(int, input().split()))
(one, cur, lcs) = (0, 0, 0)
for i in num:
	if i == 0:
		cur += 1
		lcs = max(lcs, cur)
	else:
		one += 1
		cur = max(cur - 1, 0)
print(one + lcs if one != n else one - 1)


n = int(input())
l = [int(i) for i in input().split(' ')]
ans = 0
for i in range(n):
	for j in range(i, n):
		m = l[:]
		for k in range(i, j + 1):
			m[k] = 1 - m[k]
		ans = max(ans, m.count(1))
print(ans)


n = int(input())
a = list([int(s) for s in input().split()])
m = []
for i in range(n):
	for j in range(i, n + 1):
		b = a[:]
		a_new = b[i:j]
		for (index, s) in enumerate(a_new):
			if s == 1:
				a_new[index] = 0
			elif s == 0:
				a_new[index] = 1
			b[i:j] = a_new
			m.append(b)
otvet = max((s.count(1) for s in m))
print(otvet)


n = int(input())
x = list(map(int, input().split()))
a = []
count = 0
for i in range(n):
	if x[i] == 1:
		a.append(-1)
		count += 1
	else:
		a.append(1)
m = -2
s = 0
for i in range(n):
	s += a[i]
	if s > m:
		m = s
	if s < 0:
		s = 0
print(m + count)


n = int(input())
line = [int(j) for j in input().split()]
if n == 1:
	if line[0] == 1:
		print(0)
	else:
		print(1)
else:
	sum = 0
	for i in range(n):
		sum += line[i]
	res = []
	k = 0
	max1 = sum
	while k < n:
		max_ = sum
		for j in range(k, n):
			s = sum
			for t in range(k, j + 1):
				if line[t] == 1:
					s -= 1
				else:
					s += 1
			if s > max_:
				max_ = s
		if max_ > max1:
			max1 = max_
		k += 1
	if sum == n:
		print(sum - 1)
	else:
		print(max1)


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			high = mid
		else:
			low = mid + 1
	return low

def check(time, m, assistants):
	summ = 0
	for p in assistants:
		time_needed_with_rest = p[0] * p[1] + p[2]
		time_needed_without_rest = p[0]
		baloons_can_be_inflated = p[1]
		summ += time // time_needed_with_rest * baloons_can_be_inflated
		ntime = time % time_needed_with_rest
		summ += min(ntime // time_needed_without_rest, baloons_can_be_inflated)
	return summ >= m
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys

def solve():
	n = int(input())
	positions = list(read())
	m = 0
	ones = positions.count(1)
	zeroes = n - ones
	if zeroes == 0:
		print(n - 1)
		return
	for i in range(n):
		for j in range(i, n):
			m = max(m, max(positions[i:j + 1].count(1), positions[i:j + 1].count(0)) + ones - positions[i:j + 1].count(1))
	print(m)
for _ in range(1):
	solve()


num = eval(input())
data = [eval(i) for i in input().split()]
count = data.count(1)
add = -100
for i in range(num):
	for j in range(i + 1, num + 1):
		k = data[i:j].count(0) - data[i:j].count(1)
		add = max(add, k)
print(count + add)


n = int(input())
b = list(map(int, input().split()))
a = [0] * (n + 2)
for i in range(n):
	a[i + 1] = b[i]
dp = [0] * (n + 2)
for i in range(1, n + 1):
	if a[i] == 1:
		dp[i] = max(dp[i - 1] - 1, 0)
	else:
		dp[i] = dp[i - 1] + 1
m = max(dp)
m = max(m, 0)
if m == 0:
	print(b.count(1) - 1)
else:
	print(b.count(1) + m)


n = int(input())
l = [int(i) for i in input().split()]
a = 0
d = 0
if n == 1:
	print(1 - l[0])
elif l == n * [1]:
	print(n - 1)
else:
	for i in range(n):
		if l[i] == 0:
			for j in range(n - i):
				j = j + 1
				a = max(a, j - 2 * sum((i for i in l[i:i + j])))
	d = max(d, a)
	print(sum(l) + d)


n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(n):
	for j in range(i, n):
		b = a[:]
		for u in range(i, j + 1):
			if b[u]:
				b[u] = 0
			else:
				b[u] = 1
		res = max(res, b.count(1))
print(res)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
m = 0
for i in range(n):
	for j in range(i, n):
		b = a[i:j + 1]
		c = b.count(0) - b.count(1)
		if c + s > m:
			m = c + s
print(m)


def count(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		if a[i] == 1:
			max_ending_here -= 1
		else:
			max_ending_here += 1
		if max_ending_here > max_so_far:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far
n = int(input())
a = []
ans = 0
a = input().split()
for i in range(n):
	a[i] = int(a[i])
	if a[i] == 1:
		ans += 1
if ans == n:
	ans -= 1
else:
	ans += count(a, n)
print(ans)


(N, X) = (int(input()), list(map(int, input().split())))
if 0 not in X:
	print(N - 1)
	exit()
(First, Last, Ones) = (X.index(0), N - X[::-1].index(0) - 1, X.count(1))
Max = Ones
for i in range(First, Last + 1):
	Temp = Ones
	for j in range(i, Last + 1):
		Temp += 1 if X[j] == 0 else -1
		Max = max(Max, Temp)
print(Max)


n = int(input())
bs = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i, n):
		ans = max(ans, sum(bs) + j - i + 1 - 2 * sum(bs[i:j + 1]))
print(ans)


a = int(input())
z = list(map(int, input().split()))
t = [z[i] for i in range(len(z))]
for i in range(len(z)):
	if z[i] == 1:
		z[i] = -1
	else:
		z[i] = 1
cur = z[0]
ma = z[0]
for i in range(1, len(z)):
	cur = max(z[i], z[i] + cur)
	ma = max(cur, ma)
print(ma + t.count(1))


n = int(input())
p = [int(x) for x in input().split()]
if n == 1:
	if p[0] == 1:
		print(0)
	else:
		print(1)
elif n == 2:
	if p[0] == 1 and p[1] == 1:
		print(1)
	else:
		print(2)
elif p.count(1) == 0:
	print(n)
elif p.count(1) == n:
	print(n - 1)
else:
	q = [0] * n
	for i in range(1, n):
		if p[i] == 1:
			q[i] = q[i - 1] - 1
		else:
			q[i] = q[i - 1] + 1
	w = [0] * n
	for i in range(1, n):
		w[i] = min(q[i], w[i - 1])
	for i in range(0, n):
		w[i] = q[i] - w[i]
	print(max(w) + p.count(1))


def number_of_ones_if_flipped(l, from_index, to_index):
	counter = 0
	for i in range(len(l)):
		if not (from_index <= i and i <= to_index):
			if l[i] == 1:
				counter += 1
		elif l[i] == 0:
			counter += 1
	return counter

def all_possible_flips(l):
	maximum = -1 * float('inf')
	for i in range(0, len(l)):
		for j in range(i, len(l)):
			if number_of_ones_if_flipped(l, i, j) > maximum:
				maximum = number_of_ones_if_flipped(l, i, j)
	return maximum

def main():
	first_line = input()
	first_line = first_line.split()
	n = int(first_line[0])
	l = input()
	l = l.split()
	for i in range(len(l)):
		l[i] = int(l[i])
	print(all_possible_flips(l))
main()


(n, L, ones) = (int(input()), [int(val) for val in input().split()], [])
for i in range(n):
	for j in range(i, n):
		ones.append((L[:i] + [1 - x for x in L[i:j + 1]] + L[j + 1:]).count(1))
print(max(ones))


m = int(input())
n = [int(i) for i in input().split()]
o = []
d = n.count(1)
for i in range(m):
	for j in range(m):
		if j >= i:
			a = n[i:j + 1].count(0)
			b = n[i:j + 1].count(1)
			c = a - b
			o.append(c)
		else:
			continue
e = max(o)
print(e + d)


n = int(input())
l = [int(_) for _ in input().split()]
dp = [0] * n
dp[0] = 0 if l[0] == 1 else 1
for i in range(1, n):
	if l[i] == 0 and dp[i - 1] <= 0:
		dp[i] = 1
	elif l[i] == 0:
		dp[i] += 1 + dp[i - 1]
	else:
		dp[i] -= 1 - dp[i - 1]
(start, end) = (0, None)
valEnd = -1
for i in reversed(range(n)):
	if valEnd < dp[i]:
		valEnd = dp[i]
		end = i
for i in range(end):
	if dp[i] <= 0:
		start = i + 1
total = 0
for i in range(start, end + 1):
	l[i] = 1 - l[i]
for el in l:
	if el == 1:
		total += 1
print(total)


n = int(input())
line = [int(i) for i in input().split()]
out = 0
for i in range(n):
	lst = line[:]
	for j in range(i, n):
		lst[j] = 1 - lst[j]
		out = max(sum(lst), out)
if n == 1:
	out = 1 - line[0]
print(out)


n = int(input())
l = [int(x) for x in input().split()]
posl = 0
posr = 0
res = 0
for i in range(n):
	cnt0 = 0
	cnt1 = 0
	for j in range(i, n):
		if l[j] == 1:
			cnt1 += 1
		else:
			cnt0 += 1
		if cnt0 - cnt1 > res:
			res = cnt0 - cnt1
			posl = i
			posr = j
print(l[:posl].count(1) + l[posl:posr + 1].count(0) + l[posr + 1:].count(1))


n = int(input())
q = [int(x) for x in input().split()]
bres = 0
for i in range(n):
	for j in range(i, n):
		now = 0
		for x in range(n):
			if x >= i and x <= j:
				now += 1 - q[x]
			else:
				now += q[x]
		bres = max(bres, now)
print(bres)


n = int(input())
arr = list(map(int, input().rstrip().split(' ')))
prefixArr = [0] * (n + 1)
count1 = 0
for i in range(n):
	if arr[i] == 1:
		count1 += 1
		adder = -1
	else:
		adder = 1
	prefixArr[i + 1] = max(prefixArr[i] + adder, 0)
if max(prefixArr) == 0:
	print(count1 - 1)
else:
	print(max(prefixArr) + count1)


import itertools
import collections
import sys

def solve_bruteforce(xs):
	n = len(xs)
	if n == 1:
		return len(xs) - sum(xs)
	return max((sum(xs[:i]) + sum(xs[j:]) - sum(xs[i:j]) + (j - i) for (i, j) in itertools.combinations(n, 2)))

def solve_slow(xs):
	n = len(xs)
	assert n > 0
	a = [1 if x == 0 else -1 for x in xs]
	b = collections.defaultdict(int)
	for (i, acc) in enumerate(itertools.accumulate(a)):
		b[i] = acc
	deltaS = max((b[j] - b[i] for i in range(-1, n) for j in range(i + 1, n)))
	if deltaS <= 0:
		return sum(xs) - 1
	else:
		return sum(xs) + deltaS

def solve_old(xs):
	a = [1 if x == 0 else -1 for x in xs]
	b = itertools.accumulate(a)
	deltaS = 0
	min_so_far = 0
	for bk in b:
		deltaS = max(deltaS, bk - min_so_far)
		min_so_far = min(min_so_far, bk)
	if deltaS == 0:
		return sum(xs) - 1
	else:
		return sum(xs) + deltaS

def solve(xs):
	n = len(xs)
	a = [1 if x == 0 else -1 for x in xs]
	m = collections.defaultdict(int)
	maxval = 0
	for i in range(n):
		m[i] = max(m[i - 1] + a[i], a[i])
		maxval = max(m[i], maxval)
	deltaS = maxval
	if deltaS == 0:
		return sum(xs) - 1
	else:
		return sum(xs) + deltaS

def test():
	xs = [0]
	assert solve(xs) == 1
	xs = [1]
	assert solve(xs) == 0
	xs = [0, 1, 0]
	assert solve(xs) == 2
	xs = [0, 0, 1, 0, 0]
	assert solve(xs) == 4
	xs = [0, 0, 0, 0]
	assert solve(xs) == 4
	xs = [1, 1, 1]
	assert solve(xs) == 2

def main():
	n = int(input())
	xs = [int(i) for i in input().strip().split()]
	assert len(xs) == n
	result = solve(xs)
	print(result)
main()


n = int(input())
arr = list(map(int, input().strip().split(' ')))
one = arr.count(1)
if sum(arr) == n:
	print(sum(arr) - 1)
else:
	for i in range(n):
		if arr[i] == 1:
			arr[i] = -1
		else:
			arr[i] = 1
	ans = 0
	s = 0
	for i in range(n):
		s = s + arr[i]
		if s < 0:
			s = 0
		else:
			ans = max(ans, s)
	print(one + ans)


n = int(input())
a = list(map(int, input().split()))
b = [[-1, 1][i == 0] for i in a]
S = a.count(1)
best = 0
SUM = 0
for i in b:
	SUM = max(i, SUM + i)
	best = max(best, SUM)
print(S + best if S != n else n - 1)


amount = int(input())
data = [int(i) for i in input().split()]
count = [0, 0]
_max = 0
initi = 0
for i in data:
	initi += i
for i in range(amount):
	for j in range(i + 1, amount):
		for k in range(i, j + 1):
			count[data[k]] += 1
		_max = max(_max, count[0] - count[1])
		count[0] = 0
		count[1] = 0
if _max == 0:
	if initi == 0:
		print(1)
	else:
		print(initi - 1)
else:
	print(initi + _max)


n = int(input())
a = list(map(int, input().split()))
mx = 0
for i in range(n):
	for j in range(n):
		if j < i:
			continue
		cnt = 0
		for k in range(i):
			if a[k]:
				cnt += 1
		for k in range(i, j + 1):
			if not a[k]:
				cnt += 1
		for k in range(j + 1, n):
			if a[k]:
				cnt += 1
		mx = max(mx, cnt)
print(mx)


from sys import stdin
n = int(input())
A = list(map(int, stdin.readline().split()))
m = 0
z = 0
if set(A) == set([1]):
	print(n - 1)
	exit(0)
for i in A:
	if i == 1:
		if z <= 0:
			z = 0
		else:
			z -= 1
	else:
		z += 1
	m = max(z, m)
print(A.count(1) + m)


n = int(input())
l = list(map(int, input().split()))
m = -1
x = 0
for i in l:
	if i == 1:
		x = x - 1
	else:
		x = x + 1
	if x > m:
		m = x
	if x < 0:
		x = 0
print(sum(l) + m)


n = int(input())
a = list(map(int, input().split()))
m = 0
i1 = 0
j1 = 0
for i in range(n - 1):
	for j in range(i + 1, n):
		c = j - i + 1 - 2 * sum(a[i:j + 1])
		if c > m:
			m = c
			i1 = i
			j1 = j
c = sum(a) + (j1 - i1 + 1) - 2 * sum(a[i1:j1 + 1])
print(c)


n = int(input())
a = [int(i) for i in input().split()]
m = 0
for i in range(n):
	for j in range(i, n):
		m = max(m, a[:i].count(1) + a[i:j + 1].count(0) + a[j + 1:].count(1))
print(m)


N = int(input())
s = input().split(' ')
arr = []
ones = 0
for i in s:
	arr.append(int(i))
	if i == '1':
		ones = ones + 1
if ones == N:
	print(N - 1)
else:
	max = -1
	tsum = 0
	for i in arr:
		if i == 0:
			tsum = tsum + 1
		else:
			tsum = tsum - 1
		if tsum < 0:
			tsum = 0
		if tsum > max:
			max = tsum
	print(ones + max)


n = int(input())
a = [int(x) for x in input().split()]
count = a.count(1)
m = 0
if len(a) == a.count(1):
	if len(a) == 1:
		print(0)
	else:
		print(len(a) - 1)
else:
	for i in range(n):
		for j in range(i, n):
			if a[j] == 0:
				one = a[i:j].count(1)
				zer = a[i:j].count(0)
				if zer > one:
					maxi = zer - one
					if maxi > m:
						m = maxi
	print(m + count + 1)


__author__ = 'JohnHook'
n = int(input())
a = list(map(int, input().split()))
if 1 not in a:
	print(n)
	exit()
p = [a[0]]
for i in range(1, n):
	p.append(p[i - 1] + a[i])
mx = 0
for i in range(1, n):
	for j in range(i):
		mx = max(mx, p[j] + (i - j) - (p[i] - p[j]) + (p[n - 1] - p[i]))
print(mx)


n = int(input())
b = [int(i) for i in input().split(' ')]
count_0 = []
for (idx, i) in enumerate(b):
	if i == 0:
		count_0.append(idx)
if len(count_0) == 1:
	print(n)
elif len(count_0) == 0:
	print(n - 1)
else:
	result = [1]
	for j in range(0, len(count_0) - 1):
		score = result[-1] + count_0[j] - count_0[j + 1] + 2
		if score <= 0:
			score = 1
		result.append(score)
	print(max(result) + n - len(count_0))


t = int(input())
l = list(map(int, input().split()))
b = [0] * t
for i in range(t):
	if l[i] == 0:
		b[i] = 1
	else:
		b[i] = -1
var = 0
var1 = 0
maxm = 0
for i in range(t - 1):
	sumi = 0
	for j in range(i, t):
		sumi += b[j]
		if sumi > maxm:
			maxm = sumi
			var = i
			var1 = j
for k in range(var, var1 + 1):
	if l[k] == 0:
		l[k] = 1
	else:
		l[k] = 0
print(sum(l))


a = int(input())
arr = list(map(int, input().split(' ')))
score = 0
maxx = 0
if arr.count(0) == 0:
	print(a - 1)
	exit()
for i in range(a):
	if arr[i] == 0:
		if score < 0:
			score = 1
		else:
			score += 1
	else:
		score -= 1
	if score > maxx:
		maxx = score
print(maxx + arr.count(1))


n = int(input())
line = input().split()
g = []
for i in range(n):
	g.append(0)
w = []
for i in range(n):
	w.append(0)
l = [[0] * n for i in range(n)]
for i in range(n):
	for j in range(i, n):
		for k in range(n):
			if k < i:
				g[k] = line[k]
			elif k > j:
				g[k] = line[k]
			else:
				a = int(line[k])
				g[k] = str(1 - a)
		l[i][j] = g.count('1')
for u in range(n):
	w[u] = max(l[u])
num = max(w)
print(num)


def maxsubarraysum(a):
	max_so_far = 0
	max_ending_here = 0
	for i in a:
		max_ending_here += i
		if max_ending_here < 0:
			max_ending_here = 0
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
	return max_so_far
n = int(input())
s = list(map(int, input().split()))
a = []
c = 0
for i in s:
	if i == 1:
		a.append(-1)
		c += 1
	else:
		a.append(1)
if c == len(a):
	print(c - 1)
else:
	g = maxsubarraysum(a)
	print(c + g)


n = int(input())
piece = input().split()
answer = piece.count('1')
tempt1 = [0]
tempt2 = []
sum = 0
for i in range(n):
	if piece[i] == '1':
		sum = sum - 1
	else:
		sum = sum + 1
	tempt2.append(sum - min(tempt1))
	tempt1.append(sum)
answer = answer + max(tempt2)
print(answer)


n = int(input())
a = list(map(int, input().split()))
if 0 not in a:
	print(n - 1)
	quit()
b = sum(a)
for i in range(n):
	if a[i] == 0:
		a[i] -= 1
m = 0
for i in range(n + 1):
	for j in range(i, n + 1):
		k = sum(a[i:j])
		m = min(m, k)
print(b - m)


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	n = int(input())
	arr = get_array()
	b = [0] * n
	initial = 0
	for i in range(n):
		if arr[i] == 1:
			b[i] = -1
			initial += 1
		else:
			b[i] = 1
	(mx, curr_mx) = (b[0], b[0])
	for i in range(1, n):
		curr_mx = max(b[i], curr_mx + b[i])
		mx = max(curr_mx, mx)
	print(mx + initial)
main()


n = int(input())
g = list(map(int, input().split()))
cnt = g.count(1)
t = cnt
if cnt == n:
	print(n - 1)
else:
	for i in range(n):
		for j in range(i, n):
			sum = [0] * 2
			for k in range(i, j + 1):
				sum[g[k]] += 1
			if sum[0] > sum[1]:
				if cnt < t + sum[0] - sum[1]:
					cnt = t + sum[0] - sum[1]
	print(cnt)


I = lambda : map(int, input().split())
n = int(input())
li = list(I())
maxx = 0
left = [0 for i in range(n)]
right = [0 for i in range(n)]
su = 0
n1 = 0
for i in li:
	if i == 1:
		n1 += 1
for i in range(n):
	left[i] = su
	su = su + li[i]
	right[i] = n1 - su
for i in range(n):
	for j in range(i, n):
		ans = left[i] + right[j] + j - i + 1 - (right[i] - right[j] + li[i])
		maxx = max(ans, maxx)
print(maxx)


n = int(input())
a = [int(x) for x in input().split()]
b = []
s = 1
for i in range(1, n):
	if a[i] == 1 and a[i - 1] == 0:
		b.append(s)
		s = 1
	elif a[i] == a[i - 1] == 0:
		s += 1
		if i == n - 1:
			b.append(s)
	elif a[i] == 0 and a[i - 1] == 1:
		b.append(-s)
		s = 1
	else:
		s += 1
		if i == n - 1:
			b.append(-s)
ma = 0
for i in range(len(b)):
	he = 0
	j = i
	while i <= j <= len(b) - 1:
		he += b[j]
		j += 1
		if he > ma:
			ma = he
if n == 1:
	if a[0] == 1:
		print(0)
	else:
		print(1)
elif ma == 0:
	print(-he - 1)
else:
	print(ma + a.count(1))


n = int(input())
arr = [int(i) for i in input().split()]
c0 = [0 for i in range(n + 1)]
c1 = [0 for i in range(n + 1)]
cnt = 0
for i in range(n):
	if arr[i] == 1:
		c1[i + 1] = c1[i] + 1
		c0[i + 1] = c0[i]
		cnt += 1
	else:
		c0[i + 1] = c0[i] + 1
		c1[i + 1] = c1[i]
res = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		res = max(res, cnt + (c0[j] - c0[i]) - (c1[j] - c1[i]))
print(res)


n = int(input())
a = list(map(int, input().split()))
max1 = 0
if sum(a) == len(a):
	print(len(a) - 1)
	exit()
for i in range(n + 1):
	for i2 in range(i + 1):
		max1 = max(max1, len(a[i2:i]) - sum(a[i2:i]) + sum(a[i:]) + sum(a[:i2]))
print(max1)


n = int(input())
a = list(map(int, input().split()))
N = a.count(1) - 1
list1 = [N]
for i in range(0, n):
	for j in range(i, n):
		b = a[i:j + 1]
		N1 = N + 1 - b.count(1) + b.count(0)
		if N1 > N:
			list1.append(N1)
print(max(list1))


n = int(input())
A = list(map(int, input().split()))
summ = sum(A)
pref = [0] * (n + 1)
pref[0] = A[0]
for i in range(1, n):
	pref[i] = pref[i - 1] + A[i]
ans = 0
for i in range(n):
	for j in range(i, n):
		x = pref[j] - pref[i - 1]
		res = summ - x + (j - i + 1 - x)
		if res > ans:
			ans = res
print(ans)


n = int(input(''))
a = list(map(int, input().split()))
b = []
m = 0
for i in range(0, n, 1):
	for j in range(i, n, 1):
		b = a
		p = sum(a[i:j + 1])
		p = j + 1 - i - p
		if i > 0 and j + 1 == n:
			s = sum(a[:i]) + p
		elif i == 0 and j < n - 1:
			s = sum(a[j + 1:]) + p
		else:
			s = sum(a[:i]) + p + sum(a[j + 1:])
		if m <= s:
			m = s
print(m)


n = int(input())
seq = [int(x) for x in input().split()]
s = sum(seq)
solutions = []
for i in range(n):
	for j in range(i + 1, n + 1):
		solutions.append(s - 2 * sum(seq[i:j]) + (j - i))
print(max(solutions))


def main():
	n = int(input())
	a_list = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		for j in range(i, n):
			c = 0
			for k in range(n):
				if i <= k <= j:
					c += 1 - a_list[k]
				else:
					c += a_list[k]
			ans = max(ans, c)
	print(ans)
main()


n = int(input())
index = []
flip = list(map(int, input().split(' ')))[:n]
i = 0
count1 = 0
count0 = 0
maxx = -1
while i < len(flip):
	if flip[i] == 1:
		count1 += 1
		if count0 > 0:
			count0 -= 1
		i += 1
	if i < len(flip) and flip[i] == 0:
		count0 += 1
		if count0 > maxx:
			maxx = count0
		i += 1
print(count1 + maxx)


n = int(input())
a = [int(x) for x in input().split()]
ct = []
for i in range(n):
	for j in range(i, n):
		temp = [1 - x for x in a[i:j + 1]]
		ct.append(sum(a) + sum(temp) - temp.count(0))
print(max(ct))


n = int(input())
a = [int(i) for i in input().split()]
pref_z = [0]
pref_o = [0]
count_one = 0
for x in range(1, n + 1):
	if a[x - 1] == 0:
		pref_z.append(pref_z[x - 1] + 1)
		pref_o.append(pref_o[x - 1])
	else:
		count_one += 1
		pref_z.append(pref_z[x - 1])
		pref_o.append(pref_o[x - 1] + 1)
maxi = -10000
for i in range(n):
	for j in range(n):
		if j >= i:
			ones = pref_o[j + 1] - pref_o[i]
			zeros = pref_z[j + 1] - pref_z[i]
			if zeros + count_one - ones > maxi:
				maxi = zeros + count_one - ones
if 0 not in a:
	print(len(a) - 1)
else:
	print(maxi)


n = int(input())
a = [int(i) for i in input().split()]
b = [0 for i in range(n)]
for i in range(n):
	if a[i] == 1:
		b[i] = -1
	if a[i] == 0:
		b[i] = 1
summ = 0
for i in a:
	summ += i
maxsumm = 0
for i in range(n):
	l = 0
	for j in range(i, n):
		l += b[j]
		if l + summ > maxsumm:
			maxsumm = l + summ
else:
	print(maxsumm)


import sys

def main():
	n = int(sys.stdin.readline().strip())
	a = list(map(int, sys.stdin.readline().strip().split()))
	total_sum = sum(a)
	if n == 1:
		print(1 - sum(a))
		return
	max_sum = total_sum
	for i in range(n):
		for j in range(i + 1, n + 1):
			new_sum = total_sum + j - i - 2 * sum(a[i:j])
			if new_sum > max_sum:
				max_sum = new_sum
	if max_sum == total_sum:
		max_sum -= 1
	print(max_sum)
sys.exit(main())


def solve(array, n):

	def count_ones():
		ret = 0
		for k in range(n):
			if array[k] == 1:
				ret += 1
		return ret

	def generate_subarrays():
		max_num = 0
		for i in range(n):
			for j in range(i, n):
				for k in range(i, j + 1):
					array[k] = 1 - array[k]
				max_num = max(max_num, count_ones())
				for k in range(i, j + 1):
					array[k] = 1 - array[k]
		return max_num
	return generate_subarrays()
n = int(input())
array = list(map(int, input().split(' ')))
print(solve(array, n))


a = int(input())
b = input().split()
max = 0
i = 0
while i < a:
	j = i
	while j < a:
		list1 = []
		list2 = []
		list3 = []
		for k1 in range(0, i):
			list1.append(b[k1])
		for k2 in range(i, j + 1):
			list2.append(b[k2])
		for k3 in range(j + 1, a):
			list3.append(b[k3])
		if max < list1.count('1') + list2.count('0') + list3.count('1'):
			max = list1.count('1') + list2.count('0') + list3.count('1')
		j += 1
	i += 1
print(max)


n = int(input())
ar = list(map(int, input().split()))
ar.append(1)
max = 0
for i in range(n):
	for ind in range(i, n):
		q = sum(ar[:i]) + sum(ar[ind + 1:]) + (ind - i - sum(ar[i:ind + 1]))
		if q > max:
			max = q
print(max)


n = int(input())
a = list(map(int, input().split()))
dp = [0] * n
f = 0
for (i, v) in enumerate(a):
	if v == 1:
		if f == 0:
			dp[i] = 1
		elif dp[i - 1] > 0:
			dp[i] = dp[i - 1] - 1
	elif f == 0:
		dp[i] = 1
		f = 1
	else:
		dp[i] = dp[i - 1] + 1
		f = 1
if f == 0:
	print(len(a) - 1)
else:
	print(sum(a) + max(dp))


n = int(input())
a = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
	a[i] += a[i - 1]
r = 0
for i in range(0, n):
	for j in range(i + 1, n + 1):
		r = max(r, a[i] + a[n] - a[j] + (j - i - a[j] + a[i]))
print(r)


n = int(input())
A = list(map(int, input().split()))
m = 0
for i in range(n):
	for j in range(i, n):
		m = max(m, A[:i].count(1) + A[i:j + 1].count(0) + A[j + 1:].count(1))
print(m)


import array as arr
n = int(input())
a = list(map(int, input().split()))
if a.count(1) == n:
	print(n - 1)
elif a.count(0) == n:
	print(n)
else:
	c = 0
	for i in range(0, n):
		for j in range(i + 1, n):
			if a[i] == 1:
				i += 1
				if i == j:
					j += 1
			b = a.copy()
			for x in range(i, j + 1):
				if i >= n or j >= n or x >= n:
					break
				if b[x] == 0:
					b[x] = 1
				else:
					b[x] = 0
			if c < b.count(1):
				c = b.count(1)
	print(c)


n = int(input())
mas = [int(x) for x in input().split()]

def func(massive, x1, x2):
	for i in range(x1, x2 + 1):
		massive[i] = 1 - massive[i]
	x = massive.count(1)
	for i in range(x1, x2 + 1):
		massive[i] = 1 - massive[i]
	return x
mas1 = []
for z1 in range(n):
	for z2 in range(z1, n):
		mas1.append(func(mas, z1, z2))
print(max(mas1))


n = int(input())
arr = list(map(int, input().strip().split()))
memo = []

def dp(i, j):
	for i in range(n):
		for j in range(i, n):
			memo.append(arr[:i].count(1) + arr[i:j + 1].count(0) + arr[j + 1:].count(1))
dp(0, 0)
print(max(memo))


n = int(input())
a = [int(k) for k in input().split(' ')]
min1 = 0
for i in range(n + 1):
	for j in range(i + 1, n + 1):
		min1 = max(min1, sum(a) + j - i - 2 * sum(a[i:j]))
print(min1)


n = int(input())
a = [int(x) for x in input().split()]
max_current = -5
max_global = -5
for elem in a:
	if elem == 0:
		value = 1
	else:
		value = -1
	max_current = max(value, max_current + value)
	if max_current > max_global:
		max_global = max_current
print(sum(a) + max_global)


import math
n = int(input())
numbers = input().split(' ')
initial = numbers.count('1')
maximum = -math.inf
for i in range(n):
	count = 0
	while count + i < n:
		profit = 0
		for j in range(i, i + count + 1):
			if numbers[j] == '0':
				profit += 1
			else:
				profit -= 1
		count += 1
		if profit > maximum:
			maximum = profit
print(maximum + initial)


n = int(input())
a = [int(i) for i in input().split()]
total = a.count(1)
sum = 0
for i in range(n):
	for j in range(i, n):
		temp = total
		for k in range(i, j + 1):
			temp -= a[k]
			temp += 1 - a[k]
		sum = max(sum, temp)
print(sum)


def reverse(b):
	for i in range(len(b)):
		if b[i] == '0':
			b = b[0:i] + '1' + b[i + 1:len(b)]
		else:
			b = b[0:i] + '0' + b[i + 1:len(b)]
	return b

def count(b):
	c = 0
	for i in range(len(b)):
		if b[i] == '1':
			c = c + 1
	return c
no = int(input())
string = input()
string = string.replace(' ', '')
max = 0
for i in range(len(string)):
	for j in range(i, len(string)):
		cc = count(string[0:i]) + count(reverse(string[i:j + 1])) + count(string[j + 1:len(string)])
		if cc > max:
			max = cc
			if max == len(string):
				print(max)
				exit()
print(max)


n = int(input())
digits = input().split()

def count(listn, x):
	k = 0
	for i in listn:
		if i == x:
			k = k + 1
	return k
lenth = list()
for i in range(n):
	for k in range(i, n):
		num = count(digits[i:k + 1], '0') - count(digits[i:k + 1], '1')
		lenth.append(num)
print(count(digits, '1') + max(lenth))


def scan(type):
	return list(map(type, input().split()))

def kadane(arr):
	soFar = 0
	maxi = 0
	maxiSt = maxiEn = 0
	st = 0
	i = 0
	for x in arr:
		if x:
			soFar -= 1
		else:
			soFar += 1
		if soFar > maxi:
			maxiEn = i
			maxiSt = st
			maxi = soFar
		if soFar < 0:
			soFar = 0
			st = i + 1
		i += 1
	for i in range(maxiSt, maxiEn + 1):
		arr[i] = 1 - arr[i]
	ans = 0
	for x in arr:
		ans += x
	return ans
(n,) = scan(int)
arr = scan(int)
ans = kadane(arr)
print(ans)


x = int(input())
a = list(map(int, input().split()))
d = 0
s = [0] * 2
if len(a) == 1:
	if a[0] == 1:
		print(0)
	else:
		print(1)
elif a.count(1) == x:
	print(x - 1)
else:
	for i in range(x):
		for j in range(i, x):
			b = a[i:j + 1].count(1)
			c = a[i:j + 1].count(0)
			if c - b > d:
				d = c - b
				s[0] = i
				s[1] = j
	print(d + a[:i].count(1) + a[j:].count(1))


n = int(input())
a = list(map(int, input().split()))
total = 0
maxv = 0
for i in range(n):
	if a[i] == 1:
		total += 1
temp = 0
for i in range(n):
	for j in range(i + 1, n):
		temp = 0
		for k in range(i, j + 1):
			if a[k] == 0:
				temp += 1
			else:
				temp -= 1
		if maxv < temp:
			maxv = temp
if n == 1:
	print(1 - a[0])
elif maxv == 0:
	print(total - 1)
else:
	print(total + maxv)


n = int(input())
line = input().split()
x = 0
c = 0
output = 0
for i in range(n):
	if int(line[i]) == 1:
		c = c + 1
if n > 1:
	for i in range(n):
		for t in range(i + 1, n + 1):
			for j in range(i, t):
				if int(line[j]) == 1:
					x = x + 1
			s = t - i - 2 * x
			output = max(output, s)
			x = 0
	output = output + c
elif int(line[0]) == 0:
	output = 1
else:
	output = 1
if c == len(line):
	output = output - 1
print(output)


import itertools

def solve(xs):
	n = len(xs)
	if n == 1:
		return len(xs) - sum(xs)
	return max((sum(xs[:i]) + sum(xs[j:]) - sum(xs[i:j]) + (j - i) for (i, j) in itertools.combinations(range(n + 1), 2)))

def main():
	n = int(input())
	xs = [int(i) for i in input().strip().split()]
	assert len(xs) == n
	result = solve(xs)
	print(result)
main()


n = int(input())
arr = list(map(int, input().split()))
ones = arr.count(1)
if ones == 0:
	print(n)
elif ones == n:
	print(n - 1)
else:
	brr = [1 if arr[i] == 0 else -1 for i in range(n)]
	maxsum = 0
	temp = 0
	for i in range(n):
		temp = max(brr[i], temp + brr[i])
		maxsum = max(maxsum, temp)
	print(ones + maxsum)


n = int(input())
l = [int(i) for i in input().split()]
total = sum(l)
w = 0
z = 0
for i in range(n):
	c = 0
	k = total
	z = 0
	for j in range(i, n):
		if l[j] == 0:
			c += 1
		else:
			k -= 1
		z = max(z, c + k)
	w = max(w, z)
print(w)


n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for i in range(n):
	for j in range(i, n):
		for k in range(i, j + 1):
			a[k] = 1 - a[k]
		ans = max(ans, len(list(filter(lambda x: x == 1, a))))
		for k in range(i, j + 1):
			a[k] = 1 - a[k]
print(ans)


n = input()
n = int(n)
record = [int(x) for x in input().split()]
operate = []
if record.count(0) == 0:
	print(n - 1)
elif record.count(0) == 1:
	print(n)
else:
	for i in range(len(record)):
		if record[i] == 0:
			first = i
			break
	for i in range(len(record) - 1, -1, -1):
		if record[i] == 0:
			last = i
			break
	for j in range(first + 1, last + 1):
		for i in range(first, j):
			flip = record[i:j + 1]
			operate.append(record.count(1) + flip.count(0) - flip.count(1))
	print(max(operate))


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	combs = []
	for i in range(n):
		for j in range(i + 1):
			combs.append((j, i))
	bst = 0
	for interval in combs:
		curr = a.copy()
		for k in range(interval[0], interval[1] + 1):
			curr[k] = 1 - curr[k]
		bst = max(bst, sum(curr))
	print(bst)
	return
solve()


total = int(input())
array = input().split()
list1 = [0]
flag = 2
m = 0
for i in array:
	if int(i) == 0:
		if flag == 0:
			list1.append(m)
			m = 0
		m -= 1
		flag = 1
	else:
		if flag == 1:
			list1.append(m)
			m = 0
		m += 1
		flag = 0
list1.append(m)
list2 = []
continues = list1[0]
minima = list1[0]
for i in range(0, len(list1)):
	c = min(minima, min(continues, list1[i]))
	list2.append(c)
	minima = c
	if i != len(list1) - 1:
		continues = min(continues, list1[i]) + list1[i + 1]
sum = 0
for i in array:
	sum = sum + int(i)
if list2[-1] >= 0:
	print(sum - 1)
else:
	print(sum - list2[-1])


n = int(input())
numbers = list(map(int, input().split()))
max_sum = 0
if len(numbers) > 1:
	for i in range(0, len(numbers)):
		for j in range(0, len(numbers)):
			if i <= j:
				temp = numbers.copy()
				for index in range(i, j + 1):
					if temp[index] == 1:
						temp[index] = 0
					else:
						temp[index] = 1
				sum_ones = sum(temp)
				if max_sum < sum_ones:
					max_sum = sum_ones
elif numbers[0] == 0:
	max_sum = 1
print(max_sum)


n = int(input())
line = [int(i) for i in input().split()]
output = 0
for i in range(n):
	text = line[:]
	for j in range(i, n):
		text[j] = 1 - text[j]
		output = max(sum(text), output)
if n == 1:
	output = 1 - line[0]
print(output)


n = int(input())
a = list(map(int, input().split()))
(ans, s) = (0, a.count(1))
for i in range(n):
	for j in range(i + 1, n + 1):
		tmp = a[i:j].count(0) - a[i:j].count(1)
		if s + tmp > ans:
			ans = s + tmp
print(ans)


n = int(input())
lst1 = list(map(int, input().split()))
ones = 0
zeros = 0
for i in range(n):
	if lst1[i] == 0:
		zeros += 1
	else:
		ones += 1
dp = [[0 for i in range(n)] for j in range(n)]
max1 = 0
for i in range(n):
	one = 0
	zero = 0
	ans = ones
	for j in range(i, n):
		if lst1[j] == 1:
			ans -= 1
			max1 = max(max1, ans)
			dp[i][j] = ans
		else:
			ans += 1
			max1 = max(max1, ans)
			dp[i][j] = ans
print(max1)


n = int(input())
arr = list(map(int, input().split()))
maxx = 0
v = -1
count = 0
extra = 0
for i in arr:
	if i == 1:
		count += 1
		if extra > 0:
			extra -= 1
	else:
		extra += 1
		if extra > v:
			v = extra
print(count + v)


def dp011_flipping_game(n, a):
	a.insert(0, 0)
	(one, zero) = ([0], [0])
	for i in range(1, n + 1):
		one.append(one[i - 1])
		zero.append(zero[i - 1])
		if a[i] == 0:
			zero[i] += 1
		if a[i] == 1:
			one[i] += 1
	res = 0
	for i in range(1, n + 1):
		for j in range(i, n + 1):
			add = zero[j] - zero[i - 1] - (one[j] - one[i - 1])
			res = max(res, one[n] + add)
	return res
n = int(input())
a = list(map(int, input().split()))
print(dp011_flipping_game(n, a))


n = int(input())
l = list(map(int, input().split()))
if l.count(1) == n:
	print(n - 1)
else:
	m = 0
	for i in range(n):
		for j in range(n):
			x = l[:]
			for k in range(i, j + 1):
				x[k] = 1 - x[k]
			m = max(m, x.count(1))
	print(m)


maxint = 1 << 32

def maxSubArraySum(a, count, size):
	max_so_far = 0
	max_ending_here = 0
	if count == size:
		return count - 1
	for i in range(0, size):
		max_ending_here = max(max_ending_here + a[i], a[i])
		max_so_far = max(max_so_far, max_ending_here)
	return max_so_far + count
n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(n):
	if a[i] == 0:
		a[i] = 1
	else:
		a[i] = -1
		count += 1
print(maxSubArraySum(a, count, len(a)))


def get_ones(a):
	ones = 0
	for i in a:
		if i == 1:
			ones += 1
	return ones

def flip(a, l, r):
	while l <= r:
		a[l] = 1 - a[l]
		l += 1
n = int(input())
a = input().split()
b = []
for i in a:
	b.append(int(i))
high = 0
for i in range(0, n):
	for x in range(i, n):
		flip(b, i, x)
		result = get_ones(b)
		if result > high:
			high = result
		flip(b, i, x)
print(high)


n = int(input())
a = input().replace(' ', '')
ans = 0
for i in range(n):
	for j in range(i, n):
		s = a[i:j + 1]
		ans = max(ans, s.count('0') + (a[:i] + a[j + 1:]).count('1'))
print(ans)


n = int(input())
array = input().split()
one_index = []
m = []
max_zero = 0
row = 0
col = 0
for i in range(n):
	emp = [0] * n
	if array[i] == '1':
		one_index.append(i)
		emp[i] = 0
	else:
		emp[i] = 1
	m.append(emp)
	for j in range(i, n):
		if array[j] == '1':
			m[i][j] = m[i][j - 1] - 1
		else:
			m[i][j] = m[i][j - 1] + 1
		if m[i][j] >= max_zero:
			(row, col) = (i, j)
			max_zero = m[i][j]
res = col - row + 1 + len(one_index)
for _ in one_index:
	if _ >= row and _ <= col:
		res -= 2
print(res)


n = int(input())
start = [int(x) for x in input().split(' ')]
num = start.count(1)
if num == n:
	num = n - 1
else:
	for i in range(n):
		for j in range(i, n):
			change = start[i:j + 1]
			a = start.count(1) - change.count(1) + change.count(0)
			if a > num:
				num = a
print(num)


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	if a[i] == 0:
		b.append(1)
	else:
		b.append(-1)
summ = b[0]
minsum = min(b[0], 0)
ans = b[0] + a.count(1)
for i in range(1, n):
	summ += b[i]
	ans = max(ans, a.count(1) - minsum + summ)
	minsum = min(minsum, summ)
print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
n = stdint()
arr = list(stdarr())
ones = arr.count(1)
res = -1
for i in range(n):
	o = ones
	for j in range(i, n):
		if arr[j] == 1:
			o -= 1
		else:
			o += 1
		res = max(res, o)
print(res)


n = int(input())
line = input().split()
m = [int(i) for i in line]
one = m.count(1)
if 0 not in m:
	print(n - 1)
else:
	two = 0
	a = 0
	for i in range(n):
		if m[i] == 1:
			a -= 1
		elif m[i] == 0:
			a += 1
		if a < 0:
			a = 0
		if a > two:
			two = a
	print(one + two)


n = int(input())
l = [int(i) for i in input().split()]
t = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		x = j - i + 1 - sum(l[i - 1:j]) + sum(l[0:i - 1]) + sum(l[j:n])
		if x > t:
			t = x
print(t)


n = int(input())
number = [int(i) for i in input().split()]
S = sum(number)
if S == n:
	print(S - 1)
else:
	for t in range(len(number)):
		k = 0
		while k <= len(number) - t - 1:
			p = sum(number) - number[k:k + t + 1].count(1) + number[k:k + t + 1].count(0)
			if p >= S:
				S = p
			k = k + 1
	print(S)


n = int(input())
L = list(map(int, input().split()))
dp = {}
if L.count(1) == len(L):
	print(len(L) - 1)
	exit()

def calc(i, j):
	if (i, j) in dp:
		return dp[i, j]
	if i == j:
		dp[i, j] = 1 if L[i] == 0 else -1
	elif L[i] == 1 and L[j] == 1:
		dp[i, j] = calc(i + 1, j - 1) - 2
	elif L[j] == 1:
		dp[i, j] = calc(i, j - 1) - 1
	elif L[i] == 1:
		dp[i, j] = calc(i + 1, j) - 1
	else:
		dp[i, j] = 1 + max(calc(i + 1, j), calc(i, j - 1))
	return dp[i, j]
calc(0, len(L) - 1)
print(max(dp.values()) + L.count(1))


def f():
	x = l.count(1)
	y = l.count(0)
	k = y - x
	return k
list = []
n = int(input())
s = [int(i) for i in input().split()]
for i in range(len(s)):
	for j in range(i, len(s)):
		l = s[i:j + 1]
		list.append(f())
p = max(list)
print(s.count(1) + p)


def solve(a_list):
	flipping_0_curr = 0
	flipping_0_max = -1
	original_1 = 0
	for a in a_list:
		if a == 1:
			original_1 += 1
			if flipping_0_curr > 0:
				flipping_0_curr -= 1
		else:
			flipping_0_curr += 1
			if flipping_0_curr > flipping_0_max:
				flipping_0_max = flipping_0_curr
	return original_1 + flipping_0_max
n = int(input())
a_list = map(int, input().split(' '))
print(solve(a_list))


from sys import stdin, stdout, stderr, setrecursionlimit
setrecursionlimit(100000)

def debug(*e):
	if not __debug__:
		print(*e, file=stderr)

def dd(*vals):
	import inspect, re
	frame = inspect.getframeinfo(inspect.stack()[1][0])
	vs = re.search('dd\\((.+)\\)', frame.code_context[0]).group(1).split(',')
	if vs:
		debug(','.join(('{0} = {1}'.format(vs[i], v) for (i, v) in enumerate(vals))))

def trace(f):

	def traced(*args, **kw):
		debug('calling {} with args {}, {}'.format(f.__name__, args, kw))
		return f(*args, **kw)
	return traced

def read():
	return stdin.readline().rstrip()

def readarr(sep=None, maxsplit=-1):
	return read().split(sep, maxsplit)

def readint():
	return int(read())

def readintarr(sep=None, maxsplit=-1):
	return [int(a) for a in readarr(sep, maxsplit)]

def write(*args, **kwargs):
	sep = kwargs.get('sep', ' ')
	end = kwargs.get('end', '\n')
	stdout.write(sep.join((str(a) for a in args)) + end)

def writearr(arr, sep=' ', end='\n'):
	stdout.write(sep.join((str(a) for a in arr)) + end)
n = readint()
xs = readintarr()
mx = 0
for i in range(n):
	for j in range(i, n):
		rs = xs[:]
		rs[i:j + 1] = [1 - x for x in xs[i:j + 1]]
		s = sum(rs)
		if s > mx:
			mx = s
write(mx)


def Flip(n, arr):
	count = 0
	value = 0
	zeroCount = 0
	zeroSum = -1
	while count < n:
		if arr[count] == 1:
			value += 1
			if zeroCount > 0:
				zeroCount -= 1
		else:
			zeroCount += 1
			if zeroCount > zeroSum:
				zeroSum = zeroCount
		count += 1
	print(value + zeroSum)
n = int(input())
arr = list(map(int, input().split()))
Flip(n, arr)


n = int(input())
a = list(map(int, input().split()))
maxx = -1
cnt = l = r = summ = 0
for i in range(n):
	if a[i] == 1:
		summ = summ + 1
for i in range(n):
	for j in range(n):
		for k in range(i, j + 1):
			if a[k] == 0:
				cnt = cnt + 1
			else:
				cnt = cnt - 1
		if cnt >= maxx:
			maxx = cnt
			l = i
			r = j
		cnt = 0
for i in range(n):
	if i >= l and i <= r:
		if a[i] == 0:
			a[i] = 1
		else:
			a[i] = 0
	if a[i] == 1:
		cnt += 1
if summ == n:
	print(n - 1)
else:
	print(cnt)


n = int(input())
a = list(map(int, input().split()))
p = []
for i in range(len(a)):
	if a[i]:
		p.append(-1)
	else:
		p.append(1)
s = a.count(1)
dp = [0 for i in range(len(a))]
dp[0] = p[0]
for i in range(1, len(a)):
	dp[i] = max(p[i], dp[i - 1] + p[i])
print(s + max(dp))


from sys import stdin
n = int(stdin.readline())
arr = [int(x) for x in stdin.readline().split()]
n1 = arr.count(1)
ans = n1
if arr[0] == 1:
	ans -= 1
for i in range(n):
	for j in range(i, n):
		nalt1 = 0
		for k in range(i, j + 1):
			if arr[k] == 1:
				nalt1 += 1
		ans = max(ans, n1 - nalt1 + j - i + 1 - nalt1)
print(ans)


n = int(input())
l = list(map(int, input().split()))
ma = 0
for i in range(n):
	for j in range(i, n):
		t = j - i + 1 + sum(l[:i]) + sum(l[j + 1:]) - sum(l[i:j + 1])
		if t > ma:
			ma = t
print(ma)


n = int(input())
nums = list(map(int, input().split()))
b = list(map(lambda x: -1 if x == 1 else 1, nums))
current_sum = 0
best_sum = 0
for i in range(len(b)):
	current_sum = max(0, current_sum + b[i])
	best_sum = max(best_sum, current_sum)
best_sum = -1 if best_sum == 0 else best_sum
S = len(list(filter(lambda x: x == 1, nums)))
print(S + best_sum)


n = int(input())
l = list(map(int, input().split()))
if n == 1:
	print(1 - sum(l))
else:
	p = [1 - 2 * l[0]] * n
	for i in range(1, n):
		p[i] = max(p[i - 1], 0) + 1 - 2 * l[i]
	print(max(p) + sum(l))


n = int(input())
a = list(map(int, input().split()))
start = sum(a)
if start < len(a):
	mx = start
	for i in range(n):
		for j in range(i + 1, n + 1):
			r = sum(a[i:j])
			d = j - i
			if start + d - 2 * r > mx:
				mx = start + d - 2 * r
else:
	mx = start - 1
print(mx)


n = int(input())
cnt = -1
c1 = 0
c0 = 0
array = list(map(int, input().split()))
for x in range(n):
	if array[x] == 1:
		c1 += 1
		if c0 != 0:
			c0 -= 1
	else:
		c0 += 1
		if c0 > cnt:
			cnt = c0
print(c1 + cnt)


n = int(input())
nums = list(map(int, input().split()))
partial_sum = [None] * (n + 1)
partial_sum[0] = 0
for i in range(1, n + 1):
	partial_sum[i] = partial_sum[i - 1] + nums[i - 1]
max_sum = 0
max_sum_first = partial_sum[-1]
for i in range(n):
	for j in range(i + 1, n + 1):
		s = partial_sum[j] - partial_sum[i]
		if j - i - s - s + max_sum_first > max_sum:
			max_sum = j - i - s - s + max_sum_first
print(max_sum)


n = int(input())
n = [int(i) for i in input().split()]
s = sum(n)
mx = 0
count = 0
for i in n:
	if i == 0:
		count += 1
		if count > mx:
			mx = count
	elif count > 0:
		count -= 1
if mx == 0:
	s -= 1
print(s + mx)


import sys
import io
import os
total = 0
failed = 0

def debug(*args):
	if 'LOCALTEST' in os.environ:
		print(*args, file=sys.stderr)

def run(test, res):
	x = io.StringIO()
	with io.StringIO(test) as sys.stdin:
		with x as sys.stdout:
			work()
			z = x.getvalue().strip()
	sys.stdout = sys.__stdout__
	print('Passed?', z == res)
	print('Expected: ', res)
	print('Actual  : ', z)
	global total, failed
	total += 1
	failed += 1 if z != res else 0

def work():
	n = int(input())
	m = list(map(int, input().split()))
	a = [0 for i in range(n + 1)]
	for i in range(1, n + 1):
		a[i] = m[i - 1] + a[i - 1]
	debug(m)
	debug(a)
	res = 0
	for i in range(1, n + 1):
		for j in range(i, n + 1):
			c = a[n] + (j - i) + 1 - 2 * (a[j] - a[i - 1])
			res = max(c, res)
	print(res)

def test():
	run('18\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', '18')
	run('23\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1', '22')
	run('1\n0', '1')
	run('1\n1', '0')
	run('5\n1 0 0 1 0', '4')
	run('4\n1 0 0 1', '4')
	run('8\n1 0 0 0 1 0 0 0', '7')
if 'LOCALTEST' in os.environ:
	test()
	print('\n Result: %s (%d total, %d failed)' % ('FAILED' if failed > 0 else 'PASSED', total, failed))
else:
	work()


n = int(input())
integer = list(map(int, input().split(' ')))
total = []
for i in range(n):
	for t in range(i, n):
		a = sum(integer[:]) + t - i + 1 - 2 * sum(integer[i:t + 1])
		total += [a]
print(max(total))


def no_of_zeroes(i, j):
	count = 0
	for k in range(i, j + 1):
		if k < n and k > -1:
			if l[k] == 0:
				count += 1
	return count

def no_of_ones(i, j):
	count = 0
	for k in range(i, j + 1):
		if k < n and k > -1:
			if l[k] == 1:
				count += 1
	return count
n = int(input())
l = list(map(int, input().split()))
arr = [[0 for _ in range(n)] for _ in range(n)]
maximum = 0
for i in range(n):
	for j in range(i, n):
		arr[i][j] = no_of_zeroes(i, j) + no_of_ones(0, i - 1) + no_of_ones(j + 1, n - 1)
		if maximum < arr[i][j]:
			maximum = arr[i][j]
print(maximum)


n = int(input())
list1 = input().split()
sum1 = list1.count('1')
list2 = []
for i in range(1, n + 1):
	if i == n:
		j = n
		l = 0
		sum2 = 0
		if list1[n - 1] == '0':
			sum2 = sum1 + 1
		list2.append(sum2)
	else:
		for j in range(i, n + 1):
			l = 0
			for k in list1[i - 1:j]:
				if k == '0':
					l = l + 1
			sum2 = sum1 + 2 * l + i - j - 1
			list2.append(sum2)
print(max(list2))


def maxSubArraySum(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_ending_here < 0:
			max_ending_here = 0
		elif max_so_far < max_ending_here:
			max_so_far = max_ending_here
	return max_so_far
n = int(input())
l = list(map(int, input().split()))
if l.count(0) == 0:
	print(n - 1)
	exit(0)
arr = [0] * n
count = 0
for i in range(n):
	if l[i] == 1:
		count += 1
		arr[i] = -1
	else:
		arr[i] = 1
print(count + maxSubArraySum(arr, n))


def max_sum_subarray(arr, n):
	max_so_far = -1000
	max_ending = 0
	start1 = 0
	start2 = 0
	end = 0
	for i in range(n):
		if arr[i]:
			max_ending += -1
		else:
			max_ending += 1
		if max_ending > max_so_far:
			max_so_far = max_ending
			start1 = start2
			end = i
		if max_ending < 0:
			max_ending = 0
			start2 = i + 1
	return (start1, end)
n = int(input())
arr = list(map(int, input().split()))
(i, j) = max_sum_subarray(arr, n)
count = 0
for k in range(n):
	if k >= i and k <= j:
		count += 1 - arr[k]
	else:
		count += arr[k]
print(count)


n = int(input())
j = [int(j) for j in input().split()]
k = 0
num_0 = j.count(0)
num_1 = j.count(1)
max = 0
for i in range(n):
	for w in range(i, n):
		z = j[i:w + 1]
		a = z.count(1)
		b = z.count(0)
		num = num_1 + b - a
		if num > max:
			max = num
print(max)


1

def main():
	n = int(input())
	data = [int(i) for i in input().split()]
	flip = [1, 0]
	maxi = sum(data)
	change = False
	for s in range(n):
		atual = sum(data[:s])
		for e in range(s, n):
			atual += flip[data[e]]
			if atual + sum(data[e + 1:]) > maxi:
				maxi = atual + sum(data[e + 1:])
				change = True
	print(maxi if change else maxi - 1)
main()


n = int(input())
a = [int(k) for k in input().split()]
b = []
max_till_now = -99999
max_end = 0
start = 0
end = 0
s = 0
for i in range(n):
	if a[i] == 1:
		b.append(-1)
	else:
		b.append(1)
for i in range(n):
	max_end += b[i]
	if max_till_now < max_end:
		max_till_now = max_end
		start = s
		end = i
	if max_end < 0:
		max_end = 0
		s = i + 1
for i in range(start, end + 1):
	if b[i] == 1:
		a[i] = 1
	else:
		a[i] = 0
print(a.count(1))


n = int(input())
l = input().split()
z = 0
for i in range(n):
	z = z + int(l[i])
c = 0
for j in range(n):
	for k in range(j, n):
		b = 0
		e = 0
		for m in range(j, k + 1):
			a = int(l[m])
			d = a
			if a == 0:
				a = 1
			else:
				a = 0
			b = b + a
			e = e + d
		f = b - e
		if f > c:
			c = f
z = z + c
if c == 0:
	z = z - 1
print(z)


n = int(input())
bits = list(map(int, input().split()))
ones = bits.count(1)
Max = float('-inf')

def count(num, i, j):
	for x in range(i, j + 1):
		num[x] = 1 - num[x]
	return num.count(1)
for i in range(n):
	for j in range(i, n):
		Max = max(Max, count(bits[:], i, j))
print(Max)


n = int(input())
s = input().split()
s = [int(i) for i in s]
b = 0
c = 0
d = 0
e = 0
for i in range(n):
	if s[i] == 0:
		for w in range(i, n):
			if s[w] == 0:
				b += 1
			if s[w] == 1:
				b -= 1
			if b > c:
				c = b
	if c > d:
		d = c
	b = 0
	c = 0
for i in range(n):
	if s[i] == 1:
		e += 1
if n == e:
	print(n - 1)
else:
	print(e + d)


n = int(input())
aa = [int(a) for a in input().split()]
st = aa.count(1)
ans = -1
for i in range(n):
	for j in range(i, n):
		sub = aa[i:j + 1]
		z = sub.count(0)
		o = j - i - z + 1
		ans = max(ans, z - o)
print(ans + st)


n = int(input())
a = [int(x) for x in input().split()]
somme = sum(a)
max = 1 - a[0]
for x in range(n - 1):
	for y in range(x, n):
		nouvelle_somme = sum(a[:x]) + (y - x + 1) - sum(a[x:y + 1]) + sum(a[y + 1:])
		if max <= nouvelle_somme:
			max = nouvelle_somme
print(max)


n = int(input())
a = [int(i) for i in input().split()]
maxim = 0
for i in range(n):
	for j in range(i, n):
		tmp = list(a)
		for k in range(i, j + 1):
			tmp[k] = 1 - tmp[k]
		maxim = max(sum(tmp), maxim)
print(maxim)


n = int(input())
v = []
a = 0
x = [int(x) for x in input().split()]
for i in range(n):
	if x[i] == 1:
		v.append(-1)
		a += 1
	else:
		v.append(1)
x = 0
ans = 0
if a == n:
	print(n - 1)
	exit()
for j in range(n):
	x = max(0, x + v[j])
	ans = max(ans, x)
print(a + ans)


n = int(input())
A = [int(x) for x in input().split()]
m = -1
i = 0
j = 0

def calc(a, i, j):
	b = [x for x in a]
	for x in range(i, j + 1):
		b[x] = not b[x]
	return b.count(1)
for i in range(n):
	for j in range(i, n):
		m = max(m, calc(A, i, j))
print(m)


n = int(input())
t = list(map(int, input().split()))
if t.count(1) == n:
	print(n - 1)
elif t.count(0) == n:
	print(n)
else:
	u = 0
	for k in range(n):
		for j in range(k + 1, n + 1):
			a = t[k:j].count(0) + t[:k].count(1) + t[j:].count(1)
			if a > u:
				u = a
	print(u)


n = int(input())
a = list(map(int, input().split()))
m = 0
for i in range(1, n + 1):
	for k in range(n):
		m = max(m, a[k:k + i].count(0) + a[:k].count(1) + a[k + i:].count(1))
print(m)


n = int(input())
l = list(map(int, input().split()))
strt = 0
stop = 0
for i in range(n):
	if l[i] == 0:
		strt = i + 1
		break
for i in range(n - 1, -1, -1):
	if l[i] == 0:
		stop = i + 1
		break
if strt < 1 or stop < 1:
	if n == 1:
		print(0)
	else:
		print(n - 1)
else:
	one = 0
	for i in l:
		if i == 1:
			one += 1
	x = int(one)
	mx = one
	for i in range(strt - 1, stop):
		for j in range(strt - 1, stop):
			one = x
			for k in range(i, j + 1):
				if l[k] == 0:
					one += 1
				if l[k] == 1:
					one -= 1
			mx = max(mx, one)
	print(mx)


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
tcid = 0
tcmax = 99999999

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

def perr(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(eval, o or split()))

def num():
	return eval(line())

def mainloop(tcid=1):
	n = num()
	a = nums()
	best = 0
	for ii in range(n):
		for jj in range(ii, n):
			tmp = 0
			for kk in range(n):
				tmp += a[kk] if kk < ii or kk > jj else 1 - a[kk]
			best = max(best, tmp)
	print(best)
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


from collections import Counter, defaultdict
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
n = I()
a = LI()
ans = -1
c = 0
for i in range(n):
	d = 0
	for j in range(i, n):
		if a[j] == 0:
			d += 1
		ans = max(ans, c + d + sum(a[j + 1:]))
	if a[i] == 1:
		c += 1
print(ans)


def coins():
	n = int(input())
	coin = [int(i) for i in input().split()]
	if n == 1:
		return 0 if coin[0] == 1 else 1
	su = sum(coin)
	if su == n:
		return n - 1
	flip = [0 if i == 1 else 1 for i in coin]
	no_flip = [0] * n
	for i in range(1, n):
		no_flip[i] = coin[i - 1] + no_flip[i - 1]
	no_flip_r = [0] * n
	for i in range(n - 2, 0, -1):
		no_flip_r[i] = coin[i + 1] + no_flip_r[i + 1]
	max = 0
	for i in range(n):
		aux = 0
		tot = 0
		for j in range(i, n):
			aux += flip[j]
			test = aux + no_flip_r[j] + no_flip[i]
			if test > tot:
				tot = test
		if tot > max:
			max = tot
	return max
print(str(coins()))


n = int(input())
s = input().split()
total = 0
a = 0
X = 0
for i in range(0, n):
	total += int(s[i])
M = total
for i in range(0, n):
	for j in range(i, n):
		a += int(s[j])
		if a <= (j - i) / 2:
			X = total + (j - i) + 1 - 2 * a
		if X >= M:
			M = X
	a = 0
if total == n:
	M = total - 1
print(M)


_ = input()
numbers = [int(i) for i in input().split()]
flip = []
gain = 0
for n in numbers:
	if n == 0:
		gain += 1
	elif gain > 0:
		gain -= 1
	flip.append(gain)
print([numbers.count(1) + max(flip), numbers.count(1) - 1][max(flip) == 0])


def msb(a):
	c = 0
	d = 0
	for b in a:
		c = max(0, c + b)
		d = max(c, d)
	return d
input()
l = list(map(int, input().split()))
nl = []
for i in l:
	if i == 0:
		nl.append(1)
	else:
		nl.append(-1)
ms = msb(nl)
if ms == 0:
	print(sum(l) - 1)
	exit(0)
print(ms + sum(l))


num = int(input())
list1 = []
inp = input()
list2 = []
list3 = []
p = 0
list1.extend([int(i) for i in inp.split()])
for i in range(num):
	for j in range(i, num):
		list3 = list1[:]
		for k in range(i, j + 1):
			if list3[k] == 1:
				list3[k] = 0
			else:
				list3[k] = 1
		p = 0
		for t in range(num):
			if list3[t] == 1:
				p += 1
		list2.append(p)
print(max(list2))


n = int(input())
l = list(map(int, input().strip().split()))
ans = 0
sum = 0
for x in l:
	if x == 1:
		sum += 1
for i in range(0, n):
	for j in range(i, n):
		z = 0
		o = 0
		for k in range(i, j + 1):
			if l[k] == 1:
				o += 1
			else:
				z += 1
		ans = max(ans, sum - o + z)
print(ans)


def kdane(arr):
	n = len(arr)
	max_so_far = 0
	max_till_here = 0
	for i in range(n):
		max_till_here += arr[i]
		if max_till_here < 0:
			max_till_here = 0
		if max_till_here > max_so_far:
			max_so_far = max_till_here
	return max_so_far
n = int(input())
arr = list(map(int, input().split()))
initial = sum(arr)
ans = []
for i in arr:
	if i == 1:
		ans.append(-1)
	else:
		ans.append(1)
x = kdane(ans)
if n == 1:
	if arr[0] == 1:
		print(0)
	else:
		print(1)
elif sum(arr) == n:
	print(n - 1)
else:
	print(x + initial)


(n, t) = (int(input()), list(map(int, input().split())))
k = t.count(1)
p = [0] * n
(s, m) = (0, 0)
for i in range(0, n):
	s += [-1, 1][t[i]]
	t[i] = s
	if s > m:
		m = s
	p[i] = m
(m, s) = (t[-1], p[-1] - t[-1])
for i in range(2, n + 1):
	m = min(t[-i], m)
	d = p[-i] - m
	if s < d:
		s = d
print(k + s - (k == n))


def input_int():
	return int(input())

def input_str():
	return str(input())

def get_result(n, arr):
	s = 0
	for i in range(n):
		s = s + arr[i]
	result = 0
	for i in range(n):
		curr = s
		for j in range(i, n):
			curr = curr + 1 - 2 * arr[j]
			result = max(result, curr)
	return result
n = input_int()
arr = [int(x) for x in input().split()]
print(get_result(n, arr))


n = int(input())
numb = list(map(int, input().split()))
total = 0
long = 0
last = 1
maxi = 0
for i in range(n):
	if 0 not in numb:
		total = n - 1
		break
	if numb[i] == 1:
		if long <= 0:
			long = 0
		else:
			long -= 1
		total += 1
		last = 1
	else:
		long += 1
		if long > maxi:
			maxi = long
		last = 0
print(total + maxi)


n = int(input())
x = input().split()
xx = []
for i in range(n):
	for j in range(i + 1, n + 1):
		m = x.count('1') + x[i:j].count('0') - x[i:j].count('1')
		xx.append(m)
print(max(xx))


n = int(input())
a = [int(i) for i in input().split()]
if n == 1:
	if a[0] == 0:
		print(1)
	else:
		print(0)
	exit()
dp = [0] * n
ones = a[0]
if a[0]:
	dp[0] = -1
else:
	dp[0] = 1
for i in range(1, n):
	if a[i] == 1:
		ones += 1
		dp[i] = dp[i - 1] - 1
	else:
		dp[i] = dp[i - 1] + 1
if ones == n:
	print(n - 1)
	exit()
mn = 0
mx = 0
for i in range(n):
	mn = min(mn, dp[i])
	mx = max(mx, dp[i] - mn)
print(ones + mx)


from sys import stdin
inp = lambda : stdin.readline().strip()
n = int(inp())
a = [int(x) for x in inp().split()]
ans = 0
left = 0
totalOnes = a.count(1)
for i in range(n):
	ones = 0
	zeroes = 0
	for j in range(i, n):
		if a[j] == 0:
			zeroes += 1
		else:
			ones += 1
		toCome = totalOnes - (left + ones)
		tmpAns = zeroes + left + toCome
		ans = max(tmpAns, ans)
	if a[i] == 1:
		left += 1
print(ans)


n = int(input())
line = [int(x) for x in input().split()]
ans = sum(line)
line = [-1 if x == 1 else 1 for x in line]
maxsubsum = -1
cur = 0
for i in range(n):
	cur += line[i]
	maxsubsum = max(maxsubsum, cur)
	cur = 0 if cur < 0 else cur
print(ans + maxsubsum)


num = int(input())
ber = 0
lost = [0]
s = 0
last = list(map(int, input().split()))
lust = list(last)
if 0 not in last:
	print(sum(last) - 1)
else:
	for i in range(num):
		if last[i] == 0:
			for j in range(i, num):
				if lust[j] == 0:
					lust[j] = 1
				elif lust[j] == 1:
					lust[j] = 0
				lost.append(sum(lust))
			lust = list(last)
		elif last[i] == 1:
			pass
	print(max(lost))


input()
a = input().split(' ')
a = ''.join(a)
ans = 0
for i in range(len(a)):
	temp = a.count('1')
	tmax = 0
	c1 = 0
	for j in a[i:]:
		if j == '1':
			temp -= 1
		else:
			temp += 1
		tmax = max(tmax, temp)
	ans = max(ans, tmax)
print(ans)


n = int(input())
arr = [int(i) for i in input().split()]
ans = 0
for i in range(n):
	for j in range(i, n):
		test = arr[:i] + list(map(lambda x: 1 - x, arr[i:j + 1])) + arr[j + 1:]
		ans = max(ans, test.count(1))
print(ans)


n = int(input())
lst = [int(i) for i in input().split()]
output = []
for i in range(n):
	for j in range(i + 1, n + 1):
		total = lst[:i].count(1) + lst[i:j].count(0) + lst[j:].count(1)
		output.append(total)
print(max(output))


n = int(input())
a = list(map(int, input().split()))
seg = []
e = [a[0]] * n
k = 0
for i in range(n):
	if a[i] == 0:
		if i > 0:
			e[i] = e[i - 1]
		if k == 0:
			seg.append([i, i])
			k += 1
		elif seg[k - 1][1] == i - 1:
			seg[k - 1][1] = i
		else:
			seg.append([i, i])
			k += 1
	elif i > 0:
		e[i] = e[i - 1] + 1
if k == 0:
	print(n - 1)
else:
	dp = [0] * k
	dp2 = [0] * k
	dp[0] = seg[0][1] - seg[0][0] + 1
	dp2[0] = seg[0][1] - seg[0][0] + 1
	for i in range(1, k):
		dp2[i] = max(dp2[i - 1] + seg[i][1] - 2 * seg[i][0] + seg[i - 1][1] + 2, seg[i][1] - seg[i][0] + 1)
		dp[i] = max(dp2[i], dp[i - 1])
	print(dp[k - 1] + e[n - 1])


def utada(x, y):
	p = x
	while a[x] == y:
		x += 1
		if x == len(a):
			break
	p1 = x
	return p1 - p

def haru0(p):
	s0 = g[p]
	s1 = 0
	q = p + 1
	v = [a.count(1) + s0]
	s = 0
	while q + 1 < len(g):
		s1 += g[q]
		s0 += g[q + 1]
		s = a.count(1) + s0 - s1
		q += 2
		v.append(s)
	return max(v)
n = int(input())
g = []
b = []
q = 0
a = [int(i) for i in input().split()]
i = 0
if a[0] == 0:
	start = 0
else:
	start = 1
if a[len(a) - 1] == 0:
	end = 0
else:
	end = 1
while i < len(a):
	if a[i] == 1:
		if i == 0:
			f = utada(i, 1)
			g.append(f)
		elif a[i - 1] == 0:
			f = utada(i, 1)
			g.append(f)
	if a[i] == 0:
		if i == 0:
			f = utada(i, 0)
			g.append(f)
		elif a[i - 1] == 1:
			f = utada(i, 0)
			g.append(f)
	i += 1
for p in range(len(g)):
	if start == 0 and p % 2 == 0:
		b.append(haru0(p))
	if start == 1 and p % 2 != 0:
		b.append(haru0(p))
if b == []:
	b.append(a.count(1) - 1)
print(max(b))


n = int(input())
l = list(map(int, input().split()))
c1 = 0
c0 = 0
max = -1
for i in range(0, n):
	if l[i] == 1:
		c1 = c1 + 1
		if c0 > 0:
			c0 = c0 - 1
	if l[i] == 0:
		c0 = c0 + 1
		if c0 > max:
			max = c0
ans = c1 + max
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
m = 0
m_left = 0
m_right = 0
t = 0
t_left = 0
for i in range(n):
	if a[i] == 0:
		t += 1
		if m < t:
			m = t
			m_left = t_left
			m_right = i
	elif t - 1 > 0:
		t -= 1
	else:
		t = 0
		t_left = i + 1
for i in range(m_left, m_right + 1):
	a[i] = 1 - a[i]
print(a.count(1))


n = int(input())
l = [0] + [int(x) for x in input().split()]
for i in range(1, len(l)):
	l[i] += l[i - 1]
ans = 0
for i in range(0, len(l)):
	for j in range(i + 1, len(l)):
		ans = max(ans, l[i] - l[0] + l[-1] - l[j] + j - i - l[j] + l[i])
print(ans)


input()
a = [i * 2 - 1 for i in list(map(int, input().split()))]
if a.count(-1) == 0:
	print(len(a) - 1)
else:
	m = 0
	b = 0
	for i in a:
		b += i
		if b < m:
			m = b
		if b > 0:
			b = 0
	print(a.count(1) - m)


def s():
	n = int(input())
	ar = list(map(int, input().split(' ')))
	sm = sum(ar)
	ans = abs(ar[0] - 1)
	for i in range(n - 1):
		for j in range(i, n):
			ones = ar[i:j + 1].count(1)
			zeros = j - i + 1 - ones
			ans = max(ans, sm + zeros - ones)
	print(ans)
s()


n = int(input())
arr = [int(a) for a in input().split()]
b = [0] * n
c = [0] * 2 * n
currentP = sum(arr)
for index in range(n):
	if arr[index] == 1:
		b[index] = -1
	else:
		b[index] = 1
for i in range(n):
	for j in range(i, n):
		c.append(sum(b[i:j + 1]) + currentP)
print(max(c))


n = int(input())
a = list(map(int, input().split()))
s = 0
S = 0
if 0 not in a:
	print(len(a) - 1)
	quit()
for i in a:
	if i == 0:
		s += 1
		S = max(S, s)
	elif i == 1 and s > 0:
		s -= 1
print(S + a.count(1))


n = int(input())
line = input().split()
each = []
total = []
x = 0
for i in line:
	if i == '1':
		each.append(-1)
		x += 1
	else:
		each.append(1)
for i in range(len(each)):
	for item in range(i, len(each)):
		total.append(sum(each[i:item + 1]))
end = max(total)
print(x + end)


n = int(input())
num_list = list(map(int, input().split()))
score_list = [0] * n
for i in range(n):
	score_list[i] = num_list[i:].count(0) - num_list[i:].count(1)
max_one = -1
for i in range(n):
	if i != n - 1:
		dis_score = score_list[i] - score_list[i + 1]
		for k in range(i + 1, n):
			if k != n - 1:
				dis_score = max([dis_score, score_list[i] - score_list[k + 1]])
			else:
				dis_score = max([dis_score, score_list[i]])
		max_one = max([dis_score, max_one])
	if i == n - 1:
		dis_score = score_list[i]
		max_one = max([dis_score, max_one])
print(max_one + num_list.count(1))


n = int(input())
integers = list(map(int, input().split()))[:n]
b = []
for i in integers:
	if i == 1:
		b.append(-1)
	else:
		b.append(+1)

def maxSubarray(a, size):
	S = integers.count(1)
	current_max = a[0]
	max_so_far = a[0]
	for i in range(1, size):
		current_max = max(a[i], current_max + a[i])
		max_so_far = max(max_so_far, current_max)
	return max_so_far + S
print(maxSubarray(b, len(b)))


n = int(input())
a = input().split()
l = [0]
zbr = 0
for i in range(n):
	if a[i] == '1':
		zbr += 1
	l.append(zbr)
r = []
for i in range(n):
	for j in range(i, n):
		p = l[i]
		s = j - i + 1 - (l[j + 1] - l[i])
		k = l[-1] - l[j + 1]
		r.append(p + s + k)
print(max(r))


n = int(input())
a = [int(x) for x in input().split()]
b = [1 - 2 * x for x in a]
s = 0
m = b[0]
for i in range(n):
	s += b[i]
	m = max(m, s)
	if s < 0:
		s = 0
print(sum(a) + m)


n = int(input())
arr = list(map(int, input().split()))
b = []
for i in range(n):
	if arr[i] == 0:
		b.append(1)
	else:
		b.append(-1)
start = None
end = None
curr = 0
mx = 0
pair = None
for i in range(n):
	if start == None and b[i] == 1:
		start = i
		end = i
		curr += b[i]
	elif start != None:
		curr += b[i]
		end = i
	if curr > mx:
		mx = curr
		pair = (start, end)
	elif curr <= 0:
		curr = 0
		start = None
		end = None
ans = 0
if sum(arr) == len(arr):
	print(sum(arr) - 1)
else:
	for i in range(n):
		if pair[0] <= i <= pair[1]:
			ans += 1 - arr[i]
		else:
			ans += arr[i]
	print(ans)


n = int(input())
a = list(map(int, input().split()))
x = -a[0]
for i in range(n):
	for j in range(i + 1, n + 1):
		b = a[i:j]
		x = max(x, b.count(0) - b.count(1))
print(a.count(1) + x)


n = int(input())
l = list(map(int, input().split()))
a = b = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		a = sum(l[0:i]) + l[i:j].count(0) + sum(l[j:n])
		if a > b:
			b = a
print(b)


n = input()
a = input().split(' ')
for i in range(0, len(a)):
	a[i] = int(a[i])
cnt = 0
for i in range(0, len(a)):
	for j in range(i, len(a)):
		b = a.copy()
		for k in range(i, j + 1):
			b[k] = 1 - b[k]
		x = 0
		for k in range(0, len(b)):
			if b[k] == 1:
				x += 1
		cnt = max(cnt, x)
print(cnt)


n = int(input())
input_list = input().split()
num = []
tot = 0
for i in range(n):
	num.append(int(input_list[i]))
	tot += num[i]
ans = 0
for i in range(n):
	for j in range(i, n):
		cur = tot
		for k in range(i, j + 1):
			if num[k] == 1:
				cur -= 1
			else:
				cur += 1
		if cur > ans:
			ans = cur
print(ans)


def kadane(a, size):
	max_so_far = a[0]
	curr_max = a[0]
	for i in range(1, size):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
b = [-1 if i == 1 else 1 for i in a]
print(kadane(b, n) + s)


(n, a) = (int(input()), [int(x) for x in input().split()])
if not 0 in a:
	print(n - 1)
	quit()
(s, m) = (0, 0)
for row in a:
	row = 1 if row == 0 else -1
	s = max(s + row, row)
	m = max(m, s)
print(m + sum(a))


n = int(input())
a = [int(i) for i in input().split()]
mo = 0
bian = 0
k = 0
if 0 not in a:
	print(sum(a) - 1)
	exit()
else:
	a_0 = a.index(0)
summ = a_0
sum1 = a_0
for i in range(a_0, n):
	k = summ + a[i]
	bian = sum1 + 1 - a[i]
	mo = sum(a[0:i]) + 1 - a[i]
	summ = max(bian, mo, k)
	sum1 = max(bian, mo)
print(summ)


import itertools as it
n = int(input())
a = list(map(int, input().split()))
q = a.count(1)
for i in range(n):
	a[i] = 1 if a[i] == 0 else -1
s = a[0]
minSum = min(a[0], 0)
ans = q + a[0]
for i in range(1, n):
	s += a[i]
	ans = max(ans, q + s - minSum)
	minSum = min(minSum, s)
print(ans)


n = int(input())
a = input().split()
for k in range(n):
	a[k] = int(a[k])
b = sum(a)
d = 0
for l in range(n):
	if a[l] == 0:
		a[l] = 1
	else:
		a[l] = -1
for i in range(n):
	c = 0
	for j in range(i, n):
		c = c + a[j]
		if c >= d:
			d = c
if b == len(a):
	print(b - 1)
else:
	print(d + b)


n = int(input())
l = [int(i) for i in input().split()]
z = l.count(1)
for i in range(n):
	if l[i] == 1:
		l[i] = -1
	else:
		l[i] = 1
curr = maxi = -1000000000
for i in l:
	curr = max(i, curr + i)
	maxi = max(maxi, curr)
print(maxi + z)


n = int(input())
a = [int(i) for i in input().split()]
dp = []
for i in range(n):
	dp.append([0] * n)
dp[0][n - 1] = a.count(0)
for j in range(n - 2, -1, -1):
	dp[0][j] = dp[0][j + 1] + (1 if a[j + 1] else -1)
for i in range(1, n):
	for j in range(n - 1, i - 1, -1):
		dp[i][j] = dp[i - 1][j] + (1 if a[i - 1] else -1)
mV = []
for i in dp:
	mV.append(max(i))
print(max(mV))


n = int(input())
array = [int(j) for j in input().split(' ')]
totalInicial = sum(array)
mejorTotal = totalInicial
for i in range(n):
	for j in range(i, n):
		sumaActual = 0
		for k in range(len(array)):
			if k >= i and k <= j:
				sumaActual += 1 - array[k]
			else:
				sumaActual += array[k]
		if sumaActual > mejorTotal:
			mejorTotal = sumaActual
if mejorTotal == totalInicial:
	print(mejorTotal - 1)
else:
	print(mejorTotal)


n = int(input())
s = input().split()
l = []
for i in range(len(s)):
	for j in range(len(s)):
		if len(s) - j >= 0:
			a = s[i:len(s) - j].count('0')
			b = s[i:len(s) - j].count('1')
			l.append(a - b)
		else:
			pass
	l.sort()
if '0' in s:
	print(s.count('1') + l[len(l) - 1])
else:
	print(s.count('1') - 1)


n = int(input())
l = list(map(int, input().split()))
k = 0
c = 0
for i in range(n):
	if l[i] == 1:
		k = k + 1
if k == n and n == 1:
	c = 1
k1 = 0
ma = 0
for i in range(n):
	if l[i] == 0:
		k1 = k1 + 1
		if k1 > ma:
			ma = k1
	else:
		if k1 > ma:
			ma = k1
		k1 = k1 - 1
		if k1 < 0:
			k1 = 0
if c != 0:
	print(0)
if c == 0 and n == k:
	print(n - 1)
elif c == 0 and n != k:
	print(k + ma)


final = list()
n = int(input())
strin = input()
st = strin.split()
total = 0
for i in range(n):
	total += int(st[i])
tot = total
for i in range(n):
	for j in range(n - i):
		lll = 1 - int(st[j + i])
		total = total - int(st[j + i]) + lll
		final.append(total)
	total = tot
ko = max(final)
print(ko)


n = int(input())
arr = list(map(int, input().split()))
count1 = 0
extra0 = 0
extra0max = -1
for i in arr:
	if i == 1:
		count1 += 1
		if extra0 > 0:
			extra0 -= 1
	else:
		extra0 += 1
		extra0max = max(extra0max, extra0)
print(count1 + extra0max)


from collections import defaultdict
import math
import sys
n = int(input())
numbers = [int(i) for i in input().split(' ')]
memo = [0] * (n + 1)
sol_idx = -1
delta = 0
for (idx, number) in enumerate(numbers, start=1):
	if number == 0:
		memo[idx] = memo[idx - 1] + 1
	else:
		memo[idx] = memo[idx - 1] - 1
	if memo[idx] <= 0:
		memo[idx] = 0
	if memo[idx] > delta:
		delta = memo[idx]
		sol_idx = idx
if delta == 0:
	print(n - 1)
else:
	start_idx = 0
	for i in range(sol_idx, 0, -1):
		if memo[i] == 0:
			start_idx = i + 1
			break
	count = 0
	start_idx -= 1
	sol_idx -= 1
	for (idx, number) in enumerate(numbers):
		if (idx < start_idx or idx > sol_idx) and number == 1:
			count += 1
		elif (idx >= start_idx and idx <= sol_idx) and number == 0:
			count += 1
	print(count)


while True:
	try:
		n = int(input())
		(dif, dif_highest) = (0, 0)
		L = list(map(int, input().split()))
		if L.count(0) == 0:
			print(n - 1)
			break
		num_1 = L.count(1)
		for i in range(len(L)):
			if L[i] != 1:
				break
			L[i] = 9
		for i in L:
			if i == 0:
				dif += 1
			elif i == 1:
				dif -= 1
				if dif < 0:
					dif = 0
			if dif_highest <= dif:
				dif_highest = dif
		result = num_1 + dif_highest
		print(result)
	except EOFError:
		break


n = int(input())
nums = [int(x) for x in input().split()]
num_of_ones = []

def flipper(start, end):
	flipped = nums.copy()
	for i in range(start, end):
		if nums[i] == 0:
			flipped[i] = 1
		elif nums[i] == 1:
			flipped[i] = 0
	num_of_ones.append(flipped.count(1))
if n == 1:
	if nums[0] == 0:
		print(1)
	else:
		print(0)
else:
	for s in range(0, n):
		for e in range(s + 1, n + 1):
			flipper(s, e)
	print(max(num_of_ones))


def main():
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = -1
	(ones, onesInterval, subarrsum) = (0, 0, 0)
	for i in range(n):
		subarrsum += -1 if arr[i] else 1
		ones += arr[i]
		if subarrsum > ans:
			ans = subarrsum
			onesInterval += arr[i]
		if subarrsum < 0:
			subarrsum = 0
			onesInterval = 0
	print(ans + ones - onesInterval)
main()


n = int(input())
numbers = [int(i) for i in input().split()]
for i in range(n):
	if numbers[i] == 1:
		numbers[i] = -1
	else:
		numbers[i] = 1
minim = numbers.count(-1)
end = ans = 0
for i in numbers:
	end = max(end + i, minim)
	ans = max(ans, end)
print(ans if minim != ans else ans - 1)


def f(i, j):
	global a
	s = a.copy()
	for i in range(i, j + 1):
		s[i] = 1 - s[i]
	return s
n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
	for j in range(i, n):
		ans.append(sum(f(i, j)))
print(max(ans))


__author__ = 'myduomilia'
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i, n):
		val = 0
		for k in range(0, i):
			val += a[k]
		for k in range(i, j + 1):
			val += 1 - a[k]
		for k in range(j + 1, n):
			val += a[k]
		ans = max(ans, val)
print(ans)


def maxsub(list1):
	length = len(list1)
	S = [list1[0]] + (length - 1) * [0]
	for i in range(1, length):
		S[i] = max(S[i - 1] + list1[i], list1[i])
	return max(S)
ints = int(input())
nums = [int(x) for x in input().split(' ')]
dnums = []
for element in nums:
	if element == 1:
		dnums.append(-1)
	else:
		dnums.append(1)
print(maxsub(dnums) + sum(nums))


N = int(input())
X = input().split(' ')
Y = X[:]
Z = []
a = 0
b = 0
c = 0
while a <= N - 1:
	while c <= N - a - 1:
		while b <= c:
			Y[a + b] = str(1 - int(Y[a + b]))
			b = b + 1
		Z.append(Y.count('1'))
		b = 0
		Y = X[:]
		c = c + 1
	c = 0
	a = a + 1
print(max(Z))


n = int(input())
data = [int(x) for x in input().split()]
ones = [0] * n
nulls = [0] * n
if data[0] == 1:
	ones[0] = 1
else:
	nulls[0] = 1
for i in range(1, n):
	if data[i] == 0:
		nulls[i] = nulls[i - 1] + 1
		ones[i] = ones[i - 1]
	else:
		nulls[i] = nulls[i - 1]
		ones[i] = ones[i - 1] + 1
ans = -1
for i in range(n):
	for j in range(i, n):
		seg_ones = ones[j] - ones[i]
		seg_nulls = nulls[j] - nulls[i]
		if data[i] == 1:
			seg_ones += 1
		else:
			seg_nulls += 1
		if ans < seg_nulls - seg_ones:
			ans = seg_nulls - seg_ones
print(data.count(1) + ans)


n = int(input())
L = input().split()
M = []
a = 0
p = 0
q = 0
for i in L:
	a = a + int(i)
while p <= q and p <= n - 1:
	while q <= n - 1:
		b = 0
		for i in range(p, q + 1):
			b = b + int(L[i])
		c = q + 1 - p - 2 * b
		M.append(c)
		q = q + 1
	p = p + 1
	q = p
print(a + max(M))


n = int(input())
import copy
a = [int(k) for k in input().split()]
b = copy.copy(a)
summ = []
for i in range(0, n):
	for j in range(i, n):
		for k in range(i, j + 1):
			b[k] = 1 - b[k]
		summ.append(sum(b))
		b = copy.copy(a)
print(max(summ))


from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n = int(input())
arr = list(map(int, input().split()))
if arr.count(1) == n:
	print('%d\n' % (n - 1))
else:
	cnt1 = 0
	cnt0 = 0
	max = 0
	for t in range(n):
		if arr[t] == 0:
			cnt0 += 1
		else:
			cnt1 += 1
		if cnt0 - cnt1 < 0:
			cnt0 = 0
			cnt1 = 0
		if max < cnt0 - cnt1:
			max = cnt0 - cnt1
	print('%d\n' % (max + arr.count(1)))


n = int(input())
a = list(map(int, input().split()))
minsum = 0
maxsum = 0
for i in range(len(a)):
	for j in range(i, len(a)):
		tempminsum = sum(a[i:j + 1])
		tempmaxsum = j - i + 1 - tempminsum
		if tempmaxsum - tempminsum > maxsum - minsum:
			maxsum = tempmaxsum
			minsum = tempminsum
if a.count(1) == n:
	print(n - 1)
else:
	print(max(sum(a), sum(a) - minsum + maxsum))


n = int(input())
arr = [int(i) for i in input().split()]
cnt = 0
for i in range(n):
	if arr[i] == 1:
		arr[i] = -1
		cnt += 1
	else:
		arr[i] = 1
res = -1
current = 0
for i in range(n):
	current = max(arr[i], current + arr[i])
	res = max(res, current)
print(res + cnt)


n = int(input())
a = [int(x) for x in input().split()]
(l, r, s) = (0, 0, 0)
(ml, mr, ms) = (0, 0, None)
for x in a:
	if s < 0:
		s = 0
		l = r
	s += 1 if x == 0 else -1
	if ms is None or s > ms:
		(ml, mr, ms) = (l, r, s)
	r += 1
print(sum(a) + ms)


import os
import sys
from io import BytesIO, IOBase
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')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def isqrt(n):
	x = n
	y = (x + 1) // 2
	while y < x:
		x = y
		y = (x + n // x) // 2
	return x

def abs(x):
	return x if x >= 0 else -x

def sumelem(x, start, end):
	sum = 0
	for i in range(start, end + 1):
		sum += x[i]
	return sum
for t in range(1):
	maxs = -10
	n = int(input())
	flipseq = list(map(int, input().split()))
	no1 = flipseq.count(1)
	for i in range(n):
		if flipseq[i] == 1:
			flipseq[i] = -1
		else:
			flipseq[i] = 1
	for i in range(n):
		for j in range(i, n):
			maxs = max(maxs, sumelem(flipseq, i, j))
	print(no1 + maxs)


n = int(input())
s = [int(i) for i in input().split()]
output = []
z1 = s.count(1)
z0 = s.count(0)
for m in range(0, len(s)):
	for q in range(m, len(s)):
		count1 = 0
		count0 = 0
		w = s[m:q + 1]
		for i in w:
			if i == 1:
				count1 += 1
			elif i == 0:
				count0 += 1
		zong = z1 + count0 - count1
		output.append(zong)
Output = max(output)
print(Output)


n = int(input())
a = list(map(int, input().split()))
ones = [0 for i in range(n + 1)]
zeros = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	if a[i - 1] == 0:
		zeros[i] += zeros[i - 1] + 1
		ones[i] = ones[i - 1]
	else:
		zeros[i] = zeros[i - 1]
		ones[i] += ones[i - 1] + 1
if ones[-1] == 0 and zeros[-1] == 1:
	lmax = 1
else:
	lmax = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		v = zeros[j] - zeros[i] - (ones[j] - ones[i])
		lmax = max(lmax, ones[-1] + v)
print(max(lmax, 0))


n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(0, n):
	for j in range(i, n):
		m = 0
		for k in range(0, i):
			m += 1 if l[k] else 0
		for k in range(i, j + 1):
			m += 1 if not l[k] else 0
		for k in range(j + 1, n):
			m += 1 if l[k] else 0
		ans = max(ans, m)
print(ans)


n = int(input())
s = input().split()
m = -1
if s == ['1']:
	print(0)
elif s == ['0']:
	print(1)
else:
	for i in range(n - 1):
		pref = s[:i].count('1')
		for j in range(i, n):
			c = pref + s[i:j + 1].count('0') + s[j + 1:].count('1')
			if c > m:
				m = c
	print(m)


n = int(input())
row = [int(i) for i in input().split()]
ones = row.count(1)
differences = []
if n == 1:
	print(1 - ones)
else:
	for i in range(0, n):
		for j in range(i, n):
			difference = row[i:j + 1].count(0) - row[i:j + 1].count(1)
			differences.append(difference)
	print(ones + max(differences))


from sys import stdin
n = int(stdin.readline().rstrip())
arr = list(map(int, stdin.readline().rstrip().split(' ')))
zeroes = [0] * (len(arr) + 2)
ones = [0] * (len(arr) + 2)
sum_zeroes = 0
sum_ones = 0
for (i, e) in enumerate(arr):
	if e:
		sum_ones += 1
	else:
		sum_zeroes += 1
	zeroes[i] = sum_zeroes
	ones[i] = sum_ones
m = float('-inf')
for i in range(len(arr)):
	for j in range(i, len(arr)):
		m = max(m, zeroes[j] - zeroes[i - 1] + ones[i - 1] + ones[len(arr) - 1] - ones[j])
print(m)


n = int(input())
arr = list(map(int, input().split()))
gain = list(bytearray(n))
for i in range(n):
	if arr[i] == 0:
		gain[i] = 1
	else:
		gain[i] = -1
maxi = add = gain[0]
for i in range(1, n):
	add += gain[i]
	if gain[i] > add:
		add = gain[i]
	if add > maxi:
		maxi = add
print(f'{maxi + arr.count(1)}')


n = input()
a = list(map(int, input().split()))
prev = 0
result = None
for item in a:
	if result is None or result < prev + 1 - 2 * item:
		result = prev + 1 - 2 * item
	prev = max(0, prev + 1 - 2 * item)
print(result + sum(a))


def rec(i, p, n, arr, dp):
	if i == n:
		dp[i, i] = 1 - arr[i]
		return p + (1 - arr[i])
	maxi = rec(i + 1, p + 1 if arr[i] == 1 else p, n, arr, dp)
	dp[i, i] = 1 - arr[i] + (n - i - dp[i + 1, n])
	maxi = max(p + dp[i, i], maxi)
	for j in range(i + 1, n + 1):
		dp[i, j] = 1 - arr[i] + dp[i + 1, j]
		maxi = max(p + dp[i, j], maxi)
	return maxi
n = int(input())
arr = [int(x) for x in input().split()]
print(rec(0, 0, n - 1, arr, {}))


n = int(input())
mas = list(map(int, input().split()))
(a, b) = (0, 0)
lst = []
for (i, x) in enumerate(mas):
	if x == 1:
		a += 1
	else:
		b += 1
	lst.append([a, b])
n -= 1
if lst[n][1] == 0:
	print(n)
	from sys import exit
	exit()
if lst[n][1] == n + 1:
	print(n + 1)
	from sys import exit
	exit()
res = lst[n][0]
t = res
n += 1
for (i, x) in enumerate(mas):
	(q, v) = (lst[i][0], lst[i][1])
	for j in range(i + 1, n):
		(d, f) = (lst[j][0] - q, lst[j][1] - v)
		if f > d:
			if res < t + f - d:
				res = t + f - d
print(res)


a = int(input())
b = [int(x) for x in input().split()]
n = 0
for l in range(0, a):
	if b[l] == 1:
		n += 1
summy = 0
mx = 0
if n == a:
	mx = -1
else:
	for i in range(0, a):
		for j in range(i + 1, a + 1):
			for k in range(i, j):
				if b[k] == 1:
					summy -= 1
				else:
					summy += 1
			if summy > mx:
				mx = summy
				summy = 0
			else:
				summy = 0
print(mx + n)


n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i, n):
		temp = sum(l[:i]) + l[i:j + 1].count(0) + sum(l[j + 1:])
		ans = max(temp, ans)
print(ans)


n = int(input())
l = list(map(int, input().split()))
g = 0
d = 0
if n == 1:
	print(1 - l[0])
elif l == [1] * n or l == [0] * n:
	print(n - l[0])
else:
	for i in range(n):
		m = 0
		if l[i] == 0:
			for j in range(i, n):
				m = l[i:j + 1].count(0) - l[i:j + 1].count(1)
				if m > d:
					d = m
	print(l.count(1) + d)


q = int(input())
numbers = [int(i) for i in input().split()]
q1 = numbers.count(1)
diff = 0
nmax = 0
if len(numbers) == q1:
	print(q1 - 1)
else:
	for i in range(len(numbers)):
		if numbers[i] == 0:
			diff += 1
			if nmax < diff:
				nmax = diff
		elif numbers[i] == 1:
			diff -= 1
			if diff < 0:
				diff = 0
	print(nmax + q1)


n = int(input())
s = input().split()
for i in range(n):
	s[i] = int(s[i])
ans = sum(s)
if len(s) == 1:
	print(1 - s[0])
elif s.count(0) == len(s):
	print(len(s))
elif s.count(1) == len(s):
	print(sum(s) - 1)
else:
	r = 0
	if s[0] == 0:
		s = [1] + s
		r = 1
	L = [0]
	for i in range(1, n + r):
		if s[i] != s[i - 1]:
			L.append(i)
	L.append(n + r)
	m = len(L)
	maxx = 0
	for i in range(2, m, 2):
		ans = sum(s) - r + L[i] - L[i - 1]
		e = ans
		for j in range(i + 2, m, 2):
			ans = ans + L[j] - L[j - 1] - (L[j - 1] - L[j - 2])
			if ans > e:
				e = ans
		if e > maxx:
			maxx = e
	print(maxx)


input()
a = list(map(int, input().split(' ')))
for i in range(len(a)):
	if a[i] == 1:
		a[i] = -1
	else:
		a[i] = 1
maxsofar = 0
j = a.count(-1)
maxendinghere = j
for i in a:
	maxendinghere = max(maxendinghere + i, j)
	maxsofar = max(maxendinghere, maxsofar)
if maxsofar == j:
	print(maxsofar - 1)
else:
	print(maxsofar)


n = int(input())
a = list(map(int, input().split()))
one_count = [0] * (n + 1)
zero_count = [0] * (n + 1)
ones = 0
zeros = 0
for i in range(n):
	if a[i] == 1:
		ones += 1
	else:
		zeros += 1
	one_count[i + 1] = ones
	zero_count[i + 1] = zeros
m = 0
for i in range(n):
	for j in range(i, n):
		count = one_count[i] + one_count[n] - one_count[j + 1] + zero_count[j + 1] - zero_count[i]
		if m < count:
			m = count
print(m)


n = int(input())
Line = list(map(int, input().split()))
data = 2 * [0]
for i in Line:
	data[i] = data[i] + 1
m = 0
for i in range(n):
	for j in range(i, n):
		p = 0
		for k in range(i, j + 1):
			if Line[k] == 0:
				p = p + 1
			elif Line[k] == 1:
				p = p - 1
		m = max(m, p)
if data[0] != 0:
	print(data[1] + m)
else:
	print(data[1] - 1)


def main():
	N = int(input())
	l = list(map(int, input().strip().split()))
	count = 0
	for i in range(N):
		for j in range(i, N):
			count = max(count, sum(l[:i]) + sum(l[j + 1:]) + (j - i + 1) - sum(l[i:j + 1]))
	print(count)
main()


n = int(input())
mat = map(int, input().split())
a = []
step = 0
for i in mat:
	a.append(i)
result = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		step = sum(a) - 2 * sum(a[i:j]) + j - i
		if result < step:
			result = step
print(result)


n = int(input())
a = [int(t) for t in input().split()]
s = []
for i in range(0, n):
	for j in range(i, n):
		total = sum(a)
		for k in range(i, j + 1):
			if a[k] == 1:
				total = total - 1
			else:
				total += 1
		s.append(total)
print(max(s))


def CF327():
	N = int(input())
	nums = list(map(int, input().split()))
	res = sum(nums)
	nums = [1 if x == 0 else -1 for x in nums]
	(curr, best) = (0, 0)
	for i in range(N):
		if curr + nums[i] < 0:
			best = max(best, curr)
			curr = 0
		else:
			curr += nums[i]
			best = max(best, curr)
	best = max(best, curr)
	if best == 0:
		return res - 1
	res += best
	return res
res = CF327()
print(res)


n = int(input())
s = list(map(int, input().split()))
st = 0
for i in range(n):
	for j in range(i, n):
		sr = sum(s[0:i]) + j - i + 1 - sum(s[i:j + 1]) + sum(s[j + 1:n])
		st = max(sr, st)
print(st)


n = int(input())
num = [int(x) for x in input().split()]
m = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		m = max(m, sum((x == 0 for x in num[i:j])) + sum((x == 1 for x in num[j:n])) + sum((x == 1 for x in num[0:i])))
print(m)


n = int(input())
a = list(map(int, input().split()))
m = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		x = sum(a[:i]) + (j - i) - sum(a[i:j]) + sum(a[j:])
		m = max(x, m)
print(m)


n = int(input())
l = list(map(int, input().strip().split()))
m = 0
if l.count(0) == 0:
	print(n - 1)
elif l.count(0) == 1:
	print(1)
else:
	for i in range(n - 1):
		d = l[0:i]
		s = 0
		c = 0
		for j in range(i + 1, n):
			e = l[j + 1:]
			a = l[i:j + 1]
			s = a.count(1) + d.count(1) + e.count(1)
			for k in range(len(a)):
				if a[k] == 0:
					a[k] = 1
				else:
					a[k] = 0
			c = a.count(1) + d.count(1) + e.count(1)
			m = max(s, c, m)
	print(m)


n = input()
n = int(n)
integers = input().split()
integers = [int(x) for x in integers]
m = sum(integers)
div_i = list(range(n))
for i in div_i:
	div_j = div_i[i:]
	for j in div_j:
		new = []
		k = 0
		ij = list(range(i, j + 1))
		while k < n:
			if k in ij:
				x = 1 - integers[k]
				new.append(x)
			else:
				new.append(integers[k])
			k += 1
		if sum(new) >= m:
			m = sum(new)
if m == sum(integers):
	print(m - 1)
else:
	print(m)


def solve(s):
	(res, t) = (0, 0)
	for i in range(len(s)):
		if s[i] == 1:
			t += 1
		else:
			t -= 1
			if t < 0:
				t = 0
		res = max(res, t)
	return res
n = int(input())
a = [int(x) for x in input().split()]
s = []
for i in range(n):
	if a[i] == 0:
		s.append(1)
	else:
		s.append(-1)
if str(a).count('1') == n:
	print(n - 1)
else:
	print(solve(s) + str(a).count('1'))


n = int(input())
ai = list(map(int, input().split()))
ans = sum(ai)
num = ans
ans = max(ans, n - num)
for i in range(n):
	for j in range(i, n):
		num1 = 0
		num2 = 0
		for z in range(i, min(j + 1, n)):
			if ai[z] == 0:
				num1 += 1
			else:
				num2 += 1
		ans = max(ans, num - num2 + num1)
if sum(ai) == n:
	ans -= 1
print(ans)


n = int(input())
arr = [int(i) for i in input().split()]
pre = [0] * n
pre[0] = 1 - arr[0]
for i in range(1, n):
	pre[i] = 1 - arr[i] + pre[i - 1]
ans = float('-inf')
for i in range(n):
	for j in range(i, n):
		zero = pre[j] - (pre[i - 1] if i > 0 else 0)
		ones = j - i + 1 - zero
		ans = max(ans, zero - ones)
original = 0
for i in range(n):
	if arr[i]:
		original += 1
print(original + ans)


n = int(input())
lst = list(map(int, input().split()))
ones = lst.count(1)
last_ele = ones
for i in range(n):
	if lst[i] == 1:
		lst[i] = max(ones - 1, last_ele - 1)
	else:
		lst[i] = max(ones + 1, last_ele + 1)
	last_ele = lst[i]
print(max(lst))


n = int(input())
s = list(map(int, input().split()))
sum = 0
for t in range(n):
	sum += s[t]
if sum == n:
	print(n - 1)
else:
	output = []
	for i in range(n):
		for j in range(n - i):
			counter = 0
			for k in range(i, i + j + 1):
				counter += s[k]
			if counter < (j + 1) / 2:
				output.append(j + 1 - 2 * counter)
			else:
				output.append(int(0))
	maxi = max(output)
	print(sum + maxi)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	if n == 1:
		print(1 - a[0])
		exit()
	for x in a:
		if x == 1:
			b.append(-1)
		else:
			b.append(1)
	s = sum(a)
	max_sum = 0
	t = max(b[0], 0)
	for i in range(1, n):
		t = max(t + b[i], 0)
		max_sum = max(max_sum, t)
	if max_sum == 0:
		max_sum = -1
	print(s + max_sum)
main()


R = lambda : map(int, input().split())
n = int(input())
a = list(R())
if sum(a) == n:
	print(n - 1)
	exit(0)
(res, cnt, s) = (0, 0, 0)
for x in a:
	s += x
	cnt += 1 if not x else -1
	cnt = max(0, cnt)
	res = max(res, cnt)
print(s + res)


n = int(input())
arr = [int(x) for x in input().split()]
val = arr.count(1)
ans = 0
for i in range(0, n):
	for j in range(i, n):
		ans = max(ans, val + arr[i:j + 1].count(0) - arr[i:j + 1].count(1))
print(ans)


import string
n = int(input())
l = ''.join(input().split())
N = 0
for i in range(n):
	for j in range(i, n):
		m = l.count('1') + j + 1 - i - 2 * l[i:j + 1].count('1')
		if m > N:
			N = m
print(N)


a = int(input())
p = [int(x) for x in input().split()]
print(max((p[i:j + 1].count(0) + p[:i].count(1) + p[j + 1:].count(1) for i in range(a) for j in range(i + 1, a + 1))))


n = int(input())
a = [int(x) for x in input().split()]
mx = 0
for i in range(n):
	for j in range(i, n):
		flip = [1 - x if i <= k <= j else x for (k, x) in enumerate(a)]
		mx = max(mx, sum(flip))
print(mx)


x = int(input())
li = list(map(int, input().split()))
(ans, cnt) = (0, 0)
for j in range(x):
	cnt = 0
	for i in range(j, x):
		if li[i] == 0:
			cnt += 1
		ans = max(ans, li[:j].count(1) + cnt + li[i + 1:].count(1))
print(ans if li.count(0) else x - 1)


total_number = int(input())
numbers = list(map(int, input().split()))
current_peak = 0
peak = 0
for number in numbers:
	if number == 0:
		current_peak += 1
	else:
		current_peak -= 1
	if current_peak < 0:
		current_peak = 0
	if peak < current_peak:
		peak = current_peak
if peak == 0:
	peak = -1
print(numbers.count(1) + peak)


n = int(input())
line = [int(x) for x in input().split()]
value = sum(line)
if n == 1:
	value = 1 - value
elif value == n:
	value -= 1
else:
	for i in range(n):
		for j in range(i + 1, n):
			new_line = line.copy()
			for y in range(i, j + 1):
				new_line[y] = 1 - new_line[y]
				value = max(sum(new_line), value)
print(value)


n = int(input())
inList = input().split()
aList = []
osum = 0
for a in inList:
	aList.append(int(a))
	osum += int(a)
maxf = -1
for i in range(n):
	for j in range(i, n):
		isum = 0
		for k in range(i, j + 1):
			isum += aList[k]
		flag = j + 1 - i - 2 * isum
		maxf = max(maxf, flag)
print(osum + maxf)


def parser():
	while 1:
		data = list(input().split(' '))
		for number in data:
			if len(number) > 0:
				yield number
input_parser = parser()

def get_word():
	global input_parser
	return next(input_parser)

def get_number():
	data = get_word()
	try:
		return int(data)
	except ValueError:
		return float(data)
n = get_number()
A = []
B = []
zero_count = 0
for i in range(n):
	a = get_number()
	A.append(a)
	if a == 0:
		zero_count += 1
		B.append(1)
	else:
		B.append(-1)

def max_subarray(B):
	i = 0
	j = 0
	s = 0
	maxSum = 0
	tempMax = 0
	for k in range(0, len(B)):
		tempMax += B[k]
		if tempMax > maxSum:
			maxSum = tempMax
			i = s
			j = k
		if tempMax < 0:
			s = k + 1
			tempMax = 0
	return (maxSum, i, j)
if zero_count == 0:
	print(sum(A) - 1)
else:
	(_, i, j) = max_subarray(B)
	for k in range(i, j + 1):
		A[k] = 1 - A[k]
	print(sum(A))


n = int(input())
a = list(map(int, input().split()))
(c, i, j) = (0, 0, 0)
(mx, mxi, mxj) = (0, 0, 0)
for (k, x) in enumerate(a):
	if x == 0:
		(c, j) = (c + 1, k)
	else:
		c = c - 1
		if c <= 0:
			(c, i) = (0, k + 1)
	if c >= mx:
		(mx, mxi, mxj) = (c, i, j)
if mxj <= mxi:
	(mxi, mxj) = (0, 0)
s = 0
for (i, x) in enumerate(a):
	if i < mxi or i > mxj:
		s += x
	else:
		s += 1 - x
print(s)


n = int(input())
flips = list(map(int, input().split()))
a = [0]
ones = 0
for k in range(n):
	a.append(a[-1] - 2 * flips[k] + 1)
	if flips[k] == 1:
		ones += 1
m = a[1] - a[0]
for i in range(len(a)):
	for j in range(i + 1, len(a)):
		if a[j] - a[i] > m:
			m = a[j] - a[i]
print(m + ones)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return list(map(int, input().split()))

def I():
	return int(input())

def LS():
	return input().split()

def S():
	return input()

def main():
	n = I()
	l = LI()
	sm = sum(l)
	mx = -inf
	for i in range(n):
		for j in range(1, n + 1):
			if i + j > n:
				continue
			a = l[i:i + j].count(0)
			mx = max(mx, sm + 2 * a - j)
	return mx
print(main())


def solve():
	totalones = arr.count(1)
	maxones = 0
	for i in range(len(arr)):
		for j in range(i, len(arr)):
			ones = 0
			zeroes = 0
			ones += arr[i:j + 1].count(0)
			zeroes += arr[i:j + 1].count(1)
			maxones = max(maxones, abs(totalones - zeroes) + ones)
	return maxones
n = int(input())
arr = list(map(int, input().split()))
print(solve())


n = int(input())
l = list(map(int, input().split(' ')))
cur = 0
for elm in l:
	if elm == 1:
		cur += 1
f = [0] * n
for i in range(n):
	if l[i] == 1:
		f[i] = -1
	else:
		f[i] = 1
arr = [0] * n
arr[0] = f[0]
for i in range(1, n):
	arr[i] = max(arr[i - 1] + f[i], f[i])
s = max(arr)
print(cur + s)


input()
a = [int(i) for i in input().split()]
s = 0
for i in range(len(a)):
	for j in range(i, len(a)):
		a0 = a[:]
		for k in range(i, j + 1):
			a0[k] = (a0[k] - 1) ** 2
		s = max(s, sum(a0))
print(s)


n = int(input())
line = input().split()
line1 = line[:]
max = 0
s = 0
for i in range(0, n):
	for j in range(0, i + 1):
		for k in range(j, i + 1):
			if line1[k] == '1':
				line1[k] = '0'
			else:
				line1[k] = '1'
		for p in range(n):
			if line1[p] == '1':
				s += 1
				if s > max:
					max = s
		s = 0
		line1 = line[:]
print(max)


n = int(input())
a = [int(x) for x in input().split()]
mx = -1
st = 0
end = -1
for i in range(n):
	c1 = 0
	c0 = 0
	for j in range(i, n):
		if a[j] == 0:
			c0 += 1
		else:
			c1 += 1
		res = c0 - c1
		if res > mx:
			st = i
			end = j
			mx = res
if a.count(0) == 0:
	print(n - 1)
else:
	c1 = 0
	i = 0
	while i < n and i < st:
		if a[i] == 1:
			c1 += 1
		i += 1
	i = end + 1
	while i < n:
		if a[i] == 1:
			c1 += 1
		i += 1
	i = st
	c = 0
	while i <= end:
		if a[i] == 1:
			c += 1
		i += 1
	print(mx + c1 + c)


n = int(input())
sum = 0
b = []
l = input().split()
if '0' in l:
	if '1' not in l:
		c = len(l)
	elif '1' in l:
		for i in range(n):
			for j in range(i + 1, n + 1):
				q = l[0:i + 1].count('1')
				d = l[i + 1:j + 1].count('0')
				e = l[j + 1:n + 1].count('1')
				sum = q + d + e
				b.append(sum)
				c = max(b)
elif '0' not in l:
	c = len(l) - 1
print(c)


n = int(input())
a = input().split()
sum = 0
l = []
for i in range(n):
	a[i] = int(a[i])
	sum += a[i]
	l.append(a[i])
if sum == n:
	print(sum - 1)
else:
	for i in range(n):
		for k in range(i, n + 1):
			sum1 = 0
			for g in range(n):
				a[g] = l[g]
			for j in range(i, k):
				if a[j] == 0:
					a[j] = 1
				else:
					a[j] = 0
			for h in range(n):
				sum1 += a[h]
			if sum1 > sum:
				sum = sum1
	print(sum)


n = input()
l = [1 - 2 * int(x) for x in input().split()]
s = 0
maxs = 0
ones = 0
for x in l:
	if x == -1:
		ones += 1
	s += x
	if s < 0:
		s = 0
	if maxs < s:
		maxs = s
ones = ones + maxs if ones != len(l) else ones - 1
print(ones)


n = int(input())
l = list(map(int, input().split()))
ll = [0]
for i in range(1, n + 1):
	ll.append(ll[i - 1] + l[i - 1])
mx = ll[-1]
cou = 0
for i in range(n + 1):
	if i < n:
		if l[i] == 0:
			cou += 1
	for j in range(i + 1, n + 1):
		if ll[-1] - (ll[j] - ll[i]) + (j - i - (ll[j] - ll[i])) > mx:
			mx = ll[-1] - (ll[j] - ll[i]) + (j - i - (ll[j] - ll[i]))
if cou == 0:
	print(n - 1)
else:
	print(mx)


n = int(input())
A = list(map(int, input().split()))
M = [[0 for j in range(n)] for i in range(n)]
ones = 0
m = -2
for i in range(n):
	if A[i] == 0:
		M[i][i] = 1
	else:
		M[i][i] = -1
		ones += 1
	m = max(m, M[i][i])
for i in range(n):
	for j in range(i + 1, n):
		M[i][j] = M[i][j - 1] + 1 if A[j] == 0 else M[i][j - 1] - 1
		m = max(m, M[i][j])
print(ones + m)


def get_sum(arr):
	res = 0
	for ele in arr:
		res += ele
	return res
n = int(input())
arr = list(map(int, input().split()))
res = 0
for i in range(n):
	for j in range(i, n):
		temp = arr.copy()
		for k in range(i, j + 1):
			temp[k] = 1 - temp[k]
		res = max(res, get_sum(temp))
print(res)


m = int(input())
a = input()
b = a.split()
ans = 0
s = 0
t = 0
if b.count('1') == m:
	ans = m - 1
else:
	for i in range(m):
		for j in range(m - i):
			if b[i + j] == '0':
				s = s + 1
			else:
				s = s - 1
			if s > t:
				t = s
		s = 0
	p = b.count('1')
	ans = p + t
print(ans)


l = int(input())
a = input().split()
b = []
for i in range(l):
	for j in range(i + 1, l + 1):
		b.append(a.count('1') - j + i + 2 * a[i:j].count('0'))
print(max(b))


n = int(input())
l = list(map(int, input().split()))
b = []
c1 = 0
for i in l:
	if i:
		b += [-1]
		c1 += 1
	else:
		b += [1]
(ma, pre) = (-101, 0)
for i in range(n):
	pre = max(pre, 0)
	pre += b[i]
	ma = max(ma, pre)
print(c1 + ma)


n = int(input())
s = input()
m = 0
for i in s:
	s = s.replace(' ', '')
for i in range(0, len(s)):
	for j in range(i, len(s)):
		k = s[i:j + 1]
		for z in k:
			k = k.replace('0', '-2')
		for z in k:
			k = k.replace('1', '0')
		for z in k:
			k = k.replace('-2', '1')
		s1 = s[:i] + k + s[j + 1:]
		if s1.count('1') > m:
			m = s1.count('1')
print(m)


n = int(input())
l = list(map(int, input().split()))
m = -1
for i in range(n):
	for j in range(i, n):
		c = 0
		for ind in range(n):
			if i <= ind <= j:
				if l[ind] == 0:
					c += 1
			elif l[ind] == 1:
				c += 1
		m = max(m, c)
print(m)


from collections import deque
t = int(input())
x = [int(x) for x in input().split()]
x = deque(x)
sub = 0
gain = -(len(x) - 2)
if t > 1:
	try:
		first = x.index(0)
		if x[0] == 0:
			x.appendleft(1)
			sub += 1
		if x[len(x) - 1] == 0:
			x.append(1)
			sub += 1
		y = deque([])
		for i in range(len(x)):
			if x[i] == 1:
				y.append(i)
		for i in range(len(y)):
			c = 0
			for j in range(i + 1, len(y)):
				t = y[j] - y[i] - 1
				g = t - 2 * c
				if g > gain:
					gain = g
				c += 1
		print(len(y) - sub + gain)
	except ValueError:
		print(len(x) - 1)
elif x[0] == 1:
	print(0)
else:
	print(1)


Max = 0
n = int(input())
nums = [int(i) for i in input().split()]
nums1 = [int(i) for i in nums]
for i in range(n):
	for j in range(i, n):
		for m in range(i, j + 1):
			nums[m] = 1 - nums[m]
		if nums.count(1) > Max:
			Max = nums.count(1)
		nums = [int(i) for i in nums1]
print(Max)


n = int(input())
a = list(map(int, input().split()))
pref = [0] * n
pref[0] = 1 if a[0] == 1 else 0
for i in range(1, n):
	if a[i] == 1:
		pref[i] = 1
	pref[i] += pref[i - 1]

def getPref(pref, a, b):
	if a == 0:
		return pref[b]
	return pref[b] - pref[a - 1]
max_add = -1
for i in range(n):
	for j in range(i, n):
		max_add = max(max_add, j - i + 1 - 2 * getPref(pref, i, j))
print(pref[n - 1] + max_add)


(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, dgraphs, mod) = (lambda : sorted(zz()), {}, 10 ** 9 + 7)
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
from bisect import bisect as bs
from itertools import accumulate as ac

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())
l = list(map(int, z().split()))
ans = 0
for i in range(n):
	for j in range(i + 1):
		p = l[:]
		for i in range(j, i + 1):
			if p[i] == 0:
				p[i] = 1
			else:
				p[i] = 0
		one = p.count(1)
		ans = max(one, ans)
print(ans)


def flip(seq):
	for i in range(len(seq)):
		if seq[i] == 0:
			seq[i] = 1
		else:
			seq[i] = 0
	return seq

def sumseq(seeq):
	summ = 0
	for i in seeq:
		summ += i
	return summ
rets = []
digs = int(input())
lsit = tuple(map(int, input().split()))
temp = list(lsit)
for i in range(digs):
	for j in range(i + 1, digs + 1):
		temp[i:j] = flip(temp[i:j])
		rets.append(sumseq(temp))
		temp = list(lsit)
print(max(rets))


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
n = mint()
a = list(mints())
s = sum(a)
r = 0
for i in range(n):
	z = s
	for j in range(i, n):
		z -= a[j]
		z += 1 - a[j]
		r = max(r, z)
print(r)


n = int(input())
a = list(map(int, input().strip().split(' ')))
cnt1 = a.count(1)
if n == 1:
	print(1 - a[0])
elif cnt1 == n:
	print(n - 1)
else:
	max1 = cnt1
	for i in range(n):
		for j in range(i + 1, n):
			cnt = cnt1 + a[i:j + 1].count(0) - a[i:j + 1].count(1)
			max1 = max(max1, cnt)
	print(max1)


n = int(input())
a = [int(i) for i in input().split()]
max_sum = -1
res = 0
cou = 0
for i in a:
	if i == 0:
		cou += 1
	else:
		res += 1
		cou -= 1
	if cou > max_sum:
		max_sum = cou
	if cou < 0:
		cou = 0
print(max_sum + res)


n = int(input())
a = [int(x) for x in input().split()]
cnt = 1
c = 1
o = 0
z = 0
l = []
for i in range(n):
	if a[i] == 0:
		z += 1
		for j in range(i + 1, n):
			if a[j] == 0:
				cnt += 1
			elif a[j] == 1:
				cnt -= 1
			c = max(c, cnt)
		cnt = 1
		l.append(c)
		c = 1
	else:
		o += 1
if z > 0:
	print(o + max(l))
else:
	print(n - 1)


import math
from bisect import bisect_left, bisect, bisect_right
from itertools import accumulate

def transformer(i):
	if i:
		return -1
	return 1

def index(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return -1

def find_lt(a, x):
	i = bisect_left(a, x)
	if i:
		return a[i - 1]
	raise ValueError

def find_le(a, x):
	i = bisect_right(a, x)
	if i:
		return a[i - 1]
	raise ValueError

def find_gt(a, x):
	i = bisect_right(a, x)
	if i != len(a):
		return a[i]
	raise ValueError

def find_ge(a, x):
	i = bisect_left(a, x)
	if i != len(a):
		return a[i]
	raise ValueError
st = ''

def func(n, l):
	l1 = list(map(transformer, l))
	one = l.count(1)
	maxcur = maxglo = l1[0]
	for i in range(1, n):
		maxcur = max(maxcur + l1[i], l1[i])
		maxglo = max(maxglo, maxcur)
	return maxglo + one
n = int(input())
l1 = list(map(int, input().split()))
print(func(n, l1))


import sys
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))
A = [0] * n
sum = l.count(1)
for i in range(n):
	if l[i] == 0:
		A[i] = 1
	else:
		A[i] = -1
s = A[0]
ans = -1
maxsum = s
for i in range(1, n):
	maxsum = max(A[i], s + A[i])
	s = maxsum
	ans = max(ans, maxsum)
ans = max(ans, s)
print(ans + sum)


def main():
	n = int(input())
	ip = [int(item) for item in input().split(' ')]
	sum_ip = sum(ip)
	prefix_sum = []
	prefix_sum.append(ip[0])
	for i in range(1, len(ip)):
		prefix_sum.append(prefix_sum[i - 1] + ip[i])
	dp = []
	for i in range(n):
		temp = []
		for j in range(n):
			temp.append(0)
		dp.append(temp)
	j = 0
	temp = 0
	for c in range(n):
		for i in range(n - c):
			j = i + c
			temp = max_1s(i, j, ip, prefix_sum, sum_ip)
			if j - 1 >= 0 and i + 1 < n:
				dp[i][j] = max(temp, dp[i][j - 1], dp[i + 1][j])
			else:
				dp[i][j] = temp
	print(dp[0][n - 1])

def max_1s(i, j, ip, prefix_sum, sum_ip):
	i_j_ones = prefix_sum[j] - prefix_sum[i]
	if ip[i] == 1:
		i_j_ones += 1
	return j + 1 - i - 2 * i_j_ones + sum_ip
main()


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
c = []
d = []
for i in range(0, len(a)):
	for j in range(i, len(a)):
		for k in range(i, j + 1):
			a[k] = 1 - a[k]
		c.append(a)
		a = b.copy()
for i in range(len(c)):
	d.append(c[i].count(1))
print(max(d))


n = int(input().strip(' '))
l = list(map(int, input().split()))
count = 0
ans = 0
length = 0
for i in range(n):
	if l[i] == 1:
		count += 1
for i in range(n):
	if l[i] == 1:
		if length > 0:
			length -= 1
	if l[i] == 0:
		length += 1
	ans = max(ans, length)
if ans == 0:
	print(count - 1)
else:
	print(ans + count)


n = int(input())
arr = list(map(int, input().split()))
(ones, zeros, extraOne) = (0, 0, -1)
for i in arr:
	if i == 1:
		ones += 1
		if zeros > 0:
			zeros -= 1
	else:
		zeros += 1
		if zeros > extraOne:
			extraOne = zeros
print(ones + extraOne)


def main():
	input()
	s = input()
	t = []
	c1 = 0
	for i in s:
		if i == '0':
			t.append(1)
		elif i == '1':
			t.append(-1)
			c1 += 1
	maxe = maxk = 0
	for i in t:
		maxe += i
		if maxe < 0:
			maxe = 0
		elif maxk < maxe:
			maxk = maxe
	if maxk == 0:
		print(c1 - 1)
	else:
		print(maxk + c1)
main()


n = int(input())
line = [int(j) for j in input().split()]
m = []
for k in range(n):
	for j in range(k, n):
		m.append(line[k:j + 1].count(0) - line[k:j + 1].count(1))
i = max(m)
print(line.count(1) + i)


import time, sys
x = time.localtime().tm_sec
n = int(input())
num = input()
arr = []
counting = 0
for m in range(0, len(num)):
	if num[m] == ' ':
		arr.append(int(num[counting:m]))
		counting = m + 1
	elif m == len(num) - 1:
		arr.append(int(num[counting:len(num)]))
starting_index = None
ending_index = None
i = 0
count = 0
check = False
while check is False:
	y = time.localtime().tm_sec
	if y - x == 8:
		sys.exit('TIME LIMIT EXCEEDED')
	if arr[i] == 0:
		starting = i
		one_count = 0
		zero_count = 0
		data = [False] * len(arr)
		for j in range(i, len(arr)):
			if arr[j] == 0 and data[j] is False:
				zero_count += 1
				data[j] = True
				diff = zero_count - one_count
				if diff > count:
					count = diff
					starting_index = i
					ending_index = j
			elif arr[j] == 0:
				zero_count += 1
			elif arr[j] == 1:
				one_count += 1
	if i == len(arr) - 1:
		break
	i += 1
if starting_index is None or ending_index is None:
	print(n - 1)
else:
	string_val = ''
	for w in arr:
		string_val += str(w)
	for l in range(starting_index, ending_index + 1):
		if string_val[l] == '0':
			if l == 0:
				string_val = '1' + string_val[l + 1:len(string_val)]
			elif l == len(string_val) - 1:
				string_val = string_val[0:len(string_val) - 1] + '1'
			else:
				string_val = string_val[0:l] + '1' + string_val[l + 1:len(string_val)]
		elif l == 0:
			string_val = '0' + string_val[l + 1:len(string_val)]
		elif l == len(string_val) - 1:
			string_val = string_val[0:len(string_val) - 1] + '0'
		else:
			string_val = string_val[0:l] + '0' + string_val[l + 1:len(string_val)]
	result = 0
	for z in string_val:
		if z == '1':
			result += 1
	print(result)


n = int(input())
a = list(input().split())
for i in range(0, n):
	a[i] = int(a[i])
s = n - sum(a)
for i in range(0, n):
	for j in range(i, n):
		ss = 0
		for k in range(i, j + 1):
			ss += a[k]
		if s < sum(a) - ss + j - i + 1 - ss:
			s = sum(a) - ss + j - i + 1 - ss
print(s)


import re
n = int(input())
m = input()
s = []
l = []
r = re.split('\\s+', m)
for i in range(n):
	if int(r[i]) == 0:
		s.append(0)
	else:
		s.append(1)
i = 0
while i < n:
	j = 0
	while j <= i:
		sum = 0
		for k in range(n):
			if j <= k <= i:
				sum += 1 - s[k]
			else:
				sum += s[k]
		l.append(sum)
		j += 1
	i += 1
a = max(l)
print(a)


n = int(input())
a = list(map(int, input().split()))
d = [0] * n
d[0] = 1 if a[0] == 1 else 0
for i in range(1, n):
	if a[i] == 1:
		d[i] = d[i - 1] + 1
	else:
		d[i] = d[i - 1]
m = 0
for i in range(n):
	k = d[i] if a[i] == 0 else d[i] - 1
	for j in range(i, n):
		c = k + j - i + 1 - (d[j] - k) + d[n - 1] - d[j]
		if c > m:
			m = c
print(m)


def get(inp):
	(cur, ma) = (0, -10 ** 10)
	(l, r, prev) = (0, 0, 0)
	for i in range(len(inp)):
		if inp[i] == 0:
			cur += 1
		else:
			cur -= 1
		if cur > ma:
			l = prev
			ma = cur
			r = i
		if cur < 0:
			cur = 0
			prev = i + 1
	return (l, r, ma)
n = int(input().strip())
(cur, ma) = (0, 0)
(st, en) = (0, 0)
arr = [int(__) for __ in input().strip().split()]
(l, r, maa) = get(arr)
ans = arr.count(1)
for i in range(l, r + 1):
	if arr[i] == 1:
		ans -= 1
	else:
		ans += 1
print(ans)


def conv(a):
	for i in range(len(a)):
		if a[i] == 1:
			a[i] = -1
		else:
			a[i] = 1
	return a

def dp(r):
	p = len(r)
	b = conv(r)
	c = [0] * p
	if b[0] > 0:
		c[0] = b[0]
	else:
		c[0] = 0
	for i in range(1, p):
		if c[i - 1] + b[i] > 0:
			c[i] = c[i - 1] + b[i]
		else:
			c[i] = 0
	return c
n = int(input())
r = list(map(int, input().split(' ')))
t = r.count(1)
if t == n:
	print(n - 1)
else:
	v = dp(r)
	print(max(v) + t)


n = int(input())
l = list(map(int, input().split()))
m = []
if len(set(l)) == 1 and l[0] == 0:
	print(n)
elif len(set(l)) == 1 and l[0] == 1:
	print(n - 1)
else:
	for i in range(n):
		for j in range(i, n):
			m.append(l[i:j + 1].count(0) + (l[:i] + l[j + 1:]).count(1))
	print(max(m))


n = int(input())
a = [str(x) for x in input().split()]
s = ''.join(a)
ma = 0
if n == 1:
	ma = 1 - int(a[0])
	print(ma)
else:
	for i in range(n):
		if a[i] == '0':
			for j in range(i, n):
				if a[j] == '0':
					if j != n - 1:
						t = s[:i].count('1') + s[i:j + 1].count('0') + s[j + 1:].count('1')
						ma = max(ma, t)
					else:
						t = s[:i].count('1') + s[i:j + 1].count('0')
						ma = max(ma, t)
	if ma == 0:
		print(n - 1)
	else:
		print(ma)


n = int(input())
A = input().split()
m = 0
for i in range(n):
	if A[i] == '1':
		m += 1
B = []
C = []
for i in range(n):
	a = 0
	for j in range(n - i):
		if A[i + j] == '0':
			a = a - 1
			B.append(a)
		if A[i + j] == '1':
			a = a + 1
			B.append(a)
	B.sort()
	C.append(B[0])
C.sort()
k = -C[0] + m
print(k)


from copy import deepcopy
n = int(input())
s = [int(s) for s in input().split()]
t = []
p = deepcopy(s)
for i in range(n):
	for j in range(i, n):
		p[j] = 1 - p[j]
		t.append(sum(p))
	p = deepcopy(s)
print(max(t))


n = int(input())
arr = [-1 if x == '1' else 1 for x in input().split()]
global_max = 0
current_max = 0
end = 0
while end < n:
	current_max += arr[end]
	if current_max < 0:
		current_max = 0
	if global_max < current_max:
		global_max = current_max
	end += 1
if global_max == 0:
	if n == 1:
		print(global_max)
	else:
		print(n - 1)
else:
	for i in arr:
		if i == -1:
			global_max += 1
	print(global_max)


t = int(input())
a = list(map(int, input().split()))
countone = 0
countzero = -1
zero = 0
for i in range(t):
	if a[i] == 1:
		countone += 1
		if zero > 0:
			zero -= 1
	else:
		zero += 1
		if zero > countzero:
			countzero = zero
print(countone + countzero)


n = int(input())
a = [int(x) for x in input().split()]
count = 0
for x in a:
	if x == 1:
		count += 1
maxi = -1
for i in range(0, n):
	ones = 0
	for j in range(i, n):
		if a[j] == 1:
			ones += 1
		maxi = max(maxi, count + j - i + 1 - 2 * ones)
print(maxi)


n = int(input())
l = input().split()
l1 = len(l)
for i in range(l1):
	l[i] = int(l[i])
r = sum(l)

def sun(l, x, y, r):
	(s, s1) = (0, 0)
	l1 = len(l)
	for i in range(x):
		s += l[i]
	for i in range(y + 1, l1):
		s += l[i]
	for i in range(x, y + 1):
		s += 1 - l[i]
	if s > r:
		return s
	else:
		return s1
if n == r:
	print(r - 1)
else:
	high = 1
	for i in range(l1 - 1):
		for j in range(i + 1, l1):
			k = sun(l, i, j, r)
			if k > high:
				high = k
	print(high)


input()
a = list(map(int, input().split()))
b = [0] + [0 for i in a]
c = [0] + [0 for i in a]
d = -1000
for i in range(len(a)):
	b[i + 1] = b[i] + a[i]
	c[i + 1] = c[i] + 1 - a[i]
for i in range(len(a)):
	for j in range(i + 1):
		d = max(d, c[i + 1] - c[j] - b[i + 1] + b[j])
print(d + b[-1])


n = int(input())
ls = list(map(int, input().split()))
results = []
for i in range(len(ls)):
	for j in range(len(ls[i:])):
		results.append(sum(ls[:i] + [0 if n == 1 else 1 for n in ls[i:j + i + 1]] + ls[j + i + 1:]))
print(max(results))


n = int(input())
x = input().split()
a = []
for i in x:
	a.append(int(i))
(q, w, e, r) = (0, 0, 0, -1)
for i in range(n):
	q += a[i]
	w += 1 - a[i] * 2
	r = max(r, w - e)
	e = min(w, e)
ans = q + r
print(ans)


n = int(input())
li = list(map(int, input().split()))
maxl = 0
start = 0
end = 0
itr = 0
i = 0
j = 0
for i in range(n):
	if li[i] == 0:
		itr = 0
		for j in range(i, n):
			if li[j] == 0:
				itr += 1
			else:
				itr -= 1
			if itr > maxl:
				maxl = itr
				start = i
				end = j
if itr > maxl:
	start = i
	end = j
for k in range(start, end + 1):
	li[k] = 1 - li[k]
count = 0
for k in range(n):
	if li[k] == 1:
		count += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
initial_ones = a.count(1)
max_ones = 0
for i in range(n):
	current_ones = initial_ones
	for j in range(i, n):
		if a[j]:
			current_ones -= 1
		else:
			current_ones += 1
		if current_ones > max_ones:
			max_ones = current_ones
print(max_ones)


def main():
	from itertools import accumulate
	n = int(input())
	arr = [int(x) for x in input().split()]
	ones = list(accumulate(arr))
	ans = 0
	ones_inside = lambda i, j: ones[j] - ones[i - 1] if i else ones[j]
	ones_outside = lambda i, j: ones[n - 1] - ones_inside(i, j)
	for i in range(0, n):
		for j in range(i, n):
			partial = ones_outside(i, j) + (j - i + 1 - ones_inside(i, j))
			ans = max(ans, partial)
	print(ans)
main()


n = int(input())
ip = [int(i) for i in input().split()]
maxOnes = 0
for i in range(n - 1):
	for j in range(i, n):
		a = ip[:]
		tempOnes = 0
		for k in range(i, j + 1):
			a[k] = 1 - a[k]
		maxOnes = max(maxOnes, a.count(1))
if n == 1 and ip[0] == 1:
	print(0)
elif n == 1 and ip[0] == 0:
	print(1)
else:
	print(maxOnes)


def addList(x):
	s = 0
	for i in range(len(x)):
		s = s + x[i]
	return s
n = int(input())
data = [int(i) for i in input().split()]
num = []
for i in range(n):
	for j in range(i + 1, n + 1):
		num.append(addList(data) - addList(data[i:j]) + data[i:j].count(0))
num.sort()
if num != []:
	print(num[-1])
else:
	print(addList(data) - 1)


n = int(input())
s = [int(i) for i in input().split()]
a = []
total = 0
for i in range(n):
	total = total + s[i]
a.append(total)
for i in range(n):
	k = total
	for j in range(i, n):
		k = k + 1 - 2 * s[j]
		a.append(k)
max_value = max(a)
if max_value == total:
	max_value -= 1
print(max_value)


n = int(input())
ll = list(input().split())
l = []
br = 0
v = 0
sol = 0
for i in range(n):
	l.append(int(ll[i]))
suma = [0]
for i in range(n):
	suma.append(l[i] + suma[i])
for i in range(1, n + 1):
	for j in range(i, n + 1):
		v = suma[i - 1] + j - i + 1 - (suma[j] - suma[i - 1]) + suma[n] - suma[j]
		if v > sol:
			sol = v
print(sol)


n = int(input())
a = [int(x) for x in input().split()]
s = 0
m = -10 ** 9
for i in range(n):
	if a[i] == 0:
		s += 1
		m = max(m, s)
	else:
		s -= 1
		m = max(m, s)
		if s < 0:
			s = 0
print(sum(a) + m)


n = int(input())
s = [int(x) for x in input().split()]
sums = sum(s)
if n == 1:
	print(1 - sums)
elif n == sums:
	print(sums - 1)
else:
	for i in range(n):
		for j in range(i, n):
			news = s.copy()
			for k in range(i, j + 1):
				news[k] = 1 - news[k]
			sums = max(sums, sum(news))
	print(sums)


n = int(input())
arr = list(map(int, input().split()))
pre = [0] * (n + 1)
for i in range(1, n + 1):
	pre[i] = pre[i - 1] + arr[i - 1]
mm = -1
for i in range(n):
	for j in range(i, n):
		local = 0
		local += pre[i]
		local += pre[n] - pre[j + 1]
		local += j - i + 1
		local -= pre[j + 1] - pre[i]
		mm = max(mm, local)
print(mm)


n = int(input())
arr = list(map(int, input().split()))
total_1 = arr.count(1)
gain = 0
for i in range(n):
	for j in range(i, n):
		w = arr[i:j + 1]
		count_1 = w.count(1)
		count_0 = w.count(0) + total_1 - count_1
		gain = max(gain, count_0)
print(gain)


n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
barr = [1 if i == 0 else -1 for i in arr]
msum = mhere = 0
for v in barr:
	mhere = max(mhere + v, 0)
	msum = max(msum, mhere)
if max(barr) == -1:
	msum = -1
print(s + msum)


from sys import stdin
n = int(input())
l = list(map(int, stdin.readline().rstrip().split(' ')))
b = [0] * n
for i in range(n):
	if l[i] == 0:
		b[i] = 1
	else:
		b[i] = -1
s = 0
m = 0
for i in range(n):
	s = max(b[i], s + b[i])
	m = max(s, m)
z = sum(l)
if z == n:
	print(n - 1)
else:
	print(m + sum(l))


n = int(input())
flips = input().split()
if n <= 2:
	if n == 1:
		print(flips.count('0'))
	elif '0' not in flips:
		print(1)
	else:
		print(2)
elif '0' not in flips:
	print(n - 1)
elif flips.count('0') == 1:
	print(n)
else:
	b = flips.index('0')
	cn = []
	for i in range(b, n - 1):
		for j in range(i + 1, n):
			flips_ = flips[i:j + 1]
			ones = flips_.count('0') - flips_.count('1')
			t = (ones, i, j)
			cn.append(t)
	c = max(cn)
	flips_r = flips[:c[1]] + flips[c[2] + 1:]
	print(flips[c[1]:c[2] + 1].count('0') + flips_r.count('1'))


n = int(input())
line = input()
line = line.split()
for i in range(n):
	line[i] = int(line[i])
summ = sum(line)
s = 0
ss = []
sss = []
for i in range(n):
	for j in range(i, n):
		if line[j] == 0:
			s = s + 1
		elif line[j] == 1:
			s = s - 1
		ss.append(s)
	s = 0
	sss.append(max(ss))
change = max(sss)
print(summ + change)


n = int(input())
lis = list(map(int, input().split()))
maximum = [-1]
for j in range(0, n):
	for i in range(j + 1):
		maximum.append(j - i + 1 - 2 * sum(lis[i:j + 1]))
print(sum(lis) + max(maximum))


n = int(input())
a = list(map(int, input().split()))
dp = [i * -1 if i == 1 else i + 1 for i in a]
max_sum_curr = max_sum_till_now = 0
for x in dp:
	max_sum_curr = max(0, max_sum_curr + x)
	max_sum_till_now = max(max_sum_till_now, max_sum_curr)
if max_sum_till_now:
	print(sum(a) + max_sum_till_now)
else:
	print(sum(a) - 1)


n = int(input())
a = [int(x) for x in input().split()]
ones = a.count(1)
answer = [sum(a) - 2 * sum(a[i:j]) + j - i for i in range(n) for j in range(i + 1, n + 1)]
print(max(answer))


n = int(input())
a = list(map(int, input().split()))
mas = [0] * n
if a[0] == 0:
	mas[0] = 1
else:
	mas[0] = -1
m = mas[0]
for i in range(1, n):
	mas[i] = max(mas[i - 1] + (-1) ** a[i], (-1) ** a[i])
	m = max(m, mas[i])
print(a.count(1) + m)


n = int(input())
a = [int(i) for i in input().split()]
ones = 0
point = 0
best = 0
for i in range(n):
	ones += a[i]
	if a[i] == 1:
		if point > 0:
			point -= 1
		else:
			point = 0
	else:
		point += 1
	best = max(best, point)
if ones == n:
	print(n - 1)
else:
	print(ones + best)


def maxSubArraySum(a, size):
	max_so_far = a[0]
	curr_max = a[0]
	for i in range(1, size):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far
n = int(input())
a = list(map(int, input().split()))
b = []
count1 = 0
for e in a:
	if e == 0:
		b.append(1)
	else:
		b.append(-1)
		count1 += 1
x = maxSubArraySum(b, n)
print(count1 + x)


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i, n):
		for k in range(i, j + 1):
			a[k] ^= 1
		now = 0
		for k in range(n):
			now += a[k]
		ans = max(ans, now)
		for k in range(i, j + 1):
			a[k] ^= 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
ones = zeros = ans = 0
for i in range(n):
	if a[i] == 0:
		zeros = max(0, zeros)
		zeros += 1
	if a[i] == 1:
		if zeros > ans:
			ans = zeros
		zeros -= 1
		ones += 1
	if i == n - 1 and zeros > ans:
		ans = zeros
if ans == 0:
	print(ones - 1)
else:
	print(ones + ans)


n = int(input())
a_list = input().split()
d = []
q = a_list + d
s = a_list.count('1')
b = [s - 1]
for i in range(n):
	for j in range(i, n):
		for k in range(i, j + 1):
			if int(a_list[k]) == 0:
				a_list[k] = '1'
			else:
				a_list[k] = '0'
			p = a_list.count('1')
			b.append(p)
		a_list = q
		q = a_list + d
print(max(b))


n = int(input())
nums = list(map(int, input().split()))
maxn = -1
if n == 1:
	print(n - sum(nums))
	exit(0)
for i in range(n):
	for j in range(i, n):
		s = sum(nums[:i]) + j - i + 1 - sum(nums[i:j + 1]) + sum(nums[j + 1:])
		maxn = max(maxn, s)
print(maxn)


from sys import stdin, stdout
from sys import maxsize

def solve():
	pass
test = 1
for t in range(0, test):
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = 0
	for i in range(n):
		if a[i] == 1:
			cnt += 1
	if cnt == len(a):
		print(cnt - 1)
	else:
		store = 0
		for i in range(n):
			temp = 0
			s = 0
			for j in range(i, n):
				if a[j] == 0:
					temp += 1
				else:
					temp -= 1
				s = max(s, temp)
			store = max(store, s)
		print(cnt + store)
	ans = solve()


n = int(input())
line = list(map(int, input().split()))
line3 = []
line2 = []
b = int(line.count(1))
if line.count(0) != 0:
	for i in range(len(line)):
		total = 0
		if line[i] == 0:
			for k in line[i:]:
				if k == 1:
					total -= 1
					line3.append(total)
				else:
					total += 1
					line3.append(total)
			line2.append(max(line3))
		else:
			continue
	a = int(max(line2))
	print('{}'.format(a + b))
else:
	print(b - 1)


n = int(input())
one = 0
zero = 0
zeroMax = -1
s = [int(z) for z in input().split()]
for k in s:
	if k == 1:
		one += 1
		if zero > 0:
			zero -= 1
	if k == 0:
		zero += 1
		if zero > zeroMax:
			zeroMax = zero
coN = one + zeroMax
print(coN)


def flip(t):
	for i in range(len(t)):
		t[i] = 1 - t[i]
	return t
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		ans = max(ans, (l[:i] + flip(l[i:j]) + l[j:]).count(1))
print(ans)


n = int(input())
a = list(map(int, input().split()))
cnt1 = a.count(1)
for i in range(n):
	if a[i] == 1:
		a[i] = -1
	else:
		a[i] = 1
ma = -float('inf')
ans = -float('inf')
for i in range(n):
	ma = max(a[i], ma + a[i])
	ans = max(ans, ma)
print(ans + cnt1)


n = int(input())
l = list(map(int, input().split()))
z = []
for i in range(n):
	su = l[:i].count(1)
	x = []
	for j in range(i, n):
		if l[j] == 0:
			su += 1
		k = l[j + 1:].count(1)
		x.append(su + k)
	z.append(max(x))
if n == 1:
	print(1 - l.count(1))
else:
	print(max(z))


import sys
from sys import maxsize
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
res = a.count(1)
if res == len(a):
	print(n - 1)
else:
	size = len(a)
	max_so_far = -maxsize - 1
	max_ending_here = 0
	count = 0
	start = 0
	end = 0
	s = 0
	for i in range(0, size):
		if a[i] == 1:
			max_ending_here = max_ending_here + -1
		else:
			max_ending_here = max_ending_here + 1
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
			start = s
			end = i
		count += 1
		if max_ending_here < 0:
			max_ending_here = 0
			s = i + 1
			count = 0
	summ = 0
	summ = summ + a[:start].count(1)
	summ = summ + a[start:end].count(0)
	summ = summ + a[end + 1:len(a)].count(1)
	summ += 1
	print(summ)


n = int(input())
m = map(int, input().split())
m = list(m)
v = []
x = m.count(1)
for i in range(n):
	for j in range(i, n):
		a = m[i:j + 1].count(1)
		b = m[i:j + 1].count(0)
		s = x + b - a
		v.append(s)
print(max(v))


import sys
f = sys.stdin
n = int(f.readline())
a = []
for s in f.readline().split():
	a += [int(s)]
m = 0
for i in range(n):
	for j in range(i, n):
		s1 = sum(a[:i])
		s2 = sum((1 - t for t in a[i:j + 1]))
		s3 = sum(a[j + 1:])
		m = max(m, s1 + s2 + s3)
print(m)


__author__ = 'runekri3'
n = int(input())
numbers = list(map(int, input().split()))
standard_ones = numbers.count(1)
max_diff = -100
for i in range(n):
	for j in range(i + 1, n + 1):
		diff = numbers[i:j].count(0) - numbers[i:j].count(1)
		if diff > max_diff:
			max_diff = diff
print(standard_ones + max_diff)


n = int(input())
numbers = list(map(int, input().split()))
numbers = [1 if x == 0 else -1 for x in numbers]
a = 0
ans = 0
for i in numbers:
	a += i
	ans = max(ans, a)
	a = max(a, 0)
print(ans + numbers.count(-1)) if 1 in numbers else print(len(numbers) - 1)


n = eval(input())
data = list(map(int, input().split()))
count = [0, 0]
for each in data:
	count[each] += 1
best = 0
for i in range(n):
	for j in range(i, n):
		total = count[1]
		for k in range(i, j + 1):
			if data[k] == 0:
				total += 1
			else:
				total -= 1
		if total > best:
			best = total
print(best)


n = int(input())
a = [int(i) for i in input().split()]
count = 0
for i in range(n):
	count += a[i]
maxones = 0
for i in range(n):
	curcount = 0
	for j in range(i, n):
		curcount += 1 - a[j]
		if a[j] == 1:
			curcount -= 1
		maxones = max(maxones, count + curcount)
print(maxones)


input()
arr = list(map(int, input().split()))
left = -1
right = 0
left_tmp = -1
right_tmp = 0
ans = 0
ans_tmp = 0
for (j, i) in enumerate(arr):
	ans_tmp += 1 - 2 * i
	if ans_tmp < 0:
		ans_tmp = 0
		left_tmp = j
	else:
		right_tmp = j
	if ans_tmp > ans:
		ans = ans_tmp
		right = right_tmp
		left = left_tmp
for i in range(left + 1, right + 1):
	arr[i] = 0 if arr[i] else 1
if left == right == 0:
	arr[0] = 0 if arr[0] else 1
print(sum(arr))


n = int(input())
t = 0
total = []
list = [int(y) for y in input().split()]
for i in range(n):
	for j in range(i, n + 1):
		for k in range(i, j):
			t += list[k]
		total += [list.count(1) - 2 * t + int(j) - int(i)]
		t = 0
b = max(total)
for s in range(n):
	t += list[s]
if t == n:
	print(n - 1)
else:
	print(b)


n = int(input())
a = list(map(int, input().split()))
mx = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		t = sum(a[0:i]) + a[i:j].count(0) + sum(a[j:n])
		if t > mx:
			mx = t
print(mx)


n = int(input())
line = list(map(int, input().split()))
S = sum(line)
s = []
for i in range(n):
	for k in range(0, n - i):
		Sk = sum((line[l] for l in range(k, k + i + 1)))
		s.append(i + 1 - 2 * Sk)
S += max(s)
print(S)


n = int(input())
numbers = list(map(int, input().split()))
cnt = numbers.count(1)
MAX = 0
for i in range(n):
	for j in range(i, n):
		MAX = max(MAX, cnt + (j - i + 1) - 2 * sum(numbers[i:j + 1]))
print(MAX)


n = int(input())
arr = list(map(int, input().split()))
(mx, gain, count, idx) = (0, 0, 0, 0)
while idx < n:
	count += arr[idx]
	gain += [1, -1][arr[idx]]
	if gain < 0:
		gain = 0
	mx = max(mx, gain)
	idx += 1
if mx == 0:
	mx = -1
print(count + mx)


line1 = int(input())
line2 = input().split(' ')
ints = [int(x) for x in line2]

def fliplist(listx, index1, index2):
	list1 = listx[index1:index2 + 1]
	list2 = [1 - x for x in list1]
	return listx[0:index1] + list2 + listx[index2 + 1:]
maximum = 0
for t in range(line1):
	for u in range(t, line1):
		maximum = max(maximum, fliplist(ints, t, u).count(1))
print(maximum)


n = int(input())
s = [int(i) for i in input().split()] + [1]
a = m = sum(s) - 1
if 0 in s:
	for i in range(s.index(0), n + 1):
		if s[i]:
			if not s[i - 1]:
				m = max(a, m)
			a -= 1
		else:
			if s[i - 1] and a < sum(s) - 1:
				a = sum(s) - 1
			a += 1
	print(m)
else:
	print(m - 1)


n = int(input())
l = input().split()
x = []
for i in range(n):
	for j in range(i + 1, n + 1):
		ans = l.count('1') + l[i:j].count('0') - l[i:j].count('1')
		x.append(ans)
print(max(x))


n = int(input())
tab = list(map(int, input().split()))
maxones = None
for i in range(n):
	temp = 0
	for j in range(i, n):
		if tab[j] == 0:
			temp += 1
		else:
			temp -= 1
		if maxones == None:
			maxones = temp
		else:
			maxones = max(temp, maxones)
number_of_ones = 0
for i in range(len(tab)):
	if tab[i] == 1:
		number_of_ones += 1
print(number_of_ones + maxones)


n = int(input())
l = input().split()
m = []
for i in range(n):
	for j in range(i + 1, n + 1):
		p = l.count('1') + l[i:j].count('0') - l[i:j].count('1')
		m.append(p)
print(max(m))


n = int(input())
x = [int(i) for i in input().split()]
biggest = -1
for i in range(0, len(x)):
	for j in range(i + 1, len(x) + 1):
		ans = x[i:j].count(0) - x[i:j].count(1)
		if ans >= biggest:
			biggest = ans
print(x.count(1) + biggest)


n_len = int(input())
seq = input().split()
seq = [int(i) for i in seq]

def get_sum(lst):
	og_sum = 0
	for i in lst:
		og_sum += i
	return og_sum

def flip(b):
	b = 1 - b
	return b
list_of_sums = []
for i in range(n_len):
	end = i + 1
	while end <= n_len:
		new_seq = seq[:]
		for f in range(i, end):
			new_seq[f] = flip(new_seq[f])
		new_sum = get_sum(new_seq)
		list_of_sums.append(new_sum)
		end += 1
print(max(list_of_sums))


def func(a, size):
	max_so_far = a[0]
	curr_max = a[0]
	for i in range(1, size):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far
n = int(input())
l = list(map(int, input().split()))
b = []
for i in l:
	if i == 1:
		b.append(-1)
	else:
		b.append(1)
print(l.count(1) + func(b, len(b)))


def change(x):
	if x == 0:
		return 1
	else:
		return -1

def s():
	n = int(input())
	ar = list(map(int, input().split(' ')))
	ar2 = list(map(change, ar))
	sm_init = sum(ar)
	sm = -100
	best = -100
	for i in range(n):
		sm = max(ar2[i], ar2[i] + sm)
		best = max(sm, best)
	print(best + sm_init)
s()


import sys
my_file = sys.stdin
n = int(my_file.readline().strip())
a = [int(i) for i in my_file.readline().strip().split()]
max = 0
for end in range(n, -1, -1):
	for begin in range(end):
		s = sum(a[:begin] + [i ^ 1 for i in a[begin:end + 1]] + a[end:])
		if s > max:
			max = s
print(max)


import math
from collections import defaultdict
ml = lambda : map(int, input().split())
ll = lambda : list(map(int, input().split()))
ii = lambda : int(input())
ip = lambda : list(input())
ips = lambda : input().split()
t = ii()
s = ll()
ans = 0
for i in range(t):
	lol = s[:]
	for j in range(i, t):
		if lol[j] == 1:
			lol[j] = 0
		else:
			lol[j] = 1
		c = 0
		for k in range(t):
			if lol[k] == 1:
				c += 1
		ans = max(ans, c)
print(ans)


n = int(input())
list = [int(i) for i in input().split()]
x = sum(list)
y = 0
a = 0
for i in list:
	if i == 0:
		y += 1
		if y > a:
			a = y
	elif y > 0:
		y -= 1
if a == 0:
	a -= 1
print(x + a)


n = int(input())
N = [int(num) for num in input().split(' ', n - 1)]
A = [0] * n
i = p = 0
while i < n:
	j = i
	while j < n:
		if N[j] == 0:
			p += 1
		elif N[j] == 1 and p > 0:
			p -= 1
		A[i] = max(A[i], p)
		j += 1
	p = 0
	i += 1
x = max(A)
y = sum(N)
if x > 0:
	print(x + y)
else:
	print(y - 1)


m = int(input())
a = input().split()
a.append('2')
n = 1
b = []
for i in range(m):
	if a[i + 1] == a[i]:
		n += 1
	else:
		b.append(n)
		n = 1
if len(b) == 1:
	if a[0] == '0':
		print(m)
	else:
		print(m - 1)
if len(b) == 2:
	print(sum(b))
if len(b) > 2:
	if a[0] == '0':
		b = [0] + b
for i in range(len(b)):
	if i % 2:
		b[i] = -b[i]
re = []
for i in range(1, len(b), 2):
	for j in range(i, len(b), 2):
		re.append(sum(b[i:j + 1]))
if len(b) > 2:
	print(sum([b[i] for i in range(0, len(b), 2)]) - min(re))


n = int(input())
a = [int(i) for i in input().split()]
i = 0
l = -1
r = -1
ans = a.count(1)
diff = 0
left = -1
right = -1
while i < n:
	j = i
	mxDif = 0
	countDif = 0
	countZ = 0
	countOne = 0
	while j < n:
		if a[j]:
			countOne += 1
		else:
			countZ += 1
		countDif = countZ - countOne
		if countDif > mxDif:
			mxDif = countDif
			l = i
			r = j
		j += 1
	if diff < mxDif:
		diff = mxDif
		left = l
		right = r
	i += 1
if left == -1 and right == -1:
	print(ans - 1)
else:
	for i in range(left, right + 1):
		if a[i] == 0:
			ans += 1
		else:
			ans -= 1
	print(ans)


n = int(input())
vals = [int(x) for x in input().split()]
c0 = c1 = 0
c0_max = -1
for v in vals:
	if v == 0:
		c0 += 1
		if c0 > c0_max:
			c0_max = c0
	else:
		c1 += 1
		if c0 > 0:
			c0 -= 1
print(c1 + c0_max)


n = int(input())
nums = list(map(int, input().split()))
gain = [1 if i == 0 else -1 for i in nums]
s = sum(nums)

def kadane(gain):
	max_current = max_global = gain[0]
	for i in range(1, n):
		max_current = max(gain[i], max_current + gain[i])
		max_global = max(max_global, max_current)
	return max_global
print(s + kadane(gain))


n = int(input())
line = str(input()).split()
num1 = line.count('1')
length = len(line)
A = []
if n == 1 and num1 == 1:
	print(0)
elif n == 1 and num1 == 0:
	print(1)
else:
	for i in range(length):
		for m in range(i + 1, length + 1):
			change_num = 0
			for j in range(i, m):
				if line[j] == '0':
					change_num = change_num + 1
				else:
					change_num = change_num - 1
			A.append(str(change_num))
	num = int(A[0])
	for i in range(len(A)):
		if int(A[i]) > num:
			num = int(A[i])
	print(num + num1)


n = int(input())
arr = list(map(int, input().split()))
from collections import Counter
from itertools import accumulate
from math import inf
c = Counter(arr)
res = c[1]
dp = [1] * n
for (i, a) in enumerate(arr):
	if a:
		dp[i] = -1
acc = list(accumulate(dp))
mx = acc[0]
mn = 0
for i in range(n):
	mn = min(acc[i], mn)
	if i > 0:
		mx = max(mx, acc[i] - mn)
if c[1] == n:
	print(n - 1)
else:
	print(c[1] + mx)


n = int(input())
res = [int(i) for i in input().split()]
s = sum(res)
mx = 0
c = 0
for i in range(n):
	if res[i] == 0:
		mx += 1
		if c < mx:
			c = mx
	elif mx > 0:
		mx -= 1
	else:
		mx = 0
if c == 0:
	c = -1
print(s + c)


n = int(input())
arr = [int(i) for i in input().split()]
dp = [0] * n
dp[0] = 0
if 0 in arr:
	s = arr.index(0)
	for i in range(s, n):
		if arr[i] == 0:
			dp[i] = dp[i - 1] + 1
		else:
			dp[i] = dp[i - 1] - 1
			if dp[i] < 0:
				dp[i] = 0
	x = max(dp)
	a = arr.count(1)
	print(a + x)
else:
	print(n - 1)


p = []
b = []
q = []
n = int(input())
s = 0
if n > 2:
	p = [int(i) for i in input().split()]
	for i in range(n):
		for j in range(i + 1, n):
			for m in range(i):
				if p[m] == 1:
					s = s + 1
			for m in range(j, n):
				if p[m] == 1:
					s = s + 1
			for m in range(i, j + 1):
				if p[m] == 0:
					s = s + 1
			b.append(s)
			s = 0
	b.sort()
	print(b[len(b) - 1])
elif n == 1:
	g = int(input())
	if g == 0:
		print('1')
	else:
		print('0')
elif n == 2:
	(x, y) = map(int, input().split())
	if x + y == 1 or x + y == 0:
		print('2')
	if x + y == 2:
		print('1')


def sum(a):
	s = 0
	i = 0
	while i < len(a):
		s += int(a[i])
		i += 1
	return s

def max(a):
	i = 0
	s = 0
	while i < len(a):
		if s < a[i]:
			s = a[i]
		i += 1
	return s
n = int(input())
a = input().split()
j = 1
tot = []
if n == 1 and a == ['1']:
	tot.append(0)
elif n == 1 and a == ['0']:
	tot.append(1)
while j <= len(a):
	i = 0
	while i <= len(a) - j:
		tot.append(sum(a[0:i]) + j - sum(a[i:i + j]) + sum(a[i + j:n]))
		i += 1
	j += 1
print(max(tot))


n = int(input())
a = list(map(int, input().split()))
b = [0] * n
Max = 0
for i in range(n):
	for j in range(i, n, 1):
		cnt = 0
		for k in range(n):
			if i <= k and k <= j:
				b[k] = 1 - a[k]
			else:
				b[k] = a[k]
			if b[k] == 1:
				cnt += 1
		if Max < cnt:
			Max = cnt
print(Max)


length = int(input())
nums = [True if x == '1' else False for x in input().split()]
highest = 0
for i in range(len(nums)):
	for j in range(i, len(nums)):
		count = 0
		for k in range(i, j + 1):
			nums[k] = not nums[k]
		for k in nums:
			if k:
				count += 1
		if count > highest:
			highest = count
		for k in range(i, j + 1):
			nums[k] = not nums[k]
print(highest)


def count(a):
	return a.count(0) - a.count(1)
N = int(input())
intergers = [int(i) for i in input().split()]
subSets = []
for i in range(N):
	for j in range(i + 1, N + 1):
		subSets.append(intergers[i:j])
countList = []
for i in subSets:
	countList.append(count(i))
print(sum(intergers) + max(countList))


n = int(input())
lis = list(map(int, input().split()))
s = ''.join(map(str, lis))
ans = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		count = s[:i].count('1')
		tmp = ''
		for char in s[i:j]:
			if char == '0':
				tmp += '1'
			else:
				tmp += '0'
		count += s[j:].count('1')
		ans = max(ans, count + tmp.count('1'))
print(ans)


n = int(input())
s = list(map(int, input().split()))
count = s.count(1)
add = -100
for i in range(n):
	for j in range(i + 1, n + 1):
		k = s[i:j].count(0) - s[i:j].count(1)
		add = max(add, k)
print(count + add)


n = int(input())
arr = [int(x) for x in input().split()]
ans = []
for i in range(n):
	for j in range(n):
		if i > j:
			continue
		arr2 = [1 - arr[x] if x >= i and x <= j else arr[x] for x in range(n)]
		ans.append(sum(arr2))
print(max(ans))


n = int(input())
a = list(map(int, input().split()))
c = -1
(z, o, r) = (0, 0, 0)
for i in a:
	if i == 1:
		o += 1
		if z > 0:
			z -= 1
	else:
		z += 1
		c = max(c, z)
print(o + c)


a = int(input())
l = input().split()
ansl = []
summ = 0
for i in range(a + 1):
	for j in range(i, a + 1):
		m = 0
		for k in range(i, j):
			if l[k] == '0':
				m += 1
			elif l[k] == '1':
				m -= 1
			ansl.append(m)
for o in range(a):
	summ += int(l[o])
print(summ + max(ansl))


n = int(input())
s = input()
s = s[::2]
m = 0
t = len(s)
if s.count('1') == t:
	print(t - 1)
else:
	i = 0
	while i <= t:
		l = 0
		for j in range(i):
			if s[j] == '1':
				l += 1
		for z in range(t, i - 1, -1):
			k = l
			for j in range(i, z):
				if s[j] == '0':
					k += 1
			for j in range(z, t):
				if s[j] == '1':
					k += 1
			m = max(m, k)
		i += 1
	print(m)


n = int(input())
a = list(map(int, input().split()))
sol = 0
inv = [1, 0]
for i in range(0, n):
	for j in range(i, n):
		val = 0
		for k in range(0, n):
			if k < i or k > j:
				val += a[k]
			else:
				val += inv[a[k]]
		sol = max(sol, val)
print(sol)


n = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i, n):
		count = 0
		for k in range(n):
			if k <= j and k >= i:
				count += 1 - arr[k]
			else:
				count += arr[k]
		ans = max(count, ans)
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
val = cur = 1 - a[0] * 2
c1 = a[0]
for i in a[1:]:
	cur = max(1 - i * 2, cur + 1 - i * 2)
	val = max(val, cur)
	c1 += i
print(c1 + val)


def I():
	return list(map(int, input().split()))
n = int(input())
l = I()
su = []
s = 0
for i in l:
	s += i
	su.append(s)
su.append(0)
m = s
(a, b) = (0, 0)
for j in range(n):
	for i in range(j, n):
		mh = su[i] - su[j - 1]
		if s - mh + (abs(i - j) - mh + 1) > s - m + (abs(b - a) - m + 1):
			m = mh
			(a, b) = (i, j)
print(s - m + (abs(b - a) - m + 1))


def flipping_game(n, l):
	mx = 0
	h = sum(l)
	for i in range(0, n):
		for j in range(i, n):
			d = sum(l[i:j + 1])
			y = h - 2 * d + j - i + 1
			mx = max(mx, y)
	return mx
n = int(input())
a = [int(j) for j in input().split()]
print(flipping_game(n, a))


n = int(input())
nums = input().split()
numslist = [int(x) for x in nums]
original_num = 0
res = 0
for x in numslist:
	if x == 1:
		original_num += 1
for i in range(n):
	for j in range(i, n):
		tmp = original_num
		for k in range(j - i + 1):
			if numslist[i + k] == 0:
				tmp += 1
			else:
				tmp -= 1
		res = max(res, tmp)
print(res)


n = int(input())
a = list(map(int, input().split()))
output = 0
if n == 1:
	output = 1 - a[0]
else:
	for i in range(1, n + 1):
		for j in range(i, n + 1):
			test = a[:]
			out = sum(test[0:i - 1]) + sum(test[j:n]) + j - i + 1 - sum(test[i - 1:j])
			output = max(out, output)
print(output)


num = int(input())
line = input().split(' ')
c = 0
d = 0
for i in range(0, num):
	line[i] = int(line[i])
for i in range(0, num):
	if line[i] == 0:
		c += 1
	elif line[i] == 1 and c > 0:
		c -= 1
	else:
		c = 0
	d = max(d, c)
e = sum(line)
if e != num:
	print(e + d)
else:
	print(e - 1)


n = int(input())
mayor = 0
cadena = [0]
cadena.extend(input().split(' '))
guia = 0
for elem in cadena:
	pair = [0, 0]
	if elem == '1':
		pair[0] = cadena[guia - 1][0]
		pair[1] = cadena[guia - 1][1] + 1
	elif elem == '0':
		pair[0] = cadena[guia - 1][0] + 1
		pair[1] = cadena[guia - 1][1]
	cadena[guia] = pair
	guia += 1
for i in range(1, n + 1):
	for j in range(i, n + 1):
		aux = cadena[n][1] - (cadena[j][1] - cadena[i - 1][1]) + (cadena[j][0] - cadena[i - 1][0])
		mayor = aux if aux > mayor else mayor
print(mayor)


n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
all_sum = sum(a)
ans = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		sum = all_sum
		for k in range(i, j + 1):
			if a[k] == 1:
				sum -= 1
			elif a[k] == 0:
				sum += 1
		ans = max(ans, sum)
print(ans)


n = int(input())
m = list(map(int, input().split())) + [0]
mm = m[:]
otvet = []
ms = []
if m.count(1) == n:
	print(n - 1)
	exit()
for i in range(n):
	mm = m[:]
	for j in range(i, n + 1):
		for ii in range(i, j):
			mm[ii] = 1 - m[ii]
		otvet.append(sum(mm))
print(max(otvet))


from typing import List, Tuple

def parse_input() -> Tuple[int, List[int]]:
	return (int(input()), [int(x) for x in input().split()])

def f(x: int) -> int:
	return [-1, 1][x == 0]

def solve(n: int, a: List[int]) -> int:
	current = f(a[0])
	best = f(a[0])
	for (idx, elem) in enumerate(a[1:]):
		current = max(current + f(elem), f(elem))
		best = max(best, current)
	return best + a.count(1)
(n, a) = parse_input()
print(solve(n, a))


n = int(input())
num = [int(i) for i in input().split()]
res = -1
for lft in range(n):
	for rt in range(lft, n):
		cnt = 0
		for k in range(n):
			if k in range(lft, rt + 1):
				cnt += 1 - num[k]
			else:
				cnt += num[k]
		if cnt > res:
			res = cnt
print(res)


n = int(input())
list = [int(i) for i in input().split()]
sum3 = 0
for p in range(n):
	if p == 0:
		sum1 = 0
	else:
		sum1 = sum(list[0:p])
	for q in range(p, n):
		a = 0
		if q == n - 1:
			sum2 = 0
		else:
			sum2 = sum(list[q + 1:n])
		for k in list[p:q + 1]:
			if k == 0:
				a += 1
			if sum1 + sum2 + a >= sum3:
				sum3 = sum1 + sum2 + a
print(sum3)


n = int(input())
se = input().split()
e = []
for h in range(n):
	e.append(se[h])
c = 0
d = []
for i in range(0, len(se)):
	for a in range(i, len(se)):
		e[a] = 1 - int(e[a])
		for b in range(0, len(e)):
			if e[b] == 1 or e[b] == '1':
				c = c + 1
			d.append(c)
		c = 0
	e = []
	for v in range(n):
		e.append(se[v])
d.sort()
print(d[len(d) - 1])


import sys
import math
import itertools
import collections

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base=3):
	newnumber = ''
	while number > 0:
		newnumber = str(number % base) + newnumber
		number //= base
	return newnumber
n = ii()
a = li()
suma = sum(a)
ans = 0
if n == 1:
	print(1 - a[0])
else:
	for i in range(1, n + 1):
		for j in range(n - i + 1):
			ans = max(ans, suma + i - 2 * sum(a[j:j + i]))
	print(ans)


x = int(input())
s = 0
g = 10000 * [0]
c = -1
f = list(map(int, input().split()))
L = tuple(f)
for n in range(0, x):
	if f[n] == 0:
		g[0] = x
for n in range(0, x):
	if f[n] == 1:
		g[0] = x - 1
	else:
		break
for i in range(0, x):
	for j in range(i, x):
		for p in range(i, j + 1):
			f[p] = 1 - f[p]
		for q in range(0, x):
			s = s + f[q]
		c = c + 1
		g[c] = s
		s = 0
		f = list(L)
print(max(g))


from sys import stdin, stdout
import math
from itertools import permutations, combinations, permutations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
mod = 1000000007

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def printIn(ob):
	return stdout.write(str(ob) + '\n')

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def myCode():
	n = I()
	lst = L()
	max_lst = []
	for i in range(n):
		for j in range(i, n):
			max_lst.append(j - i + 1 + sum(lst[:i]) + sum(lst[j + 1:]) - sum(lst[i:j + 1]))
	print(max(max_lst))

def main():
	myCode()
main()


n = int(input())
zeros_and_ones = list(map(int, input().split()))
num_of_ones = zeros_and_ones.count(1)
cnt = 0
max_arr = []
print_ans = True
for i in zeros_and_ones:
	if 0 not in zeros_and_ones:
		print_ans = False
		print(num_of_ones - 1)
		break
	if cnt >= 0:
		if i == 1 and cnt != 0:
			cnt -= 1
		if i == 0:
			cnt += 1
	max_arr.append(cnt)
if print_ans:
	print(num_of_ones + max(max_arr))


n = int(input())
l = list(map(int, input().split()))
m = -1
s = 0
for i in range(n):
	s += 1 if l[i] == 0 else -1
	if s > m:
		m = s
	if s <= 0:
		s = 0
		c_l = 0
print(sum(l) + m)


n = int(input())
ln = input().split(' ')
maxl = 0
for i in range(n):
	for j in range(i, n):
		tab = ln[i:j + 1]
		a = ln[:i + 1]
		b = ln[j:]
		l = tab.count('0') + a.count('1') + b.count('1')
		if l > maxl:
			maxl = l
if ln.count('0') == 0:
	print(n - 1)
else:
	print(maxl)


n = int(input())
line = input().split()
list = []
sum = 0
max = 0
for i in range(n):
	list.append(int(line[i]))
List = []
for i in range(n):
	List.append(list[i])
for j in range(n):
	for i in range(j + 1):
		for a in range(i, j + 1):
			list[a] = 1 - list[a]
		for s in range(n):
			sum += list[s]
		if max < sum:
			max = sum
		else:
			pass
		sum = 0
		list = []
		for q in range(n):
			list.append(List[q])
print(max)


n = int(input())
a = list(map(int, input().split()))
s = a.count(1)
mx = -float('inf')
for i in range(n):
	for j in range(i, n):
		cnt = a[i:j + 1].count(1)
		mx = max(mx, s - cnt + j - i + 1 - cnt)
print(mx)


n = int(input())
a = [int(i) for i in input().split()]
q = []
if len(a) > 1:
	for j in range(n):
		for i in range(j + 1):
			s = sum(a[0:i]) + sum(a[j + 1:]) + sum([1 - x for x in a[i:j + 1]])
			q.append(s)
	print(max(q))
else:
	print(1 - a[0])


n = int(input())
x = input().split()
y = [0 for i in range(n)]
(S, s, m) = (0, 0, -1)
for i in range(n):
	x[i] = int(x[i])
	S += x[i]
for i in range(n):
	for j in range(i, n):
		t = 0
		for k in range(i, j + 1):
			t += x[k]
		s = j - i + 1 - 2 * t
		if m < s:
			m = s
print(m + S)


n = int(input())
arr = [int(i) for i in input().split(' ')]
(res, nonZero) = (0, 0)
for i in arr:
	if i:
		nonZero += 1
for i in range(n):
	for j in range(i, n):
		s1 = s2 = 0
		for k in range(i, j + 1):
			if arr[k]:
				s1 += 1
			else:
				s2 += 1
		res = max(res, nonZero - s1 + s2)
print(res)


n = int(input())
numbers = [int(x) for x in input().split()]
max = -1
extra0 = 0
count1 = 0
for x in numbers:
	if x == 1:
		count1 += 1
		if extra0 > 0:
			extra0 -= 1
	else:
		extra0 += 1
		if extra0 > max:
			max = extra0
print(count1 + max)


import sys

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	for i in range(n):
		for j in range(i, n):
			temp = 0
			for k in range(n):
				temp += 1 ^ a[k] if i <= k <= j else a[k]
			res = max(res, temp)
	print(res)
if sys.hexversion == 50594544:
	sys.stdin = open('test.txt')
solve()


n = int(input())
arr = [int(x) for x in input().split()]
dp = [0] * n
dp[0] = 1 - arr[0]
if n == 1 and arr[0] == 0:
	print(1)
	exit(0)
for i in range(1, n):
	dp[i] = dp[i - 1] + 1 - arr[i]
total_one = n - dp[n - 1]
m = 0
if n == total_one:
	print(n - 1)
	exit(0)
for i in range(n):
	for j in range(i + 1, n):
		if arr[i] == 0:
			val = dp[j] - dp[i] + 1
		else:
			val = dp[j] - dp[i]
		elements = j - i + 1
		temp = total_one + val - (elements - val)
		if temp > m:
			m = temp
print(m)


class MaxSum(object):

	def __init__(self, init_list):
		self.l = init_list
		self.subl = []

	def max_sum(self):
		sum = 0
		ans = 0
		if max(self.l) < 0:
			return max(self.l)
		for x in self.l:
			sum += x
			ans = sum if sum > ans else ans
			if sum < 0:
				sum = 0
		return ans
n = int(input())
l = list(map(int, input().split()))
su = sum(l)
for i in range(n):
	if l[i]:
		l[i] = -1
	else:
		l[i] = 1
n = MaxSum(l)
print(n.max_sum() + su)


n = int(input())
arr = list(map(int, input().split()))
ma = 0
for i in range(n):
	for j in range(i, n):
		ma = max(arr[:i].count(1) + arr[i:j + 1].count(0) + arr[j + 1:].count(1), ma)
print(ma)


n = int(input())
a = input().split()
b = []
for i in range(n):
	a[i] = int(a[i])
for i in range(n):
	for j in range(i, n):
		l = a[i:j + 1]
		m = l.count(0) - l.count(1)
		b.append(m)
print(max(b) + sum(a))


n = int(input())
arr = [int(i) for i in input().split()]
sums = [0]
add = 0
for a in arr:
	add += 1 if a == 1 else 0
	sums.append(add)
if n == 1:
	if arr[0] == 0:
		mx = 1
	else:
		mx = 0
else:
	mx = sums[-1] - 1
	for i in range(1, n):
		for j in range(i + 1, n + 1):
			l = j - i + 1
			ones = sums[j] - sums[i - 1]
			zeroes = l - ones
			mx = max(mx, sums[-1] + zeroes - ones)
print(mx)


n = int(input())
total = 0
list_1 = input().split()
list_1 = [int(i) for i in list_1]
if n != 1:
	for i in range(n):
		list_2 = list_1[:]
		for j in range(i, n):
			list_2[j] = 1 - list_2[j]
			total = max(sum(list_2), total)
else:
	total = 1 - list_1[0]
print(total)


n = int(input())
arr = [0] * n
cnt_cum = [0] * n
arr = [int(x) for x in input().split()]
cnt_cum[0] = arr[0]
for i in range(1, n):
	cnt_cum[i] = cnt_cum[i - 1] + arr[i]
max_cnt = 0
for i in range(0, n):
	for j in range(i, n):
		l = j - i + 1
		l_cnt = cnt_cum[j] - (cnt_cum[i - 1] if i > 0 else 0)
		max_cnt = max(max_cnt, cnt_cum[n - 1] + l - 2 * l_cnt)
print(max_cnt)


import sys
f = sys.stdin
n = int(f.readline())
a = list(map(int, f.readline().split()))
print(max((sum(a[:i]) + sum((1 - t for t in a[i:j + 1])) + sum(a[j + 1:]) for i in range(n) for j in range(i, n))))


n = int(input())
a = list(map(int, input().split()))
dp = [[0] * n for i in range(n)]
eds = sum(a)
for i in range(n):
	dp[i][i] = eds + (-1) ** a[i]
for j in range(n):
	for i in range(j - 1, -1, -1):
		dp[i][j] = dp[i + 1][j] + (-1) ** a[i]
mx = 0
for i in range(n):
	mx = max(mx, max(dp[i]))
print(mx)


n = int(input())
word = input()
L = word.split()
num = L.count('1')
if '0' not in L:
	num = n - 1
else:
	N = []
	for i in range(0, n):
		if L[i] == '0' and L[i - 1] == '1' or (i == 0 and L[i] == '0'):
			a = 0
			b = 0
			for t in range(i, n):
				if L[t] == '1':
					a = a + 1
				else:
					b = b + 1
				if a > b:
					break
				N.append(b - a)
	num = num + max(N)
print(num)


def flip(a, left, right):
	res = a.copy()
	for i in range(left, right):
		res[i] = 1 - res[i]
	return res
n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(len(a) + 1):
	for j in range(i + 1, len(a) + 1):
		res = max(res, flip(a, i, j).count(1))
print(res)


def print_range(a):
	maxcost = 0
	for i in range(len(a)):
		for j in range(i, len(a)):
			cost = sum([1 for x in a[i:j + 1] if x != 1])
			cost += sum(a[:i]) + sum(a[j + 1:])
			maxcost = max(maxcost, cost)
	print(maxcost)

def main():
	input()
	a = [int(x) for x in input().split(' ')]
	if 0 not in a:
		print(sum(a) - 1)
	else:
		print_range(a)
main()


n = int(input())
l = [int(i) for i in input().split(' ')]
cnt = sum(l)
j = [1 if i is 0 else -1 for i in l]
max1 = -1
sum1 = 0
for i in j:
	sum1 += i
	if sum1 > max1:
		max1 = sum1
	if sum1 < 0:
		sum1 = 0
print(max1 + cnt)


n = int(input())
list1 = [int(i) for i in input().split()]
list2 = list1[:]
list3 = []
for i in range(0, n, 1):
	for j in range(i, n, 1):
		if i == j:
			list1[i] = 1 - list1[i]
			x = sum(list1)
			list3.append(x)
			list1 = list2[:]
		else:
			for t in range(i, j + 1, 1):
				list1[t] = 1 - list1[t]
			x = sum(list1)
			list3.append(x)
			list1 = list2[:]
print(max(list3))


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	kek = a.count(1)
	lol = 0
	c1 = 0
	for j in a[i:]:
		if j == 1:
			kek -= 1
		else:
			kek += 1
		lol = max(lol, kek)
		ans = max(ans, kek)
print(ans)


n = int(input())
b = [-1 if i == 1 else 1 for i in map(int, input().split())]
(i, s, ms) = (0, 0, 0)
while i < n:
	if s + b[i] >= 0:
		if b[i] < 0 and s > ms:
			ms = s
		s += b[i]
	elif s > ms:
		ms = s
		s = 0
	i += 1
if s > ms:
	ms = s
print(n - 1 if b.count(1) == 0 else ms + b.count(-1))


n = int(input())
card = input().split()
z = card[0:1].count('0') - card[0:1].count('1')
for i in range(n):
	for p in range(i + 1, n + 1):
		if card[i:p].count('0') - card[i:p].count('1') > z:
			z = card[i:p].count('0') - card[i:p].count('1')
print(z + card.count('1'))


n = int(input())
seq = [int(x) for x in input().split()]
d = -1
for i in range(n):
	for j in range(i, n):
		d = max(d, j - i + 1 - 2 * sum(seq[i:j + 1]))
print(sum(seq) + d)


n = int(input())
line = list(map(int, input().split()))
count = 0
number = 0
for i in range(n):
	if line[i] == 1:
		count -= 1
	else:
		count += 1
	number = max(count, number)
	count = max(count, 0)
if line.count(1) == n:
	print(n - 1)
else:
	print(number + line.count(1))


n = int(input())
a = list(map(int, input().split()))
b = []
ones = 0
for i in a:
	if i == 1:
		b.append(-1)
		ones += 1
	else:
		b.append(1)
dp = [0] * n
dp[0] = b[0]
for i in range(1, n):
	dp[i] = max(dp[i - 1] + b[i], b[i])
print(max(dp) + ones)


n = int(input())
ori = list(map(int, input().split()))
x = sum(ori)
one = []
zero = []
if 0 not in ori:
	print(n - 1)
else:
	slice = [ori[0]]
	for i in range(1, n):
		if ori[i] == ori[i - 1]:
			slice.append(ori[i])
		else:
			if ori[i] == 0:
				one.append(slice)
			else:
				zero.append(slice)
			slice = [ori[i]]
	if ori[-1] == 0:
		zero.append(slice)
	if ori[-1] == 1:
		one.append(slice)
	if ori[0] == 0:
		if len(zero) == 1:
			print(n)
		else:
			move = []
			for t in range(len(zero) - 1):
				w = len(zero[t])
				flip = [w]
				for j in range(t + 1, len(zero)):
					num_zero = len(zero[j])
					num_one = len(one[j - 1])
					w = w + num_zero - num_one
					flip.append(w)
				flip = sorted(flip, reverse=True)
				move.append(flip[0])
			print(x + max(move))
	if ori[0] == 1:
		if len(zero) == 1:
			print(n)
		else:
			move = []
			for t in range(len(zero) - 1):
				w = len(zero[t])
				flip = [w]
				for j in range(t + 1, len(zero)):
					num_zero = len(zero[j])
					num_one = len(one[j])
					w = w + num_zero - num_one
					flip.append(w)
				flip = sorted(flip, reverse=True)
				move.append(flip[0])
			print(x + max(move))


m = int(input())
list = []
c1 = 0
c0 = 0
res = 0
list = [int(i) for i in input().split()]
for i in list:
	if i == 1:
		c1 = c1 + 1
		if c0:
			c0 = c0 - 1
	if i == 0:
		c0 = c0 + 1
	if c0 > res:
		res = c0
if res:
	print(c1 + res)
else:
	print(c1 - 1)


n = int(input())
a = [(1, -1)[i == '0'] for i in input().split()]
(tmp, mx) = (0, 1)
for i in a:
	tmp = min(tmp + i, i)
	mx = min(tmp, mx)
print(a.count(1) - mx)


n = int(input())
l1 = list(map(int, input().split()))
profit = 0
profitmax = 0
flag = 0
for i in range(0, n):
	if flag == 0:
		if l1[i] == 0:
			profit += 1
			flag = 1
			profitmax = max(profitmax, profit)
	elif flag == 1:
		if l1[i] == 0:
			profit += 1
			profitmax = max(profitmax, profit)
		else:
			profit -= 1
			if profit == 0:
				flag = 0
if l1.count(0) == 0:
	print(n - 1)
else:
	print(l1.count(1) + profitmax)


n = int(input())
a = [int(c) for c in input().split()]
noChange = sum(a)
adv = 0
max_adv = 0
try:
	startIndex = a.index(0)
	for i in range(n - startIndex):
		if a[startIndex + i] == 0:
			adv += 1
			max_adv = max(max_adv, adv)
		elif adv > 0:
			adv -= 1
	print(noChange + max_adv)
except ValueError:
	print(n - 1)


n = int(input())
l = [int(i) for i in input().split()]
if n == l.count(1):
	print(n - 1)
else:
	m = sum(l)
	for i in range(n):
		for j in range(i, n):
			s = sum(l[:i]) + sum(l[j + 1:])
			for k in range(i, j + 1):
				s = s + 1 - l[k]
			m = max(m, s)
	print(m)


n = int(input())
a = input().split()
b = []
for i in range(n):
	if a[i] == '0':
		b.append(1)
	else:
		b.append(-1)
c = []
for i in range(n):
	count = 0
	for j in range(i, n):
		count += b[j]
		c.append(count)
print(max(c) + a.count('1'))


n = int(input())
a = []
count = 0
maxm = 0
d = 0
a = list(map(int, input().split()))
for i in range(n):
	if a[i] == 0:
		d = 1
		break
if d == 0:
	print(n - 1)
	exit()
for i in range(n):
	for j in range(i, n):
		count = 0
		for k in range(i, j + 1):
			if a[k] == 0:
				count = count + 1
		for k in range(i):
			if a[k] == 1:
				count = count + 1
		for k in range(j + 1, n):
			if a[k] == 1:
				count = count + 1
		if count >= maxm:
			maxm = count
print(maxm)


(n, a) = (int(input()), input().split())
(res, cnt) = (0, 0)
for ai in a:
	cnt = cnt + 1 if ai == '0' else max(cnt - 1, 0)
	res = max(res, cnt)
cnt = a.count('1')
print(res + cnt if cnt < n else n - 1)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
ans = -1
c = 0
for i in l:
	if i == 1:
		c -= 1
	else:
		c += 1
	ans = max(c, ans)
	if c < 0:
		c = 0
print(ans + s)


n = int(input())
a = list(map(int, input().split(' ')))
result = -1
for i in range(0, n):
	t = [0] * n
	for j in range(i, n):
		t[j] = t[j - 1] + (1 - 2 * a[j])
		result = max(result, t[j])
print(result + a.count(1))


n = int(input())
b = list(map(int, input().split()))
ans = 0
s = 0
t = 0
if b.count(1) == n:
	ans = n - 1
else:
	for i in range(n):
		for j in range(i, n):
			if b[j] == 0:
				s = s + 1
			else:
				s = s - 1
			if s > t:
				t = s
		s = 0
	p = b.count(1)
	ans = p + t
print(ans)


a = int(input())
b = [i for i in range(a)]
c = input().split()
Max = 0
Sum = 0
for i in range(a):
	b[i] = int(c[i])
for i in range(a):
	if b[i] == 0:
		for j in range(a):
			Sum = 0
			for k in range(a):
				if k < i or k > j:
					Sum += b[k]
				else:
					Sum += abs(b[k] - 1)
			if Sum > Max:
				Max = Sum
if Max == 0:
	print(a - 1)
else:
	print(Max)


n = int(input())
l = list(map(int, input().split()))
r = 0
r1 = 0
index = [0, 0]
for i in range(n):
	for j in range(i, n):
		r = max(r, l[i:j + 1].count(0) + l[0:i].count(1) + l[j + 1:n].count(1))
print(r)


n = int(input())
l = [int(i) for i in input().split()]
max1 = 0
for i in range(n):
	if l[i] == 1:
		continue
	if i - 1 >= 0 and l[i - 1] == 0:
		continue
	else:
		temp = 0
		for j in range(i, n):
			if l[j] == 0:
				temp += 1
			else:
				temp -= 1
			if temp > max1:
				max1 = temp
			if temp == 0:
				break
if max1 > 0:
	print(sum(l) + max1)
else:
	print(sum(l) - 1)


n = int(input())
b = input().split()
for i in range(n):
	b[i] = int(b[i])
pst = 0
c = b[:]
c[0] = 1 - c[0]
jud = sum(c)
if jud > pst:
	pst = jud
for i in range(1, n):
	c = b[:]
	c[i] = 1 - c[i]
	jud = sum(c)
	if jud > pst:
		pst = jud
	for j in range(0, i):
		c = b[:]
		for a in range(j, i + 1):
			c[a] = 1 - c[a]
		jud = sum(c)
		if jud > pst:
			pst = jud
print(pst)


n = int(input())
a = list(map(int, input().split()))
m = 0
for s in range(n):
	for t in range(s + 1, n + 1):
		m = max(m, sum(a[:s]) + a[s:t].count(0) + sum(a[t:]))
print(m)


n = int(input())
li = list(map(int, input().split(' ')))
count = 0
si = 0
no = 0
m = 0
j = 0
ei = 0
bm = 0
am = 0
for i in li:
	if i == 0:
		count += 1
	else:
		no += 1
		count -= 1
		am += 1
		bm += 1
	if count <= 0:
		si = j + 1
		count = 0
		bm = 0
	if count > m:
		m = count
		ei = bm
		am = 0
	j += 1
if no == n:
	print(n - 1)
else:
	bm = no - ei
	print(m + bm + ei)


n = int(input())
L = [int(x) for x in input().split()]
num = 0
for i in range(n):
	for p in range(1, n - i + 1):
		L0 = L[i:i + p]
		L1 = L[:i] + L[i + p:]
		num = max(num, L0.count(0) + L1.count(1))
print(num)


import itertools as itls
import operator as op
from collections import Counter

def inputarray(func=int):
	return map(func, input().split())

def diff(v, x=0):
	v = iter(v)
	while True:
		try:
			y = next(v)
			yield (y - x)
			x = y
		except StopIteration:
			return
N = int(input())
A = list(inputarray())
R = [0] + [None] * len(A)
S = [0] + [None] * len(A)
P = [0] + list(itls.accumulate(A))
for i in range(len(A)):
	if A[i] == 0:
		R[i + 1] = max(R[i], P[i]) + 1
		S[i + 1] = max(S[i], R[i])
	else:
		R[i + 1] = max(R[i], P[i + 1])
		S[i + 1] = max(S[i], R[i]) + 1
res = max(R[-1], S[-1])
print(res if res != sum(A) else res - 1)


n = int(input())
PP = list(map(int, input().split()))
for i in range(n):
	PP.append(1)
ans = 0

def P(f, e):
	global PP, n
	a = 0
	for i in range(f):
		a += PP[i]
	for i in range(f, e):
		a += not PP[i]
	for i in range(e, n):
		a += PP[i]
	return a
for f in range(n):
	for e in range(f + 1, n + 1):
		ans = max(ans, P(f, e))
print(ans)


n = int(input())
a = list(map(int, input().split()))
l = [0] * n
r = [0] * n
for i in range(1, n):
	l[i] = l[i - 1] + (a[i - 1] == 1)
for i in range(n - 2, -1, -1):
	r[i] = r[i + 1] + (a[i + 1] == 1)
mx_add = -1
total = a.count(1)
for i in range(n):
	for j in range(i, n):
		curr_add = j - i + 1 - 2 * (total - l[i] - r[j])
		if curr_add > mx_add:
			mx_add = curr_add
print(mx_add + total)


n = int(input())
a = list(map(int, input().split()))
f = [[0] * n for i in range(n)]
M = 0
for i in range(n):
	for j in range(i, n):
		mm = 0
		for e in a[i:j + 1]:
			mm += 1 - e
		M = max(M, mm + sum(a[:i]) + sum(a[j + 1:]))
print(M)


n = int(input())
a = list(map(int, input().split()))
left = 0
right = 0
if 0 not in a:
	print(n - 1)
else:
	maxi = 0
	for i in range(n):
		for j in range(i, n):
			count = 0
			for k in range(n):
				if i <= k and k <= j:
					if a[k] == 0:
						count += 1
				elif a[k] == 1:
					count += 1
			maxi = max(maxi, count)
	print(maxi)


(l, t) = ([], [-1])
n = int(input())
m = list(map(int, input().split()))
for i in range(n):
	l.append(m[i])
for i in range(1, n + 1):
	for j in range(n - i + 1):
		u = l[j:i + j]
		(g, h) = (u.count(0), u.count(1))
		if g > h:
			t.append(g - h)
print(max(t) + l.count(1))


arg = int(input())
list1 = list(map(int, input().strip().split()))
list2 = []
for a in range(0, arg):
	if list1[a] == 0:
		list2.append(1)
	else:
		list2.append(-1)
list4 = [list2[0]]
for a in range(1, arg):
	list4.append(list4[a - 1] + list2[a])
res = list2[0]
c = list2[0]
for d in range(1, arg):
	if c < 0:
		c = 0
	c += list2[d]
	res = max(res, c)
	if c < 0:
		c = 0
print(list1.count(1) + res)


n = int(input())
nums = [int(x) for x in input().split()]
max_sum = 0
for i in range(n):
	for j in range(i, n):
		old_sum = nums[i:j + 1].count(1)
		new_sum = nums[i:j + 1].count(0)
		max_sum = max(max_sum, sum(nums) - old_sum + new_sum)
print(max_sum)


n = int(input())
s = [int(x) for x in input().split()]
max = -1
d = {}
if n == 1 and s == [1]:
	print(0)
elif n == 1 and s == [0]:
	print(1)
else:
	for i in range(n):
		for j in range(i, n):
			temp = s[i:j + 1].count(0) - s[i:j + 1].count(1)
			if temp > max:
				max = temp
	print(s.count(1) + max)


n = int(input())
inp = list(map(int, input().split()))
ones = inp.count(1)
ans = 0
for i in range(n):
	for j in range(i, n):
		seg = inp[i:j + 1]
		temp = ones - seg.count(1)
		temp += seg.count(0)
		ans = max(temp, ans)
print(ans)


import math

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	if arr.count(1) == n:
		print(n - 1)
		return
	if n == 1:
		print(0 if arr[0] == 1 else 1)
		return
	ans = 0
	for i in range(len(arr) - 1):
		for j in range(i + 1, len(arr)):
			temp = arr + [9]
			for k in range(i, j + 1):
				if temp[k] == 0:
					temp[k] = 1
				else:
					temp[k] = 0
			ans = max(ans, temp.count(1))
	print(ans)
main()


a = input()
b = input()
a = int(a)
b = [int(i) for i in b.split(' ')]
ma = -1
ans = 0
m = 0
for i in b:
	if i == 0:
		ans += 1
	else:
		ans -= 1
	if ans > ma:
		ma = ans
	elif ans < 0:
		ans = 0
	m += i
print(m + ma)


n = int(input())
x = input().split(' ')
x = [int(x[d]) for d in range(len(x))]
initial = x.count(1)
output = initial
for a in range(len(x)):
	xx = x[:]
	count = 0
	for b in range(a, len(x)):
		xx[b] = 1 - xx[b]
		count = xx.count(1)
		if count > output:
			output = count
			continue
if x.count(0) == 0:
	output = x.count(1) - 1
print(output)


import math
n = int(input())
Li = input().split()
result = []
Li_change = []
k = 0
while k < n:
	Li[k] = int(Li[k])
	Li_change.append(Li[k])
	k += 1
A = tuple(Li_change)
B = []
i = 0
while i < n:
	j = i
	while j < n:
		B = list(A)
		for a in range(i, j + 1):
			B[a] = 1 - B[a]
		m = B.count(1)
		result.append(m)
		j += 1
	i += 1
x = max(result)
print(x)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
l = L()
c = 0
for i in range(n):
	if l[i]:
		l[i] = -1
		c += 1
	else:
		l[i] = 1
s = -10 ** 10
m = -10 ** 10
for i in l:
	s = max(i, s + i)
	m = max(m, s)
print(m + c)


def MaxCrossingSubArray(arr, low, high, mid):
	leftsum = -3759873485
	sum = 0
	for i in range(mid, -1, -1):
		sum += arr[i]
		if sum > leftsum:
			leftsum = sum
			maxleft = i
	rightsum = -382974982
	sum = 0
	for i in range(mid + 1, high + 1):
		sum += arr[i]
		if sum > rightsum:
			rightsum = sum
			maxright = i
	return (maxleft, maxright, leftsum + rightsum)

def FindMaxSubarray(arr, low, high):
	if high == low:
		return (low, high, arr[low])
	else:
		mid = int(low + high) // 2
		(leftlow, lefthigh, leftsum) = FindMaxSubarray(arr, low, mid)
		(rightlow, righthigh, rightsum) = FindMaxSubarray(arr, mid + 1, high)
		(crosslow, crosshigh, crosssum) = MaxCrossingSubArray(arr, low, high, mid)
		if leftsum >= rightsum and leftsum >= crosssum:
			return (leftlow, lefthigh, leftsum)
		elif rightsum >= leftsum and rightsum >= crosssum:
			return (rightlow, righthigh, rightsum)
		else:
			return (crosslow, crosshigh, crosssum)

def solve(n, arr):
	if n == 1:
		if arr == [1]:
			return 0
		else:
			return 1
	low = 0
	high = n - 1
	arr1 = []
	for i in range(n):
		if arr[i] == 0:
			arr1.append(1)
		else:
			arr1.append(-1)
	(i, j, summ) = FindMaxSubarray(arr1, low, high)
	out = arr.count(1)
	out += summ
	return out
n = int(input())
arr = list(map(int, input().split()))
print(solve(n, arr))


n = int(input())
_input = list(map(int, input().split()))
dp = [[0, 0, 0] for i in range(n + 1)]
for i in range(1, n + 1):
	_one = 1 if _input[i - 1] == 1 else 0
	dp[i][0] = dp[i - 1][0] + _one
	dp[i][2] = max(dp[i - 1]) + _one
	_one = 1 if _input[i - 1] == 0 else 0
	dp[i][1] = max((dp[i - 1][0], dp[i - 1][1])) + _one
if len(_input) == sum(_input):
	print(sum(_input) - 1)
else:
	print(max(dp[n] + [sum(_input), len(_input) - sum(_input)]))


n = int(input())
a = [int(i) for i in input().split()]
b = [[0 for i in range(100)] for j in range(100)]
if a[0] == 1:
	b[0][0] = -1
else:
	b[0][0] = 1
m = b[0][0]
for i in range(1, n):
	if a[i] == 0:
		b[0][i] = b[0][i - 1] + 1
	else:
		b[0][i] = b[0][i - 1] - 1
	if b[0][i] > m:
		m = b[0][i]
for i in range(1, n):
	for j in range(i, n):
		if a[i - 1] == 1:
			b[i][j] = b[i - 1][j] + 1
		else:
			b[i][j] = b[i - 1][j] - 1
		if b[i][j] > m:
			m = b[i][j]
print(m + sum(a))


n = int(input())
A = [int(x) for x in input().split(' ')]
B = [0 for y in range(n)]
t = sum(A)
B[0] = 1 + t - 2 * A[0]
i = 1
while i < n:
	B[i] = max(B[i - 1] + 1 - 2 * A[i], t + 1 - 2 * A[i])
	i += 1
print(max(B))


n = int(input())
l = [int(j) for j in input().split()]
t = sum(l)
m = 0
for i in range(0, n):
	for j in range(i, n):
		d = sum(l[i:j + 1])
		f = t - d
		r = f + j - i + 1 - d
		m = max(m, r)
print(m)


num = set()
n = int(input())
l = list(map(int, input().split()))
for i in range(n):
	for j in range(i, n):
		mi = list(l)
		sh = mi[i:j + 1]
		del mi[i:j + 1]
		num.add(sum(mi) + len(sh) - sum(sh))
print('{}'.format(max(num)))


n = int(input())
flip = [int(x) for x in input().split()]
cont = 0
maior = -100000000
aux = flip.copy()
for i in range(n):
	flip = aux.copy()
	for j in range(i, n):
		flip[j] = 1 - flip[j]
		cont = flip.count(1)
		if cont > maior:
			maior = cont
print(maior)


def kadane(arr):
	n = len(arr)
	max_so_far = arr[0]
	max_ending_here = arr[0]
	for i in range(1, n):
		max_ending_here = max(arr[i], max_ending_here + arr[i])
		max_so_far = max(max_so_far, max_ending_here)
	return max_so_far
n = int(input())
a = [int(i) for i in input().split()]
b = [0 for _ in range(n)]
for i in range(n):
	if a[i] == 1:
		b[i] = -1
	else:
		b[i] = 1
gain = kadane(b)
print(a.count(1) + gain)


n = int(input())
list = input().split()
list = [int(i) for i in list]
ls = []
for i in list:
	if i == 1:
		ls.append(-1)
	else:
		ls.append(1)
thisSum = 0
maxSum = 0
for i in ls:
	thisSum += i
	if thisSum > maxSum:
		maxSum = thisSum
	elif thisSum < 0:
		thisSum = 0
if maxSum == 0:
	print(maxSum + sum(list) - 1)
else:
	print(maxSum + sum(list))


A = int(input())
B = input()
num = B.split(' ')
numreal = [int(i) for i in num]
change = []
for i in numreal:
	if i == 1:
		change.append(-1)
	else:
		change.append(1)
count = change.count(-1)
max = -2
sum = 0
for i in change:
	sum += i
	if max < sum:
		max = sum
	if sum < 0:
		sum = 0
print(max + count)


a = int(input())
s = list(map(int, input().split()))
c = 0
for i in range(a):
	for j in range(i, a):
		b = s[i:j + 1]
		p = s[0:i].count(1)
		q = s[j + 1:a].count(1)
		d = b.count(0) + p + q
		c = max(c, d)
print(c)


n = int(input())
s = input()
a = [int(x) for x in s.split()]
c = [0]
sum = 0
for x in a:
	sum = sum + x
	c.append(sum)
max = -1
for i in range(1, n + 1):
	for j in range(i, n + 1):
		bef = c[j] - c[i - 1]
		aft = j - i + 1 - bef
		ch = aft - bef
		if ch > max:
			max = ch
print(c[n] + max)


n = int(input())
arr = [int(x) for x in input().split()]
dp = [[0 for i in range(n)] for j in range(n)]
maxx = 0
imax = 0
jmax = 0
for i in range(n):
	for j in range(i, n):
		if arr[j] == 0:
			dp[i][j] = 1
		else:
			dp[i][j] = -1
		if i != j:
			dp[i][j] += dp[i][j - 1]
		if dp[i][j] > maxx:
			maxx = dp[i][j]
			imax = i
			jmax = j
for i in range(imax, jmax + 1):
	if arr[i] == 0:
		arr[i] = 1
	else:
		arr[i] = 0
c = 0
for i in range(n):
	if arr[i] == 1:
		c += 1
print(c)


import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '7\n1 0 0 1 0 0 0\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]
l = getType(int)
numbers = getArray(int)
s = [-1 if x == 1 else 1 for x in numbers]
dp = [0] * l
for i in range(l):
	dp[i] = s[i]
	if i > 0:
		dp[i] = max(dp[i - 1] + s[i], s[i])
print(max(dp) + sum(numbers))


u = int(input())
l = list(map(int, input().split()))
s = sum(l)
d = 1 - l[0] * 2
for q in range(u):
	for w in range(q, u):
		d = max(d, w - q + 1 - sum(l[q:w + 1]) * 2)
print(s + d)


n = int(input())
v = []
for word in input().split():
	v.append(int(word))
for i in range(n):
	if v[i] == 0:
		v[i] = -1
partialSum = [0] * n
for i in range(n):
	if i == 0:
		partialSum[i] = v[i]
	else:
		partialSum[i] = partialSum[i - 1] + v[i]
counter = 0
for number in v:
	if number == 1:
		counter += 1
dp = [[0 for x in range(n)] for y in range(n)]
i = 0
j = 0
min = 0
for y in range(n):
	for x in range(y, n):
		dp[y][x] = partialSum[x] - partialSum[y] + v[y]
		if min > dp[y][x]:
			(i, j, min) = (y, x, dp[y][x])
if min != 0:
	print(counter + min * -1)
else:
	print(counter - 1)


import re

def solve(a):
	max_diff = 0
	result_i = 0
	result_j = 0
	for i in range(len(a)):
		for j in range(len(a)):
			diff = a[i:j + 1].count(0) - a[i:j + 1].count(1)
			if diff > max_diff:
				max_diff = diff
				result_i = i
				result_j = j
	for i in range(len(a)):
		if i >= result_i and i <= result_j:
			a[i] = 1 - a[i]
	return a.count(1)
n = int(input())
a = [int(x) for x in input().split()]
print(solve(a))


n = int(input())
nums = [int(i) for i in input().split()]
flip = [1 - i for i in nums]
ans = 0
for i in range(n):
	for j in range(i, n):
		new = list(nums[0:i]) + list(flip[i:j + 1]) + list(nums[j + 1:])
		ans = max(ans, sum(new))
print(ans)


import math
elem = int(input())
ar = []
for x in input().split(' '):
	ar.append(int(x))
matrix = [elem * [0] for i in range(elem)]
ma = -1
for x in range(elem):
	if ar[x] == 1:
		matrix[x][x] = -1
	else:
		matrix[x][x] = 1
		if ma < matrix[x][x]:
			ma = matrix[x][x]
for x in range(elem - 1):
	matrix[x][x + 1] = matrix[x + 1][x + 1] + matrix[x][x]
	if ma < matrix[x][x + 1]:
		ma = matrix[x][x + 1]
k = 2
while True:
	for x in range(elem - 1):
		if x + k < elem:
			matrix[x][x + k] = matrix[x][x + k - 1] + matrix[x + 1][x + k] - matrix[x + 1][x + k - 1]
			if ma < matrix[x][x + k]:
				ma = matrix[x][x + k]
	k = k + 1
	if k == elem or k > elem:
		break
print(sum(ar) + ma)


n = int(input())
l = list(map(int, input().split()))
mem = [[0] * n for i in range(n)]
aa = l.count(1)
best = -1
for s in range(n):
	for e in range(s, n):
		if s == e:
			mem[s][e] = -1 if l[s] == 1 else 1
		else:
			mem[s][e] = mem[s][e - 1] + (-1 if l[e] == 1 else 1)
		best = max(best, mem[s][e])
print(aa + best)


t = int(input())
line = [int(i) for i in input().split()]
a = -1
for i in range(t):
	for j in range(i + 1, t + 1):
		b = line[i:j].count(0) - line[i:j].count(1)
		if b > a:
			a = b
		else:
			continue
print(line.count(1) + a)


n = input()
a = list(map(int, input().split()))
m = [0]
for i in range(len(a)):
	for j in range(i, len(a)):
		b = a[i:j + 1]
		m.append(sum(a) + b.count(0) - b.count(1))
print(max(m))


n = int(input())
ls = input().split()
sumval = 0
for i in range(n):
	ls[i] = int(ls[i])
	if ls[i] == 1:
		sumval += 1
maxval = 0
for i in range(0, n):
	for j in range(i, n):
		tmp = sumval
		for k in range(i, j + 1):
			if ls[k] == 1:
				tmp -= 1
			else:
				tmp += 1
		if tmp > maxval:
			maxval = tmp
print(maxval)


n = int(input())
l = list(map(int, input().split(' ')))
s = 0
sc = 0
e = 0
m_sofar = 0
m_cur = 0
for i in range(n):
	if l[i] == 1:
		m_cur = m_cur - 1
	else:
		m_cur = m_cur + 1
	if m_cur < 0:
		m_cur = 0
		sc = i + 1
	if m_cur > m_sofar:
		m_sofar = m_cur
		s = sc
		e = i
for i in range(s, e + 1):
	if l[i] == 1:
		l[i] = 0
	else:
		l[i] = 1
c = 0
for i in range(n):
	if l[i] == 1:
		c = c + 1
print(c)


len = int(input())
num = list(map(int, input().split()))
max_l = 0
for i in range(len):
	for j in range(i, len):
		loc = 0
		for k in range(0, len):
			if k >= i and k <= j:
				loc += 1 - num[k]
			else:
				loc += num[k]
		if loc >= max_l:
			max_l = loc
print(max_l)


n = int(input())
a = [int(x) for x in input().split()]
maximum = -10 ** 7
current = 0
ones_from_front = 0
for i in range(0, n):
	for j in range(i, n):
		array = list(a)
		for k in range(i, j + 1):
			array[k] = 1 - array[k]
		current = 0
		for k in range(0, n):
			if array[k] == 1:
				current += 1
		maximum = max(maximum, current)
print(maximum)


n = int(input())
l = list(map(int, input().split()))
zeros = 0
ones = 0
for i in l:
	if i == 1:
		ones += 1
	else:
		zeros += 1
maxOnes = ones
if l.count(1) == n:
	print(n - 1)
else:
	for i in range(n):
		insideZeros = 0
		insideOnes = 0
		for j in range(i, n):
			if l[j] == 0:
				insideZeros += 1
			else:
				insideOnes += 1
			if ones - insideOnes + insideZeros > maxOnes:
				maxOnes = ones - insideOnes + insideZeros
	print(maxOnes)


n = int(input())
s = list(map(int, input().split()))
a = [[]]
b = s[0]
if 0 in s:
	for e in s:
		if e == b:
			a[-1].append(e)
		else:
			b = e
			a.append([e])
	c = []
	for e in a:
		if e[0] == 0:
			c.append(len(e))
		else:
			c.append(-len(e))
	m = -float('inf')
	for x in range(len(c)):
		for y in range(len(c)):
			if x <= y:
				v = sum(c[x:y + 1])
				if v > m:
					m = v
	print(sum(s) + m)
else:
	print(sum(s) - 1)


n = int(input())
data = input().split()
list_num = []
total = 0
list_max = [0]
for i in range(n):
	list_num.append(data[i])
	total = total + int(list_num[i])
for J in range(n):
	cur = total
	for i in range(J, n):
		if int(list_num[i]) == 1:
			cur = cur - 1
		else:
			cur = cur + 1
		list_max.append(cur)
print(max(list_max))


def mssl(n):
	max_ending_here = max_so_far = 0
	for i in n:
		max_ending_here = max(0, max_ending_here + i)
		max_so_far = max(max_ending_here, max_so_far)
	return max_so_far
while True:
	try:
		n = int(input())
		l = [int(i) for i in input().split()]
		line = []
		if l == [1] * n:
			print(n - 1)
		else:
			for i in l:
				if i == 1:
					line.append(-1)
				else:
					line.append(1)
			print(mssl(line) + sum(l))
	except EOFError:
		break


import re
_ = input()
str_in = input().strip().replace(' ', '')
if str_in.count('0') == 0:
	print(len(str_in) - 1)
else:
	zero_count = [len(x) for x in re.findall('0+', str_in)]
	one_count = [-len(x) for x in re.findall('1+', str_in)]
	if len(zero_count) <= 1:
		print(len(str_in))
	elif str_in[0] == '1':
		count_all = [j for i in zip(one_count, zero_count) for j in i]
		print(str_in.count('1') + max(list((sum(count_all[x:y]) for x in range(1, len(count_all) + 1, 2) for y in range(x + 1, len(count_all) + 1, 2)))))
	else:
		count_all = [j for i in zip(zero_count, one_count) for j in i]
		if len(zero_count) != len(one_count):
			count_all.append(zero_count[-1])
		print(str_in.count('1') + max(list((sum(count_all[x:y]) for x in range(0, len(count_all), 2) for y in range(x + 1, len(count_all) + 1, 2)))))


def solve_dp(arr):
	dp_dict = {-1: 0}
	n = len(arr)
	transformed_arr = [1 if x == 0 else -1 for x in arr]
	max_val = 0
	for i in range(n):
		dp_dict[i] = max(dp_dict[i - 1] + transformed_arr[i], transformed_arr[i])
		max_val = max(dp_dict[i], max_val)
	if max_val == 0:
		return sum(arr) - 1
	return sum(arr) + max_val
n = int(input())
faces = [int(i) for i in input().split()]
assert len(faces) == n
result = solve_dp(faces)
print(result)


n = int(input())
lst = [int(i) for i in input().split()]
(count, maximum) = (lst.count(1), 0)
for i in range(n):
	for j in range(i, n):
		maximum = max(maximum, count + (j - i + 1) - 2 * sum(lst[i:j + 1]))
print(maximum)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
L = inp()
arr = inlt()
if L == arr.count(1):
	print(L - 1)
	exit()
gain = [0 for _ in range(L)]
for (i, v) in enumerate(arr):
	if v == 0:
		gain[i] = 1
	else:
		gain[i] = -1
S = arr.count(1)
mostSoFar = 0
mostEndingHere = 0
for i in range(0, L):
	mostEndingHere = max(mostEndingHere + gain[i], 0)
	mostSoFar = max(mostSoFar, mostEndingHere)
ans = S + mostSoFar
print(ans)


n = int(input())
lis = input().split() + ['1']
count = [0, 0]
lis1 = []
for i in range(len(lis)):
	if lis[i] == '1':
		count[1] += 1
		lis1.append(i)
	if lis[i] == '0':
		count[0] += 1
if count[0] == 0:
	print(count[1] - 2)
elif count[0] < n:
	lis2 = []
	for i in range(len(lis1) - 1):
		for j in range(len(lis1)):
			if j > i and lis1[j] - lis1[i] - 1 > 2 * (j - i - 1):
				lis2.append(lis1[j] - lis1[i] - 1 - 2 * (j - i - 1))
	print(count[1] + max(lis2) - 1)
else:
	print(n)


input()
s = mx = ones = 0
for c in input().split():
	if c == '1':
		ones += 1
		mx = max(0, mx - 1)
	else:
		mx += 1
		s = max(s, mx)
if not s:
	s = ones - 1
else:
	s += ones
print(s)


n = int(input())
a = list(map(int, input().split()))
g = []
for i in range(0, n):
	if a[i] == 1:
		g.append(-1)
	else:
		g.append(1)
m1 = m2 = g[0]
for i in range(1, n):
	m1 = max(g[i], m1 + g[i])
	m2 = max(m2, m1)
m1 = 0
for i in range(0, n):
	if a[i] == 1:
		m1 += 1
print(m2 + m1)


from itertools import chain
n = int(input())
a = [int(a_i) for a_i in input().split()]
ones = [[0] * n for i in range(n)]
for i in range(n):
	for j in range(i, n):
		subseq = a[i:j + 1]
		ones[i][j] = sum(a) - a[i:j + 1].count(1) + a[i:j + 1].count(0)
print(max(list(chain(*ones))))


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest, heapify, heappop, heappush, heapreplace
from math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi
from bisect import bisect_left, bisect_right
import io, os, sysconfig
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def binNumber(n, size=4):
	return bin(n)[2:].zfill(size)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())

def par(a):
	print(' '.join(list(map(str, a))))

def tdl(outerListSize, innerListSize, defaultValue=0):
	return [[defaultValue] * innerListSize for i in range(outerListSize)]

def sts(s):
	s = list(s)
	s.sort()
	return ''.join(s)

def bis(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return [i, True]
	else:
		return [-1, False]

class pair:

	def __init__(self, f, s):
		self.fi = f
		self.se = s

	def __lt__(self, other):
		return (self.fi, self.se) < (other.fi, other.se)

def dcs(l, b):
	return 4 * (l + b) ** 2 / (l * b)
n = ini()
a = iar()
mi = -1
for i in range(n):
	for j in range(i + 1, n + 1):
		b = a[i:j]
		for k in range(j - i):
			if b[k] == 0:
				b[k] = 1
			else:
				b[k] = 0
		cnt = 0
		for k in range(n):
			if k < i or k >= j:
				if a[k] == 1:
					cnt += 1
			elif b[k - i] == 1:
				cnt += 1
		mi = max(mi, cnt)
print(mi)


n = int(input())
l = [int(j) for j in input().split()]
if n == 1:
	if l[0] == 0:
		print(1)
	else:
		print(0)
else:
	m = 0
	s = 0
	t = 0
	g = 0
	ans_l = 0
	ans_r = 0
	m_p = -1
	for i in range(n):
		if l[i] == 0:
			t = 1
		else:
			t = -1
		s += t
		if s > m:
			m = s
			ans_l = m_p + 1
			ans_r = i
		if s < 0:
			s = 0
			m_p = i
	for j in range(n):
		if ans_l <= j <= ans_r:
			g += 1 - l[j]
		else:
			g += l[j]
	print(g)


x = int(input())
li = [0] + list(map(int, input().split()))
ans = 0
for i in range(x + 1):
	li[i] += li[i - 1]
for i in range(x):
	for j in range(i + 1, x + 1):
		ans = max(ans, li[i] + li[x - 1] - li[j] + j - i - li[j] + li[i])
print(ans)


n = int(input())
l = [int(i) for i in input().split()]
t = 0
ts = []
for i in range(n):
	if l[i] == 0:
		t = t + 1
	elif l[i] == 1:
		ts = ts + [t]
		t = t - 1
	if t < 0:
		t = 0
ts = ts + [t]
x = 0
for i in l:
	if i == 1:
		x = x + 1
if x == n:
	print(n - 1)
else:
	print(x + max(ts))


n = int(input())
integers = input().split()
oriInts = [int(x) for x in integers]
integers = [-1 if x == 1 else 1 for x in oriInts]
sum1 = integers[0]
opt = integers[0]
optj = 0
for j in range(n - 1):
	sum1 = integers[j + 1] + max(0, sum1)
	if opt < sum1:
		opt = sum1
		optj = j + 1
opti = optj
sumCopy = opt
while True:
	sumCopy -= integers[opti]
	if sumCopy == 0:
		break
	else:
		opti -= 1
for i in range(opti, optj + 1):
	if oriInts[i] == 1:
		oriInts[i] = 0
	else:
		oriInts[i] = 1
print(sum(oriInts))


import sys
n = int(input())
s = [0] * n
m = -2
l = [int(x) for x in input().split()]
c = sum(l)
for i in range(n):
	if l[i] == 1:
		l[i] = -1
	else:
		l[i] = 1
for i in range(n):
	for j in range(i + 1, n + 1):
		if m < sum(l[i:j]):
			m = sum(l[i:j])
print(c + m)


n = int(input())
s = input().split()
for i in range(len(s)):
	s[i] = int(s[i])
l = [0 for i in range(n)]
for i in range(len(l)):
	if s[i] == 0:
		l[i] = l[i - 1] + 1
	elif s[i] == 1 and i > 0:
		l[i] = l[i - 1] - 1
	if l[i] < 0:
		l[i] = 0
if 0 in s:
	print(max(l) + sum(s))
else:
	print(len(l) - 1)


n = int(input())
sp = [int(i) for i in input().split()]
summ = sum(sp)
maxx = summ
if summ == n:
	print(summ - 1)
else:
	for i in range(n):
		for j in range(i, n):
			summ1 = sum(sp[i:j + 1])
			len1 = len(sp[i:j + 1])
			if maxx < summ - summ1 * 2 + len1:
				maxx = summ - summ1 * 2 + len1
	print(maxx)


n = int(input())
A = [int(i) for i in input().split()]
D = []
for i in range(n):
	B = []
	count = 0
	for p in range(i, n):
		if A[p] == 0:
			count += 1
		else:
			count -= 1
		B.append(count)
	D.append(max(B))
print(sum(A) + max(D))


n = int(input())
bit = [int(i) for i in input().split()]
t = bit.count(1)
for i in range(n):
	for j in range(1, n + 1):
		flip = bit[:i].count(1) + bit[i:i + j].count(0) + bit[i + j:].count(1)
		t = max(t, flip)
if t == bit.count(1):
	t = t - 1
print(t)


n = int(input())
a = [int(x) for x in input().split()]
max_zeros = -1
zeros = ones = 0
while n != 0:
	n -= 1
	if a[n] == 1:
		ones += 1
		if zeros > 0:
			zeros -= 1
	else:
		zeros += 1
		if zeros > max_zeros:
			max_zeros = zeros
print(max_zeros + ones)


n = int(input())
a = [int(x) for x in input().split()]
b = []
ones = 0
for ele in a:
	if ele == 0:
		b.append(1)
	else:
		b.append(-1)
		ones += 1
answer = ones
current = 0
maximum_sum = -10 ** 9
for ele in b:
	current = max(ele, current + ele)
	maximum_sum = max(maximum_sum, current)
answer = ones + maximum_sum
print(answer)


n = int(input())
numbers = list(map(int, input().split()))
count_1 = numbers.count(1)
Max = 0
for i in range(n):
	for j in range(i, n):
		Max = max(Max, count_1 + (j - i + 1) - 2 * sum(numbers[i:j + 1]))
print(Max)


n = int(input())
a = [int(x) for x in input().split(' ')]
ones = sum(a)
maxswap = -1
for i in range(n):
	if a[i] == 1:
		continue
	maxswapi = 0
	swapi = 0
	for j in range(i, n):
		if a[j] == 0:
			swapi += 1
			if swapi > maxswapi:
				maxswapi = swapi
		else:
			swapi -= 1
	if maxswapi > maxswap:
		maxswap = maxswapi
print(ones + maxswap)


n = int(input())
l = map(int, input().split())
c1 = 0
c0 = 0
cmax = -1
for i in l:
	if i == 1:
		c1 += 1
		if c0 > 0:
			c0 -= 1
	else:
		c0 += 1
		if c0 > cmax:
			cmax = c0
print(c1 + cmax)


import sys
from functools import reduce
from collections import Counter
import time
import datetime
from math import sqrt, gcd

def ip():
	return int(sys.stdin.readline())

def sip():
	return sys.stdin.readline()

def mip():
	return map(int, sys.stdin.readline().split())

def mips():
	return map(str, sys.stdin.readline().split())

def lip():
	return list(map(int, sys.stdin.readline().split()))

def matip(n, m):
	lst = []
	for i in range(n):
		arr = lip()
		lst.append(arr)
	return lst

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def minJumps(arr, n):
	jumps = [0 for i in range(n)]
	if n == 0 or arr[0] == 0:
		return float('inf')
	jumps[0] = 0
	for i in range(1, n):
		jumps[i] = float('inf')
		for j in range(i):
			if i <= j + arr[j] and jumps[j] != float('inf'):
				jumps[i] = min(jumps[i], jumps[j] + 1)
				break
	return jumps[n - 1]

def dic(arr):
	return Counter(arr)

def prime(n):
	if n < 2:
		return False
	for i in range(2, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True
n = ip()
arr = lip()
ones = arr.count(1)
val = ones
if ones == n:
	print(n - 1)
else:
	for i in range(n):
		zero = 0
		onee = 0
		for j in range(i, n):
			if arr[j] == 0:
				zero += 1
			else:
				onee += 1
			if ones - onee + zero > val:
				val = ones - onee + zero
	print(val)


n = int(input())
d = list(map(int, input().split()))
mx = 0
for i in range(n):
	for j in range(i, n):
		sum = 0
		for k in range(n):
			if d[k] == 0:
				if k >= i and k <= j:
					sum += 1
			elif k < i or k > j:
				sum += 1
		mx = max(mx, sum)
print(mx)


N = int(input())
l = input().split()
line = []
y = 0
for i in l:
	if int(i) == 1:
		line.append(-1)
		y += 1
	else:
		line.append(1)
list = []
for i in range(0, N):
	a = 0
	for j in range(i, N):
		a += line[j]
		list.append(a)
s = max(list) + y
print(s)


def addition(list1):
	return len(list1) - 2 * list1.count(1)
n = int(input())
s = input()
k = list(map(int, s.split()))
x = k.count(1)
l = [-1]
for i in range(0, n):
	for j in range(i + 1, n):
		l.append(addition(k[i:j + 1]))
if len(s) == 1:
	print(k.count(0))
else:
	print(x + max(l))


input()
ones = balance = max_balance = 0
for a in map(int, input().split()):
	if a:
		(ones, balance) = (ones + 1, max(0, balance - 1))
	else:
		balance += 1
		max_balance = max(balance, max_balance)
print(ones + max_balance if max_balance else ones - 1)


n = int(input())
a = list(map(int, input().split()))
a.append(0)
a.insert(0, 0)
prfs = [0]
for i in range(1, n + 1):
	prfs.append(prfs[-1] + a[i])
ans = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		x = prfs[i - 1]
		y = j - i + 1 - (prfs[j] - prfs[i - 1])
		z = prfs[n] - prfs[j]
		ans = max(ans, x + y + z)
print(ans)


n = int(input().strip())
(cnt, ext, ext_max) = (0, 0, -1)
for a in map(int, input().split()):
	if a == 1:
		cnt += 1
		if ext > 0:
			ext -= 1
	else:
		ext += 1
		if ext > ext_max:
			ext_max = ext
print(cnt + ext_max)


input()
ones = 0
curr = ''
diffs = []
for c in input().split():
	if c != curr:
		diffs.append(0)
		curr = c
	if c == '1':
		ones += 1
		diffs[-1] -= 1
	else:
		diffs[-1] += 1
a = [0]
for d in diffs:
	a.append(a[-1] + d)
mx = -1
for i in range(len(a) - 1):
	for j in range(i + 1, len(a)):
		mx = max(mx, a[j] - a[i])
print(mx + ones)


n = int(input())
l = list(map(int, input().split(' ')))
newar = l[:]
for i in range(len(l)):
	if l[i] == 0:
		newar[i] = 1
	else:
		newar[i] = -1
maxsum = [newar[0]]
for j in range(1, len(l)):
	maxsum += [max(newar[j], maxsum[j - 1] + newar[j])]
gain = max(maxsum)
print(sum(l) + gain)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	if sum(a) == n:
		print(n - 1)
		return
	maximum = 0
	for i in range(n):
		cnt = 0
		for j in range(i, n):
			if a[j] == 1:
				cnt -= 1
			else:
				cnt += 1
			maximum = max(maximum, cnt)
	print(maximum + sum(a))
solve()


n = int(input())
l = [int(x) for x in input().split()]
m = -1
t = -1
d = 0
for i in range(n):
	for j in range(i, n):
		ones = l[i:j + 1].count(1)
		zeros = l[i:j + 1].count(0)
		if zeros > ones:
			if zeros - ones > d:
				d = zeros - ones
				m = zeros
				t = ones
ans = l.count(1)
if m == -1 and t == -1:
	print(n - 1)
else:
	print(ans + m - t)


n = int(input())
x = list(map(int, input().split()))
count1 = 0
count0 = 0
max0 = -1
for i in x:
	if i == 1:
		count1 += 1
		if count0 > 0:
			count0 -= 1
	else:
		count0 += 1
		if count0 > max0:
			max0 = count0
print(count1 + max0)


n = int(input())
a = list(map(int, input().split()))
i = 0
ans = []
for i in range(n):
	j = i
	while j < n:
		s = sum(a) - sum(a[i:j + 1]) * 2 + (j - i + 1)
		ans.append(s)
		j += 1
	i += 1
print(max(ans))


x = int(input())
b = list(map(int, input().split()))
one = b.count(1)
y = z = 0
for i in range(x):
	if b[i]:
		z -= 1
	else:
		z += 1
	y = max(y, z + one)
	z = max(z, 0)
print(y)


def f(x):
	return 1 - x
n = int(input())
a = list(map(int, input().split()))
c = []
for i in range(n):
	for j in range(n - i):
		x = a[0:j]
		y = list(map(f, a[j:j + i + 1]))
		z = a[j + i + 1:n]
		c.append(x.count(1) + y.count(1) + z.count(1))
print(max(c))


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

def flip(l, start, end):
	for i in range(start, end + 1):
		l[i] = 1 - l[i]
	return l
maxi = 0
for start in range(n):
	for end in range(n):
		if start <= end:
			b = list(l)
			flip(b, start, end)
			maxi = max(maxi, sum(b))
print(maxi)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')

def maxSubArraySum(a, size):
	max_so_far = a[0]
	curr_max = a[0]
	for i in range(1, size):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far
n = int(input())
a = list(map(int, input().split()))
d = [1] * n
for i in range(n):
	if a[i] == 1:
		d[i] = -1
print(maxSubArraySum(d, len(d)) + a.count(1))


n = int(input())
lst = list(map(int, input().split(' ')))
cnt = 0
for k in range(1, n + 1):
	for i in range(n):
		temp = lst.copy()
		for j in range(i, i + k):
			if j < n:
				temp[j] = 1 - temp[j]
		cnt = max(cnt, temp.count(1))
print(cnt)


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

def flip(i, j):
	global s
	s0 = list(map(int, s))
	for k in range(i, j + 1):
		s0[k] = 1 - s0[k]
	return sum(s0)
r = 0
for i in range(len(s)):
	for j in range(len(s)):
		if i > j:
			continue
		r0 = flip(i, j)
		if r0 > r:
			r = r0
print(r)


n = int(input())
pi = input().split()
all = list(map(int, pi))
x = all.count(0)
if x == 0:
	print(n - 1)
else:
	suit = []
	for i1 in range(n):
		for i2 in range(i1 - 1, n):
			s = 0
			for i3 in range(i1, i2 + 1):
				s += all[i3]
			s = n - x - 2 * s + i2 + 1 - i1
			suit.append(s)
	print(max(suit))


n = int(input())
li = list(map(int, input().split(' ')))
count = li.count(1)
ans = -1
sum = 0
arr = []
for item in li:
	if item == 1:
		arr.append(-1)
	else:
		arr.append(1)
for item in arr:
	sum += item
	if sum < 0:
		sum = 0
	else:
		ans = max(ans, sum)
if n == 1:
	print(1 - li[0])
elif count == n:
	print(count - 1)
else:
	print(ans + count)


import string
n = int(input())
list1 = tuple((int(i) for i in input().split()))
list2 = list(list1)
s = 0
number = 0
for i in range(n):
	for j in range(i, n):
		for x in range(i, j + 1):
			list2[x] = 1 - list2[x]
		number = list2.count(1)
		s = max(s, number)
		number = 0
		list2 = list(list1)
print(s)


def run():
	n = int(input())
	ds = list(map(int, input().split()))
	if ds.count(1) == len(ds):
		print(len(ds) - 1)
		return
	(best_i, best_j) = (0, 0)
	best_diff = -1
	for di in range(len(ds)):
		for dj in range(len(ds) + 1):
			if di == dj:
				continue
			diff = ds[di:dj].count(0) - ds[di:dj].count(1)
			if diff > best_diff:
				best_i = di
				best_j = dj
				best_diff = diff
	for i in range(len(ds[best_i:best_j])):
		if ds[best_i + i] == 1:
			ds[best_i + i] = 0
		else:
			ds[best_i + i] = 1
	print(ds.count(1))
	return
run()


n = int(input())
a = [int(i) for i in input().split()]
b = [0] * n
for i in range(0, n):
	b[i] = a[i]
m = 0
p = 0
for i in range(0, n):
	p = p + b[i]
	for j in range(i + 1, n):
		z = 0
		for k in range(i, j + 1):
			a[k] = 1 - b[k]
			z = z + a[k]
		for x in range(0, i):
			z = z + b[x]
		for x in range(j + 1, n):
			z = z + b[x]
		if z >= m:
			m = z
if p == 0:
	print(n)
elif p == n:
	print(n - 1)
else:
	print(m)


n = int(input())
a = input().split()
s = 0
m = 0
for i in range(0, n):
	for j in range(i, n):
		for k in range(i, j + 1):
			m += 1 - int(a[k])
		for k in range(0, i):
			m += int(a[k])
		for k in range(j + 1, n):
			m += int(a[k])
		s = max(s, m)
		m = 0
print(s)


n = int(input(''))
l = list(map(int, input().split()))
max1 = 0
c1 = 0
c2 = 0
if len(l) == 1:
	if l[0] == 1:
		print(0)
	else:
		print(1)
elif l.count(0) == 0:
	print(len(l) - 1)
else:
	for i in range(0, len(l)):
		for j in range(i + 1, len(l)):
			t = l[i:j + 1]
			sum1 = 0
			for k in range(0, len(t)):
				if t[k] == 0:
					sum1 += 1
			for k in range(0, i):
				sum1 += l[k]
			for k in range(j + 1, len(l)):
				sum1 += l[k]
			if max1 < sum1:
				max1 = sum1
	print(max1)


def kadane(lst):
	ans = s = 0
	for n in lst:
		s += n
		ans = max(s, ans)
		s = max(s, 0)
	if ans == 0:
		ans = max(lst)
	return ans
n = int(input())
a = list(map(int, input().split()))
b = [1 if a[i] == 0 else -1 for i in range(n)]
print(a.count(1) + kadane(b))


def main():
	n = int(input())
	a = list(map(int, input().split()))
	ones = a.count(1)
	if ones == n:
		return n - 1
	max_ones = ones
	for i in range(n):
		ctr = 0
		for j in range(i, n):
			if a[j] == 0:
				ctr += 1
			else:
				ctr -= 1
			max_ones = max(max_ones, ones + ctr)
	return max_ones
print(main())


import sys

def count(list, condition):
	result = 0
	for e in list:
		if condition(e):
			result += 1
	return result
reader = (list(map(int, line.split())) for line in sys.stdin)
next(reader)
a = next(reader)
n = len(a)
ones = count(a, lambda x: x == 1)
max = -n - 1
for i in range(0, n):
	for j in range(0, i + 1):
		cnt = count(a[j:i + 1], lambda x: x == 0) - count(a[j:i + 1], lambda x: x == 1)
		if cnt > max:
			max = cnt
print(ones + max)


n = int(input())
binary = list(map(int, input().split()))
single1 = binary.count(1)
if single1 == len(binary):
	print(single1 - 1)
else:
	s = []
	for i in binary:
		if i == 1:
			s.append(-1)
		else:
			s.append(1)
	sum1 = 0
	sum2 = -2000
	for i in s:
		sum1 += i
		if sum1 < 0:
			sum1 = 0
		sum2 = max(sum2, sum1)
	print(sum2 + single1)


t = 1
while t > 0:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	c1 = 0
	c0 = 0
	max0 = -1
	for i in l:
		if i == 1:
			c1 += 1
			if c0 > 0:
				c0 -= 1
		else:
			c0 += 1
			if c0 > max0:
				max0 = c0
	print(c1 + max0)


n = int(input())
numbers = input().split()
numbers = [int(i) for i in numbers]
max1 = 0
for i in range(n):
	for k in range(i, n):
		num = numbers[:]
		for p in range(i, k + 1):
			num[p] = 1 - num[p]
		total = sum(num)
		max1 = max(max1, total)
print(max1)


import math

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def count1(l, r):
	c = 0
	for i in range(l):
		if a[i] == 1:
			c += 1
	for i in range(r + 1, n):
		if a[i] == 1:
			c += 1
	for i in range(l, r + 1):
		if a[i] == 0:
			c += 1
	return c
n = inp()
a = inlist()
res = 0
for i in range(n):
	for j in range(i, n):
		res = max(res, count1(i, j))
print(res)


n = int(input())
arr = list(map(int, input().split()))
filtered_arr = []
for i in range(n):
	if arr[i] == 0:
		filtered_arr.append(1)
	else:
		filtered_arr.append(-1)

def max_subarray(numbers):
	best_sum = 0
	best_start = best_end = 0
	current_sum = 0
	for (current_end, x) in enumerate(numbers):
		if current_sum <= 0:
			current_start = current_end
			current_sum = x
		else:
			current_sum += x
		if current_sum > best_sum:
			best_sum = current_sum
			best_start = current_start
			best_end = current_end
	return (best_sum, best_start, best_end)
(sum_tot, start, end) = max_subarray(filtered_arr)
if start is not None and end is not None:
	for i in range(start, end + 1):
		arr[i] = 1 - arr[i]
print(arr.count(1))


n = int(input())
x = list(map(int, input().split()))
if x.count(0) != 0:
	first_index = x.index(0)
	last_index = n - x[::-1].index(0) - 1
cur_max = 0
if x.count(0) != 0:
	for i in range(n):
		for j in range(i + 1, n + 1):
			if x[i:j].count(0) + x[:i].count(1) + x[j:].count(1) > cur_max:
				cur_max = x[i:j + 1].count(0) + x[:i].count(1) + x[j:].count(1)
	if first_index == last_index:
		print(n)
	elif cur_max == 0:
		print(x.count(1) + 1)
	else:
		print(cur_max)
else:
	print(n - 1)


n = int(input())
a = [int(i) for i in input().split()]
ans = []
if a[0] == 1:
	state = -1
else:
	state = 1
ans.append(state)
for i in range(1, len(a)):
	if a[i] == 0:
		state += 1
	else:
		state -= 1
	ans.append(state)
p = 0
for i in range(1, len(ans)):
	if ans[i - 1] == -1:
		p += 1
	ans[i] = ans[i] + p
output = max(ans) + a.count(1)
print(output)


number = int(input())
total = input()
total = total.split()
total = [int(i) for i in total]
num_zero = {}
if number == 1:
	if total[0] == 1:
		print(0)
	else:
		print(1)
else:
	for i in range(number):
		for j in range(i, number):
			a = total[i:j + 1].count(0)
			b = total[i:j + 1].count(1)
			num_zero[a - b] = (i, j + 1, a, b)
	biggest = max(num_zero.keys())
	(x, y, summation, b) = num_zero[biggest]
	before = total[0:x].count(1)
	after = total[y:number].count(1)
	summation += before + after
	print(summation)


n = int(input())
a = [int(i) for i in input().split()]
s = 0
for i in range(n):
	for j in range(i, n):
		st = sum(a) - 2 * sum(a[i:j + 1]) + j - i + 1
		s = max(s, st)
print(s)


n = int(input())
number = list(map(int, input().split()))
answer = 0
if len(number) > 1:
	for i in range(len(number) - 1):
		for j in range(i + 1, len(number)):
			flip = j - i + 1 - sum(number[i:j + 1]) + number[:i].count(1) + number[j + 1:].count(1)
			if flip > answer:
				answer = flip
			else:
				answer = answer
	if number[:len(number) - 1].count(1) + 1 - number[len(number) - 1] > answer:
		answer = number[:len(number) - 1].count(1) + 1 - number[len(number) - 1]
	else:
		answer = answer
if len(number) == 1:
	answer = 1 - sum(number)
print(answer)


q = int(input())
w = list(map(int, input().split()))
e = 0
for i in range(q):
	for j in range(i, q):
		e = max(e, j - i - sum(w[i:j + 1]) + 1 + sum(w[:i]) + sum(w[j + 1:]))
print(e)


def formSumArray(l):
	arr = [l[0]]
	startIndexes = []
	endIndexes = []
	if l[0] == 0:
		startIndexes.append(0)
	for i in range(1, len(l)):
		arr.append(arr[-1] + l[i])
		if l[i - 1] == 0 and l[i] == 1:
			endIndexes.append(i - 1)
		elif l[i - 1] == 1 and l[i] == 0:
			startIndexes.append(i)
	if l[len(l) - 1] == 0:
		endIndexes.append(len(l) - 1)
	return ([0] + arr, startIndexes, endIndexes)

def next(arr, target):
	start = 0
	end = len(arr) - 1
	ans = -1
	while start <= end:
		mid = (start + end) // 2
		if arr[mid] < target:
			start = mid + 1
		else:
			ans = mid
			end = mid - 1
	return ans

def findLongestZeroSubarray(l):
	(sumarray, startIndexes, endIndexes) = formSumArray(l)
	maxnet = 0
	maxstart = -1
	maxend = -1
	for start in startIndexes:
		ind = next(endIndexes, start)
		if ind != -1:
			for i in range(ind, len(endIndexes)):
				end = endIndexes[i]
				oneCount = sumarray[end + 1] - sumarray[start + 1]
				zeroCount = end - start + 1 - oneCount
				net = zeroCount - oneCount
				if net > maxnet:
					maxnet = net
					maxstart = start
					maxend = end
	if maxstart == -1:
		return sumarray[-1] - 1
	return sumarray[maxstart] + (sumarray[-1] - sumarray[maxend + 1]) + (maxend - maxstart + 1) - (sumarray[maxend + 1] - sumarray[maxstart + 1])
input()
l = list(map(int, input().split()))
print(findLongestZeroSubarray(l))


n = int(input())
cap = [int(w) for w in input().split()]
ans = []
for i in range(n):
	for j in range(i, n):
		s = [w for w in cap]
		for a in range(i, j + 1):
			if s[a] == 1:
				s[a] = 0
			else:
				s[a] = 1
		ans.append(s.count(1))
print(max(ans))


n = int(input())
l = input().split()
a = []
a.append([0, 0])
for i in range(n):
	if l[i] == '0':
		a.append([a[i][0] + 1, a[i][1]])
	else:
		a.append([a[i][0], a[i][1] + 1])
big = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		big = max(big, a[n][1] - (a[j][1] - a[i - 1][1]) + (a[j][0] - a[i - 1][0]))
print(big)


def solution():
	n = int(input())
	nums = list(map(int, input().split()))
	max_sum = 0
	l = 0
	k = 0
	ones = 0
	for o in range(n):
		if nums[o] == 1:
			ones += 1
	for i in range(n):
		if nums[i] == 1:
			current_sum = ones - 1
		else:
			current_sum = 1 + ones
		if current_sum > max_sum:
			max_sum = current_sum
		for j in range(i + 1, n):
			if nums[j] == 1:
				current_sum -= 1
			else:
				current_sum = current_sum + 1
			if current_sum > max_sum:
				max_sum = current_sum
	print(max_sum)
solution()


input()
a = input().split(' ')
d = [0] * len(a)
p = [0] * len(a)
if a[0] == '0':
	d[0] = 1
else:
	d[0] = -1
for i in range(1, len(a)):
	if a[i] == '0':
		if d[i - 1] == -1:
			p[i] = i
			d[i] = 1
		else:
			d[i] = d[i - 1] + 1
			p[i] = p[i - 1]
	elif d[i - 1] == -1:
		p[i] = i
		d[i] = -1
	else:
		d[i] = d[i - 1] - 1
		p[i] = p[i - 1]
m = max(d)
j = d.index(m)
i = p[j]
for k in range(i, j + 1):
	if a[k] == '0':
		a[k] = '1'
	else:
		a[k] = '0'
print(a.count('1'))


def solution():
	n = int(input())
	arr = list(map(int, input().split()))
	tot = sum(arr)
	for i in range(n):
		arr[i] = -1 if arr[i] == 1 else 1
	mx = arr[0]
	curr = arr[0]
	for i in range(1, n):
		curr = max(arr[i], arr[i] + curr)
		mx = max(mx, curr)
	print(tot + mx)
	return
solution()


n = int(input())
a = [int(x) for x in input().split()]
arr = []
zeroes = 0
ones = 0
for num in a:
	if num == 0:
		zeroes += 1
	else:
		ones += 1
	arr.append((zeroes, ones))

def getCountOnes(i, j):
	if j == -1:
		return 0
	if i == 0:
		return arr[j][1]
	return arr[j][1] - arr[i - 1][1]

def getCountZeroes(i, j):
	if j == -1:
		return 0
	if i == 0:
		return arr[j][0]
	return arr[j][0] - arr[i - 1][0]

def f(i, j):
	return getCountOnes(0, i - 1) + getCountZeroes(i, j) + getCountOnes(j + 1, len(a) - 1)
maxi = 0
for i in range(len(a)):
	for j in range(i, len(a)):
		maxi = max(maxi, f(i, j))
print(maxi)


n = int(input())
a = input().split()
x = 0
while x < len(a):
	a[x] = int(a[x])
	x = x + 1
x = 0
q = a.count(1)
w = -100
y = 1
z = 0
while x < len(a):
	while y <= len(a):
		z = a[x:y].count(0) - a[x:y].count(1)
		w = max(w, z)
		y = y + 1
	x = x + 1
	y = x + 1
print(w + q)


def invs(s, i, j):
	mi = min(i, j)
	mj = max(i, j)
	ls = list(s)
	for k in range(mi, mj + 1):
		ls[k] = '1' if ls[k] == '0' else '0'
	return str(ls)
input()
s = input()
s = s.replace(' ', '')
mk = 0
for i in range(len(s)):
	for j in range(len(s)):
		t = invs(s, i, j)
		m = t.count('1')
		mk = max(m, mk)
		if mk == len(s):
			break
print(mk)


n = int(input())
a = [int(i) for i in input().split(' ')]
n1 = a.count(1)
if 0 not in a:
	print(n1 - 1)
else:
	i = 0
	n2 = 0
	b = 0
	while i < n:
		if a[i] == 1:
			b = b - 1
		elif a[i] == 0:
			b = b + 1
		if b < 0:
			b = 0
		if b > n2:
			n2 = b
		i = i + 1
	ans = n1 + n2
	print(ans)


n = int(input())
lists = [int(i) for i in input().split()]
lists2 = []
if lists == [1]:
	print('0')
elif lists == [0]:
	print('1')
else:
	for i in range(n):
		for j in range(i, n):
			lists1 = lists[:]
			for k in range(i, j + 1):
				lists1[k] = 1 - lists1[k]
			lists2.append(lists1.count(1))
	print(max(lists2))


n = int(input())
a = list(map(int, input().split()))
cntOne = sum(a)
maxi = 0
check = []
for i in range(n):
	if a[i] == 0:
		check.append(1)
	else:
		check.append(-1)
temp = 0
for i in range(n):
	if check[i] + temp < 0:
		temp = 0
	else:
		temp += check[i]
	maxi = max(maxi, temp)
if cntOne == n:
	print(n - 1)
else:
	print(maxi + cntOne)


import sys
f = sys.stdin.readline
n = int(f().rstrip())
a = list()
for x in f().rstrip().split():
	a.append(int(x))
s = 0
ones = 0
m1 = 0
for x in a:
	if x == 0:
		s += 1
	else:
		s -= 1
		ones += 1
	if s < 0:
		s = 0
	if s > m1:
		m1 = s
if ones == n:
	print(n - 1)
else:
	print(m1 + ones)


def count_interval(i, j, partial_sum):
	left = 0 if i == 0 else partial_sum[i - 1]
	right = partial_sum[j]
	return right - left

def count_before(i, partial_sum):
	if i == 0:
		return 0
	left = 0
	right = partial_sum[i - 1]
	return right - left

def count_after(j, n, partial_sum):
	if j == n:
		return 0
	if j == 0:
		left = 0
	else:
		left = partial_sum[j - 1]
	right = partial_sum[n]
	return right - left

def main():
	n = int(input())
	numbers = [int(elem) for elem in input().split()]
	if numbers.count(1) == n:
		print(n - 1)
		return
	partial_sum = [0] * n
	if numbers[0] == 1:
		partial_sum[0] = 1
	for i in range(1, n):
		number = numbers[i]
		if number == 1:
			partial_sum[i] = partial_sum[i - 1] + 1
		else:
			partial_sum[i] = partial_sum[i - 1]
	maximum = 0
	for i in range(n):
		for j in range(i, n):
			before = count_before(i, partial_sum)
			interval = count_interval(i, j, partial_sum)
			after = count_after(j, n - 1, partial_sum)
			flip = j - i + 1 - interval
			total = before + flip + after
			if total > maximum:
				maximum = total
	print(maximum)
main()


n = int(input())
a = [int(x) for x in input().strip().split()]
dp = [[None for i in range(n)] for j in range(n)]
mx = -1000
for i in range(n):
	if a[i] == 1:
		dp[i][i] = -1
		mx = max(mx, dp[i][i])
	else:
		dp[i][i] = 1
		mx = max(mx, dp[i][i])
for i in range(1, n):
	dp[0][i] = dp[0][i - 1] + dp[i][i]
	dp[i][0] = dp[0][i]
	mx = max(mx, dp[0][i])
for i in range(1, n):
	for j in range(i + 1, n):
		dp[i][j] = dp[i - 1][j] - dp[i - 1][i - 1]
		dp[j][i] = dp[i][j]
		mx = max(mx, dp[i][j])
count = 0
for _ in a:
	if _ == 1:
		count += 1
res = count + mx
print(res)


n = int(input())
a = list(map(int, input().split()))
x = a.count(1)
y = 0
if n == 1 and a[0] == 1:
	print(0)
elif x < n and x > 0:
	for i in range(n - 1):
		for j in range(i + 1, n):
			b = a[i:j + 1]
			if b.count(0) - b.count(1) > 0:
				y = max(y, b.count(0) - b.count(1))
	if y != 0:
		print(x + y)
	else:
		print(0)
elif x == 0:
	print(n)
else:
	print(n - 1)


n = int(input())
lst = list(map(int, input().split()))
(zeroes, ones) = ([], [])
if lst[0] == 0:
	zeroes.append(1)
	ones.append(0)
else:
	ones.append(1)
	zeroes.append(0)
for x in range(1, n):
	if lst[x] == 0:
		zeroes.append(zeroes[-1] + 1)
		ones.append(ones[-1])
	else:
		ones.append(ones[-1] + 1)
		zeroes.append(zeroes[-1])
zeroes.append(0)
ones.append(0)
maxm = -9999
for k in range(1, n + 1):
	for i in range(0, n + 1 - k):
		j = i + k - 1
		zeroz = zeroes[j] - zeroes[i - 1]
		onez = ones[j] - ones[i - 1]
		diff = zeroz - onez
		maxm = max(maxm, diff)
print(ones[-2] + maxm)


n = int(input())
ls = list(map(int, input().split()))
ary = [0 for i in range(n)]
b = [1 if not x else -1 for x in ls]
s = 0
ma = -9000000000.0
mast = 0
maend = 0
st = 0
end = 0
for i in range(n):
	s += b[i]
	if s > ma:
		mast = st
		maend = i + 1
		ma = s
	if s < 0:
		s = 0
		st = i + 1
print(sum(ls) + ma)


n = int(input())
a = [int(a) for a in input().split()]
cnt1 = cnt0 = 0
mx = -1
for i in a:
	if i == 0:
		cnt0 += 1
		mx = max(mx, cnt0)
	else:
		cnt1 += 1
		if cnt0 != 0:
			cnt0 -= 1
print(cnt1 + mx)


n = int(input())
p = [int(i) for i in input().split()]
p.append(2)
if 0 not in p:
	print(n - 1)
	exit()
if 1 not in p:
	print(n)
	exit()
w = []
s = 1
num = 0
for i in range(n):
	if p[i + 1] == p[i]:
		s += 1
	else:
		q = p[i] * s
		w.append([p[i], s, q])
		num += q
		s = 1
ee = 0
nn = len(w)
for i in range(nn):
	if w[i][0] == 0:
		m = 0
		for j in range(i, nn):
			m += w[j][2]
			if (j - i) % 2 == 0:
				m -= w[j][1]
			if m >= 0:
				break
			elif m < ee:
				ee = m
print(num - ee)


n = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
m = 0
max_sum = 0
max_so_far = 0
start = 0
end = 0
ar = []
reset = False
for i in arr:
	if i == 0:
		ar.append(1)
	else:
		ar.append(-1)
for i in range(n[0]):
	max_so_far += ar[i]
	if max_so_far < 0:
		max_so_far = 0
		currentStart = i + 1
		reset = True
	if max_so_far > max_sum:
		if reset == True:
			start = currentStart
			reset = False
		max_sum = max_so_far
		end = i
sum = 0
for i in range(n[0]):
	if i >= start and i <= end:
		if arr[i] == 0:
			sum += 1
	elif arr[i] == 1:
		sum += 1
print(sum)


n = int(input())
p = [int(i) for i in input().split()]
a = 0
b = 0
s = 0
for i in p:
	a += i
if n > 1:
	for i in range(n - 1):
		for j in range(i, n):
			for k in range(i, j + 1):
				s += p[k]
			if a + j - i + 1 - 2 * s > b:
				b = a + j - i + 1 - 2 * s
			s = 0
else:
	if p[0] == 1:
		b = 0
	if p[0] == 0:
		b = 1
print(b)


n = int(input())
b = [-1 if i == 1 else 1 for i in list(map(int, input().split()))]
(s, ms, ans) = (b[0], min(0, b[0]), b[0] + b.count(-1))
for i in range(1, n):
	s += b[i]
	ans = max(ans, b.count(-1) + s - ms)
	ms = min(s, ms)
print(ans)


a = [0 for i in range(105)]
n = int(input())
for (i, x) in enumerate(input().split()):
	a[i] = int(x)
ans = 0
for i in range(n):
	for j in range(i, n):
		b = a.copy()
		for k in range(i, j + 1):
			a[k] = a[k] ^ 1
		anss = 0
		for k in range(n):
			anss += a[k]
		a = b
		ans = max(ans, anss)
print(ans)


n = int(input())
d = {}
l = [int(i) for i in input().split()]
s = 0
e = []
for i in range(n):
	s += l[i]
	d[str(i)] = s
for i in range(n):
	a = i
	while a < n:
		c = d[str(n - 1)] + a - i - 2 * d[str(a)] + 2 * d[str(i)]
		e.append(c)
		a += 1
g = max(e)
if l == n * [1]:
	print(n - 1)
elif l == n * [0]:
	print(n)
else:
	print(g)


import os
import sys
from io import BytesIO, IOBase

def kadanesalgo(arr):
	left = 0
	right = 0
	maxsofar = 0
	mx = 0
	(l, r) = (0, 0)
	while r < len(arr):
		mx += arr[r]
		if mx <= 0:
			mx = 0
			l = r + 1
		elif mx > maxsofar:
			left = l
			right = r
			maxsofar = mx
		r += 1
	return (left, right)

def main():
	n = int(input())
	l = list(map(int, input().split()))
	a = [1 if l[i] == 0 else -1 for i in range(n)]
	(left, right) = kadanesalgo(a)
	for i in range(left, right + 1):
		l[i] = 1 - l[i]
	print(l.count(1))

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
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')

def nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


from math import inf
n = int(input())
a = [int(i) for i in input().split()]
m = -1 * inf
ct = 0
for i in range(n):
	c1 = 0
	c0 = 0
	if a[i] == 1:
		ct += 1
	for j in range(i, n):
		if a[j] == 1:
			c1 += 1
		else:
			c0 += 1
		if c0 - c1 > m:
			m = c0 - c1
print(ct + m)


n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
	if i == 1:
		b.append(-1)
	else:
		b.append(1)
j = 0
h = -1
for i in range(n):
	j = max(b[i] + j, b[i])
	h = max(j, h)
print(sum(a) + h)


def input_array():
	return list(map(int, input().split()))
n = int(input())
high = -1
score_total = 0
total = 0
for i in input_array():
	total += i
	if i:
		score_total -= 1
	else:
		score_total += 1
	high = max(score_total, high)
	if score_total < 0:
		score_total = 0
print(high + total)


n = int(input())
ls = list(map(int, input().split()))
ary = [0 for i in range(n)]
val = 0
for i in range(n):
	val += ls[i]
	ary[i] = val
ma = 0
for i in range(n):
	for j in range(i, n):
		ma = max(ma, ary[i] - ls[i] + (j - i + 1 - (ary[j] - ary[i])) + ary[-1] - ary[j] - ls[j])
print(ma)


n = int(input())
a = list(map(lambda x: 1 if x == '0' else 0, input().split()))
f = g = 0
for i in a:
	f += i
	g += not i
f = a.count(0)
g = a.count(1)
b = [1 if a[0] else -1]
best = b[0]
for i in range(1, n):
	l = 1 if a[i] else -1
	b.append(max(b[i - 1] + l, l))
	best = max(best, b[i])
print(f + best)


def find_index(my_list):
	i = 0
	index_i = -1
	index_j = -1
	best_acc = 0
	while i < len(my_list):
		if my_list[i] == 0:
			j = i
			first_zero = i
			last_zero = i
			acc = best_k = 0
			while j < len(my_list):
				acc += my_list[j] * -2 + 1
				best_k = max(acc, best_k)
				if my_list[j] == 0 and acc >= best_k:
					last_zero = j
				j += 1
				if acc <= 0:
					break
			if best_k > best_acc:
				best_acc = best_k
				index_i = first_zero
				index_j = last_zero
			i = j
		else:
			i += 1
	if index_i == -1:
		index_i = 0
		index_j = 0
	return (index_i, index_j)

def main():
	n = input()
	split_list = input()
	n = int(n)
	my_list = [int(x) for x in split_list.split(' ')]
	(i, j) = find_index(my_list)
	copy_test = my_list.copy()
	sub_index = [1 - x for x in copy_test[i:j + 1]]
	copy_test[i:j + 1] = sub_index
	print(sum(copy_test))
main()


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re 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
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

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 or x == 0:
			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())
l = zzz()
ans = 0
for i in range(n):
	for j in range(i + 1):
		p = l[:]
		for k in range(j, i + 1):
			p[k] = 1 - p[k]
		ans = max(ans, p.count(1))
print(ans)


length = int(input())
arr = list(map(int, input().split()))
best = 0
current = 0
start = 0
end = 0
for i in range(length):
	j = i
	while j < length:
		for x in range(i, j + 1):
			if arr[x] == 0:
				current += 1
			else:
				current -= 1
		if current > best:
			best = current
			start = i
			end = j
		j += 1
		current = 0
best = 0
for x in range(length):
	if x < start or x > end:
		if arr[x] == 1:
			best += 1
	elif arr[x] == 0:
		best += 1
print(best)


n = int(input())
x = [int(i) for i in input().split()]
y = []
for i in range(n):
	for j in range(i, n):
		a = 0
		for k in range(i, j + 1):
			a += 1 - 2 * x[k]
		y.append(a)
print(x.count(1) + max(y))


n = int(input())
s = input().split()
li = []
for ch in s:
	if ch == '1':
		li.append(-1)
	else:
		li.append(1)
maxsum = li[0]
thissum = 0
for i in li:
	thissum += i
	if thissum > maxsum:
		maxsum = thissum
	elif thissum < 0:
		thissum = 0
print(s.count('1') + maxsum)


n = int(input())
data = [int(i) for i in input().split()]
formal = sum(data)
result = 0
if data.count(0) == 0:
	result = -1
elif data.count(0) == 1:
	result = 1
else:
	l = data.index(0)
	data.reverse()
	r = n - 1 - data.index(0)
	data.reverse()
	if r < n - 1:
		j = l + 1
		while j <= r + 1:
			i = l
			while i < j:
				test = data[i:j]
				t = test.count(0) - test.count(1)
				if t > result:
					result = t
				i += 1
			j += 1
	else:
		j = l + 1
		while j <= n - 1:
			i = l
			while i < j:
				test = data[i:j]
				t = test.count(0) - test.count(1)
				if t > result:
					result = t
				i += 1
			j += 1
		k = l
		while k < n - 1:
			test = data[k:]
			t = test.count(0) - test.count(1)
			if t > result:
				result = t
			k += 1
print(formal + result)


t = int(input())
arr = list(map(int, input().split()))
m = 0
for i in range(t):
	for j in range(i, t):
		one = arr[0:i].count(1) + arr[j + 1:].count(1)
		m = max(m, arr[i:j + 1].count(0) + one)
if len(arr) == 1:
	print(abs(arr[0] - 1))
else:
	print(m)


a = 0
a = int(input())
arr = []
arr = list(map(int, input().split()))
arr2 = []
count = 0
most = 0
for i in range(a):
	for j in range(i, a):
		count = 0
		arr2 = list(arr)
		for k in range(i, j + 1):
			arr2[k] = 1 - arr[k]
		for k in arr2:
			count += k
		most = max(count, most)
if len(arr) == 1:
	print(1 - arr[0])
else:
	print(most)


import math
import sys

def dict_sort(ans):
	ans = sorted(ans.items(), reverse=True, key=lambda kv: kv[1])

def seive_primes(n):
	flag = [0] * n
	flag[1] = flag[0] = 1
	i = 2
	while i * i <= n + 1:
		if flag[i] == 0:
			j = i * i
			while j <= n + 1:
				flag[j] = 1
				j += i
		i += 1
	return flag

def is_prime(n):
	if n == 1:
		return 0
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def alphbates():
	return 'abcdefghijklmnopqrstuvwxyz'

def inp():
	ls = list(map(int, input().split()))
	return ls

def mult_inp():
	return map(int, input().split())
n = int(input())
ls = inp()
ans = 0
for i in range(n):
	for j in range(i, n):
		cnt = 0
		for k in range(n):
			if k in range(i, j + 1):
				cnt += 1 - ls[k]
			else:
				cnt += ls[k]
		ans = max(ans, cnt)
print(ans)


n = int(input())
input_str = input()
m = tmp = i = 0
while i < 2 * n - 1:
	if input_str[i] == '0':
		tmp += 1
	elif tmp > 0:
		tmp -= 1
	m = max(m, tmp)
	i += 2
if m == 0:
	print(n - 1)
else:
	print(input_str.count('1') + m)


n = int(input())
l = list(map(int, input().split()))
if 0 in l:
	r = l.count(0)
else:
	r = 0
dp0 = [0 for i in range(n + 1)]
dp1 = [0 for i in range(n + 1)]
for i in range(n):
	if l[i] == 0:
		dp0[i + 1] = dp0[i] + 1
	else:
		dp0[i + 1] = dp0[i]
for i in range(n):
	if l[i] == 1:
		dp1[i + 1] = dp1[i] + 1
	else:
		dp1[i + 1] = dp1[i]
z = float('-inf')
for i in range(1, n + 1):
	for j in range(i):
		z = max(z, dp0[i] - dp0[j] + dp1[j] + dp1[n] - dp1[i])
print(max(z, 0))


def fastio():
	import sys
	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()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
a = [0] * n
a[0] = int(l[0] == 0)
if n == 1:
	print(1 - l[0])
	exit()
i = 0
j = 0
o = 0
z = 0
m = l.count(1)
ans = 0
while j < n:
	z += l[j] == 0
	o += l[j] == 1
	ans = max(ans, m - o + z)
	while z < o:
		z -= l[i] == 0
		o -= l[i] == 1
		i += 1
	j += 1
print(ans)


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

def Sum(i, j):
	if i == 0:
		return j + 1 - sum(a[0:j + 1]) + sum(a[j + 1:])
	return sum(a[0:i]) + j - i + 1 - sum(a[i:j + 1]) + sum(a[j + 1:])
for i in range(n):
	for j in range(i + 1):
		ans = max(ans, Sum(j, i))
print(ans)


def function(ipt: list, i: int, j: int, cache=[]):
	if j == i:
		return
	ipt_slice1 = [ipt[x] for x in range(i, j)]
	if ipt_slice1 in cache:
		return
	else:
		cache.append(ipt_slice1)
	ipt_slice2 = [ipt[x] for x in range(0, i)]
	ipt_slice2 += [ipt[x] for x in range(j, len(ipt))]
	max_one = ipt_slice1.count(0) + ipt_slice2.count(1)
	global max, max_i, max_j
	if max_one > max:
		max = max_one
	function(ipt, i + 1, j)
	function(ipt, i, j - 1)
	return
n = int(input())
ipt = [int(x) for x in input().split()]
max = -1
function(ipt, 0, len(ipt))
print(max)


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 = 0
for i in range(N):
	for j in range(i + 1, N + 1):
		d = j - i - sum(A[i:j] or [0])
		ans = max(ans, sum(A[:i] or [0]) + d + sum(A[j:] or [0]))
print(ans)


x = int(input())
s = input().split()
s = [int(s[i]) for i in range(x)]
A = 0
for i in range(x):
	t = s[:]
	for j in range(i, x):
		t[j] = 1 - t[j]
		A = max(sum(t), A)
if x == 1:
	A = 1 - s[0]
print(A)


n = int(input())
bits = [int(p) for p in input().split()]

def getOnes(start, end):
	total = 0
	for i in range(len(bits)):
		if i >= start and i <= end:
			if bits[i] == 0:
				total += 1
		else:
			total += bits[i]
	return total
high = 0
for i in range(len(bits)):
	for j in range(i, len(bits)):
		temp = getOnes(i, j)
		if temp > high:
			high = temp
print(high)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = array()
ind = [i for i in range(n) if not a[i]]
ans = []
for i in range(len(ind)):
	for j in range(i, len(ind)):
		c1 = sum(a[ind[i]:ind[j] + 1])
		c0 = ind[j] - ind[i] + 1 - c1
		if c0 > c1:
			ans.append((ind[i], ind[j]))
c1 = a.count(1)
for i in ans:
	c_1 = a[:i[0]].count(1) + a[i[1] + 1:].count(1) + a[i[0]:i[1] + 1].count(0)
	c1 = max(c1, c_1)
if ans == []:
	c1 -= 1
print(c1)


n = int(input())
seq = list(map(int, input().split()))
max_un = 0
cur_un = 0
for num in seq:
	if num == 0:
		cur_un += 1
		if cur_un > max_un:
			max_un = cur_un
	elif cur_un > 0:
		cur_un -= 1
answer = seq.count(1) + max_un
if max_un == 0:
	answer -= 1
print(answer)


n = int(input())
l = list(map(int, input().split()))[:n]
g = []
(a, b) = (0, 0)
c = l.count(1)
for i in range(n):
	if l[i] == 0:
		g.append(1)
	else:
		g.append(-1)
if c == n:
	print(n - 1)
else:
	for i in range(n):
		a = max(0, a + g[i])
		b = max(b, a)
	print(c + b)


n = int(input())
lst = [int(i) for i in input().split()]
flip = []
for i in lst:
	if i:
		flip.append(-1)
	else:
		flip.append(1)
max_ending_here = 0
max_so_far = 0
for i in range(0, len(flip)):
	max_ending_here += flip[i]
	if max_ending_here < 0:
		max_ending_here = 0
	elif max_so_far < max_ending_here:
		max_so_far = max_ending_here
if max_so_far:
	print(max_so_far + lst.count(1))
else:
	print(lst.count(1) - 1)


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	if a[i] == 1:
		b.append(-1)
	else:
		b.append(1)
meh = 0
msf = 0
st = 0
en = 0
ost = 0
for i in range(n):
	meh = max(meh + b[i], 0)
	if meh == 0:
		st = i + 1
	if msf < meh:
		ost = st
		en = i
		msf = meh
ct = 0
for i in range(n):
	if ost <= i <= en:
		ct += 1 ^ a[i]
	else:
		ct += a[i]
print(ct)


n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
f = n * [0]
maxnum = 0
for i in range(n):
	ncount = 0
	for j in range(i, n):
		ncount += a[j]
		f[i] = max(f[i], s - ncount + (j - i + 1 - ncount))
	if f[i] > maxnum:
		maxnum = f[i]
print(maxnum)


n = int(input())
a = list(map(int, input().rstrip().split()))
maxm = 0
for i in range(n):
	s = sum(a[:i])
	t = 0
	for j in range(i, n):
		if a[j] == 0:
			t += 1
		u = 0
		for k in range(j + 1, n):
			if a[k] == 1:
				u += 1
		maxm = max(maxm, s + t + u)
	maxm = max(maxm, s + t + u)
print(maxm)


n = int(input())
list_a = input().split()
list_b = [] + list_a
list_sum = []
for i in range(0, n):
	list_a = [] + list_b
	for k in range(i, n):
		list_a = [] + list_b
		m = i
		sum_1 = 0
		while m <= k:
			x = int(list_a[m])
			x = 1 - x
			list_a[m] = x
			m += 1
		for j in range(0, n):
			sum_1 += int(list_a[j])
		list_sum.append(sum_1)
print(max(list_sum))


n = int(input())
a = list(map(int, input().split()))
ans = a.count(1)
if ans == n:
	print(n - 1)
else:
	for i in range(n):
		if a[i] == 0:
			a[i] = 1
		else:
			a[i] = -1
	mxt = 0
	mxs = 0
	st = end = 0
	s = e = 0
	for i in range(n):
		mxt += a[i]
		if mxt <= 0:
			mxt = 0
			s = i + 1
		if mxt > mxs:
			mxs = mxt
			st = s
			end = i
	print(max(ans, ans + mxs))


n = int(input())
a = input().split()
b = [0] * 100
c = [0] * 100
for i in range(len(a)):
	a[i] = int(a[i])
for i in range(len(a)):
	for j in range(0, i + 1):
		q = 0
		for s in range(j, i + 1):
			b[s] = 1 - a[s]
			q = q + b[s]
		for s in range(0, j):
			q = q + a[s]
		for s in range(i + 1, len(a)):
			q = q + a[s]
		c.append(q)
print(max(c))


def swap(arr, n):
	if len(arr) == 1:
		return 1 - arr[0]
	if sum(arr) == n:
		return n - 1
	result = list()
	for i in range(len(arr)):
		if arr[i] == 0:
			zero = 0
			one = 0
			for j in range(i, len(arr)):
				if arr[j] == 0:
					zero += 1
				else:
					one += 1
				if zero < one:
					break
				result = gener(i, j, arr, result)
	return max(result)

def gener(i, j, arr, result):
	temp = list([i for i in arr])
	for step in range(i, j + 1):
		temp[step] = 1 - arr[step]
		result.append(sum(temp))
	return result

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	print(swap(arr, n))
main()


w = int(input())
a = []
s = 0
t = 0
for i in input().split():
	if i == '1':
		a.append(-1)
		s += 1
	else:
		a.append(1)
		t += 1
c = 0
b = 0
for i in a:
	c = max(0, c + i)
	b = max(b, c)
if w == 1 and a[0] == -1:
	print(0)
elif t == 0:
	print(w - 1)
else:
	print(b + s)


n = int(input())
a = list(map(int, input().split()))
b = [0] * n
for i in range(n):
	if a[i] == 0:
		b[i] = 1
	if a[i] == 1:
		b[i] = -1
niz = 0
vverh = 0
sum = 0
max = 0
for i in range(n - 1):
	sum = 0
	for j in range(i, n):
		sum += b[j]
		if sum > max:
			max = sum
			niz = i
			vverh = j
for i in range(niz, vverh + 1):
	if a[i] == 1:
		a[i] = 0
	else:
		a[i] = 1
sum = 0
for i in range(n):
	sum += a[i]
print(sum)


n = int(input())
num = [int(i) for i in input().split()]
max_n = 0
for i in range(n):
	for j in range(i, n):
		nlist = num.copy()
		for k in range(i, j + 1):
			nlist[k] = 1 - nlist[k]
		if nlist.count(1) > max_n:
			max_n = nlist.count(1)
print(max_n)


n = int(input())
m = [int(x) for x in input().split(' ')]
s = []
for j in range(1, n + 1):
	for i in range(1, j + 1):
		h = m[:]
		for k in range(i, j + 1):
			h[k - 1] = 1 - h[k - 1]
		s.append(sum(h))
print(max(s))


n = int(input())
lt = list(map(int, input().split()))
a = []
if lt.count(1) == n:
	print(len(lt) - 1)
elif lt == [0]:
	print('1')
else:
	for i in range(0, n):
		for j in range(i + 1, n):
			for x in range(i, j + 1):
				lt[x] = 1 - lt[x]
			a.append(lt.count(1))
			for x in range(i, j + 1):
				lt[x] = 1 - lt[x]
	print(max(a))


n = int(input())
arr = [int(j) for j in input().split()]
arr2 = [0] * n
if arr.count(1) == n:
	print(n - 1)
else:
	if arr[0] == 0:
		arr2[0] = 1
	for i in range(1, n):
		if arr[i] == 0:
			arr2[i] = max(arr2[i - 1], 0) + 1
		else:
			arr2[i] = arr2[i - 1] - 1
	ans = arr.count(1)
	if max(arr2) > 0:
		print(max(arr2) + ans)
	else:
		print(ans)


n = int(input())
ls = [int(x) for x in input().split()]
total = sum(ls)
longest_zero = 0
current_zero = 0
for i in ls:
	if i == 0:
		current_zero += 1
		longest_zero = max(current_zero, longest_zero)
	else:
		current_zero = max(0, current_zero - 1)
if longest_zero == 0:
	longest_zero = -1
print(total + longest_zero)


z = int(input())
x = list(map(int, input().split()))
if z == 1 and x == [1]:
	print(0)
	exit()
elif z == 1 and x == [0]:
	print(1)
	exit()
q = []
for i in range(z):
	for o in range(i + 1, z + 1):
		f = x.copy()
		e = []
		d = x[i:o]
		for u in d:
			if 1 - u == 1:
				e.append(1)
			f.pop(f.index(u))
		q.append(len(e) + f.count(1))
print(max(q))


x = int(input())
l = input().split()
for i in range(0, x):
	l[i - 1] = int(l[i - 1])
long = []
score = sum(l)
t = 0
i = 0
while i < x - 1:
	t = t + 1
	if l[i] + l[i + 1] == 1:
		long.append(t)
		t = 0
	i = i + 1
if l[i - 1] + l[i] == 1:
	long.append(1)
else:
	long.append(t + 1)
p = len(long)
i = 0
for i in range(0, p):
	if i % 2 == 0 and l[0] == 1:
		long[i] = -long[i]
	if i % 2 == 1 and l[0] == 0:
		long[i] = -long[i]
a = 0
maax = 0
b = 0
t = 0
while a < len(long):
	while b < len(long):
		t = t + long[b]
		if t > maax:
			maax = t
		b = b + 1
	b = a + 1
	a = a + 1
	t = 0
score += maax
if len(long) == 1 and l[0] == 1:
	print(score - 1)
else:
	print(score)


def main():
	n = int(input())
	l = list(map(int, input().split()))
	res = sum(l)
	a = b = 0
	for (i, x) in enumerate(l):
		l[i] = b - a
		a += x
		b += 1 - x
	l.append(b - a)
	(hi, lo) = max(((i, j) for i in range(n + 1) for j in range(i + 1)), key=lambda _: l[_[0]] - l[_[1]])
	if hi == lo:
		res -= 1
	else:
		res += l[hi] - l[lo]
	return res
print(main())


n = int(input())
s = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
	s[i] = s[i - 1] + s[i]
ans = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		ans = max(ans, s[i - 1] + s[n] - s[j] + (j - i + 1) - (s[j] - s[i - 1]))
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
maxsum = 0
for i in range(n):
	for j in range(i, n):
		temp = a.copy()
		for k in range(i, j + 1):
			temp[k] = 1 - temp[k]
		if maxsum < sum(temp):
			maxsum = sum(temp)
print(maxsum)


n = int(input())
arr = list(map(int, input().split()))
max_elem = -1
for i in range(len(arr)):
	for j in range(i, len(arr)):
		curr = arr[i:j + 1].count(0) - arr[i:j + 1].count(1)
		if curr > max_elem:
			max_elem = curr
if max_elem == -1:
	print(len(arr) - 1)
else:
	print(arr.count(1) + max_elem)


def dp012_flipping_game(n, a):
	a.insert(0, 0)
	(one, res, mn, dp) = (0, 0, 0, [0])
	for i in range(1, n + 1):
		dp.append(dp[i - 1])
		if a[i] == 1:
			one += 1
			dp[i] -= 1
		else:
			dp[i] += 1
	for i in range(1, n + 1):
		res = max(res, one + dp[i] - mn)
		mn = min(mn, dp[i])
	return res
n = int(input())
a = list(map(int, input().split()))
print(dp012_flipping_game(n, a))


n = int(input())
line = [int(x) for x in input().split()]
s = sum(line)
e = 0
m = 0
if n == 1:
	print(1 - line[0])
elif sum(line) == n:
	print(n - 1)
else:
	for i in range(n - 1):
		for j in range(i + 1, n + 1):
			e = s - sum(line[i:j])
			tmp = j - i - sum(line[i:j]) + e
			if tmp > m:
				m = tmp
	e = s - sum(line[:-1])
	if sum(line[:n - 1]) + 1 - line[-1] + e > m:
		m = sum(line[:n - 1]) + 1 - line[-1] + e
	print(m)


def find(arr):
	(mlen, msofar, x) = (0, 0, 0)
	for i in range(n):
		if arr[i] == 0:
			x = 1
		else:
			x = -1
		msofar = max(msofar + x, x)
		mlen = max(mlen, msofar)
	return mlen + arr.count(1)
n = int(input())
nums = list(map(int, input().split()))
if nums.count(1) == n:
	print(n - 1)
else:
	print(find(nums))


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i, n):
		c = 0
		for k in range(n):
			if k < i and a[k] == 1 or (k > j and a[k] == 1) or (k >= i and k <= j and (a[k] == 0)):
				c += 1
		ans = max(ans, c)
print(ans)


n = int(input())
a = list(map(int, input().split()))
x = 0
for i in range(n):
	for u in range(n):
		q = a[i:u + 1]
		if a.count(1) - q.count(1) + q.count(0) >= x and q != []:
			x = a.count(1) - q.count(1) + q.count(0)
print(x)


n = int(input())
s = input()
new = [int(i) for i in s.split()]
mymax = 0
for i in range(len(new)):
	for j in range(i, len(new)):
		b = new[:]
		tmp = 0
		for k in range(i, j + 1):
			b[k] = 1 - b[k]
		for k in range(len(new)):
			tmp += b[k]
		if tmp > mymax:
			mymax = tmp
print(mymax)


from sys import stdin
c = [[int(y) for y in x.rstrip().split()] for x in stdin.readlines()][1]
c2 = [1 if x == 0 else -1 for x in c]

def mssl(l):
	best = cur = 0
	for i in l:
		cur = max(cur + i, 0)
		best = max(best, cur)
	return best
if 0 not in c:
	print(sum(c) - 1)
else:
	print(sum(c) + mssl(c2))


n = int(input())
count = 0
m = 0
a = [int(i) for i in input().split()]
for i in range(0, n):
	for j in range(i, n):
		count = 0
		for k in range(0, n):
			if k >= i and k <= j:
				count = count + (1 - a[k])
			else:
				count = count + a[k]
		if count > m:
			m = count
print(m)


n = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))

def tot(ar, i, j):
	sum = 0
	for k in range(n[0]):
		a = ar[k]
		if k >= i and k <= j:
			if a == 0:
				sum += 1
		elif a == 1:
			sum += 1
	return sum
m = 0
start = 0
end = 0
for i in range(n[0]):
	for j in range(i, n[0]):
		t = tot(arr, i, j)
		if t > m:
			start = i
			end = j
			m = t
print(m)


n = int(input())
a = list(input().split())
c = 0
ans = 0
p = []
for x in a:
	p.append(int(x))
curr = p.count(1)
for i in range(n):
	for j in range(i, n):
		c = curr
		for k in range(i, j + 1):
			c += 1 if p[k] == 0 else -1
		ans = max(ans, c)
print('{}'.format(ans))


def flip(x):
	return int(1 - x)
N = int(input())
n = 0
list2 = []
list1 = [int(i) for i in input().split()]
sum0 = sum(list1)
list2.append(sum0)
if sum0 == N:
	print(N - 1)
else:
	for i in range(N):
		list3 = list1[:]
		list4 = []
		for j in range(i, N):
			list3[j] = flip(list1[j])
			list4.append(sum(list3))
		list2.append(max(list4))
	print(max(list2))


n = int(input())
a = list(map(int, input().split()))
maxm = -1
val = 0
count = 0
for i in range(0, n):
	if a[i] == 0:
		add = 1
	else:
		add = -1
		count += 1
	if val + add > add:
		val = val + add
	else:
		val = add
	maxm = max(maxm, val)
print(count + maxm)


def input_array():
	return list(map(int, input().split()))
n = int(input())
array = input_array()
high = -1
score_total = 0
for i in range(len(array)):
	if array[i] == 1:
		score_total -= 1
	elif array[i] == 0:
		score_total += 1
	high = max(score_total, high)
	if score_total < 0:
		score_total = 0
print(high + sum(array))


n = int(input())
s = []
x = list(map(int, input().split()))
z = sum(x)
if n == 1:
	print(abs(1 - x[0]))
	exit(0)
for i in range(len(x) - 1):
	for j in range(i, len(x), 1):
		s.append(z - sum(x[i:j + 1:1]) + (j - i + 1 - sum(x[i:j + 1:1])))
print(max(s))


n = int(input())
a = list(map(int, input().split()))
countmax = 0
for j in range(len(a)):
	for i in range(j + 1):
		count = 0
		for ii in range(len(a)):
			if ii >= i and ii <= j:
				if a[ii] == 0:
					count += 1
			elif a[ii] == 1:
				count += 1
		countmax = max(countmax, count)
print(countmax)


n = int(input())
l = list(map(int, input().split()))
javoob = 0
for i in range(n):
	for j in range(i, n):
		javoob = max(javoob, sum(l[:i]) + sum(l[j + 1:]) + (j + 1 - i - sum(l[i:j + 1])))
print(javoob)


n = int(input())
aList = []
best = 0

def flip(start, end):
	temp = list(aList)
	for x in range(start, end, 1):
		temp[x] = 1 - temp[x]
	return count1s(temp)

def count1s(temp):
	temp1 = list(temp)
	count = 0
	for x in range(0, n, 1):
		count += temp1[x]
	return count
aList = [int(x) for x in input().split()]
for front in range(0, n, 1):
	for back in range(n, front, -1):
		tempBest = flip(front, back)
		if tempBest > best:
			best = tempBest
print(best)


n = int(input())
a = list(map(int, input().split()))
(cnt0, cnt1, max) = (0, 0, 0)
if a.count(1) == n:
	print(n - 1)
else:
	for i in a:
		if i == 0:
			cnt0 += 1
		else:
			cnt1 += 1
		if cnt0 - cnt1 < 0:
			cnt0 = 0
			cnt1 = 0
		if max < cnt0 - cnt1:
			max = cnt0 - cnt1
	print(max + a.count(1))


n = int(input())
array = list(map(int, input().split()))
dp = []
for x in array:
	if x:
		dp.append(-1)
	else:
		dp.append(1)
maxCurrent = 0
maxTotal = -2
for i in range(len(dp)):
	maxCurrent += dp[i]
	if maxCurrent > maxTotal:
		maxTotal = maxCurrent
	if maxCurrent < 0:
		maxCurrent = 0
print(maxTotal + str(array).count('1'))


n = int(input().strip())
arr = list(map(int, input().strip().split()))
ans = 0
for i in range(len(arr)):
	sub = []
	for j in range(i, len(arr)):
		sub.append(1 - arr[j])
		new = arr[:i] + sub + arr[j + 1:]
		ans = max(ans, new.count(1))
print(ans)


n = int(input())
temp = [int(i) for i in input().split()]
if 0 in temp:
	if temp == [0]:
		answer = 1
	else:
		answer = temp.count(1)
		for i in range(n - 1):
			for j in range(i + 1, n + 1):
				x1 = temp[i:j].count(0)
				x2 = len(temp[i:j]) - x1
				t = temp.count(1)
				t += x1
				t -= x2
				if t > answer:
					answer = t
else:
	answer = n - 1
print(answer)


n = int(input())
l = [int(i) for i in input().split()]
ini_zeros = 0
ini_ones = 0
refer = []
result = []
a = 0
c = 0
b = 0
reverse = []
if sum(l) == len(l):
	print(len(l) - 1)
else:
	for i in range(len(l)):
		if l[i] == 0:
			ini_zeros += 1
			if i != 0 and l[i - 1] == 1:
				refer.append([ini_ones])
				ini_ones = 0
			if i == len(l) - 1:
				refer.append([0, ini_zeros])
		else:
			ini_ones += 1
			if i != 0 and l[i - 1] == 0:
				refer.append([0, ini_zeros])
				ini_zeros = 0
				if i == len(l) - 1:
					refer.append([ini_ones])
	if refer[0][0] != 0:
		for i in range(len(refer)):
			a += (-1) ** (i - 1) * refer[i][-1]
			if a < 0:
				a = 0
			result.append(a)
	else:
		for i in range(len(refer)):
			a += (-1) ** i * refer[i][-1]
			if a < 0:
				a = 0
			result.append(a)
	result.sort()
	b += sum(l) + result[-1]
	print(b)


n = int(input())
al = list(map(int, input().split()))
zeros = [0]
c = 0
for a in al:
	if a == 0:
		c += 1
	else:
		c -= 1
	zeros.append(c)
y = -1
for i in range(n):
	for j in range(i, n):
		x = zeros[j + 1] - zeros[i]
		y = max(y, x)
print(sum(al) + y)


n = int(input())
line = input()
temp = line.split()
a = [0 for i in range(n)]
for i in range(n):
	a[i] = int(temp[i])
b = []
for i in range(n):
	for j in range(i, n):
		s = 0
		for k in range(i, j + 1):
			s += a[k]
		b.append(j + 1 - i - s - s)
b.sort()
b.reverse()
print(b[0] + sum(a))


global dp, arr
dp = [[-101 for i in range(100)] for j in range(100)]

def delta(i, j):
	global arr, dp
	if dp[i][j] != -101:
		return dp[i][j]
	dp[i][j] = j - i + 1 - 2 * arr[i:j + 1].count(1)
	return dp[i][j]
n = int(input())
arr = list(map(lambda i: int(i), input().split()))
maxchange = delta(0, 0)
for i in range(0, n):
	for j in range(i, n):
		maxchange = max(maxchange, delta(i, j))
print(maxchange + arr.count(1))


a = int(input())
l = list(input().split())
ma = 0
boo = True
if a == 1 and l[0] == '0':
	print('1')
	boo = False
if a == l.count('1'):
	print(l.count('1') - 1)
	boo = False
if boo:
	for i in range(len(l) + 1):
		for j in range(len(l) + 1):
			nl = l[i:j]
			n1 = nl.count('0')
			n2 = nl.count('1')
			if n1 - n2 > ma:
				ma = n1 - n2
	ans = l.count('1') + ma
	print(ans)


n = int(input())
a = list(map(int, input().split()))
l = [0] * n
for i in range(n):
	for j in range(i + 1, n + 1):
		x = a[i:j].count(0)
		y = a[:i].count(1)
		z = a[j:].count(1)
		s = x + y + z
		l[i] = max(l[i], s)
print(max(l))


n = int(input())
arr = list(map(int, input().split()))
if n == 1:
	if arr[0] == 1:
		print(0)
	else:
		print(1)
else:
	res = 0
	minus = 0
	for i in range(n):
		for j in range(i):
			temp1 = sum(arr[j:i + 1])
			temp = i - j + 1 - 2 * temp1
			if temp > res:
				res = temp
				minus = temp1
	if sum(arr) == n:
		print(n - 1)
	else:
		print(res + sum(arr))


n = int(input())
count0 = 0
count1 = 0
maxRun = -1
for i in input().split():
	t = int(i)
	if t == 1:
		count1 += 1
		if count0 > 0:
			count0 -= 1
	else:
		count0 += 1
		if count0 > maxRun:
			maxRun = count0
ans = count1 + maxRun
print(ans)


a = int(input())
b = []
_1 = 0
for i in map(int, input().split()):
	if i:
		b += [-1]
		_1 += 1
	else:
		b += [1]
(ma, pre) = (-101, 0)
for i in range(a):
	pre = max(pre, 0)
	pre += b[i]
	ma = max(ma, pre)
print(_1 + ma)


n = int(input())
a = list(map(int, input().split()))
totOneCount = 0
currOneCount = 0
currZeroCount = 0
best = 0
for i in range(n):
	if a[i] == 1:
		totOneCount += 1
		currZeroCount += 1
	else:
		currOneCount += 1
	if currZeroCount >= currOneCount:
		currZeroCount = 0
		currOneCount = 0
	elif currOneCount - currZeroCount > best:
		best = currOneCount - currZeroCount
if totOneCount == n:
	print(totOneCount - 1)
else:
	print(totOneCount + best)


n = int(input())
l = list(map(int, input().split(' ')))
res = 0
count = 0
mx = 0
for i in range(n):
	if l[i] == 0:
		count += 1
	else:
		count -= 1
	mx = max(count, mx)
	if count < 0:
		count = 0
if l.count(1) == n:
	print(n - 1)
else:
	print(mx + l.count(1))


nStr = input()
n = int(nStr)
anStr = input()
anList = anStr.split()
aLs = [0] * n
sumLs = [0] * n
for i in range(n):
	aLs[i] = int(anList[i])
if sum(aLs) == n:
	print(n - 1)
	quit()

def rflipSum(numList, startPos):
	sumMax = 0
	for i in range(startPos):
		curSum = sum(numList[0:i]) + startPos - i - sum(numList[i:startPos])
		if curSum > sumMax:
			sumMax = curSum
	return sumMax
flip = False
if aLs[0] == 1:
	sumLs[0] = 1
	flip = False
else:
	sumLs[0] = 1
	flip = True
i = 1
while i < n:
	if aLs[i] == 1:
		sumLs[i] = sumLs[i - 1] + 1
		flip = False
	else:
		sumLs[i] = max(rflipSum(aLs, i) + 1, sumLs[i - 1])
	i = i + 1
print(sumLs[n - 1])


n = int(input())
a = [int(i) for i in input().split()]
b = c = x = 0
f = 0
g = 1
for i in range(0, n):
	for j in range(i, n):
		for k in range(i, j + 1):
			if a[k] == 0:
				b += 1
			else:
				c += 1
		if x <= b - c:
			x = b - c
			f = i
			g = j + 1
		b = 0
		c = 0
for i in range(f, g):
	a[i] = 1 - a[i]
for i in range(0, n):
	if a[i] == 1:
		c += 1
print(c)


n = int(input())
arr = list(map(int, input().split()))
ans = []
x = arr.count(1)
for i in range(n):
	for j in range(i, n):
		a = arr[i:j + 1].count(1)
		b = j - i + 1 - a
		s = x + b - a
		ans.append(s)
print(max(ans))


n = int(input())
l = [int(i) for i in input().split()]
i = 1
j = 0
u = -100000
w = 0
t = 0
while i <= n:
	j = i
	while j <= n:
		p = 0
		for x in range(i - 1, j):
			if l[x] == 1:
				p = p - 1
			else:
				p = p + 1
		if p > u:
			u = p
		j = j + 1
	i = i + 1
for y in l:
	w += y
print(int(w + u))


n = int(input())
s = [int(x) for x in input().split(' ')]

def solve(s, n):
	memo = []
	maxOne = 0
	for i in range(n):
		if i == 0:
			memo.append(s[i])
		else:
			memo.append(memo[i - 1] + s[i])
	total = memo[-1]
	for i in range(n):
		for j in range(i, n):
			if i == j:
				flipValue = total + 1 if s[i] == 0 else total - 1
			elif i == 0:
				flipValue = j + 1 - memo[j] + (total - memo[j])
			else:
				flipValue = memo[i - 1] + (j + 1 - i - (memo[j] - memo[i - 1])) + (total - memo[j])
			maxOne = max(maxOne, flipValue)
	return maxOne
print(solve(s, n))


n = int(input())
a = [int(x) for x in input().split()]
if n == sum(a):
	print(n - 1)
else:
	maxSoFar = 0
	for i in range(n):
		current = 0
		for j in range(n - i):
			if a[i + j] == 0:
				current += 1
			else:
				current -= 1
			if current <= 0:
				break
			maxSoFar = max(maxSoFar, current)
	print(maxSoFar + a.count(1))


R = lambda : map(int, input().split())
n = int(input())
L = [0] + list(R())
if sum(L) == n:
	print(n - 1)
	exit(0)
elif n == 1:
	if L[1] == 0:
		print(1)
		exit(0)
for i in range(1, n + 1):
	L[i] += L[i - 1]
ma = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		ma = max(ma, j - i + 1 - (L[j] - L[i - 1]) + (L[n] - L[j]) + L[i - 1])
print(ma)


a = int(input())
x = list(map(int, input().split()))
s = 0
t = 0
m = 0
if len(x) == 1 and x[0] == 1:
	print(0)
elif len(x) == 1 and x[0] == 0:
	print(1)
elif len(x) == sum(x):
	print(len(x) - 1)
else:
	for i in x:
		if i == 0:
			s += 1
			m = max(m, s - t)
		elif s > 0 and i == 1:
			t += 1
		if s < t:
			s = 0
			t = 0
	print(sum(x) + m)


n = int(input())
v = list(map(int, input().split()))
ret = 0

def tap(x, y):
	for i in range(x, y + 1):
		v[i] = 1 - v[i]
	t = v.count(1)
	for i in range(x, y + 1):
		v[i] = 1 - v[i]
	return t
for i in range(n):
	for h in range(i, n):
		ret = max(ret, tap(i, h))
print(ret)


delta = 0
temp = 0
n = int(input())
a = [int(i) for i in input().split()]
initial = sum(a)
if initial == n:
	print(initial - 1)
else:
	for i in a:
		if i == 0:
			temp += 1
			if temp > delta:
				delta = temp
		else:
			temp -= 1
			if temp < 0:
				temp += 1
	print(delta + initial)


n = int(input())
s = list(map(int, input().split()))
c = 0
m = 0
for i in s:
	if i == 0:
		c = c + 1
	elif i == 1 and c > 0:
		if c > m:
			m = c
		c = c - 1
if c > m:
	m = c
if m == 0:
	m = s.count(1) - 1
else:
	m = m + s.count(1)
print(m)


n = int(input())
line = ''.join(input().split())
scores = []
for i in range(n):
	ones = line[:i].count('1')
	for j in range(i, n):
		zeros = line[i:j + 1].count('0')
		scores.append(ones + zeros + line[j + 1:].count('1'))
print(max(scores))


import copy
n = int(input())
a = input().split()
for i in range(len(a)):
	a[i] = int(a[i])
sumb = []
for i in range(n):
	for j in range(i, n):
		b = copy.copy(a)
		for k in range(i, j + 1):
			b[k] = 1 - b[k]
		sumb.append(sum(b))
ma = 0
for i in range(len(sumb)):
	if sumb[i] > ma:
		ma = sumb[i]
print(ma)


n = int(input())
card = [int(l) for l in input().split()]
sumx = 0
if len(card) == sum(card):
	sumx = sum(card) - 1
else:
	for i in range(n):
		if card[i] == 0:
			for j in range(i, n):
				sumx = max(sumx, sum(card) + len(card[i:j + 1]) - 2 * sum(card[i:j + 1]))
print(sumx)


n = int(input())
lst = list((int(num) for num in input().split()))[:n]
k = sum(lst)
for i in range(0, n):
	if lst[i] == 1:
		lst[i] = -1
	else:
		lst[i] = 1
if k == n:
	print(n - 1)
else:
	sum1 = sum2 = 0
	for i in range(0, n):
		sum1 = max(sum1 + lst[i], 0)
		sum2 = max(sum1, sum2)
	print(sum2 + k)


a = int(input())
b = input().split()
possibilities = []
for i in range(a + 1):
	for j in range(i, a + 1):
		possibilities.append(b[i:j])
totalones = 0
for i in b:
	if i == '1':
		totalones += 1
maxVal = -1000
for i in possibilities:
	if i != []:
		currVal = 0
		for x in i:
			if x == '1':
				currVal -= 1
			else:
				currVal += 1
		if currVal > maxVal:
			maxVal = currVal
print(totalones + maxVal)


n = int(input())
a = [int(i) for i in input().split()]
total = 0
for i in range(n):
	for j in range(i + 1, n + 1):
		if sum(a) + len(a[i:j]) - 2 * sum(a[i:j]) > total:
			total = sum(a) + len(a[i:j]) - 2 * sum(a[i:j])
print(total)


import sys
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	for j in range(i + 1):
		b = list(a)
		for k in range(j, i + 1):
			b[k] ^= 1
		ans = max(ans, sum(b))
print(ans)


n = int(input())
a_s = [int(s) for s in input().split()]
bs = [0] * (n + 1)
minimum = 0
for a in a_s:
	if a == 1:
		minimum += 1
bs[0] = minimum
for i in range(0, n):
	if a_s[i] == 0:
		bs[i + 1] = bs[i] + 1
	else:
		bs[i + 1] = max(bs[i] - 1, minimum)
res = max(bs)
print(res if res != minimum else res - 1)


n = int(input())
pointer = 0
curSum = 0
maxSum = 0
sum = 0
arr = [int(element) for element in input().split()]
for i in range(n):
	sum += arr[i]
while pointer < n:
	if arr[pointer] == 0:
		curSum += 1
	else:
		curSum += -1
	if curSum > maxSum:
		maxSum = curSum
	elif curSum < 0:
		curSum = 0
	pointer += 1
if maxSum != 0:
	print(sum + maxSum)
else:
	print(sum - 1)


def main():
	from sys import stdin
	n = int(stdin.readline())
	ans = -1
	(ones, onesInterval, subarrsum) = (0, 0, 0)
	for ai in map(int, stdin.readline().split()):
		subarrsum += -1 if ai else 1
		ones += ai
		if subarrsum > ans:
			ans = subarrsum
			onesInterval += ai
		if subarrsum < 0:
			subarrsum = 0
			onesInterval = 0
	print(ans + ones - onesInterval)
main()


def count(l):
	n = 0
	for i in range(len(l)):
		if l[i] == 1:
			n += 1
	return n

def flip(l, i, j):
	for a in range(i, j + 1):
		if l[a] == 0:
			l[a] = 1
		else:
			l[a] = 0
	return l
n = int(input())
num = list(map(int, input().split()))
maxx = 0
for i in range(n):
	for j in range(i, n):
		num1 = list(num)
		flip(num1, i, j)
		x = count(num1)
		if x >= maxx:
			maxx = x
print(maxx)


n = int(input())
List = [int(x) for x in input().split(' ')]
Sum = 0
for i in List:
	Sum += i
ans = -1
for i in range(n):
	j = i
	s = 0
	cnt = 1
	while j < n:
		s += List[j]
		AfterFlip = cnt - s
		curAns = Sum - s + AfterFlip
		ans = max(ans, curAns)
		cnt += 1
		j += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
answer = 0
for j in range(n):
	for i in range(j + 1):
		b = a.copy()
		c = b.copy()
		for k in range(i, j + 1):
			b[k] = 1 - b[k]
		if sum(b) > answer:
			answer = sum(b)
print(answer)


def main():
	n = int(input())
	ans = -1
	(ones, onesInterval, subarrsum) = (0, 0, 0)
	for ai in map(int, input().split()):
		subarrsum += -1 if ai else 1
		ones += ai
		if subarrsum > ans:
			ans = subarrsum
			onesInterval += ai
		if subarrsum < 0:
			subarrsum = 0
			onesInterval = 0
	print(ans + ones - onesInterval)
main()


n = int(input())
a = list(map(int, input().split()))
b0 = [0] * (n + 1)
b1 = [0] * (n + 1)
acc0 = 0
acc1 = 0
for (i, ai) in enumerate(a, 1):
	if ai:
		acc1 += 1
	else:
		acc0 += 1
	b0[i] = acc0
	b1[i] = acc1
ans = 0
for i in range(1, n + 1):
	for j in range(i):
		ans = max(ans, b1[j] + (b0[i] - b0[j]) + (b1[n] - b1[i]))
print(ans)


def hhh(num):
	return 1 - 2 * int(num)
n = int(input())
str1 = input()
list1 = str1.split()
sum = list1.count('1')
list2 = list(map(hhh, list1))
a = -1
for i in range(0, n):
	c = 0
	b = 0
	while i + c < n:
		c = c + 1
		b = b + list2[i + c - 1]
		a = max(a, b)
print(sum + a)


n = int(input())
m = input().split()
list1 = []
list2 = []
for i in m:
	list1.append(int(i))
c = 0
for y in list1:
	if y == 1:
		c += 1
if list1 == [1] * len(list1):
	print(len(list1) - 1)
else:
	a = 0
	while a <= len(list1):
		b = a
		while b <= len(list1):
			one = 0
			zero = 0
			for x in list1[a:b]:
				if x == 1:
					one += 1
				if x == 0:
					zero += 1
			result = zero - one
			list2.append(result)
			b = b + 1
		a = a + 1
	print(c + max(list2))


n = int(input())
a = list(map(int, input().split()))
(q, w, e, r) = (0, 0, 0, -1)
for i in range(n):
	q += a[i]
	w += 1 - a[i] * 2
	r = max(r, w - e)
	e = min(w, e)
ans = q + r
print(ans)


n = int(input())
IN = []
In = input().split()
ans = []
for i in range(0, n):
	IN.append(int(In[i]))
for i in range(1, n + 1):
	for j in range(0, n):
		l = IN[:]
		if j + i > n:
			break
		for k in range(j, j + i):
			l[k] = 1 - l[k]
		ans.append(sum(l))
result = max(ans)
print(result)


from math import sqrt
n = int(input())
arr = [1 if int(x) == 0 else -1 for x in input().split(' ')]
(max_s, sum_n, left, right, m_l) = (arr[0], 0, 0, 0, 0)
for i in range(n):
	sum_n += arr[i]
	if sum_n > max_s:
		max_s = sum_n
		right = i
		left = m_l
	if sum_n < 0:
		sum_n = 0
		m_l = i + 1
for i in range(n):
	if i < left or i > right:
		if arr[i] == -1:
			max_s += 1
	elif arr[i] == -1:
		max_s += 1
if max_s < 0:
	print(0)
else:
	print(max_s)


n = int(input())
line = [int(i) for i in input().split()]
fp = [0] * (n + 1)
b = 0
for i in range(n):
	if line[i] == 1:
		b += 1
for s in range(n):
	if line[s] == 0:
		for i in range(s, n):
			if line[i] == 0:
				fp[i] = fp[i - 1] + 1
			elif line[i] == 1:
				fp[i] = fp[i - 1] - 1
			if fp[i] == 0:
				break
if fp == [0] * (n + 1):
	print(n - 1)
else:
	c = max(fp)
	b += c
	print(b)


n = int(input())
a = [int(x) for x in input().split()]
b = [-1 if x == 1 else 1 for x in a]
k = b[0]
m = k
for e in b[1:]:
	if k + e >= e:
		k += e
	else:
		k = e
	m = max(k, m)
print(m + sum(a))


import sys
import os.path
import math
from sys import stdin, stdout
from collections import *
from math import gcd, ceil, floor
mod = int(1000000000.0 + 7)
m1 = float('inf')
if os.path.exists('Updated prg/Input3d.txt'):
	sys.stdout = open('Updated prg/Output3d.txt', 'w')
	sys.stdin = open('Updated prg/Input3d.txt', 'r')

def sinp():
	return input()

def ninp():
	return int(sinp())

def mapinp():
	return map(int, sinp().split())

def smapinp():
	return map(str, sinp().split())

def linp():
	return list(mapinp())

def sl():
	return list(sinp().split())

def prnt(a):
	print(a)

def prntl(a):
	print(a, end=' ')

def prntlist(l1):
	print(*l1)

def power(n):
	return 1 << n
n = ninp()
a = linp()
(zero, m1) = (0, float('-inf'))
for i in a:
	if i == 1:
		zero = zero - 1
	else:
		zero = zero + 1
	if zero > m1:
		m1 = zero
	if zero < 0:
		zero = 0
s1 = sum(a)
prnt(s1 + m1)


n = int(input())
A = input()
B = []
count = 0
a = 0
b = 0
o = 0
Max = 0
for i in A.split():
	B.append(int(i))
	if int(i) == 1:
		o = o + 1
if o == n:
	print(o - 1)
else:
	for j in range(0, n):
		if B[j] == 0 and count >= 0:
			count = count + 1
		elif B[j] == 1 and count > 0:
			count = count - 1
			b = b + 1
		elif B[j] == 1 and count == 0:
			b = 0
		if count > Max:
			Max = count
	print(o + Max)


n = int(input())
a = list(map(int, input().split()))
d = [0 for i in range(n)]
cnt = 0
for i in range(n):
	if a[i] == 1:
		cnt += 1
for i in range(n):
	if a[i] == 0:
		d[i] = 1
	else:
		d[i] = -1
	if i > 0:
		d[i] = max(d[i], d[i] + d[i - 1])
print(max(d) + cnt)


n = int(input())
lst = list(map(int, input().split()))
ans = 0
count = lst.count(1)
for i in range(n):
	for j in range(i, n):
		segment = lst[i:j + 1]
		value = count - segment.count(1)
		value += segment.count(0)
		ans = max(ans, value)
print(ans)


d = 0
t = 0
n = int(input())
l = [int(i) for i in input().split()]
s = sum(l)
if s == n:
	print(s - 1)
else:
	for i in l:
		if i == 0:
			t += 1
			if t > d:
				d = t
		else:
			t -= 1
			if t < 0:
				t += 1
	print(d + s)


n = int(input())
line = str(input()).split()
line = [int(it) for it in line]
room = 0
flag0 = False
flag1 = False
note_list = list()
for i in range(n):
	if line[i] == 0:
		if flag0:
			note_list.append(room * -1)
			room = 0
			flag0 = False
		room += 1
		flag1 = True
	if line[i] == 1:
		if flag1:
			note_list.append(room)
			room = 0
			flag1 = False
		room += 1
		flag0 = True
if line[n - 1] == 1:
	if flag0:
		note_list.append(room * -1)
if line[n - 1] == 0:
	if flag1:
		note_list.append(room)
max_value = -1
curr_value = -1
length = len(note_list)
for i in range(length):
	for j in range(i + 1, length + 1):
		curr_value = sum(note_list[i:j])
		if curr_value > max_value:
			max_value = curr_value
print(sum(line) + max_value)


n = int(input())
arr = list(map(int, input().split()))
tot = 0
cml = list()
max = 0
for i in arr:
	tot += i
	cml.append(tot)
for i in range(len(cml)):
	for j in range(i, len(cml)):
		tot1 = tot + (j - i + 1 - 2 * (cml[j] - cml[i] + arr[i]))
		if tot1 > max:
			max = tot1
print(max)


n = int(input())
a = list(map(int, input().split()))
k = c = 0
for i in range(n):
	if a[i] == 0:
		k = k + 1
		if c < k:
			c = k
	elif k > 0:
		k = k - 1
	else:
		k = 0
if c == 0:
	c = -1
print(sum(a) + c)


b = int(input())
a = [int(i) for i in input().split()]
p = 0
m = sum(a)
n = 0
for i in a:
	if i == 0:
		n += 1
		if n > p:
			p = n
	elif n:
		n -= 1
	if p == 0:
		p -= 1
print(p + m)


n = int(input())
a = [int(i) for i in input().split()]
max0 = 0
sum1 = 0
for i in range(n):
	for j in range(i, n):
		sum0 = 0
		for k in range(i, j + 1):
			if a[k] == 0:
				sum0 += 1
			else:
				sum0 -= 1
		if sum0 > max0:
			max0 = sum0
for i in range(n):
	sum1 += a[i]
if sum1 < n:
	print(sum1 + max0)
else:
	print(sum1 - 1)


n = int(input())
m = list(map(int, input().split()))
s = 0
if m.count(1) == n:
	print(n - 1)
	exit()
for i in range(0, n):
	for j in range(i, n + 1):
		db = m[:i].count(1) + m[i:j].count(0) + m[j:n].count(1)
		if s < db:
			s = db
print(s)


def modifiedKadane(arr):
	(bestSum, currentSum) = (arr[0], arr[0])
	(currentStart, currentEnd) = (0, 0)
	(bestStart, bestEnd) = (0, 0)
	for i in range(1, n):
		if currentSum + arr[i] >= arr[i]:
			currentSum += arr[i]
			currentEnd = i
		else:
			currentStart = currentEnd = i
			currentSum = arr[i]
		if currentSum >= bestSum:
			bestSum = currentSum
			bestStart = currentStart
			bestEnd = currentEnd
	return (bestStart, bestEnd)
n = int(input())
a = list(map(int, input().split()))
if 0 not in a:
	print(n - 1)
else:
	b = [0] * n
	for i in range(n):
		if a[i] == 1:
			b[i] -= 1
		else:
			b[i] += 1
	(i, j) = modifiedKadane(b)
	for c in range(i, j + 1):
		if a[c] == 1:
			a[c] = 0
		else:
			a[c] = 1
	print(a.count(1))


n = int(input())
s = [int(i) for i in input().split()]
l = len(s)
li = []
for i in range(l):
	for j in range(i, l):
		tmp = s[i:j + 1]
		m = 0
		for k in tmp:
			if k == 0:
				m += 1
			else:
				m -= 1
		li.append(m)
print(sum(s) + max(li))


n = int(input())
a = input().split()
s = 0
if n > 1:
	for i in a:
		if i == '1':
			s += 1
	gain = 0
	st = s
	for i in a:
		if i == '0':
			gain += 1
		else:
			gain -= 1
		if gain < 0:
			gain = 0
		if st + gain > s:
			s = gain + st
	if s == st:
		print(s - 1)
	else:
		print(s)
elif a[0] == '1':
	print(0)
else:
	print(1)


n = int(input())
a = list(map(int, input().split()))
l1 = []
count = 0
for i in range(0, n):
	count += a[i]
	l1.append(count)
maxm = -n - 1
for i in range(0, n):
	for j in range(i, n):
		x = j + 1 - i
		y = l1[j] - l1[i] + a[i]
		p = x - y - y
		maxm = max(maxm, p)
print(maxm + l1[-1])


n = int(input())
l = list(map(int, input().split()))
big = 0
s = 0
c1 = 0
for i in l:
	if i == 1:
		c1 += 1
		s = max(0, s - 1)
	else:
		s += 1
		big = max(big, s)
if c1 == n:
	print(n - 1)
else:
	print(big + c1)


def ct(s, n):
	ans = 0
	for i in s:
		if i == n:
			ans += 1
	return ans
n = int(input())
s = list(map(int, input().split()))
ans = -2
for i in range(n):
	for j in range(i + 1, n + 1):
		y = ct(s[0:i], 1) + ct(s[i:j], 0) + ct(s[j:], 1)
		if y > ans:
			ans = y
print(ans)


n = int(input())
a = list(map(int, input().split()))
c1 = 0
c0 = 0
max0 = -1
for i in range(n):
	if a[i] == 1:
		c1 += 1
		if c0 > 0:
			c0 -= 1
	else:
		c0 += 1
		if c0 > max0:
			max0 = c0
print(c1 + max0)


def result(l, n, i, j):
	sum = 0
	for k in range(n):
		if k >= i - 1 and k <= j - 1:
			sum += 1 - l[k]
		else:
			sum += l[k]
	return sum

def bestresult(l, n):
	return max([max([result(l, n, i, j) for i in range(1, j + 1)]) for j in range(1, n + 1)])
n = int(input())
l = [int(x) for x in input().split()]
print(bestresult(l, n))


n = int(input())
a = [int(i) for i in input().split()]
sum_a = sum(a)
max_sum = -1
for i in range(n):
	for j in range(i, n):
		sum1 = sum_a
		for k in range(i, j + 1):
			sum1 -= a[k]
			sum1 += 1 - a[k]
			if sum1 > max_sum:
				max_sum = sum1
print(max_sum)


n = int(input())
list = [int(k) for k in input().split()]
sum = sum(list)
sum0 = sum
sumlist = []
max = 0
for i in range(n):
	for j in range(i, n):
		if list[j] == 0:
			sum += 1
		else:
			sum -= 1
		sumlist.append(sum)
	sum = sum0
for i in sumlist:
	if i > max:
		max = i
print(max)


n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
d = [0]
t = list(range(0, int(len(a) + 1)))
if s == int(len(a)):
	if n == 1:
		for value in a:
			if int(value) == 1:
				print('0')
			else:
				print('1')
	else:
		print(int(s - 1))
else:
	for i in t:
		for j in t:
			if i >= j:
				pass
			else:
				c = sum(a[i:j])
				b = []
				for value in a[i:j]:
					if int(value) == 1:
						value = 0
						b.append(value)
					else:
						value = 1
						b.append(value)
				r = int(sum(b) - c)
				d.append(r)
	g = max(d)
	print(int(s + g))


def Calc(Nums):
	if len(Nums) == 1:
		if Nums[0] == 1:
			return 0
		else:
			return 1
	dp = [[[0, 0] for j in range(len(Nums))] for i in range(len(Nums))]
	dic = {}
	interval = None
	ones = 0
	Max = -1
	for i in range(len(Nums)):
		for j in range(i, len(Nums)):
			if Nums[j] == 0:
				dp[i][j][0] = dp[i][j - 1][0] + 1
				if j - 1 < i:
					dic[i, j] = [1, 0]
				else:
					dic[i, j] = [0, 0]
					dic[i, j][0] = dic[i, j - 1][0] + 1
					dic[i, j][1] = dic[i, j - 1][1]
					if dic[i, j][0] - dic[i, j][1] > Max:
						Max = dic[i, j][0] - dic[i, j][1]
						interval = [i, j]
						ones = dic[i, j][0]
			else:
				dp[i][j][1] = dp[i][j - 1][1] + 1
				if j - 1 < i:
					dic[i, j] = [0, 1]
				else:
					dic[i, j] = [0, 0]
					dic[i, j][1] = dic[i, j - 1][1] + 1
					dic[i, j][0] = dic[i, j - 1][0]
			if dic[i, j][0] - dic[i, j][1] > Max:
				Max = dic[i, j][0] - dic[i, j][1]
				interval = [i, j]
				ones = dic[i, j][0]
	if interval is None:
		return len(Nums) - 1
	for i in range(interval[0]):
		if Nums[i] == 1:
			ones += 1
	for i in range(interval[1] + 1, len(Nums)):
		if Nums[i] == 1:
			ones += 1
	return ones
n = int(input())
Nums = list(map(int, input().split()))
print(Calc(Nums))


n = int(input())
a = [int(x) for x in input().split()]
count = 0
for i in range(n):
	if a[i]:
		a[i] = -1
		count += 1
	else:
		a[i] = 1
profit = a[0]
temp = 0
for i in range(n):
	temp += a[i]
	profit = max(temp, profit)
	temp = max(0, temp)
print(count + profit)


n = int(input())
listan = input().split()
for i in range(0, n):
	listan[i] = int(listan[i])
listajed = []
for i in range(1, n + 2):
	for j in range(i, n + 2):
		sumjed = 0
		if i != j:
			sumjed = sumjed + listan[0:i - 1].count(1) + listan[i - 1:j - 1].count(0) + listan[j - 1:n].count(1)
			listajed.append(sumjed)
print(max(listajed))


n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
	for j in range(i, n):
		cnt = 0
		for k in range(n):
			if k < i or k > j:
				cnt += a[k]
			else:
				cnt += 1 - a[k]
		ans.append(cnt)
print(max(ans))


def compute_score(N, array, i, j):
	score = 0
	for idx in range(N):
		if idx in range(i, j + 1):
			if array[idx] == 0:
				score += 1
		elif array[idx] == 1:
			score += 1
	return score

def max_score(N, array):
	cache = []
	max_score = -1
	for __ in range(N):
		row = [None for __ in range(N)]
		cache.append(row)
	for i in range(N):
		for j in range(i, N):
			if i == 0 and j == 0:
				score = compute_score(N, array, i, j)
				cache[i][j] = score
				if score > max_score:
					max_score = score
			elif cache[i - 1][j] != None:
				if array[i - 1] == 0:
					score = cache[i - 1][j] - 1
				else:
					score = cache[i - 1][j] + 1
					if score > max_score:
						max_score = score
			elif cache[i][j - 1] != None:
				if array[j] == 1:
					score = cache[i][j - 1] - 1
				else:
					score = cache[i][j - 1] + 1
					if score > max_score:
						max_score = score
			else:
				score = compute_score(N, array, i, j)
				cache[i][j] = score
				if score > max_score:
					max_score = score
	return max_score
N = int(input())
array = list(map(int, input().split()))
print(max_score(N, array))