This is an easier version of the problem. In this version $n \le 1000$

The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot.

Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.

Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$.

The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.


-----Input-----

The first line contains a single integer $n$ ($1 \leq n \leq 1000$) — the number of plots.

The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.


-----Output-----

Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible.

If there are multiple answers possible, print any of them.


-----Examples-----
Input
5
1 2 3 2 1

Output
1 2 3 2 1 

Input
3
10 6 8

Output
10 6 6 



-----Note-----

In the first example, you can build all skyscrapers with the highest possible height.

In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.

Difficulty: MEDIUM

Solutions:
n = int(input())
max_floors = list(map(int, input().split(' ')))
best_solution = [0] * n
best_total_floors = 0
for (i, mfi) in enumerate(max_floors):
	solution = max_floors.copy()
	for j in range(i - 1, -1, -1):
		solution[j] = min(max_floors[j], solution[j + 1])
	for j in range(i + 1, len(max_floors)):
		solution[j] = min(max_floors[j], solution[j - 1])
	solution_total_floors = sum(solution)
	if solution_total_floors > best_total_floors:
		best_solution = solution
		best_total_floors = solution_total_floors
print(' '.join(map(str, best_solution)))


def cal(i):
	a = [x for x in m]
	t = a[i]
	for j in range(i - 1, -1, -1):
		a[j] = min(t, a[j])
		t = a[j]
	t = a[i]
	for j in range(i + 1, n):
		a[j] = min(t, a[j])
		t = a[j]
	return a
n = int(input())
m = [int(x) for x in input().split()]
res = cal(0)
for i in range(1, n):
	a = cal(i)
	if sum(res) < sum(a):
		res = a
print(*res)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'

def NSE(arr, n, dir):
	nse = [-1 for i in range(n)]
	if dir == 'r':
		st = [0]
		for i in range(1, n):
			while len(st) and arr[st[-1]] > arr[i]:
				nse[st[-1]] = i
				st.pop(-1)
			st.append(i)
	else:
		st = [n - 1]
		for i in range(n - 2, -1, -1):
			while len(st) and arr[st[-1]] > arr[i]:
				nse[st[-1]] = i
				st.pop(-1)
			st.append(i)
	return nse

def build_suffix(orig, arr, n, dir):
	suf = [0] * n
	if dir == 'r':
		for i in range(n):
			suf[i] = (i - arr[i]) * orig[i]
			if arr[i] != -1:
				suf[i] += suf[arr[i]]
	else:
		for i in range(n - 1, -1, -1):
			if arr[i] == -1:
				suf[i] = (n - i) * orig[i]
			else:
				suf[i] = (arr[i] - i) * orig[i] + suf[arr[i]]
	return suf
for _ in range(1):
	n = ii()
	l = il()
	nsel = NSE(l, n, 'r')
	nser = NSE(l, n, 'l')
	sufl = build_suffix(l, nsel, n, 'l')
	sufr = build_suffix(l, nser, n, 'r')
	(mxsm, ind) = (-1, -1)
	for i in range(n):
		tem = sufl[i] + sufr[i] - l[i]
		if tem > mxsm:
			mxsm = tem
			ind = i
	for i in range(ind - 1, -1, -1):
		l[i] = min(l[i + 1], l[i])
	for i in range(ind + 1, n):
		l[i] = min(l[i - 1], l[i])
	print(*l)


import sys
n = int(input())
m = list(map(int, input().split()))
INF = 10 ** 9 + 1
ans = m[:]

def solve(l, r):
	global ans
	if l == r:
		return m[l]
	rslt = 0
	minimum = min(enumerate(m[l:r + 1]), key=lambda x: x[1])
	mi = l + minimum[0]
	mv = minimum[1]
	al = (mi - l + 1) * mv + (solve(mi + 1, r) if mi < r else 0)
	ar = (r - mi + 1) * mv + (solve(l, mi - 1) if l < mi else 0)
	if al > ar:
		for i in range(l, mi + 1):
			ans[i] = mv
	else:
		for i in range(mi, r + 1):
			ans[i] = mv
	return max(al, ar)
solve(0, n - 1)
print(' '.join(map(str, ans)))


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	a = list(map(int, input().split()))
	(left_min, s, pre, suf) = ([-1] * n, [0], [0] * n, [0] * n)
	for i in range(1, n):
		while s and a[s[-1]] >= a[i]:
			s.pop()
		if s:
			left_min[i] = s[-1]
		s.append(i)
	(right_min, s) = ([n] * n, [n - 1])
	for i in range(n - 2, -1, -1):
		while s and a[s[-1]] >= a[i]:
			s.pop()
		if s:
			right_min[i] = s[-1]
		s.append(i)
	for i in range(n):
		if left_min[i] == -1:
			pre[i] = (i + 1) * a[i]
		else:
			pre[i] = pre[left_min[i]] + (i - left_min[i]) * a[i]
	for i in range(n - 1, -1, -1):
		if right_min[i] == n:
			suf[i] = (n - i) * a[i]
		else:
			suf[i] = suf[right_min[i]] + (right_min[i] - i) * a[i]
	(ma, ind) = (0, 0)
	for i in range(n):
		if i == 0:
			ma = suf[i]
		elif i == n - 1:
			if ma < pre[i]:
				ma = pre[i]
				ind = n - 1
		elif ma < pre[i] + suf[i] - a[i]:
			ma = pre[i] + suf[i] - a[i]
			ind = i
	(ans, i) = ([0] * n, ind)
	while i != -1:
		for j in range(i, left_min[i], -1):
			ans[j] = a[i]
		i = left_min[i]
	i = ind
	while i != n:
		for j in range(i, right_min[i], 1):
			ans[j] = a[i]
		i = right_min[i]
	print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


n = int(input())
a = [int(x) for x in input().split()]
cnt = 0
ans = []
for i in range(n):
	s = a[i]
	prev = a[i]
	res = []
	for j in range(i - 1, -1, -1):
		prev = min(a[j], prev)
		s += prev
		res.append(prev)
	prev = a[i]
	res = res[::-1]
	res.append(a[i])
	for j in range(i + 1, n):
		prev = min(a[j], prev)
		s += prev
		res.append(prev)
	if s > cnt:
		cnt = s
		ans = res[:]
print(' '.join((str(a) for a in ans)))


n = int(input())
a = list(map(int, input().split()))
st = []
L = [0 for _ in range(n)]
R = [0 for _ in range(n)]
for i in range(n):
	while st and a[st[-1]] >= a[i]:
		st.pop()
	if st:
		L[i] = L[st[-1]] + (i - st[-1]) * a[i]
	else:
		L[i] = (i + 1) * a[i]
	st.append(i)
st = []
for i in range(n - 1, -1, -1):
	while st and a[st[-1]] >= a[i]:
		st.pop()
	if st:
		R[i] = R[st[-1]] + (st[-1] - i) * a[i]
	else:
		R[i] = (n - i) * a[i]
	st.append(i)
node = -1
for i in range(n):
	if node == -1 or L[node] + R[node] - a[node] < L[i] + R[i] - a[i]:
		node = i
for i in range(node - 1, -1, -1):
	a[i] = min(a[i], a[i + 1])
for i in range(node + 1, n):
	a[i] = min(a[i], a[i - 1])
print(*a)


import sys
from collections import Counter

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
mx = 0
ans = []
for i in range(N):
	tmp = [0] * N
	limit = A[i]
	tmp[i] = limit
	for j in range(i + 1, N):
		limit = min(limit, A[j])
		tmp[j] = limit
	limit = A[i]
	for j in range(i - 1, -1, -1):
		limit = min(limit, A[j])
		tmp[j] = limit
	sm = sum(tmp)
	if sm > mx:
		ans = tmp
		mx = sm
print(*ans)


import sys
n = 0
S = []
h = []

def maxS():
	st = []
	left = [0] * n
	right = [n - 1] * n
	for i in range(n):
		while len(st) > 0 and st[-1][0] >= h[i]:
			right[st[-1][1]] = i - 1
			del st[-1]
		st.append((h[i], i))
	st.clear()
	for i in range(n - 1, -1, -1):
		while len(st) > 0 and st[-1][0] >= h[i]:
			left[st[-1][1]] = i + 1
			del st[-1]
		st.append((h[i], i))
	dpr = [0] * n
	dpl = [0] * n
	for i in range(n - 1, -1, -1):
		dpr[i] = h[i] * (right[i] + 1 - i)
		if right[i] < n - 1:
			dpr[i] += dpr[right[i] + 1]
	for i in range(n):
		dpl[i] = h[i] * (i - left[i] + 1)
		if left[i] > 0:
			dpl[i] += dpl[left[i] - 1]
	mx = 0
	ansc = 0
	for i in range(n):
		if mx < dpr[i] + dpl[i] - h[i]:
			mx = dpr[i] + dpl[i] - h[i]
			ansc = i
	ans = [-1] * n
	t = ansc
	for i in range(ansc, n):
		ans[i] = h[t]
		if i == right[t] and right[t] < n - 1:
			t = right[t] + 1
	t = ansc
	for i in range(ansc, -1, -1):
		ans[i] = h[t]
		if i == left[t] and left[t] > 0:
			t = left[t] - 1
	print(*ans, sep=' ')
n = int(input())
h = [int(q) for q in input().split()]
maxS()


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
M = 0
ans = [-1] * n
for i in range(n):
	b = a[:]
	for j in range(i - 1, -1, -1):
		b[j] = min(b[j], b[j + 1])
	for j in range(i + 1, n):
		b[j] = min(b[j], b[j - 1])
	S = sum(b)
	if S > M:
		M = S
		ans = b
print(*ans)


import bisect
import sys
input = sys.stdin.readline
n = int(input())
ar = list(map(int, input().split()))
tot = [0] * n
val = [0] * n
floor = [0] * n
tot[0] = 1
val[0] = ar[0]
floor[0] = ar[0]
su = [ar[0]]
l = 0
r = 1
for i in range(1, n):
	if ar[i] >= val[r - 1]:
		tot[r] = tot[r - 1] + 1
		val[r] = ar[i]
		floor[r] = floor[r - 1] + ar[i]
		su.append(floor[r])
		r += 1
	else:
		xx = bisect.bisect(val, ar[i], l, r)
		if xx == 0:
			r = 1
			floor[0] = (i + 1) * ar[i]
			tot[0] = i + 1
			val[0] = ar[i]
			su.append(floor[0])
		else:
			floor[xx] = floor[xx - 1] + (i + 1 - tot[xx - 1]) * ar[i]
			tot[xx] = i + 1
			val[xx] = ar[i]
			su.append(floor[xx])
			r = xx + 1
arco = ar[::-1]
tot = [0] * n
val = [0] * n
floor = [0] * n
tot[0] = 1
val[0] = arco[0]
floor[0] = arco[0]
su1 = [arco[0]]
l = 0
r = 1
for i in range(1, n):
	if arco[i] >= val[r - 1]:
		tot[r] = tot[r - 1] + 1
		val[r] = arco[i]
		floor[r] = floor[r - 1] + arco[i]
		su1.append(floor[r])
		r += 1
	else:
		xx = bisect.bisect(val, arco[i], l, r)
		if xx == 0:
			r = 1
			floor[0] = (i + 1) * arco[i]
			tot[0] = i + 1
			val[0] = arco[i]
			su1.append(floor[0])
		else:
			floor[xx] = floor[xx - 1] + (i + 1 - tot[xx - 1]) * arco[i]
			tot[xx] = i + 1
			val[xx] = arco[i]
			su1.append(floor[xx])
			r = xx + 1
su1 = su1[::-1]
ind = 0
ma = su[0] + su1[0] - ar[0]
for i in range(1, n):
	if i == n - 1:
		xx = su[i]
	else:
		xx = su[i] + su1[i] - ar[i]
	if xx > ma:
		ind = i
		ma = xx
for j in range(ind + 1, n):
	if ar[j] > ar[j - 1]:
		ar[j] = ar[j - 1]
for j in range(ind - 1, -1, -1):
	if ar[j] > ar[j + 1]:
		ar[j] = ar[j + 1]
print(*ar)


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = []
	best = 0
	for i in range(n):
		b = [0] * n
		cur = arr[i]
		b[i] = cur
		for j in range(i - 1, -1, -1):
			cur = min(cur, arr[j])
			b[j] = cur
		cur = arr[i]
		for j in range(i + 1, n):
			cur = min(cur, arr[j])
			b[j] = cur
		s = sum(b)
		if s > best:
			ans = b
			best = s
	print(*ans)
tt = 1
for test in range(tt):
	solve()


n = int(input())
m = tuple(map(int, input().split()))
ma = list(m)
best = 0
curlimit = m[0]
for i in range(n):
	if m[i] > curlimit:
		ma[i] = curlimit
	if curlimit > m[i]:
		curlimit = m[i]
	best += curlimit
besti = 0
bestseq = ma
for i in range(n):
	ma = list(m)
	curlimit = m[i]
	curbest = curlimit
	for j in range(i + 1, n):
		if m[j] > curlimit:
			ma[j] = curlimit
		if curlimit > m[j]:
			curlimit = m[j]
		curbest += curlimit
	curlimit = m[i]
	for j in range(i - 1, -1, -1):
		if m[j] > curlimit:
			ma[j] = curlimit
		if curlimit > m[j]:
			curlimit = m[j]
		curbest += curlimit
	if curbest > best:
		best = curbest
		besti = i
		bestseq = ma
print(*bestseq)


from sys import stdin

def inp():
	return next((ss.rstrip() for ss in stdin))

def itg():
	return int(inp())

def mpint():
	return map(int, inp().split())
n = itg()
arr = list(mpint())
left = [0] * n
right = [0] * n
left[0] = arr[0]
stack = [0]
for i in range(1, n):
	while stack and arr[i] <= arr[stack[-1]]:
		stack.pop()
	if stack:
		j = stack[-1]
		left[i] = left[j] + (i - j) * arr[i]
	else:
		left[i] = arr[i] * (i + 1)
	stack.append(i)
stack = [n - 1]
right[-1] = arr[-1]
for i in range(n - 2, -1, -1):
	while stack and arr[i] <= arr[stack[-1]]:
		stack.pop()
	if stack:
		j = stack[-1]
		right[i] = right[j] + (j - i) * arr[i]
	else:
		right[i] = arr[i] * (n - i)
	stack.append(i)
idx = max(range(n), key=lambda x: left[x] + right[x] - arr[x])
min_ = arr[idx]
for i in range(idx - 1, -1, -1):
	if arr[i] < min_:
		min_ = arr[i]
	arr[i] = min_
min_ = arr[idx]
for i in range(idx + 1, n):
	if arr[i] < min_:
		min_ = arr[i]
	arr[i] = min_
print(*arr)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7

def floor(ind, val):
	mn = val
	arr = deque([val])
	for i in range(ind - 1, -1, -1):
		if a[i] > mn:
			arr.appendleft(mn)
		else:
			mn = a[i]
			arr.appendleft(mn)
	mn = val
	for i in range(ind + 1, n):
		if a[i] > mn:
			arr.append(mn)
		else:
			mn = a[i]
			arr.append(mn)
	return arr
n = I()
a = L()
ans = floor(0, a[0])
sm = sum(ans)
for i in range(1, n):
	ans1 = floor(i, a[i])
	if sum(ans1) > sm:
		sm = sum(ans1)
		ans = ans1.copy()
print(*ans)


n = int(input())
ms = [int(a) for a in input().split()]
ms.insert(0, 0)
ms.insert(len(ms), 0)
an = []
an2 = [0]
for i in range(n + 2):
	an.append(10 ** 10)
for i in range(n + 2):
	try:
		if ms[i] <= ms[i + 1] and ms[i + 1] > ms[i + 2]:
			peak = i + 1
			an[peak] = ms[peak]
			for j in range(peak - 1, -1, -1):
				an[j] = min(an[j + 1], ms[j])
			for j in range(peak + 1, n + 2, 1):
				an[j] = min(an[j - 1], ms[j])
		if sum(an[1:-1]) > sum(an2) and an[1] != 10 ** 10:
			an2 = an[1:-1]
	except:
		pass
for a in an2:
	print(a, end=' ')


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


n = int(input())
a = list(map(int, input().split()))
st = []
f = [-1 for i in range(n)]
e = [n for i in range(n)]
for i in range(n):
	while len(st) != 0 and a[st[-1]] >= a[i]:
		st.pop()
	if st != []:
		f[i] = st[-1]
	st.append(i)
st = []
for i in range(n - 1, -1, -1):
	while len(st) != 0 and a[st[-1]] >= a[i]:
		st.pop()
	if st != []:
		e[i] = st[-1]
	st.append(i)
pre = [0 for i in range(n)]
suf = [0 for i in range(n)]
for i in range(n):
	if f[i] == -1:
		pre[i] = (i + 1) * a[i]
	else:
		ind = f[i]
		pre[i] = (i - ind) * a[i] + pre[ind]
for i in range(n - 1, -1, -1):
	if e[i] == n:
		suf[i] = (n - i) * a[i]
	else:
		ind = e[i]
		suf[i] = (ind - i) * a[i] + suf[ind]
m = 0
ind = -1
for i in range(n):
	x = pre[i] + suf[i] - a[i]
	if x > m:
		m = x
		ind = i
i = ind
prev = a[i]
res = []
for j in range(i - 1, -1, -1):
	prev = min(a[j], prev)
	res.append(prev)
prev = a[i]
res = res[::-1]
res.append(a[i])
for j in range(i + 1, n):
	prev = min(a[j], prev)
	res.append(prev)
for i in res:
	print(i, end=' ')
print()


import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
m = list(map(int, input().split()))
minID = [[None] * n for _ in range(n)]
for i in range(n):
	minID[i][i] = i
for shift in range(1, n):
	for L in range(n - shift):
		R = L + shift
		prev = minID[L][R - 1]
		if m[prev] < m[R]:
			minID[L][R] = prev
		else:
			minID[L][R] = R
bestSum = -float('inf')
for p in range(n):
	top = m[p]
	L = 0
	left = []
	currSum = 0
	while L <= p:
		nextMin = minID[L][p]
		left.append((m[nextMin], nextMin - L + 1))
		currSum += m[nextMin] * (nextMin - L + 1)
		L = nextMin + 1
	R = n - 1
	right = []
	while R >= p:
		nextMin = minID[p][R]
		if nextMin > p:
			right.append((m[nextMin], R - nextMin + 1))
			currSum += m[nextMin] * (R - nextMin + 1)
		else:
			right.append((m[nextMin], R - nextMin))
			currSum += m[nextMin] * (R - nextMin)
		R = nextMin - 1
	if currSum > bestSum:
		bestSum = currSum
		bestAns = left + right[::-1]
ans = []
for (val, t) in bestAns:
	ans.extend([val] * t)
print(*ans)


import math
n = int(input())
buldings = list(map(int, input().split()))
total = 0
answer = []
for index in range(n):
	temp = buldings[:]
	for j in range(index - 1, -1, -1):
		temp[j] = min(temp[j], temp[j + 1])
	for j in range(index + 1, n):
		temp[j] = min(temp[j], temp[j - 1])
	tempTotal = sum(temp)
	if tempTotal > total:
		total = tempTotal
		answer = temp
print(*answer)


t = int(input())
l = list(map(int, input().split()))
ans = 0
m = 0
for i in range(t):
	s = 0
	a = l[:]
	for j in range(i, 0, -1):
		if a[j] < a[j - 1]:
			a[j - 1] = a[j]
		s += a[j - 1]
	s += a[i]
	for k in range(i, t - 1):
		if a[k] < a[k + 1]:
			a[k + 1] = a[k]
		s += a[k + 1]
	if s > ans:
		ans = s
		m = i
for i in range(m, 0, -1):
	if l[i] < l[i - 1]:
		l[i - 1] = l[i]
for i in range(m, t - 1):
	if l[i + 1] > l[i]:
		l[i + 1] = l[i]
print(*l)


n = int(input())
arr = [int(j) for j in input().split()]
ma = -1
for i in range(0, n):
	arr2 = arr[:]
	for j in range(i - 1, -1, -1):
		if arr2[j] > arr2[j + 1]:
			arr2[j] = arr2[j + 1]
	for j in range(i + 1, n):
		if arr2[j] > arr2[j - 1]:
			arr2[j] = arr2[j - 1]
	if sum(arr2) > ma:
		ma = sum(arr2)
		ans = arr2[:]
print(*ans)


n = int(input())
m = list(map(int, input().split()))
mx = 0
ans = list()
for pos in range(n):
	mlis = [0 for i in range(n)]
	sum = m[pos]
	mlis[pos] = m[pos]
	for i in range(pos + 1, n):
		mlis[i] = min(mlis[i - 1], m[i])
		sum += mlis[i]
	for i in range(pos - 1, -1, -1):
		mlis[i] = min(mlis[i + 1], m[i])
		sum += mlis[i]
	if sum > mx:
		ans = mlis
		mx = sum
mm = map(str, ans)
print(' '.join(mm))


n = int(input())
nums = [int(x) for x in input().split()]
res = []
for i in range(n):
	tmp = [0] * n
	tmp[i] = nums[i]
	t = nums[i]
	for j in range(i - 1, -1, -1):
		if nums[j] > t:
			tmp[j] = t
		else:
			t = nums[j]
		tmp[j] = t
	t = nums[i]
	for j in range(i + 1, n):
		if nums[j] > t:
			tmp[j] = t
		else:
			t = nums[j]
		tmp[j] = t
	res.append(tmp)
res = max(res, key=sum)
res = ' '.join([str(x) for x in res])
print(res)


n = int(input())
a = list(map(int, input().split()))
d = [0] * n
mx = -1
t = [0] * n
for i in range(n):
	for k in range(n):
		t[k] = a[k]
	for j in range(i, n - 1):
		if t[j] < t[j + 1]:
			t[j + 1] = t[j]
	j = i
	while j > 0:
		if t[j] < t[j - 1]:
			t[j - 1] = t[j]
		j = j - 1
	ans = 0
	for i in range(n):
		ans = ans + t[i]
	if mx < ans:
		mx = ans
		for jj in range(n):
			d[jj] = t[jj]
for i in range(n):
	if i == n - 1:
		print(d[i])
	else:
		print(d[i], end=' ')


import collections
n = int(input())
m = [int(num) for num in input().split()]
rightlessque = collections.deque([])
indrightless = [n] * n
for i in range(n):
	while rightlessque and m[i] < m[rightlessque[0]]:
		indrightless[rightlessque.popleft()] = i
	rightlessque.appendleft(i)
leftlessque = collections.deque([])
indleftless = [-1] * n
for i in range(n - 1, -1, -1):
	while leftlessque and m[i] < m[leftlessque[0]]:
		indleftless[leftlessque.popleft()] = i
	leftlessque.appendleft(i)
leftval = [0] * n
for i in range(n):
	if indleftless[i] == -1:
		leftval[i] = (i + 1) * m[i]
	else:
		leftval[i] = leftval[indleftless[i]] + m[i] * (i - indleftless[i])
rightval = [0] * n
for i in range(n - 1, -1, -1):
	if indrightless[i] == n:
		rightval[i] = (n - i) * m[i]
	else:
		rightval[i] = rightval[indrightless[i]] + m[i] * (indrightless[i] - i)
maxx = 0
ind = 0
for i in range(n):
	num = leftval[i] + rightval[i] - m[i]
	if num > maxx:
		maxx = num
		ind = i
finalans = [0] * n
i = ind
while i >= 0:
	curr = i
	while curr > indleftless[i]:
		finalans[curr] = m[i]
		curr -= 1
	i = indleftless[i]
i = ind
while i < n:
	curr = i
	while curr < indrightless[i]:
		finalans[curr] = m[i]
		curr += 1
	i = indrightless[i]
for i in finalans:
	print(i, end=' ')
print()


N = int(input())
List = [int(x) for x in input().split()]
Change = List.copy()
Sum = 0
Index = -1
for high in range(N):
	Change = List.copy()
	for i in range(high - 1, -1, -1):
		if List[i] > Change[i + 1]:
			Change[i] = Change[i + 1]
	for i in range(high + 1, N):
		if List[i] > Change[i - 1]:
			Change[i] = Change[i - 1]
	temp = sum(Change)
	if temp > Sum:
		Sum = temp
		Index = high
high = Index
Change = List.copy()
for i in range(high - 1, -1, -1):
	if List[i] > Change[i + 1]:
		Change[i] = Change[i + 1]
for i in range(high + 1, N):
	if List[i] > Change[i - 1]:
		Change[i] = Change[i - 1]
print(*Change)


import sys
input = sys.stdin.readline
import collections
n = int(input())
m = [int(item) for item in input().split()]
dp = [[-1] * (n + 1) for _ in range(2)]
dp[0][0] = 0
dp[1][0] = 10 ** 10
ans = 0
ans_dq = None
for i in range(n):
	dq = collections.deque()
	l = r = i
	ret = m[i]
	lim = m[i]
	dq.append(m[i])
	while l > 0:
		l -= 1
		val = min(lim, m[l])
		dq.appendleft(val)
		ret += val
		lim = val
	lim = m[i]
	while r < n - 1:
		r += 1
		val = min(lim, m[r])
		dq.append(val)
		ret += val
		lim = val
	if ret > ans:
		ans_dq = dq
		ans = ret
print(*ans_dq)


n = int(input())
m = [0] + list(map(int, input().split())) + [0]
stk = [0] * (n + 2)
(le, ri) = ([0] * (n + 2), [0] * (n + 2))
pos = [0] * (n + 2)
ans = [0] * (n + 2)
top = 0
for i in range(1, n + 1):
	while top and stk[top] > m[i]:
		top -= 1
	le[i] = le[pos[top]] + (i - pos[top]) * m[i]
	top += 1
	(stk[top], pos[top]) = (m[i], i)
top = 0
pos[0] = n + 1
for i in range(n, 0, -1):
	while top and stk[top] > m[i]:
		top -= 1
	ri[i] = ri[pos[top]] + (pos[top] - i) * m[i]
	top += 1
	(stk[top], pos[top]) = (m[i], i)
(_max, idx) = (0, 1)
for i in range(1, n + 1):
	if le[i] + ri[i] - m[i] > _max:
		_max = le[i] + ri[i] - m[i]
		idx = i
ans[idx] = m[idx]
for j in range(idx - 1, 0, -1):
	ans[j] = min(m[j], ans[j + 1])
for j in range(idx + 1, n + 1):
	ans[j] = min(m[j], ans[j - 1])
print(*ans[1:-1])


input()
List = [int(x) for x in input().strip().split(' ')]
res = 0
for i in range(len(List)):
	L = List[:]
	ans = L[i]
	j = i - 1
	k = i + 1
	while j >= 0:
		L[j] = min(L[j], L[j + 1])
		ans += L[j]
		j -= 1
	while k < len(L):
		L[k] = min(L[k - 1], L[k])
		ans += L[k]
		k += 1
	if ans > res:
		res = ans
		R = L[:]
for x in R:
	print(x, end=' ')
print()


import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
m = list(map(int, input().split()))
smaller_left = [-1] * n
stack = []
for i in reversed(range(n)):
	val = m[i]
	while stack and val < stack[-1][0]:
		(_, j) = stack.pop()
		smaller_left[j] = i
	stack.append((val, i))
smaller_right = [-1] * n
stack = []
for i in range(n):
	val = m[i]
	while stack and val < stack[-1][0]:
		(_, j) = stack.pop()
		smaller_right[j] = i
	stack.append((val, i))
inc = []
for i in range(n):
	val = m[i]
	j = smaller_left[i]
	if j < 0:
		inc.append(val * (i + 1))
	else:
		inc.append(inc[j] + val * (i - j))
dec = [None] * n
for i in reversed(range(n)):
	val = m[i]
	j = smaller_right[i]
	if j < 0:
		dec[i] = val * (n - i)
	else:
		dec[i] = dec[j] + val * (j - i)
(bestVal, bestId) = (-1, -1)
for i in range(n):
	currVal = inc[i] + dec[i] - m[i]
	if currVal > bestVal:
		bestVal = currVal
		bestId = i
ans = [None] * n
i = bestId
while i >= 0:
	j = smaller_left[i]
	for p in range(i, j, -1):
		ans[p] = m[i]
	i = j
i = bestId
while i < n:
	j = smaller_right[i]
	if j < 0:
		j = n
	for p in range(i, j):
		ans[p] = m[i]
	i = j
print(*ans)


n = int(input())
a = list(map(int, input().split()))
left = [0 for i in range(n)]
right = [0 for i in range(n)]
s = [-1]
for i in range(n - 1, -1, -1):
	while s[-1] != -1 and a[s[-1]] > a[i]:
		s.pop()
	if s[-1] == -1:
		right[i] = a[i] * (n - i)
	else:
		right[i] = right[s[-1]] + a[i] * (s[-1] - i)
	s.append(i)
s = [-1]
for i in range(n):
	while s[-1] != -1 and a[s[-1]] > a[i]:
		s.pop()
	if s[-1] == -1:
		left[i] = a[i] * (i + 1)
	else:
		left[i] = left[s[-1]] + a[i] * (i - s[-1])
	s.append(i)
m = 0
ind = -1
for i in range(n):
	Sum = left[i] + right[i] - a[i]
	if Sum > m:
		m = Sum
		ind = i
i = ind - 1
j = ind + 1
m1 = a[ind]
m2 = a[ind]
while i >= 0:
	if a[i] > m1:
		a[i] = m1
	else:
		m1 = a[i]
	i -= 1
while j < n:
	if a[j] > m2:
		a[j] = m2
	else:
		m2 = a[j]
	j += 1
print(*a)


n = int(input())
ml = list(map(int, input().split()))
ans = 0
ansl = []
for i in range(n):
	ans1 = 0
	ansl1 = []
	top = ml[i]
	l = ml[i]
	for j in range(i, 0, -1):
		if l >= ml[j - 1]:
			l = ml[j - 1]
		ans1 += l
		ansl1 = [l] + ansl1
	r = ml[i]
	for j in range(i, n):
		if r >= ml[j]:
			r = ml[j]
		ans1 += r
		ansl1 = ansl1 + [r]
	if ans1 > ans:
		ans = ans1
		ansl = ansl1
print(*ansl)


R = lambda : map(int, input().split())
n = int(input())
arr = list(R())
res = [0] * n
for i in range(n):
	tmp = [0] * n
	tmp[i] = arr[i]
	for j in range(i - 1, -1, -1):
		tmp[j] = min(tmp[j + 1], arr[j])
	for j in range(i + 1, n):
		tmp[j] = min(tmp[j - 1], arr[j])
	if sum(res) < sum(tmp):
		res = tmp
for x in res:
	print(x, end=' ')


n = int(input())
s = [int(i) for i in input().split()]
p = [0] * n
mx = 0
for i in range(0, n):
	psum = s[i]
	for j in range(i, -1, -1):
		psum = min(s[j], psum)
		p[i] += psum
	psum = s[i]
	for j in range(i + 1, n):
		psum = min(s[j], psum)
		p[i] += psum
	if mx < p[i]:
		mx = p[i]
		mx_i = i
i = mx_i
ans = [0] * n
psum = s[mx_i]
for j in range(i, -1, -1):
	psum = min(s[j], psum)
	ans[j] = psum
psum = s[mx_i]
for j in range(i + 1, n):
	psum = min(s[j], psum)
	ans[j] = psum
print(*ans)


n = eval(input())
h = list(map(int, input().split()))
L = [0 for i in range(len(h))]
R = [0 for i in range(len(h))]
ans = [0 for i in range(len(h))]
stack = []
for i in range(len(h)):
	while len(stack) and h[stack[-1][0]] >= h[i]:
		stack.pop()
	if len(stack) == 0:
		stack.append((i, (i + 1) * h[i]))
	else:
		stack.append((i, stack[-1][1] + (i - stack[-1][0]) * h[i]))
	L[i] = stack[-1][1]
while len(stack):
	stack.pop()
stack.append((n - 1, 0))
for i in range(len(h) - 1, -1, -1):
	while len(stack) and h[stack[-1][0]] >= h[i]:
		stack.pop()
	if len(stack) == 0:
		stack.append((i, (n - i) * h[i]))
	else:
		stack.append((i, stack[-1][1] + (stack[-1][0] - i) * h[i]))
	R[i] = stack[-1][1]
(mx, which) = (0, -1)
for i in range(len(h)):
	if mx < L[i] + R[i] - h[i]:
		(mx, which) = (L[i] + R[i] - h[i], i)
mn = h[which]
ans[which] = h[which]
for j in range(which - 1, -1, -1):
	mn = min(mn, h[j])
	ans[j] = mn
mn = h[which]
for j in range(which + 1, len(h)):
	mn = min(mn, h[j])
	ans[j] = mn
for i in ans:
	print(i, end=' ')


import math
n = int(input())
mi = input().split()
mi = [int(m) for m in mi]
l = [0 for i in range(n)]
r = [0 for i in range(n)]
right_to_left = [-1 for i in range(n)]
left_to_right = [-1 for i in range(n)]
stack = [0]
for i in range(1, n, 1):
	while len(stack) != 0 and mi[stack[-1]] > mi[i]:
		left_to_right[stack.pop()] = i
	stack.append(i)
stack = [n - 1]
for i in range(n - 2, -1, -1):
	while len(stack) != 0 and mi[stack[-1]] > mi[i]:
		right_to_left[stack.pop()] = i
	stack.append(i)
l[0] = mi[0]
for i in range(1, n, 1):
	if mi[i - 1] <= mi[i]:
		l[i] = l[i - 1] + mi[i]
	else:
		li = right_to_left[i]
		if li == -1:
			l[i] = mi[i] * (i + 1)
		else:
			l[i] = l[li] + mi[i] * (i - li)
r[n - 1] = mi[n - 1]
for i in range(n - 2, -1, -1):
	if mi[i + 1] <= mi[i]:
		r[i] = r[i + 1] + mi[i]
	else:
		ri = left_to_right[i]
		if ri == -1:
			r[i] = mi[i] * (n - i)
		else:
			r[i] = r[ri] + mi[i] * (ri - i)
peak = 0
for i in range(n):
	if l[peak] + r[peak] - mi[peak] < l[i] + r[i] - mi[i]:
		peak = i
for i in range(peak, 0, -1):
	if mi[i - 1] > mi[i]:
		mi[i - 1] = mi[i]
for i in range(peak, n - 1, 1):
	if mi[i + 1] > mi[i]:
		mi[i + 1] = mi[i]
for i in range(n):
	print(mi[i], end=' ')
print()


(N,) = map(int, input().split())
X = list(map(int, input().split()))
stack = [[0, 0, 0]]
ls = []
for i in range(N):
	tmp = [1, X[i], X[i]]
	while stack:
		if stack[-1][1] < tmp[1]:
			tmp[2] = tmp[0] * tmp[1] + stack[-1][2]
			stack.append(tmp)
			break
		else:
			(x, y, z) = stack.pop()
			tmp[0] += x
	ls.append(tmp[2])
stack2 = [[0, 0, 0]]
rs = [0] * N
for i in range(N - 1, -1, -1):
	tmp = [1, X[i], X[i]]
	while stack2:
		if stack2[-1][1] < tmp[1]:
			tmp[2] = tmp[0] * tmp[1] + stack2[-1][2]
			stack2.append(tmp)
			break
		else:
			(x, y, z) = stack2.pop()
			tmp[0] += x
	rs[i] = tmp[2]
mx = 0
ii = 0
for i in range(N):
	if ls[i] + rs[i] - X[i] > mx:
		mx = ls[i] + rs[i] - X[i]
		ii = i
I = ii
i = I
rs = [0] * N
rs[i] = X[i]
b = rs[i]
while i > 0:
	i -= 1
	b = min(X[i], b)
	rs[i] = b
i = I
b = X[i]
while i < N - 1:
	i += 1
	b = min(X[i], b)
	rs[i] = b
print(*rs)


from math import *
from sys import stdin, stdout

def binarySearch(arr, x, i):
	l = i
	r = len(arr) - 1
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] <= x:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def jg(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] >= x:
			ans = m
			r = m - 1
		else:
			l = m + 1
	return ans

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

def c(s, i, a, b):
	count = 0
	for j in range(i, len(s)):
		if s[i] != s[j]:
			if s[i] == 'A':
				count += a
			else:
				count += b
			i = j
	if i < len(s) - 1:
		if s[i] == 'A':
			count += a
		else:
			count += b
	return count
import math as mt
from collections import deque

def printPrevSmaller(arr, n):
	S = deque()
	ind = deque()
	l = []
	for i in range(n):
		while len(S) > 0 and S[-1] > arr[i]:
			S.pop()
			ind.pop()
		if len(S) == 0:
			l.append(-1)
		else:
			l.append(ind[-1])
		S.append(arr[i])
		ind.append(i)
	return l
n = int(stdin.readline())
ar = list(map(int, stdin.readline().split()))
l = printPrevSmaller(ar, n)
r = printPrevSmaller(ar[::-1], n)
if n == 1:
	stdout.write(str(ar[0]))
else:
	a = []
	b = []
	for i in range(n):
		if l[i] == -1:
			a.append((i - l[i]) * ar[i])
		else:
			a.append((i - l[i]) * ar[i] + a[l[i]])
		if r[i] == -1:
			b.append((i - r[i]) * ar[::-1][i])
		else:
			b.append((i - r[i]) * ar[::-1][i] + b[r[i]])
	b = b[::-1]
	ans = 0
	c = 0
	for i in range(n):
		if c < a[i] + b[i] - ar[i]:
			c = a[i] + b[i] - ar[i]
			ans = i
	rslt = [0] * n
	rslt[ans] = ar[ans]
	temp = ar[ans]
	for i in range(ans + 1, n):
		if ar[i] < temp:
			rslt[i] = ar[i]
			temp = ar[i]
		else:
			rslt[i] = temp
	x = ans - 1
	temp = ar[ans]
	while x >= 0:
		if ar[x] < temp:
			rslt[x] = ar[x]
			temp = ar[x]
		else:
			rslt[x] = temp
		x -= 1
	rslt = map(str, rslt)
	stdout.write(' '.join(rslt))


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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 time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def prev_smaller(arr, N):
	stack = col.deque([])
	stack.append((arr[-1], N - 1))
	res = col.defaultdict(int)
	for i in range(N - 2, -1, -1):
		if len(stack) == 0:
			stack.append((arr[i], i))
			continue
		while len(stack) > 0 and stack[-1][0] > arr[i]:
			res[stack[-1][1]] = i
			stack.pop()
		stack.append((arr[i], i))
	while stack:
		res[stack[-1][1]] = -1
		stack.pop()
	return res

def next_smaller(arr, N):
	stack = col.deque([])
	stack.append((arr[0], 0))
	res = col.defaultdict(int)
	for i in range(1, N):
		if len(stack) == 0:
			stack.append((arr[i], i))
			continue
		while len(stack) > 0 and stack[-1][0] > arr[i]:
			res[stack[-1][1]] = i
			stack.pop()
		stack.append((arr[i], i))
	while stack:
		res[stack[-1][1]] = -1
		stack.pop()
	return res

def solve():
	N = getInt()
	M = getInts()
	prev_less = prev_smaller(M, N)
	next_less = next_smaller(M, N)
	left = [0] * N
	right = [0] * N
	for i in range(N):
		if prev_less[i] == -1:
			left[i] = (i + 1) * M[i]
		else:
			prev = prev_less[i]
			left[i] = left[prev] + (i - prev) * M[i]
	for i in range(N - 1, -1, -1):
		rel_pos = N - i - 1
		if next_less[i] == -1:
			right[i] = (rel_pos + 1) * M[i]
		else:
			nxt = N - next_less[i] - 1
			right[i] = right[next_less[i]] + (rel_pos - nxt) * M[i]
	best = 0
	for i in range(N):
		curr = right[i] + left[i] - M[i]
		if best < curr:
			best = curr
			best_ind = i
	ans = [0] * N
	ans[best_ind] = M[best_ind]
	cur_min = M[best_ind]
	for i in range(best_ind - 1, -1, -1):
		cur_min = min(cur_min, M[i])
		ans[i] = cur_min
	cur_min = M[best_ind]
	for i in range(best_ind + 1, N):
		cur_min = min(cur_min, M[i])
		ans[i] = cur_min
	print(*ans)
	return
solve()


import bisect
n = int(input())
a = list(map(int, input().split(' ')))
summ_l = [0] * n
l = [0] * n
left_book_numbers = [0] * n
left_book_indices = [0] * n
summ_l[0] = a[0]
l[0] = a[0]
left_book_numbers[0] = a[0]
left_book_indices[0] = 0
p = 1
for i in range(1, n):
	if a[i] <= left_book_numbers[0]:
		left_book_numbers[0] = a[i]
		left_book_indices[0] = i
		p = 1
		summ_l[i] = (i + 1) * a[i]
	elif left_book_numbers[p - 1] < a[i]:
		left_book_numbers[p] = a[i]
		left_book_indices[p] = i
		summ_l[i] = summ_l[i - 1] + a[i]
		p += 1
	else:
		j = bisect.bisect_left(left_book_numbers, a[i], 0, p)
		summ_l[i] = summ_l[left_book_indices[j - 1]] + (i - left_book_indices[j - 1]) * a[i]
		left_book_numbers[j] = a[i]
		left_book_indices[j] = i
		p = j + 1
a.reverse()
summ_r = [0] * n
l = [0] * n
left_book_numbers = [0] * n
left_book_indices = [0] * n
summ_r[0] = a[0]
l[0] = a[0]
left_book_numbers[0] = a[0]
left_book_indices[0] = 0
p = 1
for i in range(1, n):
	if a[i] <= left_book_numbers[0]:
		left_book_numbers[0] = a[i]
		left_book_indices[0] = i
		p = 1
		summ_r[i] = (i + 1) * a[i]
	elif left_book_numbers[p - 1] < a[i]:
		left_book_numbers[p] = a[i]
		left_book_indices[p] = i
		summ_r[i] = summ_r[i - 1] + a[i]
		p += 1
	else:
		j = bisect.bisect_left(left_book_numbers, a[i], 0, p)
		summ_r[i] = summ_r[left_book_indices[j - 1]] + (i - left_book_indices[j - 1]) * a[i]
		left_book_numbers[j] = a[i]
		left_book_indices[j] = i
		p = j + 1
summ_r.reverse()
a.reverse()
totals_list = []
for i in range(n):
	totals_list.append(summ_l[i] - a[i] + summ_r[i])
mi = totals_list.index(max(totals_list))
result = [0] * n
result[mi] = a[mi]
i = mi - 1
while i >= 0:
	result[i] = min(result[i + 1], a[i])
	i -= 1
i = mi + 1
while i <= n - 1:
	result[i] = min(result[i - 1], a[i])
	i += 1
for x in result:
	print(x, end=' ')
print()


def skyscappers(n, m):
	count_max = 0
	ans = []
	optimal_peak = 0
	for i in range(n):
		plan = [None] * n
		peak = m[i]
		count = peak
		plan[i] = m[i]
		for j in range(i - 1, -1, -1):
			prev = plan[j + 1]
			floor = m[j]
			if floor <= prev:
				plan[j] = floor
				count += floor
			if prev < floor:
				plan[j] = prev
				count += prev
		for j in range(i + 1, n):
			prev = plan[j - 1]
			floor = m[j]
			if floor <= prev:
				plan[j] = floor
				count += floor
			if prev < floor:
				plan[j] = prev
				count += prev
		if count > count_max:
			ans = plan
			count_max = count
			optimal_peak = i
	return ans
n = int(input())
m = list(map(int, input().split()))
arr = skyscappers(n, m)
for i in range(n):
	print(arr[i], end=' ')


def rmin(a, n):
	ans = [-1] * (n + 2)
	st = [0]
	res = []
	for i in range(1, n + 1):
		while a[st[-1]] > a[i]:
			ans[st.pop()] = n + 1 - i
		st.append(i)
	for i in range(1, n + 1):
		res.append(ans[i])
	return res
n = int(input())
inf = int(2000000000.0) + 1
m = [int(x) for x in input().split()]
m = [-inf] + m + [-inf]
ar = rmin(m, n)
al = rmin(m[::-1], n)[::-1]
lm = [0 for i in range(n)]
rm = lm.copy()
for i in range(n):
	j = n - 1 - i
	lm[i] = m[i + 1] * (i + 1)
	rm[j] = (i + 1) * m[j + 1]
	if al[i] != -1:
		lm[i] += lm[al[i] - 1] - m[i + 1] * al[i]
	if ar[j] != -1:
		rm[j] += rm[n - ar[j]] - m[j + 1] * ar[j]
th = 0
for i in range(n):
	tmp = lm[i] + rm[i] - m[i + 1]
	if tmp > th:
		th = tmp
		tind = i + 1
i = tind + 1
while i < n:
	if m[i + 1] > m[i]:
		m[i + 1] = m[i]
	i += 1
i = tind
while i > 0:
	if m[i - 1] > m[i]:
		m[i - 1] = m[i]
	i -= 1
print(*m[1:n + 1])


from copy import copy
n = int(input())
arr = list(map(int, input().split()))
best_ans = 0
best_config = [0] * n
ans = [0] * n
for pk in range(n):
	ans[pk] = arr[pk]
	i = pk - 1
	while i != -1:
		ans[i] = min(ans[i + 1], arr[i])
		i -= 1
	i = pk + 1
	while i != n:
		ans[i] = min(ans[i - 1], arr[i])
		i += 1
	sm = sum(ans)
	if sm > best_ans:
		best_ans = sm
		best_config = copy(ans)
print(*best_config)


from math import inf
n = int(input())
M = [int(i) for i in input().split()]
ans = []
ma = 0
for i in range(n):
	b = [0] * n
	b[i] = M[i]
	s = 0
	for j in range(i - 1, -1, -1):
		b[j] = min(M[j], b[j + 1])
	for j in range(i + 1, n):
		b[j] = min(M[j], b[j - 1])
	for i in b:
		s += i
	if s > ma:
		ans = b[:]
		ma = s
for i in ans:
	print(i, end=' ')


n = int(input())
m = list(map(int, input().split()))
res = 0
for i in range(n):
	tmp = [0 for _ in range(n)]
	num = m[i]
	tmp[i] = m[i]
	cur = m[i]
	for j in range(i - 1, -1, -1):
		cur = min(m[j], cur)
		tmp[j] = cur
		num += cur
	cur = m[i]
	for k in range(i + 1, n):
		cur = min(m[k], cur)
		tmp[k] = cur
		num += cur
	if num > res:
		ans = tmp
		res = num
print(*ans)


n = int(input())
list1 = list(map(int, input().split()))
ans = list1[:]
maxi = 0
for i in range(n):
	temp = list1[:]
	for j in range(i - 1, -1, -1):
		if i > 0:
			temp[j] = min(temp[j], temp[j + 1])
	for j in range(i + 1, n):
		temp[j] = min(temp[j], temp[j - 1])
	sum1 = 0
	for j in range(n):
		sum1 += temp[j]
	if sum1 > maxi:
		maxi = sum1
		ans = temp[:]
print(*ans)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations, combinations

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

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
INF = 10 ** 9
mod = int(1000000000.0) + 7

def solve(m):
	l = m[:]
	stack = []
	for i in range(n):
		while stack and m[stack[-1]] > m[i]:
			stack.pop()
		if stack:
			ind = stack[-1]
			l[i] = l[ind] + m[i] * (i - ind)
		else:
			l[i] *= i + 1
		stack.append(i)
	return l
n = int(data())
m = mdata()
l = solve(m)
r = solve(m[::-1])[::-1]
max1 = l[0] + r[0] - m[0]
ind = 0
for i in range(1, n):
	if l[i] + r[i] - m[i] > max1:
		max1 = l[i] + r[i] - m[i]
		ind = i
for i in range(ind - 1, -1, -1):
	m[i] = min(m[i], m[i + 1])
for i in range(ind + 1, n):
	m[i] = min(m[i], m[i - 1])
outl(m)


n = int(input())
l = [int(x) for x in input().split()]
final = 0
ar = [0] * n
for i in range(n):
	mn = 10 ** 10
	ans = 0
	for j in range(i, -1, -1):
		mn = min(mn, l[j])
		ans += mn
	mn = 10 ** 10
	for j in range(i + 1, n):
		mn = min(mn, l[j])
		ans += mn
	if ans > final:
		final = ans
		mn = 10 ** 10
		for j in range(i, -1, -1):
			mn = min(mn, l[j])
			ar[j] = mn
		mn = 10 ** 10
		for j in range(i + 1, n):
			mn = min(mn, l[j])
			ar[j] = mn
print(*ar)


n = int(input())
m = list(map(int, input().split()))
a = m.copy()
if m == sorted(m) or m == sorted(m)[::-1]:
	for i in range(n):
		print(m[i], end=' ')
	print()
elif n % 2 == 0 and a[:n // 2] == sorted(a[:n // 2]) and (a[n // 2:] == sorted(a[n // 2:])[::-1]):
	for i in range(n):
		print(m[i], end=' ')
	print()
elif n % 2 != 0 and a[:n // 2] == sorted(a[:n // 2]) and (a[n // 2 + 1:] == sorted(a[n // 2 + 1:])[::-1]) and (a[n // 2] >= a[n // 2 - 1]) and (a[n // 2 + 1] >= a[n // 2]):
	for i in range(n):
		print(m[i], end=' ')
	print()
else:
	a = m.copy()
	x = a[0]
	c = 0
	for i in range(1, n - 1):
		if a[:i] == sorted(a[:i]) and a[i:] == sorted(a[i:])[::-1]:
			c = 1
			break
	if c == 1:
		for i in range(n):
			print(a[i], end=' ')
		print()
	else:
		ans = []
		l = []
		for j in range(0, n):
			a = m.copy()
			for i in range(1, j + 1):
				if a[i] >= a[i - 1]:
					continue
				else:
					k = i - 1
					while k >= 0 and a[k] > a[i]:
						a[k] = a[i]
						k -= 1
			for i in range(j + 1, n - 1):
				if a[i + 1] <= a[i]:
					continue
				else:
					a[i + 1] = a[i]
			l.append(a)
			ans.append(sum(l[-1]))
		j = 0
		a = m.copy()
		for i in range(1, n):
			if a[i] >= a[i - 1]:
				continue
			else:
				k = i - 1
				while k >= 0 and a[k] > a[i]:
					a[k] = a[i]
					k -= 1
		l.append(a)
		ans.append(sum(l[-1]))
		j = n - 1
		a = m.copy()
		for i in range(n - 1):
			if a[i] >= a[i + 1]:
				continue
			else:
				a[i + 1] = a[i]
		l.append(a)
		ans.append(sum(l[-1]))
		m2 = max(ans)
		for i in range(len(l)):
			if ans[i] == m2:
				a = l[i]
				break
		for i in range(n):
			print(a[i], end=' ')
		print()


def main():
	n = int(input())
	M = list(map(int, input().split()))
	best_summa = 0
	for i in range(n):
		L = [0 for i in range(n)]
		summa = M[i]
		L[i] = M[i]
		for j in range(i - 1, -1, -1):
			summa += min(L[j + 1], M[j])
			L[j] = min(L[j + 1], M[j])
		for j in range(i + 1, n):
			summa += min(L[j - 1], M[j])
			L[j] = min(L[j - 1], M[j])
		if summa > best_summa:
			best_summa = summa
			Ans = L[:]
	print(' '.join((str(i) for i in Ans)))
main()


n = eval(input())
h = list(map(int, input().split()))
(mx, which) = (0, -1)
INF = 2000000000000
for i in range(len(h)):
	tmp = h[i]
	mn = h[i]
	for j in range(i - 1, -1, -1):
		mn = min(mn, h[j])
		tmp += mn
	mn = h[i]
	for j in range(i + 1, len(h)):
		mn = min(mn, h[j])
		tmp += mn
	if mx < tmp:
		mx = tmp
		which = i
ans = [0 for i in range(len(h))]
mn = h[which]
ans[which] = h[which]
for j in range(which - 1, -1, -1):
	mn = min(mn, h[j])
	ans[j] = mn
mn = h[which]
for j in range(which + 1, len(h)):
	mn = min(mn, h[j])
	ans[j] = mn
for i in ans:
	print(i, end=' ')


import operator
from sys import stdin
n = int(input())
floors = [int(x) for x in stdin.readline().split()]
left = [0] * n
right = [0] * n

def left_index(lst):
	m = len(lst)
	S = []
	index_list = []
	for i in range(m):
		while S != []:
			if lst[S[-1]] > lst[i]:
				S.pop()
			else:
				index_list.append(S[-1])
				break
		if S == []:
			index_list.append(i)
		S.append(i)
	return index_list

def left_right(flrs, lst):
	li = left_index(flrs)
	for i in range(n):
		if i == li[i]:
			lst[i] = flrs[i] * (i + 1)
		else:
			lst[i] = lst[li[i]] + (i - li[i]) * flrs[i]
left_right(floors, left)
left_right(floors[::-1], right)
right = right[::-1]
c_max = 0
ind = 0
for i in range(n):
	if left[i] != -1 and right[i] != -1:
		if left[i] + right[i] - floors[i] > c_max:
			c_max = left[i] + right[i] - floors[i]
			ind = i
to_print = [0] * n
to_print[ind] = floors[ind]
m = floors[ind]
for i in range(ind - 1, -1, -1):
	to_print[i] = min(floors[i], m)
	if floors[i] < m:
		m = floors[i]
m = floors[ind]
for i in range(ind + 1, n):
	to_print[i] = min(floors[i], m)
	if floors[i] < m:
		m = floors[i]
for i in to_print:
	print(int(i), end=' ')


import sys
input = sys.stdin.readline
from collections import OrderedDict

def floor_key(d, key):
	if key in d:
		return key
	return max((k for k in d if k < key))

def ceil_key(d, key):
	if key in d:
		return key
	return min((k for k in d if k > key))
n = int(input())
a = list(map(int, input().split()))
right = [0] * n
for i in range(n):
	mn = a[i]
	for j in range(i, n):
		mn = min(mn, a[j])
		right[i] += mn
left = [0] * n
for i in range(n):
	mn = a[i]
	for j in range(i, -1, -1):
		mn = min(mn, a[j])
		left[i] += mn
best = 0
besti = -1
for i in range(n):
	cur = right[i] + left[i] - a[i]
	if cur > best:
		best = cur
		besti = i
ans = [0] * n
ans[besti] = a[besti]
mn = a[besti]
for i in range(besti + 1, n):
	mn = min(mn, a[i])
	ans[i] = mn
mn = a[besti]
for i in range(besti - 1, -1, -1):
	mn = min(mn, a[i])
	ans[i] = mn
print(*ans)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List, Mapping
sys.setrecursionlimit(999999)
n = int(input())
arr = list(map(int, input().split()))
sl = [0] * n
st = []
for i in range(n):
	c = arr[i]
	sl[i] += c
	if i:
		sl[i] += sl[i - 1]
	while st and arr[st[-1]] > c:
		p = st.pop()
		front = -1 if not st else st[-1]
		mh = c if not st else max(c, arr[st[-1]])
		sl[i] -= (i - front - 1) * (arr[p] - mh)
	st.append(i)
st = []
sr = [0] * n
for i in range(n - 1, -1, -1):
	c = arr[i]
	sr[i] += c
	if i < n - 1:
		sr[i] += sr[i + 1]
	while st and arr[st[-1]] > c:
		p = st.pop()
		end = n if not st else st[-1]
		mh = c if not st else max(c, arr[st[-1]])
		sr[i] -= (end - i - 1) * (arr[p] - mh)
	st.append(i)
mx = 0
for i in range(n):
	if sl[i] + sr[i] - arr[i] > sl[mx] + sr[mx] - arr[mx]:
		mx = i
for i in range(mx - 1, -1, -1):
	arr[i] = min(arr[i], arr[i + 1])
for i in range(mx + 1, n):
	arr[i] = min(arr[i], arr[i - 1])
print(*arr)


n = int(input())
m = [int(x) for x in input().split()]
assert len(m) == n

def score(peak):
	build = [None] * n
	build[peak] = m[peak]
	score = m[peak]
	limit = m[peak]
	for i in range(peak - 1, -1, -1):
		limit = min(limit, m[i])
		build[i] = limit
		score += limit
	limit = m[peak]
	for i in range(peak + 1, n):
		limit = min(limit, m[i])
		build[i] = limit
		score += limit
	return (score, build)
best = (-1, None)
for i in range(n):
	best = max(best, score(i))
print(*best[1])


n = int(input())
m = list(map(int, input().split()))
maxfloors = 0
ind = 0
ans = list()
for i in range(n):
	chosen = [0 for i in range(n)]
	count = m[i]
	chosen[i] = m[i]
	for j in range(i - 1, -1, -1):
		chosen[j] = min(chosen[j + 1], m[j])
		count += chosen[j]
	for j in range(i + 1, n):
		chosen[j] = min(chosen[j - 1], m[j])
		count += chosen[j]
	if count > maxfloors:
		ans = chosen
		maxfloors = count
for i in range(n):
	print(ans[i], end=' ')


import atexit
import io
import sys
import math
from collections import defaultdict, Counter
n = int(input())
m = list(map(int, input().split()))
if n == 1:
	print(*m)
else:
	stack = [0] * n
	top = -1
	l = [0] * n
	r = [0] * n
	mi = 1000000000
	for i in range(n):
		if m[i] <= mi:
			mi = m[i]
			l[i] = (i + 1) * mi
		else:
			while stack[top][0] > m[i]:
				top -= 1
			l[i] = l[stack[top][1]] + (i - stack[top][1]) * m[i]
		top += 1
		stack[top] = [m[i], i]
	mi = 1000000000
	top = -1
	for i in range(n - 1, -1, -1):
		if m[i] <= mi:
			mi = m[i]
			r[i] = (n - i) * mi
		else:
			while stack[top][0] > m[i]:
				top -= 1
			r[i] = r[stack[top][1]] + (stack[top][1] - i) * m[i]
		top += 1
		stack[top] = [m[i], i]
	ind = 0
	ma = 0
	for i in range(n):
		if l[i] + r[i] - m[i] > ma:
			ma = l[i] + r[i] - m[i]
			ind = i
	m1 = m[:]
	for j in range(ind + 1, n):
		m1[j] = min(m1[j], m1[j - 1])
	for j in range(ind - 1, -1, -1):
		m1[j] = min(m1[j], m1[j + 1])
	print(*m1)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
m = list(map(int, input().split()))
heap = [[0, -1, 0]]
left = [0] * n
for i in range(n):
	while heap[-1][0] > m[i]:
		heap.pop()
	(_, j, s) = heap[-1]
	heap.append([m[i], i, s + m[i] * (i - j)])
	left[i] = heap[-1][-1]
heap = [[0, -1, 0]]
mm = m[::-1]
right = [0] * n
for i in range(n):
	while heap[-1][0] > mm[i]:
		heap.pop()
	(_, j, s) = heap[-1]
	heap.append([mm[i], i, s + mm[i] * (i - j)])
	right[i] = heap[-1][-1]
right = right[::-1]
score = 0
ind = 0
for i in range(n):
	tmp = left[i] + right[i] - m[i]
	if score < tmp:
		score = tmp
		ind = i
i = ind
ans = [0] * n
ans[i] = m[i]
left = i - 1
while left >= 0:
	ans[left] = min(m[left], ans[left + 1])
	left -= 1
right = i + 1
while right < n:
	ans[right] = min(m[right], ans[right - 1])
	right += 1
print(*ans)


N = int(input())
L = [int(x) for x in input().split()]
X = []
for i in L:
	X.append(i)
All = 0
id = -1
for next in range(N):
	X = []
	for i in L:
		X.append(i)
	for i in range(next - 1, -1, -1):
		if L[i] > X[i + 1]:
			X[i] = X[i + 1]
	for i in range(next + 1, N):
		if L[i] > X[i - 1]:
			X[i] = X[i - 1]
	temp = sum(X)
	if temp > All:
		All = temp
		id = next
next = id
X = []
for i in L:
	X.append(i)
for i in range(next - 1, -1, -1):
	if L[i] > X[i + 1]:
		X[i] = X[i + 1]
for i in range(next + 1, N):
	if L[i] > X[i - 1]:
		X[i] = X[i - 1]
for i in range(len(X)):
	print(X[i], end=' ')


def loccentr(ci, razr):
	trazr = razr[:]
	(sum, lm, rm, l, r, n) = (razr[ci], razr[ci], razr[ci], ci - 1, ci + 1, len(razr))
	while l >= 0:
		if trazr[l] < lm:
			lm = trazr[l]
		else:
			trazr[l] = lm
		sum += lm
		l -= 1
	while r < n:
		if razr[r] < rm:
			rm = trazr[r]
		else:
			trazr[r] = rm
		sum += rm
		r += 1
	trazr.append(sum)
	return trazr
n = int(input())
razr = list(map(int, input().split()))
mrazr = loccentr(0, razr)[:]
for i in range(1, n - 1):
	if razr[i] > razr[i - 1] and razr[i] >= razr[i + 1]:
		trazr = loccentr(i, razr)
		if trazr[n] > mrazr[n]:
			mrazr = trazr[:]
trazr = loccentr(n - 1, razr)
if trazr[n] > mrazr[n]:
	mrazr = trazr[:]
print(*mrazr[:n])


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

def wa(a, n):
	incsum = []
	decsum = []
	st1 = []
	thissum = 0
	le = [0, 0]
	st1.append(le)
	for i in a:
		if i >= st1[-1][0]:
			st1.append([i, 1])
			thissum += i
		else:
			c = 0
			while i < st1[-1][0]:
				thissum -= st1[-1][0] * st1[-1][1]
				thissum += i * st1[-1][1]
				c += st1[-1][1]
				st1.pop()
			st1.append([i, c + 1])
			thissum += i
		incsum.append(thissum)
	decsum = []
	thissum = 0
	st1 = [[0, 0]]
	for i in reversed(a):
		if i >= st1[-1][0]:
			st1.append([i, 1])
			thissum += i
		else:
			c = 0
			while i < st1[-1][0]:
				thissum -= st1[-1][0] * st1[-1][1]
				thissum += i * st1[-1][1]
				c += st1[-1][1]
				st1.pop()
			st1.append([i, c + 1])
			thissum += i
		decsum.append(thissum)
	decsum = list(reversed(decsum))
	fans = -1
	thisans = 0
	ansat = -1
	for i in range(n):
		if i == 0:
			thisans = decsum[i]
		elif i == n - 1:
			thisans = incsum[i]
		else:
			thisans = incsum[i] + decsum[i] - a[i]
		if thisans > fans:
			fans = max(fans, thisans)
			ansat = i
	aaa = [0] * n
	aaa[ansat] = a[ansat]
	for i in range(ansat - 1, -1, -1):
		aaa[i] = aaa[i + 1]
		if aaa[i] > a[i]:
			aaa[i] = a[i]
	for i in range(ansat + 1, n):
		aaa[i] = aaa[i - 1]
		if aaa[i] > a[i]:
			aaa[i] = a[i]
	return aaa
print(*wa(a, n))


n = int(input())
import copy
l = list(map(int, input().split()))
i = 0
m_index = 0
m = 0
pp = []
sp = 0
for i in range(n):
	if l[i] > m:
		m = l[i]
		m_index = i
ll = copy.deepcopy(l)
for x in range(n):
	m_index = x
	l = copy.deepcopy(ll)
	for i in range(m_index - 1, -1, -1):
		l[i] = min(l[i], l[i + 1])
	for j in range(m_index + 1, n):
		l[j] = min(l[j], l[j - 1])
	if sum(l) > sp:
		pp = l
		sp = sum(l)
print(*pp)


def exp(a, idx):
	n = len(a)
	b = [a[idx]]
	pr = a[idx]
	pl = a[idx]
	(l, r) = (idx - 1, idx + 1)
	ansB = b[0]
	while l >= 0 or r < n:
		if r < n:
			R = min(a[r], pr)
			b.append(R)
			pr = R
			r += 1
			ansB += R
		if l >= 0:
			L = min(a[l], pl)
			b = [L] + b
			pl = L
			l -= 1
			ansB += L
	return (ansB, b)

def main():
	n = int(input())
	a = list(map(int, input().split()))
	big = 0
	ans = []
	for i in range(n):
		(num, l) = exp(a, i)
		if num > big:
			big = num
			ans = l
	print(*ans)
main()


def skyscraper(arr):
	ans = []
	mx = 0
	for i in range(0, len(arr)):
		base = arr.copy()
		for l in range(i - 1, -1, -1):
			base[l] = min(base[l], base[l + 1])
		for r in range(i + 1, len(arr)):
			base[r] = min(base[r], base[r - 1])
		sm = sum(base)
		if sm > mx:
			ans = base.copy()
			mx = sm
	print(*ans)
	return ''
a = input()
lst = list(map(int, input().strip().split()))
print(skyscraper(lst))


def count_floor(arr, peak):
	ans = [arr[peak]]
	for i in range(peak + 1, len(arr)):
		if arr[i] <= ans[-1]:
			ans.append(arr[i])
		else:
			ans.append(ans[-1])
	i = peak - 1
	brr = [arr[peak]]
	while i >= 0:
		if arr[i] <= brr[-1]:
			brr.append(arr[i])
		else:
			brr.append(brr[-1])
		i -= 1
	brr.pop(0)
	return brr[::-1] + ans

def skycrapers(arr):
	max = 0
	ans_ = []
	for i in range(len(arr)):
		if max < sum(count_floor(arr, i)):
			max = sum(count_floor(arr, i))
			ans_ = count_floor(arr, i)
	an = [str(i) for i in ans_]
	return ' '.join(an)
n = int(input())
ar = list(map(int, input().strip().split()))
print(skycrapers(ar))


from sys import stdin, stdout
n = int(stdin.readline())
arr = [int(x) for x in input().split()]
sump = 0
final = []
for i in range(n):
	copy = [0] * n
	copy[i] = arr[i]
	for j in range(i - 1, -1, -1):
		if arr[j] > copy[j + 1]:
			copy[j] = copy[j + 1]
		else:
			copy[j] = arr[j]
	for j in range(i + 1, n):
		if arr[j] > copy[j - 1]:
			copy[j] = copy[j - 1]
		else:
			copy[j] = arr[j]
	if sump < sum(copy):
		sump = sum(copy)
		final = copy
print(*final)


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
ans = 0
ind = -1
for i in range(n):
	tmp = a[i]
	l = i
	r = i
	max_height = a[i]
	for j in range(0, l)[::-1]:
		if a[j] >= max_height:
			tmp += max_height
		else:
			tmp += a[j]
			max_height = a[j]
	max_height = a[i]
	for j in range(r + 1, n):
		if a[j] >= max_height:
			tmp += max_height
		else:
			tmp += a[j]
			max_height = a[j]
	if ans < tmp:
		ans = tmp
		ind = i
i = ind
ans = [0] * n
tmp = a[i]
l = i
r = i
max_height = a[i]
ans[i] = a[i]
for j in range(0, l)[::-1]:
	if a[j] >= max_height:
		tmp += max_height
	else:
		tmp += a[j]
		max_height = a[j]
	ans[j] = max_height
max_height = a[i]
for j in range(r + 1, n):
	if a[j] >= max_height:
		tmp += max_height
	else:
		tmp += a[j]
		max_height = a[j]
	ans[j] = max_height
print(*ans)


n = int(input())
a = list(map(int, input().split()))
b1 = [0] * n
for l in range(n):
	b = [0] * n
	(i, j) = (l - 1, l + 1)
	c = a[l]
	b[l] = a[l]
	while i != -1 or j != n:
		if j != n and (i == -1 or a[j] > a[i]):
			b[j] = min(a[j], c)
			c = b[j]
			j += 1
		else:
			b[i] = min(a[i], c)
			c = b[i]
			i -= 1
	if sum(b) > sum(b1):
		b1 = b[:]
print(*b1)


n = int(input())
a = list(map(int, input().split()))
ans = []
ma = 0
for j in range(n):
	an = [0] * n
	an[j] = a[j]
	for i in range(j - 1, -1, -1):
		if a[i] >= an[i + 1]:
			an[i] = an[i + 1]
		else:
			an[i] = a[i]
	for i in range(j + 1, n):
		if a[i] >= an[i - 1]:
			an[i] = an[i - 1]
		else:
			an[i] = a[i]
	s = sum(an)
	if s > ma:
		ind = j
		ma = s
	ans.append(an)
print(*ans[ind], sep=' ')


n = int(input())
al = [0] * n
ansl = [0] * n
ml = list(map(int, input().split()))
mx = 0
for i in range(n):
	al[i] = ml[i]
	j = i - 1
	while j >= 0:
		al[j] = min(al[j + 1], ml[j])
		j = j - 1
	j = i + 1
	while j < n:
		al[j] = min(al[j - 1], ml[j])
		j = j + 1
	cans = 0
	j = 0
	for j in range(n):
		cans += al[j]
	if cans > mx:
		mx = cans
		j = 0
		for j in range(n):
			ansl[j] = al[j]
for i in range(n - 1):
	print(ansl[i], end=' ')
print(ansl[-1])


from copy import copy
from math import *

def arr_float_inp():
	return [float(s) for s in input().split()]

def arr_int_inp():
	return [int(s) for s in input().split()]

def int_inp():
	return int(input())

def float_inp():
	return float(input())

def comp(a):
	return a[0]

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

def __starting_point():
	n = int_inp()
	arr = arr_int_inp()
	outs = []
	for j in range(len(arr)):
		max_el = (j, arr[j])
		new_arr = [0 for _ in range(len(arr))]
		new_arr[max_el[0]] = max_el[1]
		right = max_el[1]
		for i in range(max_el[0], -1, -1):
			new_arr[i] = min(arr[i], right)
			right = new_arr[i]
		left = max_el[1]
		for i in range(max_el[0], len(arr)):
			new_arr[i] = min(arr[i], left)
			left = new_arr[i]
		outs.append(new_arr)
	max_out = (sum(outs[0]), outs[0])
	for out in outs:
		new_sum = sum(out)
		if new_sum > max_out[0]:
			max_out = (new_sum, out)
	for el in max_out[1]:
		print(el, end=' ')
__starting_point()


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

def snip(i):
	if i > 0 and i < n - 1 and (arr[i - 1] >= arr[i] <= arr[i + 1]):
		return True
	if i == 0 or i == n - 1:
		return True
	return False
mounts = []
(i, j) = (0, 0)
for ind in range(n):
	if snip(ind):
		mounts.append([i, j])
		i = ind
		j = ind
	j += 1
maxval = -1
ans = []
for mount in mounts:
	(i, j) = mount
	new = arr.copy()
	middle = arr[i:j + 1]
	s = sum(middle)
	val = arr[j]
	prev = 0
	for ind in range(j + 1, n):
		new[ind] = min(new[ind], val)
		val = new[ind]
		prev += val
	val = arr[i]
	suff = 0
	for ind in range(i - 1, -1, -1):
		new[ind] = min(new[ind], val)
		val = new[ind]
		suff += val
	s += prev
	s += suff
	if maxval < s:
		maxval = s
		ans = new
print(' '.join(map(str, ans)))


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.setrecursionlimit(5 * 10 ** 5 + 5)

def nxt_smaller(a):
	stack = []
	ns = [-1] * len(a)
	for i in range(n):
		while len(stack) != 0 and stack[-1][0] > a[i]:
			curr = stack.pop()
			ns[curr[1]] = i
		stack.append([a[i], i])
	return ns

def prev_smaller(a):
	stack = []
	a.reverse()
	ns = [-1] * len(a)
	for i in range(n):
		while len(stack) != 0 and stack[-1][0] > a[i]:
			curr = stack.pop()
			ns[curr[1]] = n - i - 1
		stack.append([a[i], i])
	a.reverse()
	ns.reverse()
	return ns

def next(a, i):
	if nx[i] != -1:
		return nx[i]
	elif ns[i] == -1:
		nx[i] = (n - i) * a[i]
		return nx[i]
	else:
		nx[i] = (ns[i] - i) * a[i] + next(a, ns[i])
		return nx[i]

def prev(a, i):
	if pv[i] != -1:
		return pv[i]
	elif ps[i] == -1:
		pv[i] = (i + 1) * a[i]
		return pv[i]
	else:
		pv[i] = (i - ps[i]) * a[i] + prev(a, ps[i])
		return pv[i]
n = int(input())
a = [int(i) for i in input().split()]
ns = nxt_smaller(a)
ps = prev_smaller(a)
nx = [-1] * n
pv = [-1] * n
for i in range(n - 1, -1, -1):
	if nx[i] == -1:
		next(a, i)
for i in range(n):
	if pv[i] == -1:
		prev(a, i)
maxi = 0
ind = -1
for i in range(n):
	curr = nx[i] + pv[i] - a[i]
	if curr > maxi:
		maxi = curr
		ind = i
m = a[ind]
ans = [-1] * n
for i in range(ind, n):
	m = min(m, a[i])
	ans[i] = m
m = a[ind]
for i in range(ind - 1, -1, -1):
	m = min(m, a[i])
	ans[i] = m
print(*ans)


def solve(ns):
	n = len(ns)
	res = []
	maxv = -1
	for i in range(n):
		ms = ns[:]
		pre = ms[i]
		for j in range(i + 1, n):
			ms[j] = min(ms[j], pre)
			pre = ms[j]
		pre = ms[i]
		for j in range(i - 1, -1, -1):
			ms[j] = min(ms[j], pre)
			pre = ms[j]
		if sum(ms) > maxv:
			maxv = sum(ms)
			res = ms
	return res
n = int(input())
ns = list(map(int, input().split()))
print(' '.join(map(str, solve(ns))))


n = int(input())
l = list(map(int, input().split()))
ans = 0
p = -1
for i in range(n):
	cur = l[i]
	pre = l[i]
	for j in range(i + 1, n):
		cur += min(pre, l[j])
		pre = min(pre, l[j])
	pre = l[i]
	for j in range(i - 1, -1, -1):
		cur += min(pre, l[j])
		pre = min(pre, l[j])
	if cur > ans:
		ans = cur
		p = i
prev = l[p]
l1 = [0] * n
l1[p] = l[p]
for i in range(p + 1, n):
	prev = min(l[i], prev)
	l1[i] = prev
prev = l[p]
for i in range(p - 1, -1, -1):
	prev = min(l[i], prev)
	l1[i] = prev
for i in l1:
	print(i, end=' ')
print()


input()
arr = list(map(int, input().split()))
marr = []
for (i, limit) in enumerate(arr):
	cl = limit
	tarr = []
	for l in reversed(arr[:i]):
		cl = min(cl, l)
		tarr.append(cl)
	tarr.reverse()
	cl = limit
	for l in arr[i:]:
		cl = min(cl, l)
		tarr.append(cl)
	marr.append((tarr, sum(tarr)))
print(*max(marr, key=lambda x: x[1])[0])


import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
m = list(map(int, input().split()))

def solution(start, end, fill):
	if start > end:
		return (0, [])
	if start == end:
		return (m[start] - fill, [m[start] - fill])
	min_value = 1000000000
	min_index = []
	for i in range(start, end + 1):
		if m[i] < min_value:
			min_value = m[i]
			min_index = [i]
		elif m[i] == min_value:
			min_index.append(i)
	up_fill = start_saved = 0
	top_res = []
	start_index = start
	for index in min_index:
		(value, value2) = solution(start_index, index - 1, min_value)
		if value > up_fill:
			up_fill = value
			start_saved = start_index
			top_res = value2
		start_index = index + 1
	(value, value2) = solution(start_index, end, min_value)
	if value > up_fill:
		up_fill = value
		start_saved = start_index
		top_res = value2
	result = [min_value - fill for _ in range(end - start + 1)]
	for (i, v) in enumerate(top_res):
		result[start_saved - start + i] += v
	return ((min_value - fill) * (end - start + 1) + up_fill, result)
print(' '.join(map(str, solution(0, n - 1, 0)[1])))


n = int(input())
x = list(map(int, input().split()))
(ans, y) = ([0, 0], [0] * 1001)
for i in range(n):
	(lim, count) = (x[i], x[i])
	for i2 in range(i - 1, -1, -1):
		count += min(x[i2], lim)
		lim = min(lim, x[i2])
	lim = x[i]
	for i2 in range(i + 1, n):
		count += min(x[i2], lim)
		lim = min(lim, x[i2])
	if count > ans[0]:
		ans = [count, i]
p = ans[1]
lim = x[p]
y[p] = x[p]
for i2 in range(p - 1, -1, -1):
	y[i2] = min(x[i2], lim)
	lim = min(lim, x[i2])
lim = x[p]
for i2 in range(p + 1, n):
	y[i2] = min(x[i2], lim)
	lim = min(lim, x[i2])
for i in range(n):
	print(y[i], end=' ')


import math
import collections
import sys

def inpu():
	return input().split(' ')

def inti(a):
	for i in range(len(a)):
		a[i] = int(a[i])
	return a

def inp():
	a = inpu()
	a = inti(a)
	return a

def solution():
	N = int(input())
	A = list(map(int, input().split()))
	inc = [0] * N
	st = [(0, 1)]
	S = 0
	for (i, a) in enumerate(A):
		if a > st[-1][0]:
			S += a
			inc[i] = S
			st.append((a, 1))
		else:
			cnt = 1
			while a < st[-1][0]:
				(b, num) = st.pop()
				S -= b * num
				cnt += num
			S += cnt * a
			inc[i] = S
			st.append((a, cnt))
	dec = [0] * N
	st = [(0, 1)]
	S = 0
	for (i, a) in enumerate(reversed(A)):
		if a > st[-1][0]:
			S += a
			dec[i] = S
			st.append((a, 1))
		else:
			cnt = 1
			while a < st[-1][0]:
				(b, num) = st.pop()
				S -= b * num
				cnt += num
			S += cnt * a
			dec[i] = S
			st.append((a, cnt))
	dec.reverse()
	best = -1
	mid = -1
	for i in range(N - 1):
		if inc[i] + dec[i + 1] > best:
			mid = i
			best = inc[i] + dec[i + 1]
	ans = [0] * N
	ans[mid] = A[mid]
	prev = A[mid]
	for j in range(mid - 1, -1, -1):
		ans[j] = min(prev, A[j])
		prev = ans[j]
	mid += 1
	prev = A[mid]
	for j in range(mid, N):
		ans[j] = min(prev, A[j])
		prev = ans[j]
	print(*ans)
solution()


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
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))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

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

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

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

def power(x, y, p):
	y %= p - 1
	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 ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

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
inf = pow(10, 20)
mod = 10 ** 9 + 7

class RangeQuery:

	def __init__(self, data, func=min):
		self.func = func
		self._data = _data = [list(data)]
		(i, n) = (1, len(_data[0]))
		while 2 * i <= n:
			prev = _data[-1]
			_data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)])
			i <<= 1

	def query(self, start, stop):
		depth = (stop - start).bit_length() - 1
		return self.func(self._data[depth][start], self._data[depth][stop - (1 << depth)])
direction = {}

@iterative
def rec(l, r, a, rmq, pos):
	if l > r:
		yield 0
	mn = rmq.query(l, r + 1)
	ind = pos[mn][bl(pos[mn], l)]
	rightval = (yield rec(ind + 1, r, a, rmq, pos))
	leftval = (yield rec(l, ind - 1, a, rmq, pos))
	left = (ind - l + 1) * mn + rightval
	right = (r - ind + 1) * mn + leftval
	if left > right:
		direction[l, ind] = mn
	else:
		direction[ind, r] = mn
	yield max(left, right)

def solve(case):
	n = int(inp())
	a = lis()
	rmq = RangeQuery(a)
	pos = {}
	for i in range(n):
		if a[i] not in pos:
			pos[a[i]] = [i]
		else:
			pos[a[i]].append(i)
	for i in pos:
		pos[i].sort()
	rec(0, n - 1, a, rmq, pos)
	for i in direction:
		for j in range(i[0], i[1] + 1):
			a[j] = direction[i]
	print(' '.join((str(a[i]) for i in range(n))))
testcase(1)


def right_calculate_dp(dp_arr, m, curr_pos, curr_h_pos):
	if curr_pos == len(m):
		return 0
	if dp_arr[curr_pos][curr_h_pos][1] != 0:
		return dp_arr[curr_pos][curr_h_pos][1]
	else:
		if m[curr_h_pos] > m[curr_pos]:
			curr_h_pos = curr_pos
		dp_arr[curr_pos][curr_h_pos][1] = m[curr_h_pos] + right_calculate_dp(dp_arr, m, curr_pos + 1, curr_h_pos)
		return dp_arr[curr_pos][curr_h_pos][1]

def left_calculate_dp(dp_arr, m, curr_pos, curr_h_pos):
	if curr_pos < 0:
		return 0
	if dp_arr[curr_pos][curr_h_pos][0] != 0:
		return dp_arr[curr_pos][curr_h_pos][0]
	else:
		if m[curr_h_pos] > m[curr_pos]:
			curr_h_pos = curr_pos
		dp_arr[curr_pos][curr_h_pos][0] = m[curr_h_pos] + left_calculate_dp(dp_arr, m, curr_pos - 1, curr_h_pos)
		return dp_arr[curr_pos][curr_h_pos][0]
n = input()
m = input()
n = int(n)
m = m.split()
m = [int(i) for i in m]
dp_arr = [[[0, 0] for i in range(n)] for j in range(n)]
max_floors = 0
o_index = 0
for max_ind in range(n):
	n_floors = right_calculate_dp(dp_arr, m, max_ind, max_ind) + left_calculate_dp(dp_arr, m, max_ind, max_ind) - m[max_ind]
	if n_floors > max_floors:
		o_index = max_ind
		max_floors = n_floors
ind = o_index + 1
while ind != n:
	m[ind] = min(m[ind], m[ind - 1])
	ind = ind + 1
ind = o_index - 1
while ind >= 0:
	m[ind] = min(m[ind], m[ind + 1])
	ind = ind - 1
for i in m:
	print(i, end=' ')


def solve():
	n = int(input())
	m = list(map(int, input().split()))
	answ = [0] * n
	answc = 0
	for i in range(n):
		cur = [0] * n
		cur[i] = m[i]
		for j in range(i + 1, n):
			cur[j] = min(cur[j - 1], m[j])
		for j in range(i - 1, -1, -1):
			cur[j] = min(cur[j + 1], m[j])
		s = sum(cur)
		if s > answc:
			answc = s
			answ = cur
	print(*answ)
for i in range(1):
	solve()


n = int(input())
l = list(map(int, input().split()))
ind = 0
mx = max(l)
ans = -1
A1 = [0] * n
for ind in range(n):
	l1 = []
	l2 = []
	for i in range(ind - 1, -1, -1):
		l1.append(l[i])
	for i in range(ind, n):
		l2.append(l[i])
	curr = 1000000009
	a1 = []
	ans1 = 0
	for i in l1:
		if i < curr:
			curr = i
		ans1 += curr
		a1.append(curr)
	curr = 1000000009
	a2 = []
	ans2 = 0
	for i in l2:
		if i < curr:
			curr = i
		ans2 += curr
		a2.append(curr)
	ANS1 = ans1 + ans2
	if ans < ANS1:
		ans = ANS1
		x = 0
		a1.reverse()
		for i in a1:
			A1[x] = i
			x -= -1
		for i in a2:
			A1[x] = i
			x -= -1
print(*A1)


import sys
input = sys.stdin.readline
R = lambda : list(map(int, input().split()))
(n,) = R()
m = R()
a = [m[0]]
k = [0]
for i in range(1, n):
	if m[i - 1] <= m[i]:
		a.append(a[i - 1] + m[i])
	else:
		while k and m[k[-1]] >= m[i]:
			k.pop()
		if k:
			j = k[-1]
			a.append((i - j) * m[i] + a[j])
		else:
			a.append((i + 1) * m[i])
	k.append(i)
p = m[::-1]
b = [p[0]]
k = [0]
for i in range(1, n):
	if p[i - 1] <= p[i]:
		b.append(b[i - 1] + p[i])
	else:
		j = i - 1
		while k and p[k[-1]] >= p[i]:
			k.pop()
		if k:
			j = k[-1]
			b.append((i - j) * p[i] + b[j])
		else:
			b.append((i + 1) * p[i])
	k.append(i)
ans = 0
index = 0
for i in range(n):
	temp = a[i] + b[n - i - 1] - m[i]
	if temp > ans:
		ans = temp
		index = i
for i in range(index + 1, n):
	m[i] = min(m[i - 1], m[i])
for i in range(index - 1, -1, -1):
	m[i] = min(m[i + 1], m[i])
print(*m)


def inc(a):
	if not a:
		return []
	b = []
	x = a[-1]
	n = len(a)
	for i in range(n - 1, -1, -1):
		x = min(x, a[i])
		b.append(x)
	return b[::-1]

def dec(a):
	if not a:
		return []
	b = []
	x = a[0]
	n = len(a)
	for i in range(n):
		x = min(x, a[i])
		b.append(x)
	return b
n = int(input())
a = [int(i) for i in input().split()]
max_s = sum(dec(a))
ans = dec(a)
for i in range(n):
	x = inc(a[:i + 1])
	y = dec(a[i:])
	s = sum(x) + sum(y) - a[i]
	if s > max_s:
		max_s = s
		ans = x + y[1:]
for i in ans:
	print(i, end=' ', flush=False)
print()


n = int(input())
M = list(map(int, input().split()))
area1 = [M[0]] + [0] * (n - 1)
index = [0]
for i in range(1, n):
	if M[i] >= M[i - 1]:
		area1[i] = area1[i - 1] + M[i]
	else:
		while len(index) > 0 and M[i] < M[index[-1]]:
			index.pop()
		if len(index) == 0:
			area1[i] = (i + 1) * M[i]
		else:
			area1[i] = area1[index[-1]] + M[i] * (i - index[-1])
	index.append(i)
area2 = [0] * (n - 1) + [M[-1]]
index = [n - 1]
for i in range(n - 2, -1, -1):
	if M[i] >= M[i + 1]:
		area2[i] = area2[i + 1] + M[i]
	else:
		while len(index) > 0 and M[i] < M[index[-1]]:
			index.pop()
		if len(index) == 0:
			area2[i] = (n - i) * M[i]
		else:
			area2[i] = area2[index[-1]] + M[i] * (index[-1] - i)
	index.append(i)
I = -1
Max = 0
for i in range(n):
	area = area1[i] + area2[i] - M[i]
	if area > Max:
		Max = area
		I = i
for i in range(I - 1, -1, -1):
	if M[i] > M[i + 1]:
		M[i] = M[i + 1]
for i in range(I + 1, n):
	if M[i] > M[i - 1]:
		M[i] = M[i - 1]
print(' '.join(map(str, M)))


n = int(input())
arr = list(map(int, input().split()))
ans = []
mm = 0
for i in range(n):
	temp = arr[:]
	for j in range(i - 1, -1, -1):
		if temp[j] > temp[j + 1]:
			temp[j] = temp[j + 1]
	for j in range(i + 1, n):
		if temp[j] > temp[j - 1]:
			temp[j] = temp[j - 1]
	ss = sum(temp)
	if ss > mm:
		ans = temp[:]
		mm = ss
for i in ans:
	print(i, end=' ')
print()


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


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
n = int(input())
l = list(map(int, input().split()))
left = [0] * n
right = [0] * n
s = [-1]
for i in range(n - 1, -1, -1):
	while s[-1] != -1 and l[s[-1]] > l[i]:
		s.pop()
	if s[-1] == -1:
		right[i] = l[i] * (n - i)
	else:
		right[i] = right[s[-1]] + l[i] * (s[-1] - i)
	s.append(i)
s = [-1]
for i in range(n):
	while s[-1] != -1 and l[s[-1]] > l[i]:
		s.pop()
	if s[-1] == -1:
		left[i] = l[i] * (i + 1)
	else:
		left[i] = left[s[-1]] + l[i] * (i - s[-1])
	s.append(i)
m = 0
ind = -1
for i in range(n):
	Sum = left[i] + right[i] - l[i]
	if Sum > m:
		m = Sum
		ind = i
i = ind - 1
j = ind + 1
m1 = l[ind]
m2 = l[ind]
while i >= 0:
	if l[i] > m1:
		l[i] = m1
	else:
		m1 = l[i]
	i -= 1
while j < n:
	if l[j] > m2:
		l[j] = m2
	else:
		m2 = l[j]
	j += 1
print(*l)


n = int(input())
inf = int(2000000000.0) + 1
N = [-inf] + list(map(int, input().split())) + [-inf]
st1 = [0]
minRight = [0] * (n + 2)
st2 = [0]
minLeft = [0] * (n + 2)
for i in range(1, n + 2):
	while N[i] < N[st1[-1]]:
		minRight[st1.pop()] = i - 1
	st1.append(i)
	while N[n + 1 - i] < N[n + 1 - st2[-1]]:
		minLeft[n + 1 - st2.pop()] = n - i
	st2.append(i)
N.pop(0)
N.pop(-1)
minLeft.pop(0)
minLeft.pop(-1)
minRight.pop(0)
minRight.pop(-1)
Rminsum = [0] * (n + 1)
Lminsum = [0] * (n + 1)
for i in range(n - 1, -1, -1):
	min_el = minRight[i]
	Rminsum[i] = Rminsum[min_el] + N[i] * (min_el - i)
for i in range(0, n):
	min_el = minLeft[i]
	Lminsum[i] = Lminsum[min_el] + N[i] * (i - min_el)
res = 0
best_i = 0
for i in range(0, n):
	new = Lminsum[i] + Rminsum[i] - N[i]
	if new > res:
		res = new
		best_i = i
answer = [0] * n
answer[best_i] = N[best_i]
for i in range(best_i - 1, -1, -1):
	answer[i] = min(answer[i + 1], N[i])
for i in range(best_i + 1, n):
	answer[i] = min(answer[i - 1], N[i])
for i in range(n):
	print(answer[i], end=' ')


def do_solve(ns):
	n = len(ns)
	st = []
	tot = []
	res = []
	for (i, num) in enumerate(ns):
		while st and ns[st[-1]] >= num:
			st.pop()
			tot.pop()
		pre = 0 if not tot else tot[-1]
		length = i + 1 if not st else i - st[-1]
		tot.append(pre + ns[i] * length)
		st.append(i)
		res.append(tot[-1])
	return (res, st)

def solve(ns):
	n = len(ns)
	if len(ns) == 1:
		return ns
	(a, _) = do_solve(ns)
	(b, _) = do_solve(ns[::-1])
	b = b[::-1]
	maxi = -1
	maxp = -1
	for i in range(1, n):
		u = a[i - 1] + b[i]
		if u > maxi:
			maxi = u
			maxp = i
	(_, a) = do_solve(ns[:maxp])
	(_, b) = do_solve(ns[maxp:][::-1])
	res1 = []
	res2 = []
	p = 0
	for i in range(maxp):
		if i <= a[p]:
			res1.append(ns[a[p]])
			if i == a[p]:
				p += 1
	p = 0
	ns = ns[::-1]
	for i in range(n - maxp):
		if i <= b[p]:
			res2.append(ns[b[p]])
			if i == b[p]:
				p += 1
	res = res1 + res2[::-1]
	return res
assert solve([1, 2]) == [1, 2]
assert solve([2, 1]) == [2, 1]
assert solve([1]) == [1]
assert solve([1, 6, 4, 5, 1]) == [1, 6, 4, 4, 1]
assert solve([1, 5, 4, 3, 1]) == [1, 5, 4, 3, 1]
assert solve([1, 4, 4, 3, 1]) == [1, 4, 4, 3, 1]
assert solve([1, 2, 4, 3, 1]) == [1, 2, 4, 3, 1]
assert solve([1, 2, 2, 3, 1]) == [1, 2, 2, 3, 1]
assert solve([10, 6, 8]) == [10, 6, 6]
assert solve([1, 2, 1, 2, 1]) == [1, 2, 1, 1, 1]
assert solve([1, 2, 3, 2, 1]) == [1, 2, 3, 2, 1]
n = int(input())
ns = list(map(int, input().split()))
print(' '.join(map(str, solve(ns))))


def answer(max_floors):
	best_solution = [0] * len(max_floors)
	best_solution_sum = 0
	for i in range(0, len(max_floors)):
		solution = max_floors.copy()
		for j in range(i - 1, -1, -1):
			solution[j] = min(max_floors[j], solution[j + 1])
		for j in range(i + 1, len(max_floors)):
			solution[j] = min(max_floors[j], solution[j - 1])
		solution_sum = sum(solution)
		if solution_sum > best_solution_sum:
			best_solution_sum = solution_sum
			best_solution = solution
	return ' '.join(map(str, best_solution))
plot = int(input())
print(answer(list(map(int, input().split()))))


n = int(input())
a = list(map(int, input().split()))
m = -1
mb = []
for mi in range(n):
	b = [-1] * n
	b[mi] = a[mi]
	for i in range(mi - 1, -1, -1):
		if a[i] > b[i + 1]:
			b[i] = b[i + 1]
		else:
			b[i] = a[i]
	for i in range(mi + 1, n):
		if a[i] > b[i - 1]:
			b[i] = b[i - 1]
		else:
			b[i] = a[i]
	if m < sum(b):
		m = sum(b)
		mb = b
print(*mb)


n = int(input())
m = list(map(int, input().split()))
state = []
sm = 0
for i in range(n):
	tmp = m[:]
	for j in range(i - 1, -1, -1):
		tmp[j] = min(tmp[j], tmp[j + 1])
	for j in range(i + 1, n):
		tmp[j] = min(tmp[j], tmp[j - 1])
	tm = sum(tmp)
	if tm > sm:
		sm = tm
		state = tmp
print(*state)


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
sta1 = []
dp1 = [0] * (N + 1)
for (i, a) in enumerate(A):
	count = 0
	mustscore = dp1[i]
	while sta1 and sta1[-1][0] > a:
		(s, num) = sta1.pop()
		count += num
		mustscore += (s - a) * num
	dp1[i + 1] = mustscore
	sta1.append((a, count + 1))
sta2 = []
dp2 = [0] * (N + 1)
for i in reversed(range(N)):
	a = A[i]
	count = 0
	mustscore = dp2[i + 1]
	while sta2 and sta2[-1][0] > a:
		(s, num) = sta2.pop()
		count += num
		mustscore += (s - a) * num
	dp2[i] = mustscore
	sta2.append((a, count + 1))
PA = 10 ** 17
ind = -1
for n in range(N):
	p = dp1[n + 1] + dp2[n]
	if p < PA:
		PA = p
		ind = n
ans = A[:]
for i in range(ind + 1, N):
	A[i] = min(A[i - 1], A[i])
for i in reversed(range(ind)):
	A[i] = min(A[i], A[i + 1])
print(*A)


t = int(input())
mlist = list(map(int, input().split()))
mlist2 = [-1]
mlist2.extend(mlist)
mlist2.append(-1)
mlist = mlist2.copy()
peaks = []
for m in range(1, t + 1):
	if mlist[m] >= mlist[m - 1] and mlist[m] >= mlist[m + 1]:
		peaks.append(m)
if len(peaks) == 1:
	for i in range(1, t + 1):
		print(mlist[i], end=' ')
else:
	sumli = []
	for p in peaks:
		lesindex = p - 1
		greindex = p + 1
		sumo = mlist[p]
		still = mlist[p]
		while lesindex >= 1:
			still = min(mlist[lesindex], still)
			sumo += min(mlist[lesindex], still)
			lesindex -= 1
		still = mlist[p]
		while greindex <= t:
			still = min(mlist[greindex], still)
			sumo += min(mlist[greindex], still)
			greindex += 1
		sumli.append(sumo)
	maxindex = 0
	for i in range(len(sumli)):
		if sumli[i] > sumli[maxindex]:
			maxindex = i
	peakind = peaks[maxindex]
	lesindex = peakind - 1
	greindex = peakind + 1
	still = mlist[peakind]
	mlist2[peakind] = mlist[peakind]
	while lesindex >= 1:
		still = min(mlist[lesindex], still)
		mlist2[lesindex] = min(mlist[lesindex], still)
		lesindex -= 1
	still = mlist[peakind]
	while greindex <= t:
		still = min(mlist[greindex], still)
		mlist2[greindex] = min(mlist[greindex], still)
		greindex += 1
	for i in range(1, t + 1):
		print(mlist2[i], end=' ')


import sys

def getAr(o, n):
	stk = list()
	ans = [0] * n
	ans[0] = o[0]
	stk.append(0)
	for i in range(1, n):
		while len(stk) != 0 and o[stk[-1]] >= o[i]:
			stk.pop()
		multi = None
		if len(stk) > 0:
			ans[i] = ans[stk[-1]]
			multi = i - stk[-1]
		else:
			multi = i + 1
		ans[i] += multi * o[i]
		stk.append(i)
	return ans
n = int(input())
ar = list(map(int, input().split()))
lAr = getAr(ar, n)
rAr = getAr(ar[::-1], n)[::-1]
maxIdx = 0
for i in range(n):
	if lAr[i] + rAr[i] - ar[i] > lAr[maxIdx] + rAr[maxIdx] - ar[maxIdx]:
		maxIdx = i
ans = [0] * n
ans[maxIdx] = ar[maxIdx]
if maxIdx > 0:
	ans[maxIdx - 1] = ar[maxIdx - 1]
	for i in range(maxIdx - 2, -1, -1):
		if ar[i] > ans[i + 1]:
			ans[i] = ans[i + 1]
		else:
			ans[i] = ar[i]
if maxIdx < n - 1:
	ans[maxIdx + 1] = ar[maxIdx + 1]
	for i in range(maxIdx + 2, n):
		if ar[i] > ans[i - 1]:
			ans[i] = ans[i - 1]
		else:
			ans[i] = ar[i]
print(' '.join([str(x) for x in ans]))


n = int(input())
num = []
s = input().split()
nu = 0
for k in s:
	j = int(k)
	num.append(j)
for i1 in range(n):
	su = 0
	numd = [0 for i in range(n)]
	if i1 == 0:
		numd[i1] = num[i1]
		su += numd[i1]
		for jj in range(i1 + 1, n):
			if num[jj] > numd[jj - 1]:
				numd[jj] = numd[jj - 1]
				su += numd[jj]
			else:
				numd[jj] = num[jj]
				su += numd[jj]
		if su > nu:
			nu = su
			kt = numd[:]
	elif i1 == n - 1:
		numd[i1] = num[i1]
		su += numd[i1]
		for jj in range(n - 2, -1, -1):
			if num[jj] > numd[jj + 1]:
				numd[jj] = numd[jj + 1]
				su += numd[jj]
			else:
				numd[jj] = num[jj]
				su += numd[jj]
		if su > nu:
			nu = su
			kt = numd[:]
	else:
		numd[i1] = num[i1]
		su += numd[i1]
		for jj in range(i1 - 1, -1, -1):
			if num[jj] > numd[jj + 1]:
				numd[jj] = numd[jj + 1]
				su += numd[jj]
			else:
				numd[jj] = num[jj]
				su += numd[jj]
		for jj in range(i1 + 1, n):
			if num[jj] > numd[jj - 1]:
				numd[jj] = numd[jj - 1]
				su += numd[jj]
			else:
				numd[jj] = num[jj]
				su += numd[jj]
		if su > nu:
			nu = su
			kt = numd[:]
for i in kt:
	print(i, end=' ')
print()


from collections import defaultdict, Counter
from functools import lru_cache
from heapq import heappush, heappop
from math import gcd, floor, ceil
from sys import stdin, stdout

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

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

def solve(m):
	n = len(m)
	res = [0] * n
	maxi = 0
	for i in range(n):
		temp = [0] * n
		temp[i] = m[i]
		for j in range(i - 1, -1, -1):
			temp[j] = min(temp[j + 1], m[j])
		for k in range(i + 1, n):
			temp[k] = min(temp[k - 1], m[k])
		if sum(temp) > maxi:
			maxi = sum(temp)
			res = temp[:]
	print(' '.join([str(i) for i in res]))
n = iint()
m = ilist()
solve(m)


import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = [0] * N
for i in range(N):
	t = [0] * N
	t[i] = a[i]
	for j in range(i, 0, -1):
		t[j - 1] = min(t[j], a[j - 1])
	for j in range(i, N - 1):
		t[j + 1] = min(t[j], a[j + 1])
	if sum(t) > sum(res):
		res = t
print(*res)


n = int(input())
m = list(map(int, input().split()))
ans = []
acval = 0
for peak in range(n):
	lans = [0] * n
	val = 0
	prevmax = m[peak]
	l = peak
	while l >= 0:
		if m[l] >= prevmax:
			val += prevmax
			lans[l] = prevmax
		else:
			prevmax = m[l]
			val += m[l]
			lans[l] = m[l]
		l -= 1
	r = peak + 1
	prevmax = m[peak]
	while r < n:
		if prevmax <= m[r]:
			val += prevmax
			lans[r] = prevmax
		else:
			prevmax = m[r]
			val += prevmax
			lans[r] = prevmax
		r += 1
	if acval < val:
		ans = list(lans)
		acval = val
print(*ans)


n = int(input())
inf = int(2000000000.0) + 1
m = [-inf] + [int(x) for x in input().split()] + [-inf]
r = [0] * (n + 2)
st = [0]
for i in range(1, n + 2):
	while m[st[-1]] > m[i]:
		r[st.pop()] = i
	st.append(i)
l = [0] * (n + 2)
st = [0]
for i in range(n + 1, 0, -1):
	while m[st[-1]] > m[i]:
		l[st.pop()] = i
	st.append(i)
a = [0]
for i in range(1, n + 1):
	a.append(a[l[i]] + m[i] * (i - l[i]))
b = [0] * (n + 2)
for i in range(n, 0, -1):
	b[i] = b[r[i]] + m[i] * (r[i] - i)
p = -inf
for i in range(n + 1):
	t = a[i] + b[i + 1]
	if t > p:
		q = i
		p = t
for i in range(q - 1, 0, -1):
	m[i] = min(m[i], m[i + 1])
q += 1
for i in range(q + 1, n + 1):
	m[i] = min(m[i], m[i - 1])
for i in range(1, n + 1):
	print(m[i], end=' ')


n = int(input())
a = [int(i) for i in input().split()]
a = [0] + a + [0]
sx = 0
k = 0
for i in range(1, n + 1):
	if a[i] > a[i - 1] and a[i] >= a[i + 1] or (a[i] >= a[i - 1] and a[i] > a[i + 1]):
		s = a[i]
		mx = a[i]
		b = [s]
		for j in range(i - 1, 0, -1):
			mx = min(a[j], mx)
			s += mx
			b = [mx] + b
		mx = a[i]
		for j in range(i + 1, n + 1):
			mx = min(a[j], mx)
			s += mx
			b = b + [mx]
		if s > sx:
			sx = s
			c = b
	else:
		continue
for i in range(n):
	print(c[i], '', end='')


n = int(input())
b = list(map(int, input().split()))
best_ans = None
best_sum = 0
for i in range(n):
	ans = b[:]
	for j in range(i + 1, n):
		ans[j] = min(ans[j], ans[j - 1])
	for j in range(i - 1, -1, -1):
		ans[j] = min(ans[j], ans[j + 1])
	s = sum(ans)
	if s > best_sum:
		best_sum = s
		best_ans = ans
for i in best_ans:
	print(i, end=' ')


n = int(input())
m = list(map(int, input().split()))
sol = -1
sol2 = []
for i in range(len(m)):
	possible = m[i]
	k = m[i]
	current = [k]
	for j in range(i - 1, -1, -1):
		k = min(k, m[j])
		possible += k
		current.insert(0, k)
	k = m[i]
	for j in range(i + 1, len(m)):
		k = min(k, m[j])
		possible += k
		current.append(k)
	if possible > sol:
		sol = possible
		sol2 = current
print(' '.join(map(str, sol2)))


import math
n = int(input())
ar = list(map(int, input().split()))
ans = 0
for i in range(n):
	a = ar[:]
	s = 0
	m = a[i]
	for j in range(i, n - 1):
		if a[j] < a[j + 1]:
			a[j + 1] = a[j]
	for k in range(i, 0, -1):
		if a[k] < a[k - 1]:
			a[k - 1] = a[k]
	if ans < sum(a):
		an = a[:]
	ans = max(ans, sum(a))
print(*an)


n = int(input())
m = list(map(int, input().split()))
(ans, idx) = (0, -1)
for i in range(n):
	a = [0] * n
	a[i] = m[i]
	for j in range(i - 1, -1, -1):
		a[j] = min(m[j], a[j + 1])
	for j in range(i + 1, n):
		a[j] = min(m[j], a[j - 1])
	tot = sum(a)
	if tot > ans:
		ans = tot
		idx = i
a = [0] * n
a[idx] = m[idx]
for j in range(idx - 1, -1, -1):
	a[j] = min(m[j], a[j + 1])
for j in range(idx + 1, n):
	a[j] = min(m[j], a[j - 1])
print(*a)


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


inf = float('inf')
n = int(input())
a = [*map(int, input().split())]
for x in (0, 1):
	stack = [-1]
	s = [0] * n
	for i in range(n - 1, -1, -1):
		while a[i] < a[stack[-1]] and stack[-1] != -1:
			stack.pop()
		s[i] = (n - i) * a[i] if stack[-1] == -1 else s[stack[-1]] + a[i] * (stack[-1] - i)
		stack += [i]
	a = a[::-1]
	if x:
		ri = s[:][::-1]
	else:
		le = s[:]
s = [i + j - k for (i, j, k) in zip(le, ri, a)]
x = s.index(max(s))
for i in range(x + 1, n):
	a[i] = min(a[i], a[i - 1])
for i in range(x - 1, -1, -1):
	a[i] = min(a[i], a[i + 1])
print(*a)


n = int(input())
l = list(map(int, input().split()))
(ans, final) = (0, [])
for i in range(n):
	f = l[:]
	s = l[i]
	for j in range(i - 1, -1, -1):
		f[j] = min(f[j], f[j + 1])
		s += f[j]
	for j in range(i + 1, n):
		f[j] = min(f[j], f[j - 1])
		s += f[j]
	if s > ans:
		ans = s
		final = f[:]
print(*final)


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	M = [int(x) for x in input().split()]
	ans = float('inf')
	ansi = 0
	for i in range(N):
		tmp = 0
		mi = M[i]
		for j in range(i + 1, N):
			if M[j] > mi:
				tmp += M[j] - mi
			else:
				mi = M[j]
		mi = M[i]
		for j in range(i - 1, -1, -1):
			if M[j] > mi:
				tmp += M[j] - mi
			else:
				mi = M[j]
		tmpans = ans
		ans = min(ans, tmp)
		if ans != tmpans:
			ansi = i
	mi = M[ansi]
	for j in range(ansi + 1, N):
		if M[j] > mi:
			M[j] = mi
		else:
			mi = M[j]
	mi = M[ansi]
	for j in range(ansi - 1, -1, -1):
		if M[j] > mi:
			M[j] = mi
		else:
			mi = M[j]
	print(*M)
main()


n = int(input())
m = [int(x) for x in input().split()]
a = [0] * n
a[0] = m[0]
for i in range(1, n):
	value = m[i]
	for j in range(i - 1, -1, -1):
		if m[j] > m[i]:
			value += m[i]
		else:
			value += a[j]
			break
	a[i] = value
m.reverse()
b = [0] * n
b[0] = m[0]
for i in range(1, n):
	value = m[i]
	for j in range(i - 1, -1, -1):
		if m[j] > m[i]:
			value += m[i]
		else:
			value += b[j]
			break
	b[i] = value
b.reverse()
m.reverse()
ans = 0
index = 0
for i in range(n):
	temp = a[i] + b[i] - m[i]
	if temp > ans:
		ans = temp
		index = i
ans = [0] * n
ans[index] = m[index]
for i in range(index + 1, n):
	ans[i] = min(ans[i - 1], m[i])
for i in range(index - 1, -1, -1):
	ans[i] = min(ans[i + 1], m[i])
print(' '.join(map(str, ans)))


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

def summ(k):
	global n
	global a
	b = [0] * n
	b[k] = a[k]
	i = k - 1
	while i >= 0:
		b[i] = min(a[i], b[i + 1])
		i -= 1
	i = k + 1
	while i <= n - 1:
		b[i] = min(a[i], b[i - 1])
		i += 1
	return sum(b)
anss = []
for i in range(n):
	anss.append(summ(i))
j = anss.index(max(anss))
b = [0] * n
b[j] = a[j]
i = j - 1
while i >= 0:
	b[i] = min(a[i], b[i + 1])
	i -= 1
i = j + 1
while i <= n - 1:
	b[i] = min(a[i], b[i - 1])
	i += 1
for x in b:
	print(x, end=' ')
print()


n = int(input())
arr = list(map(int, input().split()))
final = []
cnt = -1
for i in range(n):
	ans = [0 for i in range(n)]
	ans[i] = arr[i]
	for j in range(i + 1, n):
		ans[j] = min(arr[j], ans[j - 1])
	for j in range(i - 1, -1, -1):
		ans[j] = min(arr[j], ans[j + 1])
	if cnt < sum(ans):
		final = ans[:]
		cnt = sum(ans)
print(*final)


from sys import stdin, stdout
import traceback
read = stdin.readline
write = stdout.write
writeln = lambda x: write(str(x) + '\n')

def reads(typ=int):
	return list(map(typ, read().split()))

class Sol:

	def __init__(self, n, ms):
		self.n = n
		self.ms = ms

	@classmethod
	def input(cls):
		s = cls(int(read()), reads())
		return s

	def solve(self):
		ret = self.ms
		ls = self.construct_ls(self.ms)
		rs = self.construct_ls(self.ms[::-1])[::-1]
		sums = [ls[i] + rs[i] - self.ms[i] for i in range(self.n)]
		ii = sums.index(max(sums))
		self.go_down(ii, ret)
		self.go_down_right(ii, ret)
		return ret

	def go_down(self, ii, ret):
		lim = self.ms[ii]
		i = ii
		while i >= 0:
			lim = min(lim, self.ms[i])
			ret[i] = lim
			i -= 1

	def go_down_right(self, ii, ret):
		lim = self.ms[ii]
		i = ii
		while i < self.n:
			lim = min(lim, self.ms[i])
			ret[i] = lim
			i += 1

	def construct_ls(self, ms):
		ret = [-1 for _ in range(self.n)]
		floors = []
		cur_sum = 0
		for i in range(self.n):
			mi = ms[i]
			num_mi = 1
			while floors and floors[-1] >= (mi, 1):
				(h, num) = floors.pop()
				num_mi += num
				cur_sum -= (h - mi) * num
			floors.append((mi, num_mi))
			cur_sum += mi
			ret[i] = cur_sum
		return ret
try:
	writeln(' '.join([str(x) for x in Sol.input().solve()]))
except Exception as e:
	print('Got exception:', repr(e))
	print(traceback.format_exc())


def f():
	n = int(input())
	M = [int(s) for s in input().split()]

	def goLeftFrom(s):
		sum = 0
		cur = M[s]
		for i in range(s - 1, -1, -1):
			cur = min(cur, M[i])
			sum += cur
		return sum

	def goRightFrom(s):
		sum = 0
		cur = M[s]
		for i in range(s + 1, n):
			cur = min(cur, M[i])
			sum += cur
		return sum
	ans = 0
	choose = -1
	for i in range(n):
		l = goLeftFrom(i)
		r = goRightFrom(i)
		if l + r + M[i] > ans:
			choose = i
			ans = l + r + M[i]
	rt = [0] * n
	s = choose
	rt[s] = M[s]
	cur = M[s]
	for i in range(s - 1, -1, -1):
		cur = min(cur, M[i])
		rt[i] = cur
	cur = M[s]
	for i in range(s + 1, n):
		cur = min(cur, M[i])
		rt[i] = cur
	print(' '.join((str(num) for num in rt)))
f()


n = int(input())
m = list(map(int, input().split()))
ans = 0
Ans = []
for i in range(n):
	test = [0] * n
	test[i] = m[i]
	for j in range(i - 1, -1, -1):
		test[j] = min(test[j + 1], m[j])
	for j in range(i + 1, n):
		test[j] = min(test[j - 1], m[j])
	res = sum(test)
	if res > ans:
		ans = res
		Ans = test
print(*Ans)


import sys
input = sys.stdin.readline
n = int(input())
m = [int(i) for i in input().split()]
copy = m[:]

def maxsumchecker(arr):
	(stack, lr) = ([], [0] * n)
	for i in range(n):
		while stack and stack[-1][1] > m[i]:
			stack.pop()
		if stack:
			j = stack[-1][0]
			lr[i] = lr[j] + (i - j) * m[i]
		else:
			lr[i] = (i + 1) * m[i]
		stack.append([i, m[i]])
	return lr
l = maxsumchecker(m)
r = maxsumchecker(m.reverse())
r.reverse()
(best, index) = (0, 0)
for i in range(n):
	if l[i] + r[i] - copy[i] > best:
		best = l[i] + r[i] - copy[i]
		index = i
l1 = [0] * n
l1[index] = copy[index]
(i, j) = (index - 1, index + 1)
while i >= 0:
	l1[i] = min(l1[i + 1], copy[i])
	i -= 1
while j < n:
	l1[j] = min(l1[j - 1], copy[j])
	j += 1
print(*l1)


n = int(input())
arr = list(map(int, input().split()))
ans = []
s = 0
for j in range(n):
	mx = arr[j]
	ans1 = []
	ans2 = []
	idx = j
	mi = mx
	for i in range(idx - 1, -1, -1):
		mi = min(mi, arr[i])
		ans1 = [mi] + ans1
	mi = mx
	for i in range(idx + 1, n):
		mi = min(mi, arr[i])
		ans2.append(mi)
	ts = mx + sum(ans1) + sum(ans2)
	if ts > s:
		s = ts
		ans = ans1 + [mx] + ans2
print(*ans)


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 math import gcd, ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

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

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from collections import deque
for _ in range(int(input()) if not True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	queue = []
	leftmost = [-1] * n
	for i in range(n - 1, -1, -1):
		while queue:
			if a[queue[-1]] > a[i]:
				leftmost[queue.pop()] = i
			else:
				break
		queue += [i]
	queue = []
	rightmost = [-1] * n
	for i in range(n):
		while queue:
			if a[queue[-1]] > a[i]:
				rightmost[queue.pop()] = i
			else:
				break
		queue += [i]
	pre = [0] * (n + 1)
	pre[1] = a[0]
	prelink = [0] * (n + 1)
	for i in range(1, n):
		if a[i] >= a[i - 1]:
			pre[i + 1] = pre[i] + a[i]
			prelink[i + 1] = i
		else:
			lmai = leftmost[i]
			pre[i + 1] = pre[lmai + 1] + a[i] * (i - lmai)
			prelink[i + 1] = lmai + 1
	suf = [0] * (n + 1)
	suf[1] = a[-1]
	suflink = [0] * (n + 1)
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1]:
			suf[n - 1 - i + 1] = suf[n - 1 - i] + a[i]
			suflink[n - 1 - i + 1] = n - 1 - i
		else:
			rmai = rightmost[i]
			suf[n - 1 - i + 1] = suf[n - rmai if rmai != -1 else 0] + a[i] * (rmai - i if rmai != -1 else n - i)
			suflink[n - 1 - i + 1] = n - rmai if rmai != -1 else 0
	mx = 0
	val = 0
	for i in range(0, n + 1):
		if pre[i] + suf[n - i] > mx:
			mx = pre[i] + suf[n - i]
			val = i
	oof = [val]
	while oof[-1] != 0:
		oof += [prelink[oof[-1]]]
	oof = oof[::-1]
	ans = []
	for i in range(1, len(oof)):
		ans += [a[oof[i] - 1]] * (oof[i] - oof[i - 1])
	val = n - val
	oof = [val]
	while oof[-1] != 0:
		oof += [suflink[oof[-1]]]
	oof = oof[::-1]
	ans2 = []
	for i in range(1, len(oof)):
		ans2 += [a[n - oof[i]]] * (oof[i] - oof[i - 1])
	print(*ans + ans2[::-1])


def go():
	n = int(input())
	a = list(map(int, input().split()))
	cand = [0] * n
	mx = -1
	bs = []
	for i in range(n):
		cand[i] = a[i]
		for j in range(i + 1, n):
			cand[j] = min(cand[j - 1], a[j])
		for j in range(i - 1, -1, -1):
			cand[j] = min(cand[j + 1], a[j])
		s = sum(cand)
		if s > mx:
			mx = s
			bs = cand.copy()
	print(*bs)
go()


n = int(input())
m = [int(i) for i in input().split()]
ready_solution = []
max_floors = 0
for top in range(n):
	copy_of_the_m = m.copy()
	candidate = 0
	for left in range(top - 1, -1, -1):
		if copy_of_the_m[left] <= copy_of_the_m[left + 1]:
			candidate += copy_of_the_m[left]
		else:
			candidate += copy_of_the_m[left + 1]
			copy_of_the_m[left] = copy_of_the_m[left + 1]
	for right in range(top + 1, len(m)):
		if copy_of_the_m[right] <= copy_of_the_m[right - 1]:
			candidate += copy_of_the_m[right]
		else:
			candidate += copy_of_the_m[right - 1]
			copy_of_the_m[right] = copy_of_the_m[right - 1]
	candidate += copy_of_the_m[top]
	if candidate > max_floors:
		max_floors = candidate
		ready_solution = copy_of_the_m.copy()
print(*ready_solution)


n = int(input())
a = list(map(int, input().split()))
fans = 0
height = [0] * n
ma = []
for m in range(n):
	minh = 0
	thisans = 0
	np = False
	height[m] = a[m]
	for i in range(m - 1, -1, -1):
		height[i] = height[i + 1]
		if height[i] > a[i]:
			height[i] = a[i]
	for i in range(m + 1, n):
		height[i] = height[i - 1]
		if height[i] > a[i]:
			height[i] = a[i]
	thisans = sum(height)
	if thisans > fans:
		ma = height.copy()
		fans = thisans
print(*list(ma))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
m = list(map(int, input().split()))
score = 0
lis = None
for i in range(n):
	tmp = [0] * n
	tmp[i] = m[i]
	left = i - 1
	while left >= 0:
		tmp[left] = min(m[left], tmp[left + 1])
		left -= 1
	right = i + 1
	while right < n:
		tmp[right] = min(m[right], tmp[right - 1])
		right += 1
	s = sum(tmp)
	if s > score:
		score = s
		lis = tmp
print(*lis)


def examA():
	T = I()
	ans = []
	for _ in range(T):
		A = LI()
		A.sort()
		if A[1] == 0:
			if A[2] == 0:
				cur = 0
			else:
				cur = 1
		elif A[0] == 0:
			if A[1] == 1:
				cur = 2
			else:
				cur = 3
		else:
			cur = 3
			if A[0] == 3:
				cur += 3
			elif A[0] > 3:
				cur += 4
			elif A[0] == 1:
				if A[1] >= 2:
					cur += 1
			elif A[2] == 2:
				cur += 1
			else:
				cur += 2
		ans.append(cur)
	for v in ans:
		print(v)
	return

def examB():
	ans = 0
	print(ans)
	return

def examC():
	N = I()
	A = LI()
	ans = 0
	keep = []
	for i in range(N):
		cur = [0] * N
		top = A[i]
		cur[i] = top
		for l in range(i)[::-1]:
			now = min(A[l], top)
			cur[l] = now
			top = now
		top = A[i]
		for r in range(i + 1, N):
			now = min(A[r], top)
			cur[r] = now
			top = now
		S = sum(cur)
		if S > ans:
			keep = cur
			ans = S
	print(' '.join(map(str, keep)))
	return

def examD():
	ans = 0
	print(ans)
	return

def examE():
	ans = 0
	print(ans)
	return

def examF():
	ans = 0
	print(ans)
	return
import sys, copy, bisect, itertools, heapq, math, random
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque

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

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

def LSI():
	return list(map(str, sys.stdin.readline().split()))

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

def SI():
	return sys.stdin.readline().strip()
global mod, mod2, inf, alphabet, _ep
mod = 10 ** 9 + 7
mod2 = 998244353
inf = 10 ** 18
_ep = 10 ** (-12)
alphabet = [chr(ord('a') + i) for i in range(26)]
examC()


N = int(input())
m = list(map(int, input().split()))
ans = 0

def sol(k):
	ret = l = r = m[k]
	for i in range(k + 1, N):
		if m[i] < r:
			r = m[i]
		ret += r
	for i in range(k - 1, -1, -1):
		if m[i] < l:
			l = m[i]
		ret += l
	return ret
ans_idx = -1
for i in range(N):
	X = sol(i)
	if ans < X:
		ans = X
		ans_idx = i
l = r = m[ans_idx]
for i in range(ans_idx + 1, N):
	if m[i] < r:
		r = m[i]
	else:
		m[i] = r
for i in range(ans_idx - 1, -1, -1):
	if m[i] < l:
		l = m[i]
	else:
		m[i] = l
print(*m)


n = int(input())
a = [int(x) for x in input().split()]
best = -1
bestV = -1
bests = []
for peak in range(n):
	currMin = a[peak]
	total = currMin
	newX = [0] * n
	newX[peak] = currMin
	for i in range(peak - 1, -1, -1):
		currMin = min(a[i], currMin)
		newX[i] = currMin
		total += currMin
	currMin = a[peak]
	for i in range(peak + 1, n):
		currMin = min(a[i], currMin)
		newX[i] = currMin
		total += currMin
	if total > bestV:
		bestV = total
		best = peak
	bests.append(newX)
print(*bests[best])


n = int(input())
arr = list(map(int, input().split()))
tmp = []
max_s = -10 ** 10
for i in range(n):
	ans = [0 for i in range(n)]
	ans[i] = arr[i]
	for j in range(i + 1, n):
		ans[j] = min(arr[j], ans[j - 1])
	for j in range(i - 1, -1, -1):
		ans[j] = min(arr[j], ans[j + 1])
	tocheck = sum(ans)
	if max_s < tocheck:
		tmp = ans[:]
		max_s = tocheck
print(*tmp)


n = int(input())
a = [int(i) for i in input().split()]
mx = -1
good = []
for i in range(n):
	s = a[i]
	now = a.copy()
	for j in range(i - 1, -1, -1):
		now[j] = min(a[j], now[j + 1])
		s += now[j]
	for j in range(i + 1, n):
		now[j] = min(a[j], now[j - 1])
		s += now[j]
	if s > mx:
		good = now.copy()
	mx = max(s, mx)
print(*good)


n = int(input())
arr = list(map(int, input().split()))
ind = []
maxx = max(arr)
for i in range(n):
	if arr[i] == maxx:
		ind += [i]
ans = []
maxx = 0
for x in range(n):
	t = arr[:]
	for i in range(x - 1, -1, -1):
		if t[i] > t[i + 1]:
			t[i] = t[i + 1]
	for i in range(x + 1, n):
		if t[i] > t[i - 1]:
			t[i] = t[i - 1]
	if sum(t) > maxx:
		maxx = sum(t)
		ans = t[:]
print(*ans)


N = int(input())
h = list(map(int, input().split(' ')))
ans_max = 0
ans = []
for i in range(N):
	height = h[i]
	ans_t = [0 for j in range(N)]
	ans_t[i] = h[i]
	pre = h[i]
	for j in range(i - 1, -1, -1):
		if h[j] < pre:
			pre = h[j]
		ans_t[j] = pre
		height += pre
	pre = h[i]
	for j in range(i + 1, N):
		if h[j] < pre:
			pre = h[j]
		ans_t[j] = pre
		height += pre
	if height > ans_max:
		ans_max = height
		ans = ans_t
for i in range(N):
	print(ans[i], end=' ')
print()


from __future__ import division, print_function
import os, 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 ii():
	return int(input())

def si():
	return input()

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

def li():
	return list(mi())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import math
mod = 10 ** 9
mod += 7

def main():
	n = ii()
	a = li()
	l = [0] * n
	m = pos = 0
	for i in range(n):
		d = a[i]
		c = 0
		for j in range(i + 1, n):
			d = min(a[j], d)
			c += d
		d = a[i]
		for j in range(i, -1, -1):
			d = min(a[j], d)
			c += d
		if c > m:
			m = c
			pos = i
	l[pos] = a[pos]
	d = a[pos]
	for j in range(pos + 1, n):
		d = min(a[j], d)
		l[j] = d
	d = a[pos]
	for j in range(pos, -1, -1):
		l[j] = min(d, a[j])
		d = l[j]
	print(*l)
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()


from sys import stdin

def solve():
	n = int(stdin.readline())
	m = list(map(int, stdin.readline().split()))
	msl = [-1] * n
	msp = [n] * n
	q = []
	for i in range(n):
		while q and m[q[-1]] > m[i]:
			q.pop()
		if q:
			msl[i] = q[-1]
		q.append(i)
	q = []
	for i in range(n - 1, -1, -1):
		while q and m[q[-1]] > m[i]:
			q.pop()
		if q:
			msp[i] = q[-1]
		q.append(i)
	dp1 = [0] * n
	for i in range(n):
		dp1[i] = m[i] * (i - msl[i])
		if msl[i] != -1:
			dp1[i] += dp1[msl[i]]
	dp2 = [0] * n
	for i in range(n - 1, -1, -1):
		dp2[i] += m[i] * (msp[i] - i)
		if msp[i] != n:
			dp2[i] += dp2[msp[i]]
	ansm = 0
	answc = 0
	for i in range(n):
		cur = dp1[i] + dp2[i] - m[i]
		if cur > answc:
			answc = cur
			ansm = i
	i = ansm
	cur = [0] * n
	cur[i] = m[i]
	for j in range(i + 1, n):
		cur[j] = min(cur[j - 1], m[j])
	for j in range(i - 1, -1, -1):
		cur[j] = min(cur[j + 1], m[j])
	print(*cur)
for i in range(1):
	solve()


import sys
from collections import defaultdict
from bisect import bisect_left, bisect_right
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
m = list(map(int, input().split()))
k = 1
while 1 << k < n:
	k += 1
ST = [[float('inf')] * k for _ in range(n)]
for i in range(n):
	ST[i][0] = m[i]
for j in range(1, k):
	for i in range(n):
		if i + (1 << j) > n:
			continue
		ST[i][j] = min(ST[i][j - 1], ST[i + (1 << j - 1)][j - 1])
log_len = [0] * (n + 1)
for i in range(2, n + 1):
	log_len[i] = log_len[i >> 1] + 1
left = [m[0]]
for p in range(1, n):
	pivot = m[p]
	if m[p - 1] <= pivot:
		prev = left[-1]
		left.append(prev + pivot)
	else:
		L = -1
		R = p - 1
		while L + 1 < R:
			M = L + R >> 1
			j = log_len[p - 1 - M + 1]
			val = min(ST[M][j], ST[p - 1 - (1 << j) + 1][j])
			if val > pivot:
				R = M
			else:
				L = M
		if L < 0:
			left.append(pivot * (p + 1))
		else:
			prev = left[L]
			left.append(prev + pivot * (p - L))
right = [m[-1]]
for p in range(1, n):
	pivot = m[~p]
	if m[~(p - 1)] <= pivot:
		prev = right[-1]
		right.append(prev + pivot)
	else:
		L = -1
		R = p - 1
		while L + 1 < R:
			M = L + R >> 1
			j = log_len[p - 1 - M + 1]
			val = min(ST[~(p - 1)][j], ST[~M - (1 << j) + 1][j])
			if val > pivot:
				R = M
			else:
				L = M
		if L < 0:
			right.append(pivot * (p + 1))
		else:
			prev = right[L]
			right.append(prev + pivot * (p - L))
bestSum = -float('inf')
top = -1
for i in range(n):
	currSum = left[i] + right[~i] - m[i]
	if currSum > bestSum:
		bestSum = currSum
		top = i
inv = defaultdict(list)
for (i, val) in enumerate(m):
	inv[val].append(i)
left = []
L = 0
while L <= top:
	j = log_len[top - L + 1]
	val = min(ST[L][j], ST[top - (1 << j) + 1][j])
	posID = bisect_left(inv[val], top + 1) - 1
	pos = inv[val][posID]
	left.extend([val] * (pos - L + 1))
	L = pos + 1
right = []
R = n - 1
while R >= top:
	j = log_len[R - top + 1]
	val = min(ST[top][j], ST[R - (1 << j) + 1][j])
	posID = bisect_right(inv[val], top - 1)
	pos = inv[val][posID]
	right.extend([val] * (R - pos + 1))
	R = pos - 1
ans = left + right[:-1][::-1]
print(*ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque
import math

def main():
	n = int(input())
	a = list(map(int, input().split()))
	ma = 0
	c = 0
	for i in range(n):
		b = [0] * n
		j = i - 1
		b[i] = a[i]
		mi = a[i]
		su = a[i]
		while j >= 0:
			b[j] = min(mi, a[j])
			su += b[j]
			mi = b[j]
			j -= 1
		j = i + 1
		mi = a[i]
		while j < n:
			b[j] = min(mi, a[j])
			su += b[j]
			mi = b[j]
			j += 1
		if ma < su:
			ma = su
			c = b[:]
	print(*c)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
	lis1 = []
	mini = arr[i]
	temp = 0
	for j in range(i - 1, -1, -1):
		mini = min(arr[j], mini)
		lis1.append(mini)
		temp += mini
	a = len(lis1)
	lis = []
	for j in range(1, a + 1):
		lis.append(lis1[-j])
	mini = arr[i]
	lis.append(mini)
	temp += mini
	for j in range(i + 1, n):
		mini = min(arr[j], mini)
		lis.append(mini)
		temp += mini
	if i == 0:
		num = []
		for j in range(n):
			num.append(lis[j])
		ans = max(ans, temp)
	elif temp > ans:
		num = []
		for j in range(n):
			num.append(lis[j])
		ans = max(ans, temp)
print(' '.join((str(x) for x in num)))


n = int(input())
ar = list(map(int, input().split()))
ans = []
x = 0
for i in range(n):
	an = [0] * n
	s = ar[i]
	l = i
	r = i + 1
	an[i] = ar[i]
	while l > 0:
		l -= 1
		s += min(ar[l], an[l + 1])
		an[l] = min(ar[l], an[l + 1])
	while r < n:
		s += min(ar[r], an[r - 1])
		an[r] = min(ar[r], an[r - 1])
		r += 1
	if s > x:
		x = s
		ans = an
for i in ans:
	print(i, end=' ')


def getlist(x, pivot):
	ans = [x[pivot]]
	for i in range(pivot - 1, -1, -1):
		ans = [min(ans[0], x[i])] + ans
	for i in range(pivot + 1, len(x)):
		ans += [min(ans[-1], x[i])]
	return ans
n = int(input())
x = list(map(int, input().split()))
ans = []
mx = -1
for i in range(n):
	l = getlist(x, i)
	s = sum(l)
	if s > mx:
		mx = s
		ans = l
print(*ans)


n = int(input())
m = [-1] + [int(i) for i in input().split()] + [-1]
l = [0] * (n + 2)
r = [0] * (n + 2)
ans_right = [0] * (n + 2)
st = [0]
for i in range(1, n + 2):
	while m[st[-1]] > m[i]:
		ans_right[st.pop()] = i
	st.append(i)
ans_left = [0] * (n + 2)
st_2 = [0]
for i in range(n + 1, 0, -1):
	while m[st_2[-1]] > m[i]:
		ans_left[st_2.pop()] = i
	st_2.append(i)
for i in range(1, n + 1):
	if ans_left[i] == 0:
		l[i] = i * m[i]
	else:
		j = ans_left[i]
		l[i] = l[j] + (i - j) * m[i]
o = 1
for i in range(n, 0, -1):
	if ans_right[i] == n + 1:
		r[i] = o * m[i]
	else:
		j = ans_right[i]
		r[i] = r[j] + (j - i) * m[i]
	o += 1
maxim = -99999999999
y = 0
for t in range(1, n + 1):
	tmp = l[t] + r[t] - m[t]
	if tmp > maxim:
		maxim = tmp
		y = t
ver = y - 1
for i in range(ver - 1, 0, -1):
	if m[i] > m[i + 1]:
		m[i] = m[i + 1]
for i in range(ver + 2, n + 1):
	if m[i] > m[i - 1]:
		m[i] = m[i - 1]
print(*m[1:-1])


def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	A = list(map(int, input().split()))
	S = -1
	for mid in range(N):
		ans = [0] * N
		ans[mid] = A[mid]
		prev = A[mid]
		for j in range(mid - 1, -1, -1):
			ans[j] = min(prev, A[j])
			prev = ans[j]
		prev = A[mid]
		for j in range(mid + 1, N):
			ans[j] = min(prev, A[j])
			prev = ans[j]
		if sum(ans) > S:
			S = sum(ans)
			ans_best = ans
	print(*ans_best)

def __starting_point():
	main()
__starting_point()


n = int(input())
m = [int(x) for x in input().split()]
nearestLeftSmaller = [-1 for _ in range(n)]
nearestRightSmaller = [-1 for _ in range(n)]
stack = []
for i in range(n):
	while stack and m[i] < m[stack[-1]]:
		nearestRightSmaller[stack[-1]] = i
		stack.pop()
	stack.append(i)
stack = []
for i in range(n - 1, -1, -1):
	while stack and m[i] < m[stack[-1]]:
		nearestLeftSmaller[stack[-1]] = i
		stack.pop()
	stack.append(i)
prefixSums = [-1 for _ in range(n)]
for i in range(n):
	nls = nearestLeftSmaller[i]
	if nls == -1:
		prefixSums[i] = m[i] * (i + 1)
	else:
		prefixSums[i] = m[i] * (i - nls) + prefixSums[nls]
suffixSums = [-1 for _ in range(n)]
for i in range(n - 1, -1, -1):
	nrs = nearestRightSmaller[i]
	if nrs == -1:
		suffixSums[i] = m[i] * (n - i)
	else:
		suffixSums[i] = m[i] * (nrs - i) + suffixSums[nrs]
optimalTotal = -1
optimalIdx = -1
for i in range(n):
	total = prefixSums[i] + suffixSums[i] - m[i]
	if total > optimalTotal:
		optimalTotal = total
		optimalIdx = i
ans = m.copy()
upperLimit = m[optimalIdx]
for i in range(optimalIdx, -1, -1):
	upperLimit = min(upperLimit, m[i])
	ans[i] = upperLimit
upperLimit = m[optimalIdx]
for i in range(optimalIdx, n):
	upperLimit = min(upperLimit, m[i])
	ans[i] = upperLimit

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))
oneLineArrayPrint(ans)


n = int(input())
m = list(map(int, input().split()))
best_score = 0
best_ans = []
for i in range(n):
	ans = [0 for _ in range(n)]
	ans[i] = m[i]
	score = m[i]
	(left, right) = (m[i], m[i])
	for j in range(i + 1, n):
		right = min(m[j], right)
		ans[j] = right
		score += right
	for j in range(i - 1, -1, -1):
		left = min(m[j], left)
		ans[j] = left
		score += left
	if score > best_score:
		best_score = score
		best_ans = ans
for a in best_ans:
	print(a, end=' ')


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = [int(i) for i in input().split()]
maxi = 0
ans = [0] * n
for i in range(n):
	m = a[i]
	tans = [0] * n
	tans[i] = a[i]
	for j in range(i + 1, n):
		m = min(a[j], m)
		tans[j] = m
	flag = 0
	m = a[i]
	for j in range(i - 1, -1, -1):
		m = min(a[j], m)
		tans[j] = m
	cnt = sum(tans)
	if cnt > maxi:
		maxi = cnt
		ans = tans[:]
print(*ans)


import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import *
from collections import defaultdict, deque, Counter
import math, string
from heapq import *
from operator import add
from itertools import accumulate
BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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')
inf = float('inf')
en = lambda x: list(enumerate(x))
ceil_ = lambda a, b: (a + b - 1) // b
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
arr = rr()
ans = 0

def go(a, x):
	arr = a[:]
	i = x - 1
	while i > -1:
		arr[i] = min(arr[i], arr[i + 1])
		i -= 1
	i = x + 1
	while i < len(a):
		arr[i] = min(arr[i - 1], arr[i])
		i += 1
	return arr if 0 not in arr else inf
val = inf
for i in range(n):
	x = go(arr, i)
	if x == inf:
		continue
	if sum(x) > ans:
		ans = sum(x)
		val = x
print(*val)


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10 ** 9 + 7

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

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n').split(' ')]

def li3():
	return [int(i) for i in input().rstrip('\n')]

def leftin(i, l):
	j = i - 1
	ans = []
	while j >= 0:
		if not len(ans):
			ans.append(min(l[i], l[j]))
		else:
			ans.append(min(l[j], ans[-1]))
		j -= 1
	return [sum(ans), ans[::-1]]

def rightin(i, l):
	j = i + 1
	ans = []
	while j < len(l):
		if not len(ans):
			ans.append(min(l[i], l[j]))
		else:
			ans.append(min(l[j], ans[-1]))
		j += 1
	return [sum(ans), ans]
n = val()
l = li()
currans = 0
currmax = 0
for i in range(n):
	(s1, a1) = leftin(i, l)
	(s2, a2) = rightin(i, l)
	if s1 + s2 + l[i] > currmax:
		currmax = s1 + s2 + l[i]
		currans = a1 + [l[i]] + a2
print(*currans)


def r(x, d):
	stack = [-1]
	s = [0] * x
	for i in range(x - 1, -1, -1):
		while d[i] < d[stack[-1]] and stack[-1] != -1:
			stack.pop()
		s[i] = (x - i) * d[i] if stack[-1] == -1 else s[stack[-1]] + d[i] * (stack[-1] - i)
		stack += [i]
	return s

def total():
	inf = float('inf')
	n = int(input())
	a = [*map(int, input().split())]
	le = r(n, a)
	a = a[::-1]
	ri = r(n, a)[::-1]
	a = a[::-1]
	ma = 0
	x = 0
	for i in range(n):
		if le[i] + ri[i] - a[i] > ma:
			ma = le[i] + ri[i] - a[i]
			x = i
	for i in range(x + 1, n):
		a[i] = min(a[i], a[i - 1])
	for i in range(x - 1, -1, -1):
		a[i] = min(a[i], a[i + 1])
	print(*a)
total()


N = int(input())
X = list(map(int, input().split()))
mxr = (0, 0)
for i in range(N):
	I = i
	r = X[i]
	b = r
	while i > 0:
		i -= 1
		b = min(X[i], b)
		r += b
	i = I
	b = X[i]
	while i < N - 1:
		i += 1
		b = min(X[i], b)
		r += b
	if r > mxr[0]:
		mxr = (r, I)
I = mxr[1]
i = I
rs = [0] * N
rs[i] = X[i]
b = rs[i]
while i > 0:
	i -= 1
	b = min(X[i], b)
	rs[i] = b
i = I
b = X[i]
while i < N - 1:
	i += 1
	b = min(X[i], b)
	rs[i] = b
print(*rs)


def left_nearest_neighbour(array):
	n = len(array)
	neighbours = [-1] * n
	for i in range(1, n):
		j = i - 1
		while j != -1 and array[j] >= array[i]:
			j = neighbours[j]
		neighbours[i] = j
	return neighbours

def right_nearest_neighbour(array):
	n = len(array)
	neighbours = [-1] * n
	for i in reversed(range(n - 1)):
		j = i + 1
		while j != -1 and array[j] >= array[i]:
			j = neighbours[j]
		neighbours[i] = j
	return neighbours

def FindTop(heights):
	n = len(heights)
	left_optim = [0] * n
	left_optim[0] = heights[0]
	right_optim = [0] * n
	right_optim[-1] = heights[-1]
	left_neighbours = left_nearest_neighbour(heights)
	right_neighbours = right_nearest_neighbour(heights)
	for i in range(1, n):
		l_neigh = left_neighbours[i]
		if l_neigh == -1:
			left_optim[i] = heights[i] * (i + 1)
		else:
			left_optim[i] = heights[i] * (i - l_neigh) + left_optim[l_neigh]
	for i in reversed(range(n - 1)):
		r_neigh = right_neighbours[i]
		if r_neigh == -1:
			right_optim[i] = heights[i] * (n - i)
		else:
			right_optim[i] = heights[i] * (r_neigh - i) + right_optim[r_neigh]
	max_heights = [left_optim[i] + right_optim[i] - heights[i] for i in range(n)]
	top = max_heights.index(max(max_heights))
	return (left_neighbours, right_neighbours, top)

def OptimizeHeight(heights):
	n = len(heights)
	(left_neighbours, right_neighbours, top) = FindTop(heights)
	optim_heights = [0] * n
	optim_heights[top] = heights[top]
	i = top
	while i >= 0:
		j = left_neighbours[i]
		if j == -1:
			optim_heights[:i + 1] = [heights[i] for _ in range(i + 1)]
			i = -1
		else:
			optim_heights[j + 1:i + 1] = [heights[i] for _ in range(j + 1, i + 1)]
			i = j
	i = top
	while i >= 0:
		j = right_neighbours[i]
		if j == -1:
			optim_heights[i:] = [heights[i] for _ in range(i, n)]
			i = -1
		else:
			optim_heights[i:j] = [heights[i] for _ in range(i, j)]
			i = j
	return optim_heights
n = int(input())
heights = [int(x) for x in input().split(' ')]
optim_heights = OptimizeHeight(heights)
optim_heights = [str(h) for h in optim_heights]
print(' '.join(optim_heights))


n = int(input())
l = list(map(int, input().split()))
b = max(l)
su = 0
ans = []
for i in range(n):
	z = l[:]
	for j in range(i - 1, -1, -1):
		z[j] = min(z[j], z[j + 1])
	for j in range(i + 1, n):
		z[j] = min(z[j], z[j - 1])
	if su < sum(z):
		su = sum(z)
		ans = z
print(*ans)


n = int(input())
a = [*map(int, input().split())]
m = 0
ans = []
for i in range(n):
	t = a[:]
	for j in range(i - 1, -1, -1):
		t[j] = min(t[j], t[j + 1])
	for j in range(i + 1, n):
		t[j] = min(t[j], t[j - 1])
	if sum(t) > m:
		m = sum(t)
		ans = t[:]
print(*ans)


n = int(input())
l = list(map(int, input().split()))
m = min(l)
index = 0
summ = 0
l1 = [0]
for i in range(n):
	l2 = [0] * n
	l2[i] = l[i]
	for j in range(i - 1, -1, -1):
		if l[j] > l2[j + 1]:
			l2[j] = l2[j + 1]
		else:
			l2[j] = l[j]
	for j in range(i + 1, n):
		if l2[j - 1] < l[j]:
			l2[j] = l2[j - 1]
		else:
			l2[j] = l[j]
	if summ < sum(l2):
		l1[0] = l2
		summ = sum(l2)
for i in range(n):
	print(l1[0][i], end=' ')


n = int(input())
l = list(map(int, input().split()))
m = 0
res = []
for i in range(n):
	r = [0] * n
	p = l[i]
	for j in range(i - 1, -1, -1):
		r[j] = min(p, l[j])
		p = r[j]
	p = l[i]
	for j in range(i + 1, n):
		r[j] = min(p, l[j])
		p = r[j]
	r[i] = l[i]
	s = sum(r)
	if s > m:
		m = s
		res = r
print(*res)


import sys
n = int(sys.stdin.readline().strip())
m = list(map(int, sys.stdin.readline().strip().split()))
left = []
right = []
a = []
highest = 0
floors = 0
for i in range(n):
	if highest > m[i]:
		for j in range(i - 1, -1, -1):
			if a[j] <= m[i]:
				break
			floors -= a[j] - m[i]
			a[j] = m[i]
	left.append(floors)
	floors += m[i]
	a.append(m[i])
	highest = m[i]
a = []
highest = 0
floors = 0
for i in range(n - 1, -1, -1):
	if highest > m[i]:
		for j in range(i + 1, n):
			if a[n - 1 - j] <= m[i]:
				break
			floors -= a[n - 1 - j] - m[i]
			a[n - 1 - j] = m[i]
	right.append(floors)
	floors += m[i]
	a.append(m[i])
	highest = m[i]
right.reverse()
maximum = -1
highest = -1
for i in range(n):
	floors = left[i] + right[i] + m[i]
	if maximum < floors:
		maximum = floors
		highest = i
floors = []
high = m[highest]
for i in range(highest, -1, -1):
	high = min(high, m[i])
	floors.append(str(high))
floors.reverse()
high = m[highest]
for i in range(highest + 1, n):
	high = min(high, m[i])
	floors.append(str(high))
sys.stdout.write(' '.join(floors))


n = int(input())
l = list(map(int, input().split()))
l3 = []
l4 = []
for i in range(n):
	ll = [0] * n
	s = 0
	a = l[i]
	for j in range(i, -1, -1):
		if l[j] >= a:
			ll[j] = a
		else:
			ll[j] = l[j]
			a = l[j]
		s += a
	a = l[i]
	for j in range(i, n):
		if l[j] >= a:
			ll[j] = a
		else:
			ll[j] = l[j]
			a = l[j]
		s += a
	s -= l[i]
	l3.append(ll)
	l4.append(s)
a = max(l4)
l5 = []
for i in range(n):
	if l4[i] == a:
		l5.append(l3[i])
l6 = []
for i in l5:
	c = 0
	for j in range(n):
		if i[j] == l[j]:
			c += 1
	l6.append(c)
a = l6.index(max(l6))
print(*l5[a])


N = int(input())
m = list(map(int, input().split()))
sumAns = 0
ans = []
for i in range(N):
	l = [0 for i in range(N)]
	Sum = 0
	l[i] = m[i]
	Sum += l[i]
	for j in reversed(range(0, i)):
		l[j] = min(m[j], l[j + 1])
		Sum += l[j]
	for j in range(i + 1, N):
		l[j] = min(m[j], l[j - 1])
		Sum += l[j]
	if Sum > sumAns:
		sumAns = Sum
		ans = l
print(' '.join(map(str, ans)))


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


n = int(input())
a = list(map(int, input().split()))
fv = 0
fv_l = a
for i in range(n):
	(st, end) = (i, i)
	s = a[i]
	out = s
	out_l = [a[i]]
	while st > 0:
		st -= 1
		s = min(s, a[st])
		out += s
		out_l.append(s)
	s = a[i]
	out_l = out_l[::-1]
	while end < n - 1:
		end += 1
		s = min(s, a[end])
		out += s
		out_l.append(s)
	if out > fv:
		fv = out
		fv_l = out_l
print(*fv_l)


n = int(input())
a = [0] + list(map(int, input().split())) + [0]

def mnsprava(a):
	inf = int(2000000000.0) + 1
	ans1 = [0] * (n + 2)
	st = [0]
	for i in range(1, n + 2):
		while a[st[-1]] > a[i]:
			ans1[st.pop()] = n - i + 1
		st.append(i)
	ans1.reverse()
	return ans1[1:-1]
alf = mnsprava(a)
a.reverse()
right = [0] * (n + 2)
for i in range(1, n + 1):
	if a[i] >= a[i - 1]:
		right[i] = a[i] + right[i - 1]
	else:
		right[i] = right[alf[i - 1]] + a[i] * (i - alf[i - 1])
bet = mnsprava(a)
a.reverse()
left = [0] * (n + 2)
for i in range(1, n + 1):
	if a[i] >= a[i - 1]:
		left[i] = a[i] + left[i - 1]
	else:
		left[i] = left[bet[i - 1]] + a[i] * (i - bet[i - 1])
right.reverse()
imax = 0
mx = 0
for t in range(1, n + 1):
	if left[t] + right[t] - a[t] > mx:
		mx = left[t] + right[t] - a[t]
		imax = t
ans = [0] * (n + 2)
ans[imax] = a[imax]
for i in range(imax - 1, 0, -1):
	if a[i] < ans[i + 1]:
		ans[i] = a[i]
	else:
		ans[i] = ans[i + 1]
for i in range(imax + 1, n + 1):
	if a[i] < ans[i - 1]:
		ans[i] = a[i]
	else:
		ans[i] = ans[i - 1]
for i in range(1, n + 1):
	print(ans[i], end=' ')


n = int(input())
maxs = [*map(int, input().split())]
low_before = [float('inf') for _ in range(n)]
biggest = 0
for p in range(n):
	low_after = float('inf')
	count = 0
	curr_answer = []
	new_p = maxs[p]
	if p - 1 >= 0 and new_p < low_before[p - 1]:
		for l in range(0, p):
			if new_p < low_before[l]:
				low_before[l] = new_p
	low_before[p] = new_p
	for i in range(n):
		v = maxs[i]
		if i >= p:
			ans = min(v, low_after)
			low_after = ans
			curr_answer.append(ans)
			count += ans
		elif i < p:
			cap = low_before[i]
			ans = min(v, cap)
			curr_answer.append(ans)
			count += ans
	if count > biggest:
		best_answer = curr_answer
		best_low = low_before.copy()
		best_p = p
		biggest = count
print(*best_answer)


n = int(input())
m = list(map(int, input().split()))
l = []
r = [0] * n
stack = []
m_l = [0] * n
m_r = [0] * n
for i in range(n):
	h = m[i]
	while stack and h < stack[-1][0]:
		stack.pop()
	if stack:
		(ph, pi) = stack[-1]
		l.append(h * (i - pi) + l[pi])
	else:
		l.append(h * (i + 1))
	stack.append((h, i))
stack = []
for i in range(n - 1, -1, -1):
	h = m[i]
	while stack and h <= stack[-1][0]:
		stack.pop()
	if stack:
		(ph, pi) = stack[-1]
		r[i] = h * (pi - i) + r[pi]
	else:
		r[i] = h * (n - i)
	stack.append((h, i))
max_score = 0
pivot = -1
for i in range(n):
	score = l[i] + r[i] - m[i]
	if score > max_score:
		max_score = score
		pivot = i
rstack = []
lstack = []
for i in range(pivot):
	h = m[i]
	while lstack and h < lstack[-1][0]:
		lstack.pop()
	lstack.append((h, i))
for i in range(n - 1, pivot, -1):
	h = m[i]
	while rstack and h < rstack[-1][0]:
		rstack.pop()
	rstack.append((h, i))
p = 0
out = [0] * n
out[pivot] = m[pivot]
for i in range(pivot):
	(ch, ci) = lstack[p]
	out[i] = ch
	if i == ci:
		p += 1
p = 0
for i in range(n - 1, pivot, -1):
	(ch, ci) = rstack[p]
	out[i] = ch
	if i == ci:
		p += 1
print(*out)


n = int(input())
arr = list(map(int, input().split()))
maxsum = 0
maxsumAt = 0
for i in range(n):
	keep = arr[i]
	sum = keep
	for j in range(i - 1, -1, -1):
		keep = min(keep, arr[j])
		sum += keep
	keep = arr[i]
	for k in range(i + 1, n):
		keep = min(keep, arr[k])
		sum += keep
	if sum > maxsum:
		maxsum = sum
		maxsumAt = i
ans = [0] * n
ans[maxsumAt] = arr[maxsumAt]
i = maxsumAt
keep = ans[maxsumAt]
for j in range(i - 1, -1, -1):
	keep = min(keep, arr[j])
	ans[j] = keep
keep = ans[maxsumAt]
for k in range(i + 1, n):
	keep = min(keep, arr[k])
	ans[k] = keep
print(*ans)


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 collections import defaultdict
n = int(input())
vals = list(map(int, input().split()))
left = [0 for s in range(n)]
right = [0 for s in range(n)]
stackr = []
dictr = defaultdict(int)
for s in range(n - 1, -1, -1):
	if len(stackr) > 0 and vals[s] <= stackr[-1]:
		cv = 1
		off = 0
		while len(stackr) > 0 and vals[s] <= stackr[-1]:
			v0 = stackr.pop()
			off += v0 * dictr[v0]
			cv += dictr[v0]
			dictr[v0] = 0
		dictr[vals[s]] = cv
		right[s] = right[s + 1] - off + vals[s] * cv
	else:
		if s == n - 1:
			right[s] = vals[s]
		else:
			right[s] = right[s + 1] + vals[s]
		dictr[vals[s]] += 1
	stackr.append(vals[s])
stackl = []
dictl = defaultdict(int)
for s in range(n):
	if len(stackl) > 0 and vals[s] <= stackl[-1]:
		cv = 1
		off = 0
		while len(stackl) > 0 and vals[s] <= stackl[-1]:
			v0 = stackl.pop()
			off += v0 * dictl[v0]
			cv += dictl[v0]
			dictl[v0] = 0
		dictl[vals[s]] = cv
		left[s] = left[s - 1] - off + vals[s] * cv
	else:
		if s == 0:
			left[s] = vals[s]
		else:
			left[s] = left[s - 1] + vals[s]
		dictl[vals[s]] += 1
	stackl.append(vals[s])
maxsofar = 0
ind = 0
for s in range(n):
	sum1 = left[s] + right[s] - vals[s]
	if sum1 > maxsofar:
		maxsofar = sum1
		ind = s
summy = 0
minny = vals[ind]
a1 = []
prev = vals[ind]
for b in range(ind, -1, -1):
	v0 = min(minny, vals[b])
	a1.append(v0)
	if vals[b] < minny:
		minny = vals[b]
a1.reverse()
for b in range(ind + 1, n):
	v1 = min(prev, vals[b])
	a1.append(v1)
	prev = v1
print(*a1)


n = int(input())
cap = list(map(int, input().split()))
solution = [0 for i in range(n)]
best = 0
bestsum = 0
for i in range(0, n):
	max_height = cap[i]
	for j in range(i, n):
		max_height = min(max_height, cap[j])
		solution[j] = max_height
	max_height = cap[i]
	for j in range(1, i + 1):
		max_height = min(max_height, cap[i - j])
		solution[i - j] = max_height
	newsum = sum(solution)
	if newsum > bestsum:
		bestsum = newsum
		best = i
highest = best
max_height = cap[highest]
for i in range(highest, n):
	max_height = min(max_height, cap[i])
	solution[i] = max_height
max_height = cap[highest]
for i in range(0, highest + 1):
	max_height = min(max_height, cap[highest - i])
	solution[highest - i] = max_height
for i in range(n):
	print(solution[i], end=' ')


def prevSmaller(array):
	stack = []
	result = []
	n = len(array)
	for num in range(n):
		while stack and stack[-1][0] >= array[num]:
			stack.pop()
		if stack:
			result.append(stack[-1][1])
		else:
			result.append(0)
		stack.append([array[num], num + 1])
	return result
n = int(input())
b = list(map(int, input().split()))
rever = []
for j in b:
	rever.append(j)
res = prevSmaller(b)
res2 = prevSmaller(rever[::-1])
dp = [0 for i in range(n + 1)]
dp2 = [0 for i in range(n + 1)]
for i in range(n):
	prev = res[i]
	prev2 = res2[i]
	dp[i + 1] = dp[prev] + b[i] * (i - prev + 1)
	dp2[n - i - 1] = dp2[n - prev2] + b[n - i - 1] * (i - prev2 + 1)
m = 0
for i in range(n):
	res = dp[i + 1] + dp2[i] - b[i]
	if res > m:
		req = i
		m = res
j = req
m = b[j]
while j >= 0:
	if b[j] > m:
		b[j] = m
	m = min(m, b[j])
	j += -1
j = req
m = b[j]
while j < n:
	if b[j] > m:
		b[j] = m
	m = min(m, b[j])
	j += 1
print(*b)


n = int(input())
a = list(map(int, input().split()))
ans_sum = 0
ans = a[:]
for max_i in range(n):
	ca = a[:]
	min_v = a[max_i]
	for i in range(max_i - 1, -1, -1):
		if ca[i] > min_v:
			ca[i] = min_v
		else:
			min_v = ca[i]
	min_v = a[max_i]
	for i in range(max_i + 1, n):
		if ca[i] > min_v:
			ca[i] = min_v
		else:
			min_v = ca[i]
	v = sum(ca)
	if ans_sum < v:
		ans_sum = v
		ans = ca
print(*ans)


n = int(input())
arra = list(map(int, input().split()))
i = 1
maxi = 0
arra = [0] + arra + [0]
while i <= n:
	array = arra[:]
	add = array[i]
	j = 0
	k = 1
	while True:
		if i - j == 0:
			break
		if array[i - j - 1] > array[i - j]:
			array[i - j - 1] = array[i - j]
		add += array[i - j - 1]
		j += 1
	while True:
		if i + k == n + 1:
			break
		if array[i + k] > array[i + k - 1]:
			array[i + k] = array[i + k - 1]
		add += array[i + k]
		k += 1
	i += 1
	if add > maxi:
		maxi = add
		store = i
		hanu = array
hanu.pop(0)
hanu.pop(-1)
for i in hanu:
	print(i, end=' ')
print()


import sys
n = int(input())
m = list(map(int, input().split()))
INF = 10 ** 9 + 1
left = [0] * n
right = [0] * n
stack = []
for i in range(n):
	while stack and stack[-1][1] > m[i]:
		stack.pop()
	if stack:
		j = stack[-1][0]
		left[i] = left[j] + (i - j) * m[i]
	else:
		left[i] = (i + 1) * m[i]
	stack.append((i, m[i]))
stack = []
for i in range(n - 1, -1, -1):
	while stack and stack[-1][1] > m[i]:
		stack.pop()
	if stack:
		j = stack[-1][0]
		right[i] = right[j] + (j - i) * m[i]
	else:
		right[i] = (n - i) * m[i]
	stack.append((i, m[i]))
peak = -1
maximum = -1
for i in range(n):
	t = left[i] + right[i] - m[i]
	if maximum < t:
		maximum = t
		peak = i
ans = m[:]
pre = m[peak]
for i in range(peak - 1, -1, -1):
	if ans[i] > pre:
		ans[i] = pre
	else:
		pre = ans[i]
pre = m[peak]
for i in range(peak + 1, n):
	if ans[i] > pre:
		ans[i] = pre
	else:
		pre = ans[i]
print(' '.join(map(str, ans)))


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	n = int(input())
	limit = [int(x) for x in input().split()]
	ans = 0
	ans_list = []
	for i in range(n):
		temp = [x for x in limit]
		for j in range(i - 1, -1, -1):
			temp[j] = min(temp[j], temp[j + 1])
		for j in range(i + 1, n):
			temp[j] = min(temp[j], temp[j - 1])
		if sum(temp) > ans:
			ans = sum(temp)
			ans_list = [x for x in temp]
	print(*ans_list)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

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

class IOWrapper(IOBase):

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


from sys import stdin, stdout
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
dict = []
val = -1
i = 0
while i < n:
	v = a[i]
	j = i - 1
	arr = []
	pmin = v
	while j >= 0:
		v += min(a[j], pmin)
		arr.append(min(a[j], pmin))
		pmin = min(pmin, min(a[j], pmin))
		j -= 1
	arr = arr[::-1]
	arr.append(a[i])
	j = i + 1
	pmin = v
	while j < n:
		v += min(a[j], pmin)
		arr.append(min(a[j], pmin))
		pmin = min(pmin, min(a[j], pmin))
		j += 1
	if v > val:
		val = max(val, v)
		dict = arr
	i += 1
print(*dict)


from sys import stdin
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
stack = [(0, 0)]
left = [0]
for (i, x) in enumerate(a):
	i += 1
	while stack[-1][0] > x:
		stack.pop()
	left.append(left[stack[-1][1]] + x * (i - stack[-1][1]))
	stack.append((x, i))
stack = [(0, 0)]
right = [0]
for (i, x) in enumerate(a[::-1]):
	i += 1
	while stack[-1][0] > x:
		stack.pop()
	right.append(right[stack[-1][1]] + x * (i - stack[-1][1]))
	stack.append((x, i))
right.reverse()
right.pop()
left.pop(0)
high = -1
peak = -1
for x in range(n):
	if right[x] + left[x] - a[x] > high:
		high = right[x] + left[x] - a[x]
		peak = x
new = [0 for x in range(n)]
new[peak] = a[peak]
for x in range(peak - 1, -1, -1):
	new[x] = min(new[x + 1], a[x])
for x in range(peak + 1, n):
	new[x] = min(new[x - 1], a[x])
print(' '.join([str(x) for x in new]))


import sys
from _collections import deque
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
front = [0] * n
back = [0] * n
slide = deque([])
for i in range(n):
	while slide and a[i] < a[slide[-1]]:
		slide.pop()
	if not slide:
		front[i] = (i + 1) * a[i]
	else:
		front[i] = front[slide[-1]] + (i - slide[-1]) * a[i]
	slide.append(i)
slide = deque([])
b = a[::-1]
for i in range(n):
	while slide and b[i] < b[slide[-1]]:
		slide.pop()
	if not slide:
		back[i] = (i + 1) * b[i]
	else:
		back[i] = back[slide[-1]] + (i - slide[-1]) * b[i]
	slide.append(i)
back = back[::-1]
val = id = -1
for (ind, (f, i, b)) in enumerate(zip(front, a, back)):
	if f - i + b > val:
		val = f - i + b
		id = ind
ans = [0] * n
ans[id] = a[id]
t = val = a[id]
for i in range(id - 1, -1, -1):
	if a[i] >= t:
		ans[i] = t
	else:
		ans[i] = a[i]
		t = a[i]
for i in range(id + 1, n, 1):
	if a[i] >= val:
		ans[i] = val
	else:
		ans[i] = a[i]
		val = a[i]
print(*ans)


from heapq import heappop, heappush
n = int(input())
a = list(map(int, input().split()))
acc_l = [0] * n
hq = []
total = 0
for (i, x) in enumerate(a):
	cnt = 1
	minus = 0
	while hq and -hq[0][0] > x:
		p = heappop(hq)
		minus += p[0] * p[1]
		cnt += p[1]
	total += minus + x * cnt
	acc_l[i] = total
	heappush(hq, (-x, cnt))
acc_r = [0] * n
hq = []
total = 0
for (i, x) in zip(range(n - 1, -1, -1), reversed(a)):
	cnt = 1
	minus = 0
	while hq and -hq[0][0] > x:
		p = heappop(hq)
		minus += p[0] * p[1]
		cnt += p[1]
	total += minus + x * cnt
	acc_r[i] = total
	heappush(hq, (-x, cnt))
high_i = 0
max_v = 0
for i in range(n):
	if acc_l[i] + acc_r[i] - a[i] > max_v:
		high_i = i
		max_v = acc_l[i] + acc_r[i] - a[i]
min_v = a[high_i]
for i in range(high_i - 1, -1, -1):
	if a[i] > min_v:
		a[i] = min_v
	else:
		min_v = a[i]
min_v = a[high_i]
for i in range(high_i + 1, n):
	if a[i] > min_v:
		a[i] = min_v
	else:
		min_v = a[i]
print(*a)


n = int(input())
a = list(map(int, input().split()))
l = [0] * n
r = [n - 1] * n
stack = [0]
for i in range(1, n):
	while len(stack) > 0 and a[stack[-1]] >= a[i]:
		stack.pop()
	if len(stack) > 0:
		l[i] = stack[-1]
	else:
		l[i] = i
	stack.append(i)
stack = [n - 1]
for i in range(n - 2, -1, -1):
	while len(stack) > 0 and a[stack[-1]] >= a[i]:
		stack.pop()
	if len(stack) > 0:
		r[i] = stack[-1]
	else:
		r[i] = i
	stack.append(i)
area_l = [0] * n
area_r = [0] * n
for i in range(n):
	if l[i] == i:
		area_l[i] = a[i] * (i + 1)
	else:
		area_l[i] = area_l[l[i]] + a[i] * (i - l[i])
for i in range(n - 1, -1, -1):
	if r[i] == i:
		area_r[i] = a[i] * (n - i)
	else:
		area_r[i] = area_r[r[i]] + a[i] * (r[i] - i)
max_area = 0
summit = 0
for i in range(n):
	if area_l[i] + area_r[i] - a[i] > max_area:
		summit = i
		max_area = area_l[i] + area_r[i] - a[i]
for i in range(summit - 1, -1, -1):
	if a[i] > a[i + 1]:
		a[i] = a[i + 1]
for i in range(summit, n - 1):
	if a[i] < a[i + 1]:
		a[i + 1] = a[i]
print(*a)


n = int(input())
l = [int(x) for x in input().split()]
maxi = [0, 0]
for i in range(n):
	if i == 0:
		count = l[i]
		last = l[i]
		for j in range(1, n):
			if l[j] >= last:
				count += last
			else:
				count += l[j]
				last = l[j]
	elif i == n - 1:
		count = l[i]
		last = l[i]
		for j in range(n - 2, -1, -1):
			if l[j] >= last:
				count += last
			else:
				count += l[j]
				last = l[j]
	else:
		count = l[i]
		last = l[i]
		for j in range(i + 1, n):
			if l[j] >= last:
				count += last
			else:
				count += l[j]
				last = l[j]
		last = l[i]
		for j in range(i - 1, -1, -1):
			if l[j] >= last:
				count += last
			else:
				count += l[j]
				last = l[j]
	if count > maxi[0]:
		maxi = [count, i]
for i in range(maxi[1] + 1, n):
	if l[i] >= l[i - 1]:
		l[i] = l[i - 1]
for i in range(maxi[1] - 1, -1, -1):
	if l[i] >= l[i + 1]:
		l[i] = l[i + 1]
for i in range(n):
	print(l[i], end=' ')


from sys import stdin
input = stdin.readline
n = int(input())
q = list(map(int, input().split()))
peaksSums = [0] * n
currBestList = []
currHighestSum = -1
for i in range(0, n):
	m = q[:]
	for j in range(i - 1, -1, -1):
		if m[j] > m[j + 1]:
			m[j] = m[j + 1]
	for j in range(i + 1, n):
		if m[j] > m[j - 1]:
			m[j] = m[j - 1]
	if sum(m) > currHighestSum:
		currBestList = m[:]
		currHighestSum = sum(m)
print(*currBestList)


from math import *

def binarySearch(arr, x, i):
	l = i
	r = len(arr) - 1
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] <= x:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def jg(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] >= x:
			ans = m
			r = m - 1
		else:
			l = m + 1
	return ans

def ceil(a, b):
	if a % b == 0:
		return int(a / b)
	else:
		return a // b + 1
n = int(input())
ar = list(map(int, input().split()))
ans = []
sm = [0] * n
for i in range(n):
	ans.append([0] * n)
for i in range(n):
	ans[i][i] = ar[i]
	x = i + 1
	y = i - 1
	mx = ar[i]
	while x < n:
		if mx > ar[x]:
			mx = ar[x]
			ans[i][x] = ar[x]
		else:
			ans[i][x] = mx
		x += 1
	mx = ar[i]
	while y >= 0:
		if mx > ar[y]:
			mx = ar[y]
			ans[i][y] = ar[y]
		else:
			ans[i][y] = mx
		y -= 1
	sm[i] = sum(ans[i])
i = sm.index(max(sm))
print(*ans[i])


n = int(input())
otv = [0 for i in range(n)]
mx = 0
m = list(map(int, input().split()))
for s in range(n):
	ans = [0 for i in range(n)]
	ans[s] = m[s]
	i = s - 1
	j = s + 1
	while i >= 0 or j < n:
		if i >= 0:
			ans[i] = min(m[i], ans[i + 1])
		if j < n:
			ans[j] = min(m[j], ans[j - 1])
		i -= 1
		j += 1
	smm = 0
	for i in ans:
		smm += i
	if smm > mx:
		otv = ans.copy()
		mx = smm
for i in otv:
	print(i, end=' ')


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
next = [n] * n
q = []
for i in range(n - 1, -1, -1):
	while q and a[q[-1]] > a[i]:
		q.pop()
	if q:
		next[i] = q[-1]
	q.append(i)
prev = [-1] * n
q = []
for i in range(n):
	while q and a[q[-1]] > a[i]:
		q.pop()
	if q:
		prev[i] = q[-1]
	q.append(i)
right = [0] * n
for i in range(n - 1, -1, -1):
	cnt = next[i] - i
	right[i] = a[i] * cnt
	j = next[i]
	if j < n:
		right[i] += right[j]
left = [0] * n
for i in range(n):
	cnt = i - prev[i]
	left[i] = a[i] * cnt
	j = prev[i]
	if j >= 0:
		left[i] += left[j]
best = 0
besti = -1
for i in range(n):
	cur = right[i] + left[i] - a[i]
	if cur > best:
		best = cur
		besti = i
ans = [0] * n
ans[besti] = a[besti]
mn = a[besti]
for i in range(besti + 1, n):
	mn = min(mn, a[i])
	ans[i] = mn
mn = a[besti]
for i in range(besti - 1, -1, -1):
	mn = min(mn, a[i])
	ans[i] = mn
print(*ans)


import sys
input = sys.stdin.readline
n = int(input())
l = [int(i) for i in input().split()]
g = [0] * len(l)
summa = 0
for index in range(n):
	l1 = [0] * len(l)
	l1[index] = l[index]
	(i, j) = (index - 1, index + 1)
	while i >= 0:
		l1[i] = min(l1[i + 1], l[i])
		i -= 1
	while j < n:
		l1[j] = min(l1[j - 1], l[j])
		j += 1
	if sum(l1) > summa:
		summa = sum(l1)
		g = l1
print(*g)


n = int(input())
ar = list(map(int, input().split()))
perflo = 0
perli = []
for i in range(0, n):
	flo = 0
	li = [0] * n
	li[i] = ar[i]
	fron = ar[i]
	flo += ar[i]
	for j in range(i + 1, n):
		if ar[j] >= fron:
			li[j] = fron
			flo += li[j]
		else:
			li[j] = ar[j]
			fron = ar[j]
			flo += li[j]
	back = ar[i]
	brr = list(ar[0:i][::-1])
	for k in range(0, i):
		if brr[k] >= back:
			li[i - (k + 1)] = back
			flo += back
		else:
			li[i - (k + 1)] = brr[k]
			back = brr[k]
			flo += brr[k]
	if perflo < flo:
		perflo = flo
		perli = li.copy()
print(*perli)


n = int(input())
m = list(map(int, input().split()))
(left, right) = ([m[0]], [m[n - 1]])
stack = [(m[0], 1)]
for i in range(1, n):
	res = left[-1]
	num_tmp = 0
	while stack and stack[-1][0] > m[i]:
		res -= stack[-1][0] * stack[-1][1]
		num_tmp += stack[-1][1]
		stack.pop()
	res += m[i] * (num_tmp + 1)
	stack.append((m[i], num_tmp + 1))
	left.append(res)
stack = [(m[n - 1], 1)]
for i in range(n - 2, -1, -1):
	res = right[-1]
	num_tmp = 0
	while stack and stack[-1][0] > m[i]:
		res -= stack[-1][0] * stack[-1][1]
		num_tmp += stack[-1][1]
		stack.pop()
	res += m[i] * (num_tmp + 1)
	stack.append((m[i], num_tmp + 1))
	right.append(res)
(maxi, maxi_ind) = (0, -1)
for i in range(n):
	if left[i] + right[n - i - 1] - m[i] > maxi:
		maxi = left[i] + right[n - i - 1] - m[i]
		maxi_ind = i
i = maxi_ind
ans = [0 for _ in range(n)]
num = m[i]
ans[i] = m[i]
cur = m[i]
for j in range(i - 1, -1, -1):
	cur = min(m[j], cur)
	ans[j] = cur
	num += cur
cur = m[i]
for k in range(i + 1, n):
	cur = min(m[k], cur)
	ans[k] = cur
	num += cur
print(*ans)


import sys
from math import floor, ceil
import bisect
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 30
MOD = 10 ** 7
n = int(ri())
a = Ri()
l = [0] * n
l[0] = a[0]
sta = [0]
for i in range(1, len(a)):
	if a[i] > a[i - 1]:
		l[i] = l[i - 1] + a[i]
		sta.append(i)
	else:
		while len(sta) > 0 and a[sta[-1]] > a[i]:
			sta.pop()
		pos = 0
		if len(sta) == 0:
			l[i] = (i + 1) * a[i]
		else:
			pos = sta[-1]
			l[i] = l[pos] + (i - pos) * a[i]
		sta.append(i)
r = [0] * n
a = a[::-1]
r[0] = a[0]
sta = [0]
for i in range(1, len(a)):
	if a[i] > a[i - 1]:
		r[i] = r[i - 1] + a[i]
		sta.append(i)
	else:
		while len(sta) > 0 and a[sta[-1]] > a[i]:
			sta.pop()
		pos = 0
		if len(sta) == 0:
			r[i] = (i + 1) * a[i]
		else:
			pos = sta[-1]
			r[i] = r[pos] + (i - pos) * a[i]
		sta.append(i)
r = r[::-1]
ind = -1
maxx = -1
a = a[::-1]
for i in range(len(a)):
	if l[i] + r[i] - a[i] > maxx:
		maxx = l[i] + r[i] - a[i]
		ind = i
for i in range(ind + 1, len(a)):
	a[i] = min(a[i - 1], a[i])
for i in range(ind - 1, -1, -1):
	a[i] = min(a[i + 1], a[i])
print(*a)


n = int(input())
M = list(map(int, input().split()))
ans = []
sum = 0
for maxF in range(n):
	AnsLi = [0] * n
	AnsLi[maxF] = M[maxF]
	maxC = M[maxF]
	sumF = 0
	for i in range(maxF - 1, -1, -1):
		if M[i] <= maxC:
			maxC = M[i]
			AnsLi[i] = M[i]
		else:
			AnsLi[i] = maxC
		sumF += AnsLi[i]
	maxC = M[maxF]
	for i in range(maxF, n):
		if M[i] <= maxC:
			maxC = M[i]
			AnsLi[i] = M[i]
		else:
			AnsLi[i] = maxC
		sumF += AnsLi[i]
	if sumF > sum:
		ans = AnsLi
		sum = sumF
print(' '.join((str(k) for k in ans)))


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


n = int(input())
arr = list(map(int, input().strip().split()))
inc = [0 for i in range(n)]
dec = [0 for i in range(n)]
for i in range(n):
	last = arr[i]
	sc = arr[i]
	for j in range(i - 1, -1, -1):
		if arr[j] < last:
			sc += arr[j]
			last = arr[j]
		else:
			sc += last
	inc[i] = sc
	last = arr[i]
	sc = 0
	for j in range(i + 1, n):
		if arr[j] < last:
			sc += arr[j]
			last = arr[j]
		else:
			sc += last
	dec[i] = sc
maxi = 0
b_point = None
for i in range(n):
	if inc[i] + dec[i] > maxi:
		b_point = i
		maxi = inc[i] + dec[i]
ans = [0 for i in range(n)]
ans[b_point] = arr[b_point]
last = arr[b_point]
for i in range(b_point, -1, -1):
	if arr[i] < last:
		ans[i] = arr[i]
		last = arr[i]
	else:
		ans[i] = last
last = arr[b_point]
for i in range(b_point + 1, n):
	if arr[i] < last:
		ans[i] = arr[i]
		last = arr[i]
	else:
		ans[i] = last
print(*ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq 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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

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()
m = array()
ans = []
total = 0
for i in range(n):
	a = [1] * n
	a[i] = m[i]
	for j in range(i + 1, n):
		a[j] = min(a[j - 1], m[j])
	for j in range(i - 1, -1, -1):
		a[j] = min(a[j + 1], m[j])
	temp = sum(a)
	if temp > total:
		total = temp
		ans = a
print(*ans)


import sys, os, io
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
import math

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

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

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 si():
	return input()
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

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

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

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = 1
a = []
for _ in range(t):
	n = ii()
	l = li()
	mm = 0
	mi = 0
	for top in range(n):
		cur = 0
		m = l[top]
		cur = l[top]
		j = top - 1
		temp = [0] * n
		temp[top] = l[top]
		while j >= 0:
			m = min(m, l[j])
			temp[j] = m
			cur += m
			j -= 1
		j = top + 1
		m = l[top]
		while j < n:
			m = min(m, l[j])
			temp[j] = m
			cur += m
			j += 1
		if mm < cur:
			mi = top
			mm = cur
		a.append(temp)
	tt = []
	cur = 0
	print(*a[mi])


from collections import deque
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
q = deque([])
ans = [0] * n
tmp_ans = 0
for i in range(n):
	ind = i
	l_ind = i
	while True:
		if not q:
			q.append((a[i], l_ind))
			tmp_ans += a[i] * (l_ind + 1)
			break
		if q[-1][0] <= a[i]:
			tmp_ans += a[i] * (l_ind - q[-1][1])
			q.append((a[i], l_ind))
			break
		else:
			(height, l) = q.pop()
			if not q:
				tmp_ans -= height * (l + 1)
			else:
				tmp_ans -= height * (l - q[-1][1])
			ind = l
	ans[i] = tmp_ans
b = a[::-1]
q = deque([])
ans2 = [0] * n
tmp_ans = 0
for i in range(n):
	ind = i
	l_ind = i
	while True:
		if not q:
			q.append((b[i], l_ind))
			tmp_ans += b[i] * (l_ind + 1)
			break
		if q[-1][0] <= b[i]:
			tmp_ans += b[i] * (l_ind - q[-1][1])
			q.append((b[i], l_ind))
			break
		else:
			(height, l) = q.pop()
			if not q:
				tmp_ans -= height * (l + 1)
			else:
				tmp_ans -= height * (l - q[-1][1])
			ind = l
	ans2[i] = tmp_ans
max_ans = 0
ind = -1
for i in range(n):
	tmp = ans[i] + ans2[n - 1 - i] - a[i]
	if max_ans < tmp:
		max_ans = tmp
		ind = i
i = ind
ans = [0] * n
tmp = a[i]
l = i
r = i
max_height = a[i]
ans[i] = a[i]
for j in range(0, l)[::-1]:
	if a[j] >= max_height:
		tmp += max_height
	else:
		tmp += a[j]
		max_height = a[j]
	ans[j] = max_height
max_height = a[i]
for j in range(r + 1, n):
	if a[j] >= max_height:
		tmp += max_height
	else:
		tmp += a[j]
		max_height = a[j]
	ans[j] = max_height
print(*ans)


n = int(input())
m = list(map(int, input().split()))
ans = []
totalmax = 0
for top in range(n):
	lst = [0] * n
	lst[top] = m[top]
	for i in range(top - 1, -1, -1):
		lst[i] = min(lst[i + 1], m[i])
	for i in range(top + 1, n):
		lst[i] = min(lst[i - 1], m[i])
	total = sum(lst)
	if totalmax < total:
		ans = lst
		totalmax = total
print(*ans, sep=' ')


from sys import exit

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

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

def liis():
	return list(map(int, input().split()))
n = ii()
a = liis()
ans = 0
for i in range(n):
	new_a = a[:]
	start = i
	end = i
	while start > 0:
		start -= 1
		new_a[start] = min(new_a[start], new_a[start + 1])
	while end < n - 1:
		end += 1
		new_a[end] = min(new_a[end], new_a[end - 1])
	if sum(new_a) > ans:
		new_ans = new_a[:]
		ans = sum(new_a)
print(' '.join(map(str, new_ans)))


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from functools import *
from itertools import permutations, combinations, groupby
import sys
import bisect
import string
import math
import time
import random

def Golf():
	(*a,) = map(int, open(0))

def S_():
	return input()

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

def LS():
	return [i for i in input().split()]

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

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

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoI():
	return [ord(i) - 97 for i in input()]

def ItoS(nn):
	return chr(nn + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def GI(V, E, Directed=False, index=0):
	org_inp = []
	g = [[] for i in range(n)]
	for i in range(E):
		inp = LI()
		org_inp.append(inp)
		if index == 0:
			inp[0] -= 1
			inp[1] -= 1
		if len(inp) == 2:
			(a, b) = inp
			g[a].append(b)
			if not Directed:
				g[b].append(a)
		elif len(inp) == 3:
			(a, b, c) = inp
			aa = (inp[0], inp[2])
			bb = (inp[1], inp[2])
			g[a].append(bb)
			if not Directed:
				g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}):
	mp = [1] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [1] + [mp_def[j] for j in s] + [1]
	mp += [1] * (w + 2)
	return (h + 2, w + 2, mp, found)

def bit_combination(k, n=2):
	rt = []
	for tb in range(n ** k):
		s = [tb // n ** bt % n for bt in range(k)]
		rt += [s]
	return rt

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
input = lambda : sys.stdin.readline().rstrip()

def ran_input():
	import random
	n = random.randint(4, 16)
	(rmin, rmax) = (1, 10)
	a = [random.randint(rmin, rmax) for _ in range(n)]
	return (n, a)

def ran_input():
	import random
	n = random.randint(4, 16)
	(rmin, rmax) = (1, 10)
	a = [random.randint(rmin, rmax) for _ in range(n)]
	return (n, a)
show_flg = False
show_flg = True
ans = 0
n = I()
m = LI()
tot = 0
for i in range(n):
	(l, r) = ([], [])
	tmp = m[i]
	p = m[i]
	for j in range(i + 1, n):
		if m[j] < p:
			p = m[j]
		tmp += p
		r += [p]
	p = m[i]
	for j in range(i - 1, -1, -1):
		if m[j] < p:
			p = m[j]
		tmp += p
		l += [p]
	if tmp > tot:
		tot = tmp
		ans = l[::-1] + [m[i]] + r
print(*ans)


n = int(input())
m = list(map(int, input().split()))
su = 0
si = []
for q in range(n):
	s = m[q]
	k = m[q]
	sia = [k]
	for i in range(q - 1, -1, -1):
		k = min(k, m[i])
		s += k
		sia.append(k)
	sia = sia[::-1]
	k = m[q]
	for i in range(q + 1, n):
		k = min(k, m[i])
		s += k
		sia.append(k)
	if s > su:
		su = s
		si = sia
for i in si:
	print(i, end=' ')


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	m = [0] + list(map(int, input().split())) + [0]
	stk = [0] * (n + 2)
	(le, ri) = ([0] * (n + 2), [0] * (n + 2))
	ans = [0] * (n + 2)
	ret = [0] * (n + 2)
	top = 0
	for i in range(1, n + 1):
		while top and stk[top] > m[i]:
			top -= 1
		le[i] = le[ans[top]] + (i - ans[top]) * m[i]
		top += 1
		(stk[top], ans[top]) = (m[i], i)
	top = 0
	ans[0] = n + 1
	for i in range(n, 0, -1):
		while top and stk[top] > m[i]:
			top -= 1
		ri[i] = ri[ans[top]] + (ans[top] - i) * m[i]
		top += 1
		(stk[top], ans[top]) = (m[i], i)
	(_max, idx) = (0, 1)
	for i in range(1, n + 1):
		if le[i] + ri[i] - m[i] > _max:
			_max = le[i] + ri[i] - m[i]
			idx = i
	ret[idx] = m[idx]
	for j in range(idx - 1, 0, -1):
		ret[j] = min(m[j], ret[j + 1])
	for j in range(idx + 1, n + 1):
		ret[j] = min(m[j], ret[j - 1])
	for i in range(1, n + 1):
		print(ret[i], end=' ')
	print()
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')
main()


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')
n = int(input())
vals = list(map(int, input().split()))
maxy = 0
arr = []
for s in range(n):
	summy = 0
	minny = vals[s]
	a1 = []
	prev = vals[s]
	for b in range(s, -1, -1):
		v0 = min(minny, vals[b])
		summy += v0
		a1.append(v0)
		if vals[b] < minny:
			minny = vals[b]
	a1.reverse()
	for b in range(s + 1, n):
		v1 = min(prev, vals[b])
		summy += v1
		a1.append(v1)
		prev = v1
	if summy > maxy:
		maxy = summy
		arr = a1
print(*arr)


n = int(input())
arr = list(map(int, input().split()))
back = [0] * n
front = [0] * n
back[n - 1] = arr[n - 1]
stack = [n - 1]
for i in range(n - 2, -1, -1):
	while stack and arr[i] < arr[stack[-1]]:
		stack.pop()
	if len(stack) == 0:
		back[i] = arr[i] * (n - i)
	else:
		back[i] = arr[i] * (stack[-1] - i) + back[stack[-1]]
	stack.append(i)
front[0] = arr[0]
stack = [0]
for i in range(1, n):
	while stack and arr[i] < arr[stack[-1]]:
		stack.pop()
	if len(stack) == 0:
		front[i] = arr[i] * (i + 1)
	else:
		front[i] = arr[i] * (i - stack[-1]) + front[stack[-1]]
	stack.append(i)
pivo = -1
temp = 0
for i in range(n):
	x = front[i] + back[i] - arr[i]
	if x > temp:
		temp = x
		pivo = i
res = [0] * n
l = 1
res[pivo] = str(arr[pivo])
(left_max, right_max) = (arr[pivo], arr[pivo])
while pivo - l >= 0 or pivo + l < len(arr):
	if pivo - l >= 0:
		left_max = min(left_max, arr[pivo - l])
		res[pivo - l] = str(left_max)
	if pivo + l < len(arr):
		right_max = min(right_max, arr[pivo + l])
		res[pivo + l] = str(right_max)
	l += 1
print(' '.join(res))


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
n = inputnum()
m = inputlist()
ans = [0] * n
anssum = 0
for i in range(n):
	cur = [0] * n
	cur[i] = m[i]
	cursum = m[i]
	temp = m[i]
	for j in reversed(range(i)):
		cur[j] = min(m[j], temp)
		cursum += min(m[j], temp)
		temp = min(m[j], temp)
	temp = m[i]
	for j in range(i + 1, n):
		cur[j] = min(m[j], temp)
		cursum += min(m[j], temp)
		temp = min(m[j], temp)
	if cursum > anssum:
		anssum = cursum
		ans = cur
print(*ans)


def r():
	stack = [0]
	ns = [-1] * (n + 2)
	for i in range(n, -1, -1):
		while a[i] < a[stack[-1]]:
			ns[stack.pop()] = i
		stack += [i]
	s = [0] * (n + 2)
	for i in range(1, len(a)):
		s[i] = s[ns[i]] + a[i] * (i - ns[i]) if ns[i] else i * a[i]
	return s
inf = float('inf')
n = int(input())
a = [-inf] + [*map(int, input().split())] + [-inf]
le = r()
a = a[::-1]
ri = r()[::-1]
a = a[::-1]
s = [i + j - k for (i, j, k) in zip(le, ri, a)][1:-1]
x = s.index(max(s))
a = a[1:-1]
for i in range(x + 1, n):
	a[i] = min(a[i], a[i - 1])
for i in range(x - 1, -1, -1):
	a[i] = min(a[i], a[i + 1])
print(*a)


def get(i):
	global n, m
	cur = [0] * n
	cur[i] = m[i]
	for j in range(i - 1, -1, -1):
		cur[j] = min(m[j], cur[j + 1])
	for j in range(i + 1, n):
		cur[j] = min(m[j], cur[j - 1])
	return cur
n = int(input())
m = list(map(int, input().split()))
ans = -1
res = []
for i in range(n):
	a = get(i)
	s = sum(a)
	if ans < s:
		ans = s
		res = a.copy()
print(*res)


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


import sys
import math
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
maxi = -1
ans = [0] * n
for i in range(n):
	curr = [0] * n
	curr[i] = arr[i]
	for j in range(i - 1, -1, -1):
		curr[j] = min(arr[j], min(curr[j + 1], arr[i]))
	for j in range(i + 1, n):
		curr[j] = min(arr[j], min(arr[i], curr[j - 1]))
	sum = 0
	for j in range(n):
		sum += curr[j]
	if sum > maxi:
		maxi = sum
		for j in range(n):
			ans[j] = curr[j]
for i in range(n):
	print(ans[i], end=' ')
print()


from copy import copy
n = int(input())
arr = list(map(int, input().split()))
c = copy(arr)
best = 0
best_arr = None
for j in range(n):
	(l, r) = (arr[j], arr[j])
	total = arr[j]
	c[j] = arr[j]
	for i in range(j - 1, -1, -1):
		l = min(l, arr[i])
		total += l
		c[i] = l
	for i in range(j + 1, n):
		r = min(r, arr[i])
		total += r
		c[i] = r
	if total > best:
		best = total
		best_arr = copy(c)
print(' '.join(map(str, best_arr)))


def main():
	N = int(input())
	m = list(map(int, input().split()))
	LV = (N - 1).bit_length()
	N0 = 2 ** LV
	data = [0] * (2 * N0)
	lazy = [None] * (2 * N0)

	def gindex(l, r):
		L = l + N0 >> 1
		R = r + N0 >> 1
		lc = 0 if l & 1 else (L & -L).bit_length()
		rc = 0 if r & 1 else (R & -R).bit_length()
		for i in range(LV):
			if rc <= i:
				yield R
			if L < R and lc <= i:
				yield L
			L >>= 1
			R >>= 1

	def propagates(*ids):
		for i in reversed(ids):
			v = lazy[i - 1]
			if v is None:
				continue
			lazy[2 * i - 1] = lazy[2 * i] = data[2 * i - 1] = data[2 * i] = v >> 1
			lazy[i - 1] = None

	def update(l, r, x):
		(*ids,) = gindex(l, r)
		propagates(*ids)
		L = N0 + l
		R = N0 + r
		v = x
		while L < R:
			if R & 1:
				R -= 1
				lazy[R - 1] = data[R - 1] = v
			if L & 1:
				lazy[L - 1] = data[L - 1] = v
				L += 1
			L >>= 1
			R >>= 1
			v <<= 1
		for i in ids:
			data[i - 1] = data[2 * i - 1] + data[2 * i]

	def query(l, r):
		propagates(*gindex(l, r))
		L = N0 + l
		R = N0 + r
		s = 0
		while L < R:
			if R & 1:
				R -= 1
				s += data[R - 1]
			if L & 1:
				s += data[L - 1]
				L += 1
			L >>= 1
			R >>= 1
		return s
	a = [0] * N
	update(0, 1, m[0])
	a[0] = m[0]
	for i in range(1, N):
		start = 0
		end = i - 1
		while end - start > 1:
			test = (end + start) // 2
			if query(test, test + 1) > m[i]:
				end = test
			else:
				start = test
		if query(start, start + 1) > m[i]:
			update(start, i + 1, m[i])
			a[i] = query(0, i + 1)
		elif query(end, end + 1) > m[i]:
			update(end, i + 1, m[i])
			a[i] = query(0, i + 1)
		else:
			update(i, i + 1, m[i])
			a[i] = a[i - 1] + m[i]
	update(0, N, 0)
	m = m[::-1]
	b = [0] * N
	update(0, 1, m[0])
	b[0] = m[0]
	for i in range(1, N):
		start = 0
		end = i - 1
		while end - start > 1:
			test = (end + start) // 2
			if query(test, test + 1) > m[i]:
				end = test
			else:
				start = test
		if query(start, start + 1) > m[i]:
			update(start, i + 1, m[i])
			b[i] = query(0, i + 1)
		elif query(end, end + 1) > m[i]:
			update(end, i + 1, m[i])
			b[i] = query(0, i + 1)
		else:
			update(i, i + 1, m[i])
			b[i] = b[i - 1] + m[i]
	b = b[::-1]
	m = m[::-1]
	ans = max((a[i] + b[i] - m[i] for i in range(N)))
	for i in range(N):
		if a[i] + b[i] - m[i] == ans:
			res = [0] * N
			res[i] = m[i]
			for j in range(i - 1, -1, -1):
				res[j] = min(res[j + 1], m[j])
			for j in range(i + 1, N):
				res[j] = min(res[j - 1], m[j])
			print(*res)
			break
main()


from math import *
from collections import *
n = int(input())
a = list(map(int, input().split()))
ans = 0
fres = []
for i in range(n):
	s = a[i]
	prev = a[i]
	res = []
	for j in range(i - 1, -1, -1):
		prev = min(a[j], prev)
		s += prev
		res.append(prev)
	prev = a[i]
	res = res[::-1]
	res.append(a[i])
	for j in range(i + 1, n):
		prev = min(a[j], prev)
		s += prev
		res.append(prev)
	if s > ans:
		ans = s
		fres = res[:]
for i in fres:
	print(i, end=' ')


from copy import deepcopy
n = int(input())
m = list(map(int, input().split()))
cur = [0] * n
res = [0] * n
maxiCount = 0
maxi = max(m)
for i in range(n):
	count = m[i]
	res[i] = m[i]
	for j in range(i + 1, n):
		res[j] = min(m[j], res[j - 1])
		count += res[j]
	for j in range(i - 1, -1, -1):
		res[j] = min(m[j], res[j + 1])
		count += res[j]
	if count > maxiCount:
		cur = deepcopy(res)
		maxiCount = count
print(' '.join(map(str, cur)))


n = int(input())
m = list(map(int, input().split()))
curm = 0
ele = min(m)
res = []
for akidx in range(n):
	arr = [m[akidx]]
	prev = m[akidx]
	for i in range(akidx + 1, n):
		prev = min(prev, m[i])
		arr.append(prev)
	arr = arr[::-1]
	prev = arr[-1]
	for i in range(akidx - 1, -1, -1):
		prev = min(m[i], prev)
		arr.append(prev)
	sm = sum(arr)
	if sm > curm:
		curm = sm
		res = arr
print(' '.join([str(x) for x in res[::-1]]))


def cal(l, n):
	lc = [0]
	for i in range(1, n, 1):
		if l[i] >= l[i - 1]:
			lc.append(lc[-1] + l[i - 1])
		else:
			co = 0
			j = i - 1
			if j == 0:
				lc.append(lc[-1] + l[i])
			else:
				while 1:
					if j < 0:
						lc.append(co * l[i])
						break
					elif l[j] <= l[i]:
						v1 = co * l[i] + lc[j + 1]
						lc.append(v1)
						break
					else:
						j -= 1
						co += 1
	return lc
n = int(input())
lis = list(map(int, input().split()))
if n == 1 or n == 2:
	print(*lis)
else:
	left = cal(lis, n)
	numd = lis[::-1]
	ri = cal(numd, n)
	right = ri[::-1]
	i1 = -1
	su = 0
	for i in range(n):
		va = lis[i] + left[i] + right[i]
		if va > su:
			su = va
			i1 = i
	if i1 == 0:
		print(lis[i1], end=' ')
		ma = lis[i1]
		for i in range(1, n, 1):
			if lis[i] > ma:
				print(ma, end=' ')
			else:
				ma = lis[i]
				print(ma, end=' ')
		print()
	elif i1 == n - 1:
		rc = []
		ma = lis[i1]
		rc.append(ma)
		for i in range(n - 2, -1, -1):
			if lis[i] > ma:
				rc.append(ma)
			else:
				ma = lis[i]
				rc.append(ma)
		while len(rc) != 0:
			print(rc[-1], end=' ')
			rc.pop()
		print()
	else:
		rc = []
		ma = lis[i1]
		for i in range(i1 - 1, -1, -1):
			if lis[i] > ma:
				rc.append(ma)
			else:
				ma = lis[i]
				rc.append(ma)
		while len(rc) != 0:
			print(rc[-1], end=' ')
			rc.pop()
		print(lis[i1], end=' ')
		ma = lis[i1]
		for i in range(i1 + 1, n, 1):
			if lis[i] > ma:
				print(ma, end=' ')
			else:
				ma = lis[i]
				print(ma, end=' ')
		print()


import os, sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	m = list(map(int, input().split()))
	sr = [[-1, m[i]] for i in range(n)]
	for i in range(n - 2, -1, -1):
		y = i + 1
		while y != -1 and m[y] >= m[i]:
			y = sr[y][0]
		if y != -1:
			sr[i] = [y, m[i] * (y - i) + sr[y][1]]
		else:
			sr[i][1] = m[i] * (n - i)
	sl = [[-1, m[i]] for i in range(n)]
	for i in range(1, n):
		y = i - 1
		while y != -1 and m[y] >= m[i]:
			y = sl[y][0]
		if y != -1:
			sl[i] = [y, m[i] * (i - y) + sl[y][1]]
		else:
			sl[i][1] = m[i] * (i + 1)
	pref = [0]
	for i in m:
		pref.append(pref[-1] + i)
	val = [0]
	for i in range(1, n - 1):
		if m[i - 1] >= m[i] < m[i + 1] or m[i - 1] > m[i] <= m[i + 1]:
			val.append(i)
	val.append(n - 1)
	ans = 0
	ind = [-1, -1]
	for i in range(len(val) - 1):
		(l, r) = (val[i], val[i + 1])
		ans1 = pref[r] - pref[l + 1] + sl[l][1] + sr[r][1]
		if ans1 > ans:
			ind = [l, r]
			ans = ans1
	val = m[:]
	mini = m[ind[0]]
	for i in range(ind[0], -1, -1):
		mini = min(mini, m[i])
		val[i] = mini
	mini = m[ind[1]]
	for i in range(ind[1], n):
		mini = min(mini, m[i])
		val[i] = mini
	print(*val)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


from collections import deque
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	mx = a[i]
	q = deque()
	q.append(a[i])
	for j in range(i + 1, n):
		if a[j] <= q[-1]:
			mx += a[j]
			q.append(a[j])
		else:
			mx += q[-1]
			q.append(q[-1])
	for k in range(i - 1, -1, -1):
		if a[k] <= q[0]:
			mx += a[k]
			q.appendleft(a[k])
		else:
			mx += q[0]
			q.appendleft(q[0])
	if mx > ans:
		ans = mx
		Q = q
print(*Q)


n = int(input())
m = [int(num) for num in input().split()]
rightlessque = []
indrightless = [n] * n
lastind = -1
for i in range(n):
	while rightlessque and m[i] < m[rightlessque[lastind]]:
		lastind -= 1
		indrightless[rightlessque.pop()] = i
	rightlessque.append(i)
	lastind += 1
leftlessque = []
indleftless = [-1] * n
lastind = -1
for i in range(n - 1, -1, -1):
	while leftlessque and m[i] < m[leftlessque[lastind]]:
		lastind -= 1
		indleftless[leftlessque.pop()] = i
	leftlessque.append(i)
	lastind += 1
leftval = [0] * n
for i in range(n):
	if indleftless[i] == -1:
		leftval[i] = (i + 1) * m[i]
	else:
		leftval[i] = leftval[indleftless[i]] + m[i] * (i - indleftless[i])
rightval = [0] * n
for i in range(n - 1, -1, -1):
	if indrightless[i] == n:
		rightval[i] = (n - i) * m[i]
	else:
		rightval[i] = rightval[indrightless[i]] + m[i] * (indrightless[i] - i)
totalfloor = []
for i in range(n):
	totalfloor.append(leftval[i] + rightval[i] - m[i])
maxx = totalfloor[0]
ind = 0
for i in range(n):
	if totalfloor[i] > maxx:
		maxx = totalfloor[i]
		ind = i
finalans = [0] * n
i = ind
while i >= 0:
	curr = i
	while curr > indleftless[i]:
		finalans[curr] = m[i]
		curr -= 1
	i = indleftless[i]
i = ind
while i < n:
	curr = i
	while curr < indrightless[i]:
		finalans[curr] = m[i]
		curr += 1
	i = indrightless[i]
for i in finalans:
	print(i, end=' ')
print()


n = int(input())
m = list(map(int, input().split()))
num = 0
s = ''
for i in range(n):
	pi = m[i]
	b = pi
	k = str(pi)
	for j in range(i - 1, -1, -1):
		pi = min(m[j], pi)
		k = str(pi) + ' ' + k
		b += pi
	pi = m[i]
	for j in range(i + 1, n):
		pi = min(m[j], pi)
		k = k + ' ' + str(pi)
		b += pi
	if b > num:
		num = b
		s = k
print(s)


N = int(input())
m = list(map(int, input().split()))
L = [0] * N
L[0] = -1
stack = [0]
for i in range(1, N):
	while stack and m[stack[-1]] >= m[i]:
		stack.pop()
	if stack:
		L[i] = stack[-1]
	else:
		L[i] = -1
	stack.append(i)
dp = [0] * N
for i in range(0, N):
	if L[i] == -1:
		dp[i] = m[i] * (i + 1)
	else:
		dp[i] = m[i] * (i - L[i]) + dp[L[i]]
m = m[::-1]
R = [0] * N
R[0] = -1
stack = [0]
for i in range(1, N):
	while stack and m[stack[-1]] >= m[i]:
		stack.pop()
	if stack:
		R[i] = stack[-1]
	else:
		R[i] = -1
	stack.append(i)
dp2 = [0] * N
for i in range(0, N):
	if R[i] == -1:
		dp2[i] = m[i] * (i + 1)
	else:
		dp2[i] = m[i] * (i - R[i]) + dp2[R[i]]
m = m[::-1]
dp2 = dp2[::-1]
ans = max((dp[i] + dp2[i] - m[i] for i in range(N)))
for i in range(N):
	if dp[i] + dp2[i] - m[i] == ans:
		res = [0] * N
		res[i] = m[i]
		for j in range(i - 1, -1, -1):
			res[j] = min(res[j + 1], m[j])
		for j in range(i + 1, N):
			res[j] = min(res[j - 1], m[j])
		print(*res)
		break


import math

def dfs(a):
	if len(a) < 2:
		return a
	m = 0
	cap = math.inf
	for (i, x) in enumerate(a):
		if x < cap:
			m = i
			cap = x
	left = [min(x, cap) for x in a[:m + 1]] + dfs(a[m + 1:])
	right = dfs(a[:m]) + [min(x, cap) for x in a[m:]]
	if sum(left) > sum(right):
		return left
	else:
		return right

def tc():
	n = int(input())
	a = list(map(int, input().split()))
	ans = dfs(a)
	print(' '.join(map(str, ans)))
tc()


n = int(input())
arr = list(map(int, input().split()))
curr_max = 0
for i in range(n):
	arr_temp = [-1 for j in range(n)]
	ans = arr[i]
	num = arr[i]
	arr_temp[i] = str(arr[i])
	for j in range(i + 1, n):
		num = min(arr[j], num)
		ans += num
		arr_temp[j] = str(num)
	num = arr[i]
	for j in range(i - 1, -1, -1):
		num = min(arr[j], num)
		ans += num
		arr_temp[j] = str(num)
	if ans > curr_max:
		curr_max = ans
		perm = arr_temp
out = ' '.join(perm)
print(out)


n = int(input())
L = list(map(int, input().split()))
ans = [0] * n
sA = 0
for top in range(n):
	r = []
	if top != 0:
		left = []
		now = L[top]
		for i in range(top)[::-1]:
			now = min(now, L[i])
			left.append(now)
		r += left[::-1]
	r.append(L[top])
	if top != n - 1:
		right = []
		now = L[top]
		for i in range(top + 1, n):
			now = min(now, L[i])
			right.append(now)
		r += right
	sr = sum(r)
	if sA < sr:
		sA = sr
		ans = r
for i in range(n):
	print(ans[i], end=' ')


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

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

def read_float():
	return float(input())

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

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

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

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv2(N, M):
	ans = -1
	cand = None
	for i in range(N):
		A = [0] * N
		A[i] = M[i]
		for j in range(i - 1, -1, -1):
			A[j] = min(A[j + 1], M[j])
		for j in range(i + 1, N):
			A[j] = min(A[j - 1], M[j])
		sa = sum(A)
		if sa > ans:
			ans = sa
			cand = A
	return cand

@mt
def slv(N, M):

	@mt
	def f(M):
		l = [0]
		s = [(-1, 0)]
		for m in M:
			if m >= s[-1][0]:
				s.append((m, 1))
				l.append(l[-1] + m)
			else:
				t = l[-1]
				count = 0
				while s and s[-1][0] > m:
					(v, c) = s.pop()
					t -= (v - m) * c
					count += c
				s.append((m, count + 1))
				l.append(t + m)
		return l
	L = f(M)
	L.pop(0)
	R = f(reversed(M))
	R.reverse()
	R.pop()
	mv = 0
	mi = -1
	for i in range(N):
		v = L[i] + R[i] - M[i]
		if v > mv:
			mv = v
			mi = i
	i = mi
	A = [0] * N
	A[i] = M[i]
	for j in range(i - 1, -1, -1):
		A[j] = min(A[j + 1], M[j])
	for j in range(i + 1, N):
		A[j] = min(A[j - 1], M[j])
	return A

def main():
	N = read_int()
	M = read_int_n()
	print(*slv(N, M))
main()


def left(i, a, m):
	s = 0
	if i < 0:
		return s
	for i in range(i, -1, -1):
		if a[i] >= m:
			s += m
		else:
			m = a[i]
			s += m
		a[i] = m
	return s

def right(i, a, n, m):
	s = 0
	if i >= n:
		return s
	for i in range(i, n):
		if a[i] >= m:
			s += m
		else:
			m = a[i]
			s += m
		a[i] = m
	return s
n = int(input())
a = list(map(int, input().split()))
save = a.copy()
ans = 0
ansL = []
for i in range(n):
	temp = left(i - 1, a, a[i]) + a[i] + right(i + 1, a, n, a[i])
	if temp > ans:
		ansL = a.copy()
		ans = temp
	a = save.copy()
for i in ansL:
	print(i, end=' ')
print('')


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

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

def read_float():
	return float(input())

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

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

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

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, M):
	ans = -1
	cand = None
	for i in range(N):
		A = [0] * N
		A[i] = M[i]
		for j in range(i - 1, -1, -1):
			A[j] = min(A[j + 1], M[j])
		for j in range(i + 1, N):
			A[j] = min(A[j - 1], M[j])
		sa = sum(A)
		if sa > ans:
			ans = sa
			cand = A
	return cand

def main():
	N = read_int()
	M = read_int_n()
	print(*slv(N, M))
main()