You are given the array of integer numbers a_0, a_1, ..., a_{n} - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.


-----Input-----

The first line contains integer n (1 ≤ n ≤ 2·10^5) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 10^9 ≤ a_{i} ≤ 10^9).


-----Output-----

Print the sequence d_0, d_1, ..., d_{n} - 1, where d_{i} is the difference of indices between i and nearest j such that a_{j} = 0. It is possible that i = j.


-----Examples-----
Input
9
2 1 0 3 0 0 3 2 4

Output
2 1 0 1 0 0 1 2 3 
Input
5
0 1 2 3 4

Output
0 1 2 3 4 
Input
7
5 6 0 1 -2 3 4

Output
2 1 0 1 2 3 4

Source: codeforces | Difficulty: EASY
Tags: Constructive algorithms


Solutions:
from sys import stdin, stdout

def ri():
	return list(map(int, stdin.readline().split()))
n = int(input())
a = list(ri())
for i in range(n):
	if a[i] != 0:
		a[i] = -1
for i in range(n):
	if a[i] != 0:
		continue
	for j in range(i + 1, n):
		if a[j] == 0:
			break
		if a[j] == -1 or a[j] > j - i:
			a[j] = j - i
	for j in range(i - 1, -1, -1):
		if a[j] == 0:
			break
		if a[j] == -1 or a[j] > i - j:
			a[j] = i - j
print(' '.join(map(str, a)))


n = int(input())
a = list(map(int, input().split()))
inf = 10 ** 9
ans = [inf] * n
l = inf
for i in range(n):
	if not a[i]:
		l = 0
	ans[i] = min(ans[i], l)
	l += 1
l = inf
for i in range(n - 1, -1, -1):
	if not a[i]:
		l = 0
	ans[i] = min(ans[i], l)
	l += 1
print(' '.join(map(str, ans)))


n = int(input())
tab = [int(x) for x in input().split()]
result = [2 * n for _ in range(n)]
last_zero = -1
for i in range(n):
	if tab[i] == 0:
		last_zero = i
	if last_zero != -1:
		result[i] = min([result[i], i - last_zero])
last_zero = -1
for i in range(n - 1, -1, -1):
	if tab[i] == 0:
		last_zero = i
	if last_zero != -1:
		result[i] = min([result[i], last_zero - i])
print(' '.join(map(str, result)))


n = int(input())
a = [int(i) for i in input().split()]
j = -1
mn = [1000000000] * n
for i in range(n):
	if a[i] == 0:
		mn[i] = 0
		continue
	if j == -1 or a[i - 1] == 0:
		j = i
		while j < n and a[j] != 0:
			j += 1
		if j == n:
			break
		mn[i] = j - i
	else:
		mn[i] = j - i
a = a[::-1]
j = -1
for i in range(n):
	if a[i] == 0:
		continue
	if j == -1 or a[i - 1] == 0:
		j = i
		while j < n and a[j] != 0:
			j += 1
		if j == n:
			break
		mn[n - i - 1] = min(mn[n - i - 1], j - i)
	else:
		mn[n - i - 1] = min(mn[n - i - 1], j - i)
print(*mn)


import sys
ints = [abs(int(l)) * 9999999 for l in sys.stdin.readlines()[1].strip().split(' ')]
for m in [1, -1]:
	last = None
	for i in range(len(ints))[::m]:
		if not ints[i]:
			last = i
		if last is not None:
			ints[i] = min(abs(i - last), ints[i])
print(' '.join(map(str, ints)))


n = int(input())
a = list(map(int, input().split()))
indexList = []
for i in range(len(a)):
	if a[i] == 0:
		indexList.append(i)
if len(indexList) == 1:
	for i in range(len(a)):
		a[i] = abs(i - indexList[0])
else:
	j = 1
	(curr, nextt) = (indexList[0], indexList[0])
	for i in range(len(a)):
		if a[i] == 0:
			if j != len(indexList):
				(curr, nextt) = (nextt, indexList[j])
				j += 1
		else:
			a[i] = min(abs(i - curr), abs(i - nextt))
for var in a:
	print(var, end=' ')


import sys
n = int(input())
arr = sys.stdin.readline().split()
z = [None] * n
j = k = 0
for i in range(n):
	if arr[i] == '0':
		z[j] = i
		j += 1
z = z[0:j]
for i in range(n):
	if arr[i] == '0':
		print(0, '', end='')
		if i > z[k]:
			k += 1
	elif i < z[k]:
		print(z[k] - i, '', end='')
	elif i > z[k] and k < len(z) - 1 and (i < z[k + 1]):
		print(min(i - z[k], z[k + 1] - i), '', end='')
	elif i > z[k] and k == len(z) - 1:
		print(i - z[k], '', end='')


n = int(input())
a = list(input().split())
b = [i for i in range(n) if a[i] == '0']
c = ''
j = 0
for i in range(n):
	if b[0] >= i:
		t = b[0] - i
		c += '{} '.format(t)
	elif j != len(b) - 1:
		t = min(abs(i - b[j]), abs(i - b[j + 1]))
		if t == 0:
			j += 1
		c += '{} '.format(t)
	else:
		c += '{} '.format(i - b[j])
print(c)


n = int(input())
l = list(map(int, input().split()))
zeroes = []
for i in range(len(l)):
	if l[i] == 0:
		zeroes.append(i)
zero = -1
for i in range(n):
	zeroes.append(zeroes[-1])
s = ''
for i in range(len(l)):
	if i > zeroes[zero + 1]:
		zero += 1
	if l[i] == 0:
		s += ' 0'
	else:
		try:
			s += ' ' + str(min(abs(i - zeroes[zero]), abs(i - zeroes[zero + 1]), abs(i - zeroes[zero + 2])))
		except:
			s += ' ' + str(min(abs(zeroes[zero + 1] - i), abs(i - zeroes[zero])))
print(s[1:])


str_count = input()
count = int(str_count)
str_params = input()
arr = [int(s) for s in str_params.split(' ')]
last_zero = -count
dist = [0 for x in range(count)]
for i in range(count):
	if arr[i] == 0:
		for j in range((max(last_zero + 1, -i) + i) // 2, i):
			dist[j] = i - j
		last_zero = i
	else:
		dist[i] = i - last_zero
print(' '.join(map(str, dist)))


a = int(input())
z = [] + []
for (i, j) in zip(range(a), map(int, input().split())):
	if not j:
		z += [i]
for i in range(len(z)):
	if i == 0:
		for j in range(z[0]):
			print(z[0] - j, end=' ')
	if i != len(z) - 1:
		for j in range(z[i], z[i + 1]):
			print(min(j - z[i], z[i + 1] - j), end=' ')
	if i == len(z) - 1:
		for j in range(z[i], a):
			print(j - z[i], end=' ')


from collections import Counter
import math

def printArr(arr):
	for i in arr:
		print(i, end=' ')

def proA(arr):
	pre = [-math.inf] * len(arr)
	suff = [math.inf] * len(arr)
	if arr[0] == 0:
		pre[0] = 0
	if arr[-1] == 0:
		suff[-1] = len(arr) - 1
	n = len(arr)
	for i in range(1, len(arr)):
		if arr[i] == 0:
			pre[i] = max(pre[i - 1], i)
		else:
			pre[i] = pre[i - 1]
		if arr[n - 1 - i] == 0:
			suff[n - i - 1] = min(suff[n - i], n - 1 - i)
		else:
			suff[n - i - 1] = suff[n - i]
	ans = [0] * n
	for i in range(n):
		ans[i] = min(abs(i - pre[i]), abs(i - suff[i]))
	printArr(ans)
n = int(input())
arr = list(map(int, input().split()))
proA(arr)


i1 = int(input())
ar = list(map(str, input().split()))
flag = False
ini = 0
for i in range(len(ar)):
	if ar[i] == '0' and (not flag):
		flag = True
		ini = i
		for m in range(i + 1):
			print(i - m, end=' ')
	elif ar[i] == '0' and flag:
		if (i - ini - 1) % 2 == 0:
			for m in range(1, int((i - ini - 1) / 2) + 1):
				print(m, end=' ')
			for m in range(0, int((i - ini - 1) / 2) + 1):
				print(int((i - ini - 1) / 2) - m, end=' ')
		else:
			for m in range(1, int((i - ini - 1) / 2) + 1):
				print(m, end=' ')
			for m in range(0, int((i - ini - 1) / 2) + 2):
				print(int((i - ini - 1) / 2) + 1 - m, end=' ')
		ini = i
	elif i == len(ar) - 1 and i != 0:
		for m in range(1, i - ini + 1):
			print(m, end=' ')


n = int(input())
numIn = input().split()
answer = ''
zeros = [len(numIn)]
for i in range(n):
	numIn[i] = int(numIn[i])
	if numIn[i] == 0:
		zeros.append(i)
zeros.append(-1)
zeros_cnt = 0
current_zero = zeros[zeros_cnt]
next_zero = zeros[zeros_cnt + 1]
for i in range(n):
	current_val = numIn[i]
	if current_val == 0:
		zeros_cnt += 1
		current_zero = zeros[zeros_cnt]
		next_zero = zeros[zeros_cnt + 1]
		answer += '0 '
	else:
		current_val = min(abs(current_zero - i), abs(next_zero - i))
		answer += str(current_val) + ' '
print(answer)


n = input()
n = int(n)
l = list(map(int, input().split()))
d = [0] * n
pos = []
for i in range(0, n):
	if l[i] == 0:
		pos.append(i)
j = 0
t = 1
k1 = pos[0]
k2 = k1
k = len(pos)
while j < n:
	d[j] = min(abs(j - k1), abs(j - k2))
	j = j + 1
	if j > k2 and t < k:
		k1 = k2
		k2 = pos[t]
		t += 1
for i in range(0, n):
	print(d[i], end=' ')


import math
n = int(input())
data = []
data = list(map(int, input().split()))
l = [0] * n
r = [0] * n
p = 2 * n
p1 = 2 * n
s = ''
for i in range(n):
	l[i] = p
	if data[i] == 0:
		p = i
for i in range(n - 1, -1, -1):
	r[i] = p1
	if data[i] == 0:
		p1 = i
for i in range(n):
	if data[i] != 0:
		print(min(abs(l[i] - i), abs(r[i] - i)), end=' ')
	else:
		print(0, end=' ')


n = int(input())
p = input()
p = p.split()
x = [int(i) for i in p]
d = [n + 1 for i in range(n)]
e = [-1 for i in range(n + 2)]
e[n + 1] = 0
for i in range(0, n):
	if x[i] == 0:
		e[n] = e[n] + 1
		e[e[n]] = i
for i in range(0, n):
	if i <= e[0]:
		d[i] = e[0] - i
		continue
	if e[n + 1] == e[n]:
		d[i] = i - e[e[n]]
		continue
	for j in range(e[n + 1], e[n] + 1):
		if i < e[j]:
			e[n + 1] = j - 1
			d[i] = min(e[j] - i, i - e[j - 1])
			break
		if i == e[j]:
			e[n + 1] = j
			d[i] = 0
			break
for i in range(0, n):
	print(d[i], '', end='')


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
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 main():
	n = int(input())
	a = list(map(int, input().split()))
	ans = [n] * n
	r = 0
	for i in range(n):
		if a[i] == 0:
			ans[i] = 0
			j = r
			while j < i:
				if a[j] == 0:
					j += 1
					continue
				if r == 0 and a[r] != 0:
					ans[j] = i - j
				else:
					ans[j] = min(i - j, j - r)
				j += 1
			r = i
		if i == n - 1 and a[i] != 0:
			j = r
			while j < n:
				ans[j] = j - r
				j += 1
	print(' '.join([str(x) for x in ans]))
main()


def main():
	n = int(input())
	a = list(map(int, input().split()))
	zero_i = None
	f = []
	for (i, ai) in enumerate(a):
		if ai == 0:
			zero_i = i
		if zero_i is None:
			f.append(n)
		else:
			f.append(i - zero_i)
	zero_i = None
	b = []
	for (i, ai) in enumerate(reversed(a)):
		if ai == 0:
			zero_i = i
		if zero_i is None:
			b.append(n)
		else:
			b.append(i - zero_i)
	res = (min(fi, bi) for (fi, bi) in zip(f, reversed(b)))
	for x in res:
		print(x, end=' ')
main()


n = int(input())
tab = list(map(int, input().split()))
left = [0 for x in range(n)]
right = [0 for x in range(n)]
dist = 10 ** 9
for i in range(n):
	dist = dist + 1
	if tab[i] == 0:
		dist = 0
	left[i] = dist
dist = 10 ** 9
for i in range(n - 1, -1, -1):
	dist = dist + 1
	if tab[i] == 0:
		dist = 0
	right[i] = dist
for i in range(n):
	print(min(left[i], right[i]), end=' ')
print('')


n = int(input())
a = list(map(int, input().split()))
INF = 10 ** 9
pr = [INF for i in range(n + 2)]
suff = [INF for j in range(n + 2)]
for i in range(n):
	if not a[i]:
		pr[i + 1] = 0
		suff[i + 1] = 0
for i in range(1, n + 2):
	pr[i] = min(pr[i], pr[i - 1] + 1)
i = n
while i >= 0:
	suff[i] = min(suff[i], suff[i + 1] + 1)
	i -= 1
for i in range(n):
	print(min(pr[i + 1], suff[i + 1]), end=' ')


n = int(input())
s = input().split()
z = 0
flag = 0
c = 0
for x in s:
	e = int(x)
	if e == 0:
		if z == 0:
			for i in range(c, 0, -1):
				print(i, end=' ')
		else:
			for i in range(1, (c + 1) // 2 + 1):
				print(i, end=' ')
			for i in range((c + 1) // 2 - c % 2, 0, -1):
				print(i, end=' ')
		if flag == 0:
			z += 1
			flag = 1
		c = 0
		print('0', end=' ')
	else:
		c += 1
		flag = 0
for i in range(1, c + 1):
	print(i, end=' ')


import sys
n = int(input())
a = [-1] + list(map(int, input().split())) + [-1]
inf = 10 ** 9
(left, right) = ([inf] * (n + 2), [inf] * (n + 2))
for i in range(1, n + 1):
	left[i] = 0 if a[i] == 0 else left[i - 1] + 1
for i in range(n, 0, -1):
	right[i] = 0 if a[i] == 0 else right[i + 1] + 1
print(*(min(l, r) for (l, r) in zip(left[1:-1], right[1:-1])))


inf = 10 ** 10
input()
nums = [int(x) for x in input().split()]

def run(ns):
	curr = inf
	res = []
	for num in ns:
		if num == 0:
			curr = 0
		res.append(curr)
		curr += 1
	return res
fw = run(nums)
rew = run(reversed(nums))[::-1]
print(' '.join((str(min(fw[i], rew[i])) for i in range(len(nums)))))


import collections

def do():
	N = int(input())
	nums = [int(i) for i in input().split(' ')]
	q = collections.deque()
	seen = [0] * N
	res = [float('inf')] * N
	for (i, n) in enumerate(nums):
		if n == 0:
			q.append((i, 0))
			seen[i] = 1
	while q:
		(cur, dis) = q.popleft()
		res[cur] = min(res[cur], dis)
		for nx in [cur - 1, cur + 1]:
			if 0 <= nx < N and (not seen[nx]):
				q.append((nx, dis + 1))
				seen[nx] = 1
	print(' '.join(map(str, res)))
do()


n = int(input())
nums = [int(x) for x in input().split(' ')]
firstZero = nums.index(0)
lastZero = len(nums) - 1 - nums[::-1].index(0)
for i in range(len(nums[firstZero:])):
	if nums[firstZero + i] == 0:
		c = 0
	else:
		c += 1
	nums[firstZero + i] = c
nums = nums[::-1]
for i in range(len(nums)):
	if nums[i] == 0:
		c = 0
	else:
		c += 1
	if i > len(nums) - firstZero:
		nums[i] = c
		continue
	if nums[i] >= c or nums[i] < 0:
		nums[i] = c
print(' '.join([str(x) for x in nums[::-1]]))


n = int(input())
A = list(map(int, input().split()))
zeros = [i for (i, a) in enumerate(A) if a == 0]
k = 0
for i in range(n):
	if k + 1 < len(zeros) and abs(zeros[k] - i) > abs(zeros[k + 1] - i):
		k += 1
	print(abs(i - zeros[k]), end=' ')


n = int(input())
a = list(map(int, input().split()))
x = [0] * len(a)
y = [0] * len(a)
c1 = 0
z1 = False
c2 = 0
z2 = False
for i in range(len(a)):
	if a[i] == 0:
		z1 = True
		c1 = 1
	elif z1:
		x[i] = c1
		c1 += 1
	else:
		x[i] = 0
	if a[n - i - 1] == 0:
		z2 = True
		c2 = 1
	elif z2:
		y[n - i - 1] = c2
		c2 += 1
	else:
		y[n - i - 1] = 0
ans = ''
for i in range(len(a)):
	if x[i] == 0:
		if y[i] == 0:
			ans += '0 '
		else:
			ans += str(y[i]) + ' '
	elif y[i] == 0:
		ans += str(x[i]) + ' '
	else:
		ans += str(min(x[i], y[i])) + ' '
print(ans)


n = int(input())
(a, v, p) = (list(map(int, input().split())), [n] * n, -n)
for i in range(n):
	if a[i] == 0:
		p = i
	v[i] = min(v[i], i - p)
p = 2 * n
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		p = i
	v[i] = min(v[i], p - i)
print(' '.join(map(str, v)))


n = int(input())
ans = [10 ** 9] * n
seenindx = -1
l = [int(i) for i in input().split()]
for i in range(n):
	if l[i] == 0:
		seenindx = i
	if seenindx != -1:
		ans[i] = i - seenindx
seenindx = -1
for i in range(n - 1, -1, -1):
	if l[i] == 0:
		seenindx = i
	if seenindx != -1:
		ans[i] = min(ans[i], seenindx - i)
print(*ans)


n = int(input())
l = list(map(str, input().split()))
z = []
for i in range(n):
	if l[i] == '0':
		z.append(i)
ans = []
for i in range(0, z[0]):
	ans.append(z[0] - i)
for i in range(len(z) - 1):
	x = (z[i + 1] - z[i]) // 2
	for j in range(z[i], z[i] + x + 1):
		ans.append(j - z[i])
	for j in range(z[i] + x + 1, z[i + 1]):
		ans.append(z[i + 1] - j)
for j in range(z[len(z) - 1], n):
	ans.append(j - z[len(z) - 1])
print(*ans)


n = int(input())
a = list(map(int, input().split()))
s = []
t = 0
d = [-1] * n
for (i, j) in enumerate(a):
	if j == 0:
		s.append(i)
		d[i] = 0
for i in range(s[0]):
	d[i] = s[0] - i
for k in range(1, len(s)):
	for i in range(s[k - 1], s[k]):
		d[i] = min(s[k] - i, i - s[k - 1])
for i in range(s[-1], n):
	d[i] = i - s[-1]
print(' '.join(map(str, d)))


import bisect
from itertools import accumulate, count
import os
import sys
import math
from decimal import *
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')

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

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	for i in range(2, len(primes)):
		if primes[i] == True:
			prime.append(i)
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return fac

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def NcR(n, r):
	p = 1
	k = 1
	if n - r < r:
		r = n - r
	if r != 0:
		while r:
			p *= n
			k *= r
			m = math.gcd(p, k)
			p //= m
			k //= m
			n -= 1
			r -= 1
	else:
		p = 1
	return p

def Log2(x):
	if x == 0:
		return False
	return math.log10(x) / math.log10(2)

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))
n = int(input())
a = list(map(int, input().split()))
(q, l) = ([], [0] * n)
for i in range(n):
	if a[i] == 0:
		q.append(i)
r = len(q)
for i in range(n):
	if a[i] != 0:
		p = bisect.bisect_left(q, i)
		if p == 0:
			l[i] = q[0] - i
		elif p == r:
			l[i] = i - q[-1]
		else:
			l[i] = min(i - q[p - 1], q[p] - i)
print(*l)


n = int(input())
m = list(map(int, input().split()))
m0 = [-n * n]
for i in range(n):
	if m[i] == 0:
		m0 += [i]
m0 += [n * n]
x = 1
for i in range(n):
	z = 0
	k = x - 1
	while z == 0:
		if m[i] == 0:
			print(0, end=' ')
			z = 1
		elif i >= m0[k] and i <= m0[k + 1]:
			print(min(abs(i - m0[k]), abs(m0[k + 1] - i)), end=' ')
			z = 1
		k += 1
		x = k


import sys
inf = 1 << 30

def solve():
	n = int(input())
	a = [inf if ai != '0' else 0 for ai in input().split()]
	for i in range(n):
		if a[i] == 0:
			for j in range(i - 1, -1, -1):
				if a[j] > i - j:
					a[j] = i - j
				else:
					break
			for j in range(i + 1, n):
				if a[j] > j - i:
					a[j] = j - i
				else:
					break
	print(*a)

def __starting_point():
	solve()
__starting_point()


a = int(input())
b = list(map(int, input().split()))
if b[0] == 0:
	x = [0]
	ct = 0
else:
	x = [a]
	ct = a
for i in b[1:]:
	if i == 0:
		x.append(0)
		ct = 0
	else:
		ct += 1
		x.append(ct)
if b[-1] == 0:
	y = [0]
	ct = 0
else:
	y = [a]
	ct = a
for i in b[:-1][::-1]:
	if i == 0:
		y.append(0)
		ct = 0
	else:
		ct += 1
		y.append(ct)
y = y[::-1]
for i in range(a):
	print(min(x[i], y[i]), end=' ')


n = int(input())
a = list(map(int, input().split()))
(L, R) = ([0] * n, [0] * n)
m = -2 * 10 ** 9
for i in range(n):
	if a[i] == 0:
		(L[i], m) = (0, i)
	else:
		L[i] = i - m
m = 2 * 10 ** 9
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		(R[i], m) = (0, i)
	else:
		R[i] = m - i
s = ''
for i in range(n):
	s += str(min(R[i], L[i])) + ' '
print(s)


n = int(input())
arr = input().split()
a = arr.index('0')
pos = a
b = 10000000000.0
ans = [0] * n
i = 0
while i < n:
	ans[i] = min(a, b)
	a -= 1
	b += 1
	i += 1
	if a == -1:
		b = 1
		try:
			a = arr.index('0', pos + 1)
		except:
			a = -1
		if a != -1:
			temp = a
			a = a - pos - 1
			pos = temp
		else:
			a = 10000000000.0
print(*ans)


n = int(input())
(a, *l) = (i for (i, s) in enumerate(input().split()) if s == '0')
res = list(range(a, 0, -1))
for b in l:
	w = (b - a + 1) // 2
	res += range(w)
	res += range(b - a - w, 0, -1)
	a = b
res += range(n - a)
print(' '.join(map(str, res)))


BIG = 10000000000

def main(n, arr):
	dist = BIG
	res = []
	for x in arr:
		if x == 0:
			dist = 0
		elif dist != BIG:
			dist += 1
		res.append(dist)
	dist = BIG
	for i in range(len(arr) - 1, -1, -1):
		x = arr[i]
		if x == 0:
			dist = 0
		elif dist != BIG:
			dist += 1
		if res[i] > dist:
			res[i] = dist
	print(' '.join(map(str, res)))
n = int(input())
arr = [int(x) for x in input().split()]
main(n, arr)


n = int(input())
L = list(map(int, input().split(' ')))
zeros = []
zeros.append(10000000)
for i in range(n):
	if L[i] == 0:
		zeros.append(i)
k = len(zeros)
j = 1
dist = []
for i in range(n):
	if j < k - 1 and i - zeros[j] == 0:
		j = j + 1
	dist.append(min(abs(i - zeros[j - 1]), abs(i - zeros[j])))
for i in range(n - 1):
	print(dist[i], end=' ')
print(dist[len(dist) - 1])


n = int(input())
a = list(map(int, input().split()))
cur = 0
l = [float('+inf') for _ in range(n)]
dist = float('+inf')
for i in range(n):
	dist += 1
	if a[i] == 0:
		dist = 0
	l[i] = min(l[i], dist)
dist = float('+inf')
for i in range(n - 1, -1, -1):
	dist += 1
	if a[i] == 0:
		dist = 0
	l[i] = min(l[i], dist)
print(*l)


n = int(input())
a = list(map(int, input().split()))
d = [n] * n
zeroIndex = -1
for i in range(0, n):
	if a[i] == 0:
		zeroIndex = i
		d[i] = 0
	elif zeroIndex >= 0:
		d[i] = i - zeroIndex
zeroIndex = -1
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		zeroIndex = i
	elif zeroIndex >= 0 and zeroIndex - i < d[i]:
		d[i] = zeroIndex - i
print(' '.join(map(str, d)))


mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n = ii()
l = il()
l2 = []
for i in range(n):
	if l[i] == 0:
		l2.append(i)
l2 += [10000000]
i = 0
j = 0
while i < n:
	print(min(abs(i - l2[j]), abs(i - l2[j + 1])), end=' ')
	if abs(i - l2[j]) >= abs(i - l2[j + 1]):
		j += 1
	i += 1


import math

def A():
	(n, k) = [int(i) for i in input().split()]
	new = [[0] * n for i in range(n)]
	count = 0
	for i in range(n, 0, -1):
		if k <= 0:
			break
		k -= 1
		new[count][n - i] = 1
		if k <= 0:
			break
		for j in range(n - i + 1, n):
			if k - 2 >= 0:
				k -= 2
				new[count][j] = 1
				new[j][count] = 1
			else:
				break
		if k <= 0:
			break
		count += 1
	if k > 0:
		print('-1')
	else:
		for i in new:
			for j in i:
				print(j, end=' ')
			print()

def B():
	n = int(input())
	arr = input().split()
	result = [0] * n
	death_count = 1
	last_index = -1
	for each in range(n):
		if arr[each] == '0':
			death_count += 1
			if death_count == 2:
				death_count -= 1
				inbetween = each - last_index - 1
				if last_index != -1:
					if inbetween > 0:
						for i in range(math.ceil(inbetween / 2)):
							result[last_index + i + 1] = i + 1
							result[each - i - 1] = i + 1
				else:
					for i in range(each):
						result[i] = each - i
				last_index = each
	huo = 1
	for i in range(last_index + 1, n):
		result[i] = huo
		huo += 1
	for i in result:
		print(i, end=' ')
B()


input()
numbers = input().split()
distlist = []
dist = -1
for i in numbers:
	if i == '0':
		dist = 0
	distlist += [dist]
	if dist != -1:
		dist += 1
dist = -1
for (index, i) in enumerate(reversed(numbers)):
	ii = len(numbers) - index - 1
	if i == '0':
		dist = 0
	if dist != -1:
		distlist[ii] = min(dist, distlist[ii]) if distlist[ii] != -1 else dist
		dist += 1
print(' '.join(map(str, distlist)))


n = int(input())
a = list(map(int, input().split()))
ans = [n for i in range(n)]
temp = -n
for i in range(n):
	if a[i] == 0:
		temp = i
	ans[i] = min(ans[i], abs(i - temp))
temp = -n
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		temp = i
	ans[i] = min(ans[i], abs(i - temp))
for i in range(n):
	print(ans[i], end=' ')


n = int(input())
arr = [int(x) for x in input().split()]
INF = 1 << 30
i = 0
last_zero = -INF
nearest_zero = [INF] * n
while i < n:
	if arr[i] == 0:
		last_zero = i
		nearest_zero[i] = 0
	else:
		nearest_zero[i] = i - last_zero
	i += 1
i = n - 1
last_zero = INF
while i >= 0:
	if arr[i] == 0:
		last_zero = i
	else:
		nearest_zero[i] = min(nearest_zero[i], last_zero - i)
	i -= 1
print(' '.join(map(str, nearest_zero)))


n = int(input())
arr = list(map(int, input().split()))
rel = list(range(n))
prev = -n
for i in range(n):
	if arr[i] == 0:
		prev = i
	rel[i] = i - prev
prev = n + n
for i in range(n - 1, -1, -1):
	if rel[i] == 0:
		prev = i
	rel[i] = min(rel[i], prev - i)
print(*rel)


class CodeforcesTask803BSolution:

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

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

	def process_task(self):
		to_visit = []
		dists = [self.n + 1] * self.n
		for x in range(self.n):
			if not self.sequence[x]:
				dists[x] = 0
				if 0 < x < self.n - 1:
					if self.sequence[x - 1] or self.sequence[x + 1]:
						to_visit.append(x)
				else:
					to_visit.append(x)
		while to_visit:
			visiting = to_visit.pop()
			x = visiting - 1
			dist = 1
			while x >= 0:
				if dists[x] > dist:
					dists[x] = dist
				else:
					break
				x -= 1
				dist += 1
			x = visiting + 1
			dist = 1
			while x < self.n:
				if dists[x] > dist:
					dists[x] = dist
				else:
					break
				x += 1
				dist += 1
		self.result = ' '.join([str(x) for x in dists])

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


ind = []
x = 0
n = int(input())
listArray = [int(y) for y in input().split()]
for i in range(n):
	if listArray[i] == 0:
		ind.append(i)
ind.append(900001)
temp = 9000001
for i in range(n):
	if i <= ind[x]:
		print(min(ind[x] - i, abs(i - temp)))
	else:
		temp = ind[x]
		x = x + 1
		if listArray[i] == 0:
			print(0)
		else:
			print(1)


n = int(input())
s = input().split(' ')
v = []
z = []
for a in s:
	v.append(int(a))
	if v[len(v) - 1] == 0:
		z.append(len(v) - 1)
z.append(9999999999999)
ind = 0
for i in range(0, len(v)):
	d1 = abs(i - z[ind])
	d2 = abs(i - z[ind + 1])
	print(d1 if d1 < d2 else d2, end=' ')
	if d2 < d1:
		ind += 1


from collections import deque
import sys
n = int(sys.stdin.readline().strip())
array = list(map(int, sys.stdin.readline().strip().split(' ')))
zeros = [-float('Inf')]
for i in range(n):
	if array[i] == 0:
		zeros.append(i)
zeros.append(float('Inf'))
j = 1
for i in range(n):
	if array[i] == 0:
		j += 1
		print(array[i], end=' ')
	else:
		aux = min(abs(zeros[j] - i), abs(zeros[j - 1] - i))
		print(aux, end=' ')


n = int(input())
a = list(map(int, input().split()))
ans = [n for _ in range(n)]
tmp = n
for i in range(n):
	if a[i] == 0:
		tmp = 0
	else:
		tmp += 1
	ans[i] = min(ans[i], tmp)
tmp = n
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		tmp = 0
	else:
		tmp += 1
	ans[i] = min(ans[i], tmp)
print(' '.join(map(str, ans)))


MAX = 2 * 10 ** 5 + 1
n = int(input().strip())
ais = list(map(int, input().strip().split()))
dleft = [MAX for _ in range(n)]
dright = [MAX for _ in range(n)]
for i in range(n):
	if ais[i] == 0:
		dleft[i] = 0
	else:
		dleft[i] = MAX if i == 0 else dleft[i - 1] + 1
for i in reversed(range(n)):
	if ais[i] == 0:
		dright[i] = 0
	else:
		dright[i] = MAX if i == n - 1 else dright[i + 1] + 1
d = [min(dl, dr) for (dl, dr) in zip(dleft, dright)]
print(' '.join(map(str, d)))


n = int(input())
a = list(map(int, input().strip().split()))
res = [-1] * n
zero = []
for i in range(n):
	if a[i] == 0:
		zero.append(i)
p1 = zero[0]
p2 = zero[-1]
c = 0
for i in range(p1 + 1):
	res[i] = abs(i - p1)
for i in range(n - 1, p2 - 1, -1):
	res[i] = abs(i - p2)
for i in range(p1 + 1, p2):
	pr = zero[c]
	su = zero[c + 1]
	res[i] = min(abs(i - pr), abs(i - su))
	if i + 1 > su:
		c += 1
		(pr, su) = (su, zero[c + 1])
print(' '.join(map(str, res)))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().decode().rstrip('\n\r')
from collections import deque as dq
inf = float('inf')
mod = 10 ** 9 + 7
Cases = 1
for Case in range(Cases):
	zeros = [-inf]
	n = ri()
	a = rl()
	for i in range(n):
		if a[i] == 0:
			zeros.append(i)
	zeros.append(inf)
	nowz = 0
	nextz = 1
	for i in range(n):
		if i == zeros[nextz]:
			(nowz, nextz) = (nextz, nextz + 1)
		print(min(abs(zeros[nowz] - i), abs(zeros[nextz] - i)), end=' ')


import math
n = int(input())
lst = list(map(int, input().strip().split(' ')))
p = []
for j in range(n):
	if lst[j] == 0:
		p.append(j)
l = len(p)
if l == 1:
	for j in range(n):
		print(abs(p[0] - j), end=' ')
else:
	i1 = 0
	i2 = 1
	j = 0
	while j < n:
		if lst[j] == 0:
			print(0, end=' ')
			j += 1
		elif j < p[i1]:
			print(p[i1] - j, end=' ')
			j += 1
		elif i1 == i2:
			print(j - p[i1], end=' ')
			j += 1
		elif j > p[i1] and j < p[i2]:
			print(min(j - p[i1], p[i2] - j), end=' ')
			j += 1
		elif j > p[i2]:
			if i1 + 1 < l:
				i1 += 1
			if i2 + 1 < l:
				i2 += 1
			if i2 == l - 1 and j > p[i2]:
				print(j - p[i2], end=' ')
				j += 1


import sys
inf = 1 << 30

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = [inf] * n
	d = inf
	for i in range(n):
		if a[i] == 0:
			d = 0
		ans[i] = d
		d += 1
	d = inf
	for i in reversed(range(n)):
		if a[i] == 0:
			d = 0
		ans[i] = min(ans[i], d)
		d += 1
	print(*ans)

def __starting_point():
	solve()
__starting_point()


def main():
	try:
		while True:
			n = int(input())
			a = list(map(int, input().split()))
			b = [0] * n
			last = 400000
			for i in range(n - 1, -1, -1):
				if a[i] == 0:
					last = i
				b[i] = last - i
			last = -400000
			for i in range(n):
				if a[i] == 0:
					last = i
				print(min(i - last, b[i]), end=' ')
			print()
	except EOFError:
		pass
main()


import math

def countDistance(outList, firstZero, secondZero):
	if secondZero != -1 and firstZero != -1:
		average = int(math.ceil((firstZero + secondZero) / 2))
		for i in range(firstZero, average, 1):
			outList[i] = i - firstZero
		for i in range(average, secondZero + 1, 1):
			outList[i] = secondZero - i
	if secondZero == -1:
		for i in range(firstZero, len(outList), 1):
			outList[i] = i - firstZero
	if firstZero == -1:
		for i in range(0, secondZero + 1, 1):
			outList[i] = secondZero - i
inputList = []
outputList = []
size = int(input())
inputList = input().split()
for i in range(0, size, 1):
	inputList[i] = int(inputList[i])
	outputList.append(-1)
lastZero = -1
for i in range(0, size, 1):
	if inputList[i] == 0:
		if lastZero != -1:
			countDistance(outputList, lastZero, i)
			lastZero = i
		else:
			lastZero = i
			countDistance(outputList, -1, lastZero)
if lastZero != size - 1:
	countDistance(outputList, lastZero, -1)
for elem in outputList:
	print(elem, end=' ')


n = int(input())
p = list(map(str, range(n)))
(t, d) = ([], 0)
for q in input().split():
	if q != '0':
		d += 1
	else:
		k = d // 2
		if t:
			t += p[1:k + 1] + p[d - k::-1]
		else:
			t = p[d::-1]
		d = 0
print(' '.join(t + p[1:d + 1]))


import sys, math
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))
ans = [0] * n
k = []
for i in range(n):
	if l[i] == 0:
		k.append(i)
		ans[i] = 0
j = -1
for i in range(n):
	if l[i] != 0:
		if i < k[0]:
			ans[i] = k[0] - i
		elif i > k[-1]:
			ans[i] = i - k[-1]
		else:
			ans[i] = min(i - k[j], k[j + 1] - i)
	else:
		j += 1
print(*ans)


def zero(i):
	for j in range(i + 1, n):
		if a[j] == 0:
			return j
	return -1
n = int(input())
a = list(map(int, input().split()))
res = []
(zerol, zeror) = (2 * 10 ** 5 + 1, zero(-1))
for i in range(n):
	if a[i] != 0:
		res.append(min(abs(i - zeror), abs(i - zerol)))
	else:
		res.append(0)
		(zerol, zeror) = (zeror, zero(i))
print(' '.join(map(str, res)))


n = int(input())
arr = list(map(int, input().split()))
i = 0
while i < n and arr[i] != 0:
	i += 1
l = [k for k in range(i, 0, -1)]
j = i
while j < n:
	l += [0]
	p = j + 1
	while p < n and arr[p] != 0:
		p += 1
	if p != n:
		x = p - j - 1
		for q in range(1, x // 2 + 1):
			l += [q]
		for a in range((x + 1) // 2, 0, -1):
			l += [a]
		j = p
	else:
		y = p - j
		for b in range(1, y):
			l += [b]
		break
print(*l)


n = int(input())
A = list(map(int, input().strip().split(' ')))
ans = [n + 1 for i in range(n)]
start = -1
for i in range(len(A)):
	if A[i] == 0:
		break
start = i
ans[start] = 0
index = start
for i in range(start + 1, len(A)):
	if A[i] == 0:
		index = i
		ans[i] = 0
	else:
		ans[i] = i - index
for i in range(n - 1, -1, -1):
	if A[i] == 0:
		end = i
		break
index = end
for i in range(end - 1, -1, -1):
	if A[i] != 0:
		ans[i] = min(ans[i], index - i)
	else:
		index = i
print(*ans)


n = int(input())
arr = input().split(' ')
last_zeros = [0] * n
last_zero = -1
for i in range(n):
	arr[i] = int(arr[i])
	if arr[i] == 0:
		last_zero = i
	if last_zero == -1:
		last_zeros[i] = 10 ** 10
	else:
		last_zeros[i] = i - last_zero
last_zero = -1
for i in reversed(range(n)):
	if arr[i] == 0:
		last_zero = i
	if last_zero != -1:
		last_zeros[i] = min(last_zeros[i], last_zero - i)
for el in last_zeros:
	print(el, end=' ')


import bisect
n = int(input())
a = list(map(int, input().split()))
fw = []
for i in range(n):
	if a[i] == 0:
		fw.append(i)
d = [1 << 30 for i in range(n)]
for i in range(n):
	p = bisect.bisect_left(fw, i)
	if p < len(fw):
		d[i] = min(d[i], fw[p] - i)
	p = bisect.bisect_right(fw, i)
	if p > 0:
		p -= 1
		d[i] = min(d[i], i - fw[p])
print(' '.join(map(str, d)))


import bisect
n = int(input())
a = list(map(int, input().split()))
ind = []
ans = []
for j in range(n):
	if a[j] == 0:
		ind.append(j)
for i in range(n):
	if a[i] != 0:
		p = bisect.bisect_left(ind, i)
		if p == 0:
			ans.append(abs(ind[0] - i))
		elif p == len(ind):
			ans.append(abs(ind[-1] - i))
		else:
			ans.append(min(abs(ind[p] - i), abs(ind[p - 1] - i)))
	else:
		ans.append(0)
print(*ans)


n = int(input())
l = list(map(int, input().split()))
arr = []
for i in range(n):
	if l[i] == 0:
		arr.append(i)
for i in range(arr[0], 0, -1):
	print(i, end=' ')
print(0, end=' ')
for i in range(1, len(arr)):
	temp = arr[i] - arr[i - 1]
	t1 = temp // 2
	for j in range(1, t1 + 1):
		print(j, end=' ')
	if temp % 2 == 0:
		t1 += -1
	for j in range(t1, 0, -1):
		print(j, end=' ')
	print(0, end=' ')
for i in range(1, n - 1 - arr[len(arr) - 1] + 1):
	print(i, end=' ')


def Posicion(L):
	i = 1
	A = L
	for k in range(len(L)):
		if L[k] != 0:
			A[k] = i
			i += 1
		else:
			i = 1
	return A

def SepararDerecha(L):
	B = []
	Mayork = 0
	for k in range(len(L)):
		if L[k] == 0:
			if k > Mayork:
				Mayork = k
	c = Mayork + 1
	while c < len(L):
		B.append(L[c])
		c += 1
	return B

def SepararIzquierda(L):
	B = []
	k = 0
	while L[k] != 0:
		B.append(L[k])
		k += 1
	return B

def Invertir(L):
	B = []
	k = len(L) - 1
	while k >= 0:
		B.append(L[k])
		k -= 1
	return B
N = int(input())
L = input()
L = L.split()
A = []
for k in range(len(L)):
	A.append(int(L[k]))
PI = SepararIzquierda(A)
z = len(PI)
PIII = []
for k in range(len(PI)):
	PIII.append(z)
	z -= 1
pizquierda = len(PIII)
PD = SepararDerecha(A)
PD = Posicion(PD)
pderecha = len(PD)
D = []
for k in range(pizquierda, len(A) - pderecha):
	D.append(A[k])
AI = Posicion(D)
AD = Invertir(D)
AD = Posicion(AD)
AD = Invertir(AD)
B = []
for k in range(len(AD)):
	if AD[k] < AI[k]:
		B.append(AD[k])
	else:
		B.append(AI[k])
C = []
for k in range(len(PIII)):
	C.append(PIII[k])
for k in range(len(B)):
	C.append(B[k])
for k in range(len(PD)):
	C.append(PD[k])
Respuesta = str(C[0])
for k in range(1, len(C)):
	Respuesta += ' ' + str(C[k])
print(Respuesta)


n = int(input())
a = list(map(int, input().split()))
dist = [n] * n
d = n
for i in range(n):
	if a[i] == 0:
		d = 0
	else:
		d += 1
	dist[i] = d
d = n
for i in reversed(range(n)):
	if a[i] == 0:
		d = 0
	else:
		d += 1
	if d is not None:
		dist[i] = min(dist[i], d)
print(' '.join(map(str, dist)))


def solve(n, a):
	prefix_dist = []
	suffix_dist = []
	INF = 1000000000
	prefix_dist.append(0 if a[0] == 0 else INF)
	for i in range(1, n):
		if a[i] == 0:
			prefix_dist.append(0)
			continue
		prefix_dist.append(min(prefix_dist[i - 1] + 1, INF))
	suffix_dist.append(0 if a[n - 1] == 0 else INF)
	for i in range(n - 2, -1, -1):
		if a[i] == 0:
			suffix_dist.append(0)
			continue
		suffix_dist.append(min(suffix_dist[n - i - 2] + 1, INF))
	suffix_dist = list(reversed(suffix_dist))
	sol = []
	for i in range(n):
		sol.append(min(prefix_dist[i], suffix_dist[i]))
	return sol

def main():
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	print(*solve(n, a))
main()


n = int(input())
x = [int(a) for a in input().split()]
L = [1000000 for i in range(0, n)]
P = [1000000 for i in range(0, n)]
for i in range(0, n):
	if x[i] == 0:
		L[i] = 0
	elif i > 0:
		L[i] = L[i - 1] + 1
for i in range(n - 1, -1, -1):
	if x[i] == 0:
		P[i] = 0
	elif i < n - 1:
		P[i] = P[i + 1] + 1
for i in range(0, n):
	print(min(L[i], P[i]), end=' ')


n = int(input())
list = [None] * n
s = input().split()
for i in range(0, n):
	list[i] = int(s[i])
a = [100100100] * n
last = -1
for i in range(0, n):
	if list[i] == 0:
		last = i
	if last > -1:
		a[i] = min(a[i], abs(i - last))
last = -1
for i in range(n - 1, -1, -1):
	if list[i] == 0:
		last = i
	if last > -1:
		a[i] = min(a[i], abs(i - last))
for i in range(0, n):
	print(a[i], end=' ')


n = int(input())
lst = list(map(int, input().split()))
(mas, length) = ([], 0)
for (i, x) in enumerate(lst):
	if x == 0:
		mas.append(i)
		length += 1
from bisect import bisect
res = []
for (i, x) in enumerate(lst):
	if x == 0:
		res.append(0)
	else:
		a = bisect(mas, i)
		if a == 0:
			res.append(mas[0] - i)
		elif a == length:
			res.append(i - mas[length - 1])
		else:
			res.append(min(i - mas[a - 1], mas[a] - i))
print(*res)


n = int(input())
a = list(map(int, input().split()))
(l, r, ll, rr, c) = ([0] * n, [0] * n, -10 ** 9, 10 ** 9, [])
for i in range(n):
	if not a[i]:
		ll = i
	l[i] = i - ll
for i in range(n - 1, -1, -1):
	if not a[i]:
		rr = i
	r[i] = rr - i
for i in range(n):
	c.append(str(min(l[i], r[i])))
print(' '.join(c))


n = int(input())
valores = [int(e) for e in input().split(' ')]
d_i = 1000000
distancias = [0 for i in range(n)]
for i in range(len(distancias)):
	d_i += 1
	distancias[i] = d_i
	if valores[i] == 0:
		distancias[i] = 0
		d_i = 0
teste = False
for i in range(len(valores) - 1, -1, -1):
	if valores[i] == 0:
		d_i = 0
		teste = True
	if teste:
		if d_i < distancias[i]:
			distancias[i] = d_i
		d_i += 1
[print(e, end=' ') for e in distancias]


from array import *
n = int(input())
ar = list(map(int, input().split()))
dist1 = []
dist2 = []
for i in range(n):
	dist1.append(0)
	dist2.append(0)
st = n
for i in range(n):
	if ar[i] != 0:
		dist1[i] = st
	else:
		st = 0
	st += 1
st = n
ar.reverse()
for i in range(n):
	if ar[i] != 0:
		dist2[i] = st
	else:
		st = 0
	st += 1
dist2.reverse()
for i in range(n):
	m = min(dist1[i], dist2[i])
	print(m, end=' ')
print('\n')


n = int(input())
a = input().split()
d = [0 for x in range(n)]
dist = n
for i in range(n):
	if a[i] == '0':
		dist = 0
		j = i
		while d[j] >= dist and j >= 0:
			d[j] = dist
			dist += 1
			j -= 1
		dist = 0
	else:
		dist += 1
		d[i] = dist
print(' '.join([str(i) for i in d]))


n = int(input())
l = list(map(int, input().split()))
s = l.count(0)
ind = l.index(0)
m = []
if s == n:
	for i in range(n):
		print(0, end=' ')
else:
	for i in range(n):
		if l[i] == 0:
			m.append(i)
	c = 0
	i = 0
	while c != s - 1:
		while abs(m[c] - i) < abs(m[c + 1] - i) and i < n:
			print(abs(m[c] - i), end=' ')
			i = i + 1
		c = c + 1
	for j in range(i, n):
		print(abs(m[c] - j), end=' ')


points = int(input())
coord = [int(n) for n in input().split()]
distance = [points] * points
total_solved = 0
n_zeros = coord.count(0)
index = []
prev = 0
for i in range(n_zeros):
	prev = int(coord.index(0, prev))
	index.append(prev)
	distance[prev] = 0
	prev += 1
total_solved += len(index)
new_index = []
now = 1
while total_solved < points:
	for elem in index:
		if elem == 0:
			if distance[elem + 1] > now:
				distance[elem + 1] = now
				new_index.append(elem + 1)
		elif elem == points - 1:
			if distance[elem - 1] > now:
				distance[elem - 1] = now
				new_index.append(elem - 1)
		else:
			if distance[elem + 1] > now:
				distance[elem + 1] = now
				new_index.append(elem + 1)
			if distance[elem - 1] > now:
				distance[elem - 1] = now
				new_index.append(elem - 1)
	total_solved += len(new_index)
	now += 1
	index = list(new_index)
	new_index.clear()
for elem in distance:
	print(elem, end=' ')


import math
import re
import random
n = int(input())
a = list(map(int, input().split()))
b = []
q = 0
for i in range(n):
	if a[i] != 0 and i != n - 1:
		q += 1
	elif a[i] == 0 and i != n - 1:
		b.append(q)
		q = 0
	elif a[i] == 0 and i == n - 1:
		b.append(q)
		b.append(0)
		break
	else:
		q += 1
		b.append(q)
		break
c = []
for j in range(len(b)):
	if j == 0:
		for i in range(b[0]):
			c.append(b[0] - i)
		c.append(0)
	elif j == len(b) - 1:
		for i in range(b[j]):
			c.append(i + 1)
	elif b[j] % 2 == 0:
		for i in range(b[j] // 2):
			c.append(i + 1)
		for i in range(b[j] // 2):
			c.append(b[j] // 2 - i)
		c.append(0)
	else:
		for i in range(b[j] // 2 + 1):
			c.append(i + 1)
		for i in range(b[j] // 2):
			c.append(b[j] // 2 - i)
		c.append(0)
print(' '.join(map(str, c)))


n = int(input())
arr = list(map(int, input().split()))
ans = [-1 for i in range(n)]
ind = []
for i in range(n):
	if arr[i] == 0:
		ind.append(i)
d = 0
for i in range(n):
	if d + 1 < len(ind) and abs(ind[d] - i) > abs(ind[d + 1] - i):
		d += 1
	print(abs(i - ind[d]), end=' ')


n = int(input())
l = list(map(int, input().split()))
ans = [400001 for i in range(n)]
zs = []
for x in range(n):
	if l[x] == 0:
		zs.append(x)
		ans[x] = 0
for i in zs:
	lp = i - 1
	rp = i + 1
	cntL = 1
	cntR = 1
	while lp != -1:
		if ans[lp] <= cntL:
			break
		ans[lp] = cntL
		cntL += 1
		lp -= 1
	while rp != n:
		if ans[rp] <= cntR:
			break
		ans[rp] = cntR
		cntR += 1
		rp += 1
print(' '.join([str(x) for x in ans]))


INF = 10000000

def main():
	n = int(input())
	a = input().split()
	b = [INF for x in range(n)]
	c = [INF for x in range(n)]
	tmpb = -INF
	tmpc = -INF
	for i in range(n):
		s = int(a[i])
		if s == 0:
			tmpb = i
			b[i] = 0
		else:
			b[i] = i - tmpb
	a = list(reversed(a))
	for i in range(n):
		s = int(a[i])
		if s == 0:
			tmpc = i
			c[i] = 0
		else:
			c[i] = i - tmpc
	ans = ''
	for i in range(n):
		if b[i] < c[n - 1 - i]:
			ans += str(b[i]) + ' '
		else:
			ans += str(c[n - 1 - i]) + ' '
	print(ans)
main()


import math, string, itertools, collections, re, fractions, array, copy
import bisect
import heapq
from itertools import chain, dropwhile, permutations, combinations
from collections import deque, defaultdict, OrderedDict, namedtuple, Counter, ChainMap

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

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

def LIST(n, m=None):
	return [0] * n if m is None else [[0] * m for i in range(n)]

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

def MI(n=None, m=None):
	if n is None:
		(n, m) = VI()
	arr = LIST(n)
	for i in range(n):
		arr[i] = VI()
	return arr

def MS(n=None, m=None):
	if n is None:
		(n, m) = VI()
	arr = LIST(n)
	for i in range(n):
		arr[i] = input()
	return arr

def MIT(n=None, m=None):
	if n is None:
		(n, m) = VI()
	a = MI(n, m)
	arr = LIST(m, n)
	for (i, l) in enumerate(a):
		for (j, x) in enumerate(l):
			arr[j][i] = x
	return arr

def main(info=0):
	n = I()
	a = VI()
	z = [i for i in range(n) if a[i] == 0]
	i = 0
	j = 0
	x = [j for j in range(z[0], -1, -1)]
	for i in range(1, len(z)):
		d = z[i] - z[i - 1]
		x += [j for j in range(1, d // 2)]
		if d % 2 == 1 and d > 2:
			x += [d // 2]
		x += [j for j in range(d // 2, -1, -1)]
	x += [j for j in range(1, n - z[-1])]
	print(' '.join((str(j) for j in x)))
main()


s = input()
n = int(s)
l = []
s = input()
num = s.split(' ')
for i in range(n):
	if int(num[i]) == 0:
		l.append(i)
start = 0
res = [0] * n
for i in range(n):
	while start < len(l) - 1 and l[start + 1] < i:
		start += 1
	res[i] = abs(i - l[start])
	if start < len(l) - 1:
		res[i] = min(res[i], l[start + 1] - i)
out = ''
for i in range(n):
	out += str(res[i]) + ' '
out = out[:-1]
print(out)


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')
from bisect import bisect_left
from collections import Counter
n = int(input())
arr = list(map(int, input().split()))
ls = []
for i in range(n):
	if arr[i] == 0:
		ls.append(i)
for i in range(n):
	m = n
	var = bisect_left(ls, i)
	if var == 0:
		m = min(m, abs(i - ls[0]))
	elif var == len(ls):
		m = min(m, abs(i - ls[-1]))
	else:
		m = min(min(abs(i - ls[var - 1]), abs(i - ls[var])), m)
	print(m, end=' ')


n = int(input())
a = [s for s in input().split()]
d = [n] * n
for i in range(n):
	if a[i] == '0':
		d[i] = 0
		j = i - 1
		k = 0
		while j >= 0 and a[j] != '0':
			k += 1
			if k < d[j]:
				d[j] = k
			j -= 1
		j = i + 1
		k = 0
		while j < n and a[j] != '0':
			k += 1
			if k < d[j]:
				d[j] = k
			j += 1
print(' '.join([str(s) for s in d]))


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


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

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

def pt(x):
	print(x)

def ip():
	return input()

def it():
	return int(input())

def sl(x):
	return [t for t in x]

def spl(x):
	return x.split()

def aj(liste, item):
	liste.append(item)

def bin(x):
	return '{0:b}'.format(x)

def listring(l):
	return ' '.join([str(x) for x in l])

def ptlist(l):
	print(' '.join([str(x) for x in l]))
n = it()
a = lt()
(l, r) = (-float('inf'), a.index(0))
i = 0
d = [-1 for _ in range(n)]
b = []
for i in range(n):
	if a[i] == 0:
		b.append(i)
b.append(n - 1)
b.insert(0, 0)
pointeur = 0
while pointeur < len(b) - 1:
	for j in range(b[pointeur], b[pointeur + 1] + 1):
		if pointeur == 0:
			d[j] = b[pointeur + 1] - j
		elif pointeur == len(b) - 2:
			d[j] = j - b[pointeur]
		else:
			d[j] = min(j - b[pointeur], b[pointeur + 1] - j)
	pointeur += 1
ptlist(d)


n = int(input())
l = list(map(int, input().split()))
ans = [n] * n
zero_ind = -1
for i in range(n):
	if l[i] == 0:
		zero_ind = i
		ans[i] = 0
	elif zero_ind >= 0:
		ans[i] = abs(i - zero_ind)
zero_ind = -1
for i in range(n - 1, -1, -1):
	if l[i] == 0:
		zero_ind = i
	elif zero_ind >= 0 and ans[i] > abs(zero_ind - i):
		ans[i] = zero_ind - i
print(*ans)


length = int(input())
listofNum = input()
listofNum = list(map(int, listofNum.split()))
output = [0] * length
zeroPos = []
count = 0
for i in range(length):
	if listofNum[i] == 0:
		count += 1
		zeroPos.append(i)
for i in range(zeroPos[0], -1, -1):
	output[i] += zeroPos[0] - i
for i in range(zeroPos[count - 1], length):
	output[i] += i - zeroPos[count - 1]
if count >= 2:
	pos_1 = 0
	pos_2 = 1
	while pos_2 < count:
		midpoint = int((zeroPos[pos_1] + zeroPos[pos_2]) / 2)
		if zeroPos[pos_2] - zeroPos[pos_1] != 1:
			if (zeroPos[pos_2] - zeroPos[pos_1] - 1) % 2 == 1:
				for i in range(zeroPos[pos_1], midpoint):
					output[i] += i - zeroPos[pos_1]
				for i in range(zeroPos[pos_2], midpoint - 1, -1):
					output[i] += zeroPos[pos_2] - i
			if (zeroPos[pos_2] - zeroPos[pos_1] - 1) % 2 == 0:
				for i in range(zeroPos[pos_1], midpoint + 1):
					output[i] += i - zeroPos[pos_1]
				for i in range(zeroPos[pos_2], midpoint, -1):
					output[i] += zeroPos[pos_2] - i
		pos_1 += 1
		pos_2 += 1
outputStr = ''
for x in output:
	outputStr += str(x) + ' '
print(outputStr)


import sys

def readlines(type=int):
	return list(map(type, sys.stdin.readline().split()))

def read(type=int):
	return type(sys.stdin.readline().strip())
joint = lambda it, sep=' ': sep.join([str(i) if type(i) != list else sep.join(map(str, i)) for i in it])

def solve(n, arr):
	zeroind = -10 ** 6
	left = [zeroind] * n
	right = [zeroind] * n
	for i in range(n):
		if arr[i] == 0:
			left[i] = 0
			zeroind = i
		left[i] = i - zeroind
	zeroind = 10 ** 6
	for i in range(n - 1, -1, -1):
		if arr[i] == 0:
			right[i] = 0
			zeroind = i
		right[i] = zeroind - i
	result = [min(left[i], right[i]) for i in range(n)]
	return joint(result)

def main():
	n = read()
	arr = readlines()
	print(solve(n, arr))
main()


from bisect import bisect_left as bl
t = int(input())
a = list(map(int, input().split()))
(q, l) = ([], [0] * t)
for i in range(t):
	if a[i] == 0:
		q.append(i)
r = len(q)
for i in range(t):
	if a[i] != 0:
		p = bl(q, i)
		if p == 0:
			l[i] = q[0] - i
		elif p == r:
			l[i] = i - q[-1]
		else:
			l[i] = min(i - q[p - 1], q[p] - i)
print(*l)


n = int(input())
a = list(map(int, input().split()))
f = [None] * n
b = [None] * n
pos = None
for i in range(n):
	if a[i] == 0:
		pos = i
	f[i] = pos
pos = None
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		pos = i
	b[i] = pos
for i in range(n):
	fd = None if f[i] is None else i - f[i]
	bd = None if b[i] is None else b[i] - i
	if fd is None:
		print(bd, end=' ')
	elif bd is None:
		print(fd, end=' ')
	else:
		print(min(fd, bd), end=' ')


n = int(input())
l = list(map(int, input().strip().split(' ')))
count = 0
q = 0
while not q == len(l):
	if l[q] == 0:
		break
	l[q] = q + 1
	q += 1
l[:q] = l[:q][::-1]
for i in range(q, len(l)):
	if l[i] == 0:
		p = i - 1
		t = 1
		count = 0
		while not l[p] == 0:
			l[p] = min(l[p], t)
			t += 1
			p -= 1
	else:
		count += 1
		l[i] = count
for k in l:
	print(k, end=' ')


n = int(input())
s = list(map(int, input().split()))
ans = []
a = []
b = []
c = n + 1
for i in range(n):
	if s[i] == 0:
		c = 0
		a.append(c)
	else:
		c += 1
		a.append(c)
c = n + 1
for i in reversed(range(n)):
	if s[i] == 0:
		c = 0
		b.append(c)
	else:
		c += 1
		b.append(c)
for (x, y) in zip(a, b[::-1]):
	ans.append(str(min(x, y)))
print(' '.join(ans))


def qa():
	(n, k) = map(int, input().split())
	if k > n:
		print('-1')
		return
	matrix = [[0] * n for _ in range(n)]
	for i in range(k):
		matrix[i][i] = 1
	for line in matrix:
		print(' '.join(map(str, line)))

def qb():
	_ = input()
	d = [*map(int, input().split())]
	zeroIndex = [0 if v == 0 else len(d) for (i, v) in enumerate(d)]
	counter = len(d)
	for i in range(len(d)):
		if zeroIndex[i] == 0:
			counter = 0
			continue
		counter += 1
		zeroIndex[i] = min(counter, zeroIndex[i])
	counter = len(d)
	for i in range(len(d) - 1, -1, -1):
		if zeroIndex[i] == 0:
			counter = 0
			continue
		counter += 1
		zeroIndex[i] = min(counter, zeroIndex[i])
	print(' '.join(map(str, zeroIndex)))
qb()


N = int(input())
A = list(map(int, input().split()))
ans = [N for i in range(N)]
prev = -N
for i in range(N):
	if A[i] == 0:
		prev = i
	ans[i] = i - prev
prev = N + N
for i in range(N - 1, -1, -1):
	if A[i] == 0:
		prev = i
	ans[i] = min(ans[i], prev - i)
print(*ans)


import sys
from bisect import bisect_left as lb
from collections import deque
from queue import PriorityQueue as pq
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7
n = ii()
a = il()
pre = []
x = float('inf')
for i in range(n):
	if a[i] == 0:
		x = 0
		pre.append(x)
		x += 1
		continue
	else:
		pre.append(x)
		x += 1
suf = []
x = float('inf')
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		x = 0
		suf.append(x)
		x += 1
		continue
	else:
		suf.append(x)
		x += 1
suf.reverse()
for i in range(n):
	print(min(pre[i], suf[i]), end=' ')


n = int(input())
s = [int(i) for i in input().split()]
d = [10 ** 9 + 7 for i in range(n)]
cnt = 0
for i in range(n):
	if s[i] == 0:
		d[i] = 0
		cnt = 1
	elif cnt:
		d[i] = cnt
		cnt += 1
cnt = 0
for i in range(n - 1, -1, -1):
	if s[i] == 0:
		cnt = 1
	elif cnt:
		if d[i] >= cnt:
			d[i] = cnt
		cnt += 1
print(*d)


n = int(input())
f = list(map(int, input().split()))
zeroes = []
for i in range(len(f)):
	if f[i] == 0:
		zeroes.append(i)
cur0 = 0
if len(zeroes) > 1:
	next0 = 1
else:
	next0 = None
for i in range(len(f)):
	end = ' '
	if i == len(f) - 1:
		end = '\n'
	if next0 is None:
		print(abs(i - zeroes[cur0]), end=end)
	elif abs(i - zeroes[cur0]) < abs(i - zeroes[next0]):
		print(abs(i - zeroes[cur0]), end=end)
	else:
		print(abs(i - zeroes[next0]), end=end)
		cur0 = next0
		if cur0 == len(zeroes) - 1:
			next0 = None
		else:
			next0 = next0 + 1


n = int(input())
a = list(map(int, input().split()))
s = [i for i in range(n) if a[i] == 0]
c = 0
for i in range(n):
	if c + 1 < len(s) and abs(s[c] - i) > abs(s[c + 1] - i):
		c += 1
	print(abs(i - s[c]))


n = int(input())
l = [int(x) for x in input().split()]
l2 = []
for i in range(len(l)):
	if l[i] == 0:
		l2.append(i)
temp = 0
l3 = []
while temp < n and temp <= l2[0]:
	l3.append(l2[0] - temp)
	temp += 1
j = 1
while j < len(l2):
	while temp <= l2[j]:
		d1 = abs(l2[j - 1] - temp)
		d2 = abs(l2[j] - temp)
		l3.append(min(d1, d2))
		temp += 1
	j += 1
for i in range(temp, n, 1):
	l3.append(abs(i - l2[int(len(l2)) - 1]))
for k in l3:
	print(k, end=' ')


inf = 10 ** 6
n = int(input())
a = list(map(int, input().split()))
dist = [inf] * n
for i in range(len(a)):
	if not a[i]:
		dist[i] = 0
		cur = 1
		i1 = i
		while i1 - 1 > -1 and a[i1 - 1] != 0:
			dist[i1 - 1] = min(dist[i1 - 1], cur)
			i1 -= 1
			cur += 1
		i1 = i
		cur = 1
		while i1 + 1 < n and a[i1 + 1] != 0:
			dist[i1 + 1] = min(dist[i1 + 1], cur)
			i1 += 1
			cur += 1
print(*dist)


from sys import maxsize, stdout, stdin, stderr
import re
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
stpr = lambda x: stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
n = I()
ls = lint()
arr = [n] * n
d = n
for i in range(n):
	if ls[i] == 0:
		d = 0
	else:
		d += 1
		arr[i] = d
d = n
for i in range(n - 1, -1, -1):
	if ls[i] == 0:
		d = 0
	else:
		d += 1
	arr[i] = min(d, arr[i])
star(arr)


import sys, math, queue
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
n = int(input())
a = list(map(int, input().split()))
l = [None for i in range(n)]
r = [None for i in range(n)]
for i in range(n):
	if a[i] == 0:
		l[i] = i
	elif l[i - 1] is not None:
		l[i] = l[i - 1]
	if a[-1 - i] == 0:
		r[-1 - i] = n - 1 - i
	elif r[-1 - i + 1] is not None:
		r[-1 - i] = r[-1 - i + 1]
a = [-1 for i in range(n)]
for i in range(n):
	if l[i] is None:
		a[i] = r[i] - i
	elif r[i] is None:
		a[i] = i - l[i]
	else:
		a[i] = min(r[i] - i, i - l[i])
print(*a)


n = int(input())
a = []
a = list(map(int, input().split()))
dist = pow(10, 9)
d = [dist] * len(a)
for i in range(len(a)):
	if a[i] == 0:
		d[i] = 0
		dist = 0
	elif dist < d[i]:
		dist += 1
		d[i] = dist
for i in range(len(a) - 1, -1, -1):
	if a[i] == 0:
		d[i] = 0
		dist = 0
	elif dist + 1 < d[i]:
		dist += 1
		d[i] = dist
for i in d:
	print(str(i) + ' ', end='')


def solve():
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	d = [float('inf')] * n
	prev = -float('inf')
	for i in range(n):
		if a[i] == 0:
			prev = i
		d[i] = min(d[i], i - prev)
	next = float('inf')
	for i in range(n - 1, -1, -1):
		if a[i] == 0:
			next = i
		d[i] = min(d[i], next - i)
	return d
print(*solve())


n = int(input())
a = list(map(int, input().split()))
dis = 0
l = []
for i in range(n):
	if a[i] == 0:
		dis = 0
		l.append(i)
	else:
		dis += 1
		a[i] = dis
i = n - 1
dis = 0
while i >= 0:
	if i <= l[-1] and i >= l[0]:
		if a[i] == 0:
			dis = 0
		else:
			dis += 1
			a[i] = min(a[i], dis)
	elif i <= l[0]:
		if a[i] == 0:
			dis = 0
		else:
			dis += 1
			a[i] = dis
	i -= 1
print(*a)


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

def zeros(d):
	inf = 10000000000
	l = -inf
	for (i, x) in enumerate(d, 1):
		if x == 0:
			yield (l, i)
			l = i
	yield (l, inf)
n = int(input())
num = read_ints()
print(*(min(i - a, b - i) for (a, b) in zeros(num) for i in range(max(a, 0) + 1, min(n, b) + 1)))


from bisect import *
n = int(input())
l = list(map(int, input().split()))
zero = [-n]
curr = 0
for i in range(n):
	if l[i] == 0:
		zero.append(i)
x = 2
zero.append(2 * n)
for i in range(zero[1] + 1):
	print(zero[1] - i, end=' ')
for i in range(zero[1] + 1, n):
	print(min(zero[x] - i, i - zero[x - 1]), end=' ')
	if l[i] == 0:
		x += 1


from sys import stdin, stdout
from math import *
inf = int('0x3f3f3f3f', 16)
n = int(stdin.readline())
arr = [1 if x is not '0' else 0 for x in stdin.readline().rstrip().split()]
indicies = [-inf]
for i in range(n):
	if not arr[i]:
		indicies.append(i)
indicies.append(inf)
ptr = 0
for i in range(n):
	print(str(min(abs(indicies[ptr] - i), abs(indicies[ptr + 1] - i))), end=' ')
	if indicies[ptr + 1] == i:
		ptr += 1


n = int(input())
a = [int(i) for i in input().split()]
count1 = [n + 1 for i in range(n)]
count2 = [n + 1 for i in range(n)]
for i in range(n):
	if a[i] != 0 and i > 0:
		count1[i] = 1 + count1[i - 1]
	if a[i] == 0:
		count1[i] = 0
	if a[n - i - 1] != 0 and i > 0:
		count2[n - i - 1] = 1 + count2[n - i]
	if a[n - i - 1] == 0:
		count2[n - i - 1] = 0
for i in range(n):
	print(min(count1[i], count2[i]), end=' ')


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

def findDistancesToZero(n, a):
	pos_zero_left = a.index('0')
	pos_zero_right = a.index('0')
	pos = 0
	positions = ''
	while pos < n:
		worked = False
		while pos <= pos_zero_right:
			print(str(min(abs(pos - pos_zero_left), abs(pos - pos_zero_right))), end=' ')
			pos += 1
			worked = True
		pos_zero_left = pos_zero_right
		try:
			pos_zero_right = a.index('0', pos)
		except:
			pos_zero_right = pos_zero_left
			False
		if worked:
			pos -= 1
		if pos_zero_left == pos_zero_right:
			pos += 1
			while pos < n:
				print(str(abs(pos - pos_zero_left)), end=' ')
				pos += 1
			pos -= 1
		pos += 1
	return positions
findDistancesToZero(n, a)


n = int(input())
x = list(map(int, input().split()))
list_of_zeros = []
for i in range(n):
	if x[i] == 0:
		list_of_zeros.append(i + 1)
k = 0
for j in range(n):
	if j + 1 < list_of_zeros[0]:
		print(list_of_zeros[0] - j - 1, end=' ')
	elif j + 1 == list_of_zeros[k]:
		if k < len(list_of_zeros) - 1:
			k += 1
		print(0, end=' ')
	elif j + 1 > list_of_zeros[-1]:
		print(j + 1 - list_of_zeros[-1], end=' ')
	else:
		print(min(j + 1 - list_of_zeros[k - 1], list_of_zeros[k] - j - 1), end=' ')


from math import *
length = int(input())
a = list(map(int, input().strip().split()))
p = [-inf]
for i in range(length):
	if a[i] == 0:
		p.append(i)
p.append(inf)
i = 0
j = 1
b = [0 for _ in range(length)]
for k in range(length):
	if k < p[j]:
		b[k] = min(k - p[i], p[j] - k)
	if k == p[j]:
		j += 1
		i += 1
for i in b:
	print(i, end=' ')


import sys
import math
n = int(input())
a = list(map(int, input().split()))
ans = [n for i in range(n)]
lind = -n
for i in range(n):
	if a[i] == 0:
		lind = i
	ans[i] = min(ans[i], abs(i - lind))
lind = -n
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		lind = i
	ans[i] = min(ans[i], abs(i - lind))
for i in range(n):
	print(ans[i], end=' ')


n = int(input())
a = list(map(int, input().split()))
INF = float('inf')
tmp = INF
b = [INF] * n
for i in range(n):
	if a[i] == 0:
		tmp = 0
	b[i] = tmp
	tmp += 1
tmp = INF
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		tmp = 0
	b[i] = min(b[i], tmp)
	tmp += 1
print(*b)