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
Tags: Dynamic programming

Data structures

Complete search

Greedy algorithms
Skill types: Dynamic programming

Data structures

Complete search

Greedy algorithms

Starter code:

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


import sys
input = sys.stdin.readline
n = int(input())
ma = 0
ans = [0 for i in range(n)]
it = list(map(int, input().split()))
for i in range(1, n):
	tot = 0
	mi = 0
	suff = [0 for i in range(n)]
	suff[i] = it[i]
	for j in range(i - 1, -1, -1):
		suff[j] = it[j]
		if suff[j + 1] < suff[j]:
			suff[j] = suff[j + 1]
	for k in range(i + 1, n):
		if it[k] <= suff[k - 1]:
			suff[k] = it[k]
		else:
			suff[k] = suff[k - 1]
	x = sum(suff)
	if x >= ma:
		ans = suff
		ma = x
suff = [0 for i in range(n)]
suff[0] = it[0]
for i in range(1, n):
	suff[i] = min(it[i], suff[i - 1])
if sum(suff) > ma:
	ans = suff
print(*ans)


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)


n = int(input())
m = list(map(int, input().split()))
mold = list(m)
mres = [0]
for ind in range(n):
	m = list(mold)
	for i in range(ind + 1, n):
		m[i] = min(m[i - 1], m[i])
	for i in range(ind - 1, -1, -1):
		m[i] = min(m[i + 1], m[i])
	if sum(m) >= sum(mres):
		mres = list(m)
print(' '.join(list(map(str, mres))))


from collections import deque
n = int(input())
b = [0]
a = list(map(int, input().split()))
a = b + a
lef = [0 for i in range(n + 5)]
ansl = [0 for i in range(n + 5)]
righ = [0 for i in range(n + 5)]
ansr = [0 for i in range(n + 5)]
ans = -int(2000000000.0)
for k in range(1, n + 1):
	i = k - 1
	while i > 0 and a[i] > a[k]:
		i = lef[i]
	lef[k] = i
	ansl[k] = ansl[i] + (k - i) * a[k]
for k in range(n, 0, -1):
	i = k + 1
	while i <= n and a[i] > a[k]:
		i = righ[i]
	righ[k] = i
	ansr[k] = ansr[i] + (i - k) * a[k]
	if ansr[k] + ansl[k] - a[k] > ans:
		ans = ansr[k] + ansl[k] - a[k]
		ind = k
for k in range(ind - 1, 0, -1):
	a[k] = min(a[k], a[k + 1])
for k in range(ind + 1, n + 1):
	a[k] = min(a[k], a[k - 1])
a = deque(a)
a.popleft()
a = list(a)
print(*a)


import sys
import math
from collections import defaultdict
from collections import deque
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
read = lambda : list(map(int, input().split()))
go = lambda : 1 / 0

def write(*args, sep='\n'):
	for i in args:
		sys.stdout.write('{}{}'.format(i, sep))
INF = float('inf')
MOD = int(1000000000.0 + 7)
YES = 'YES'
NO = 'NO'
for _ in range(1):
	try:
		n = int(input())
		arr = read()
		ans = (-INF, -INF)
		for i in range(n):
			temp = arr[i]
			top = arr[i]
			for j in range(i + 1, n):
				temp += min(top, arr[j])
				top = min(top, arr[j])
			top = arr[i]
			for j in reversed(range(i)):
				temp += min(top, arr[j])
				top = min(top, arr[j])
			ans = max(ans, (temp, i))
		ans_arr = arr[:]
		(_, i) = ans
		top = arr[i]
		for j in range(i + 1, n):
			top = min(top, arr[j])
			ans_arr[j] = top
		top = arr[i]
		for j in reversed(range(i)):
			top = min(top, arr[j])
			ans_arr[j] = top
		print(*ans_arr)
	except ZeroDivisionError:
		continue
	except Exception as e:
		print(e)
		continue


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())
res = list(map(int, input().split()))
di = {}
for j in range(0, n):
	r = l = res[j]
	m = res.copy()
	for i in range(j + 1, n):
		if m[i] > r:
			m[i] = r
		r = m[i]
	for i in range(j - 1, -1, -1):
		if m[i] > l:
			m[i] = l
		l = m[i]
	di[sum(m)] = m
for i in di[max(di.keys())]:
	print(i, end=' ')


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


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

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

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

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

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	n = iin()
	l = ain()
	suma = 0
	maxi = -1
	ans = deque([])
	for i in range(n):
		if maxi == -1:
			maxi = l[i]
			suma += l[i]
			ans.append(l[i])
		else:
			ans.append(min(maxi, l[i]))
			suma += min(maxi, l[i])
			maxi = min(maxi, l[i])
	d = {suma: ans}
	maxi = -1
	ans = deque([])
	suma = 0
	for i in range(n - 1, -1, -1):
		if maxi == -1:
			maxi = l[i]
			suma += l[i]
			ans.appendleft(l[i])
		else:
			ans.appendleft(min(maxi, l[i]))
			suma += min(maxi, l[i])
			maxi = min(maxi, l[i])
	d[suma] = ans
	x = max(l)
	for i in range(1, n - 1):
		k = i - 1
		p = deque([l[i]])
		maxi = l[i]
		suma = l[i]
		while k >= 0:
			h = min(maxi, l[k])
			suma += h
			maxi = h
			p.appendleft(h)
			k -= 1
		k = i + 1
		maxi = l[i]
		while k < n:
			h = min(maxi, l[k])
			suma += h
			maxi = h
			p.append(h)
			k += 1
		d[suma] = p
	xx = max(d.keys())
	print(*d[xx])
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')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


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


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(n,) = I()
max_array = I()
ans = []
idx = {}
for i in range(n):
	idx[max_array[i]] = i
for key in sorted(max_array):
	index = idx[key]
	m = max_array[:]
	for i in range(index - 1, -1, -1):
		m[i] = min(m[i + 1], m[i])
	for i in range(index + 1, n):
		m[i] = min(m[i - 1], m[i])
	if sum(m) > sum(ans):
		ans = m
print(*ans)


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)


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10 ** 9 + 7
inf = float('inf')

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

def LI():
	return list(map(int, sys.stdin.readline().split()))
n = I()
m = [0] + LI() + [0]
ans = 0
x = 0
for i in range(1, n + 1):
	tmp = m[i]
	tmp2 = m[i]
	for j in range(i)[::-1]:
		if tmp >= m[j]:
			tmp2 += m[j]
			tmp = m[j]
		else:
			tmp2 += tmp
	tmp = m[i + 1]
	for j in range(i + 1, n):
		if tmp >= m[j]:
			tmp2 += m[j]
			tmp = m[j]
		else:
			tmp2 += tmp
	tmp2 += tmp
	if ans <= tmp2:
		ans = tmp2
		x = i - 1
m = m[1:-1]
ans = [None] * n
tmp = m[x]
ans[x] = tmp
for i in range(x)[::-1]:
	if tmp >= m[i]:
		ans[i] = m[i]
		tmp = m[i]
	else:
		ans[i] = tmp
if x >= n - 1:
	print(*ans)
	quit()
tmp = m[x + 1]
ans[x + 1] = tmp
for i in range(x + 1, n):
	if tmp >= m[i]:
		ans[i] = m[i]
		tmp = m[i]
	else:
		ans[i] = tmp
print(*ans)


import heapq
import operator
n = int(input())
m = list(map(int, input().split()))
M = [m[i] for i in range(n)]
peaks = []
p = []
l = [0 for _ in range(n)]
r = [0 for _ in range(n)]
st = []
for i in range(0, n):
	while st != [] and m[i] <= m[st[-1]]:
		st.pop()
	if st == []:
		l[i] = m[i] * (i + 1)
	else:
		l[i] = l[st[-1]] + m[i] * (i - st[-1])
	st.append(i)
while st != []:
	st.pop()
for i in range(0, n):
	j = n - i - 1
	while st != [] and m[j] <= m[st[-1]]:
		st.pop()
	if st == []:
		r[j] = m[j] * (i + 1)
	else:
		r[j] = r[st[-1]] + m[j] * (st[-1] - j)
	st.append(j)
maxf = 0
I = 0
for i in range(n):
	if l[i] + r[i] - m[i] >= maxf:
		maxf = l[i] + r[i] - m[i]
		I = i
for i in range(n):
	if i < I:
		m[I - (i + 1)] = min(m[I - i], m[I - (i + 1)])
	if i > I:
		m[i] = min(m[i], m[i - 1])
print(*m)


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


import sys

def makeMountain(M, i):
	m = M[i]
	curr = m
	left = []
	for j in range(i - 1, -1, -1):
		curr = min(M[j], curr)
		left.append(curr)
	curr = m
	right = []
	for j in range(i + 1, len(M)):
		curr = min(M[j], curr)
		right.append(curr)
	return left[::-1] + [m] + right

def solveEasy(N, M):
	best = tuple()
	for i in range(len(M)):
		total = sum(makeMountain(M, i))
		best = max(best, (total, i))
	i = best[1]
	return ' '.join(map(str, makeMountain(M, i)))

def solve(N, M):

	def makeSlope(M):
		dp = [M[0]]
		stack = [(M[0], 1)]
		start = 0
		for i in range(1, len(M)):
			total = dp[-1]
			while stack and stack[-1][0] >= M[i]:
				(h, w) = stack.pop()
				total -= h * w
				start -= w
			h = M[i]
			w = i - start
			stack.append((h, w))
			start += w
			dp.append(total + h * w)
		return dp
	left = makeSlope(M)
	right = makeSlope(M[::-1])[::-1]
	best = tuple()
	for i in range(len(M)):
		best = max(best, (left[i] + right[i] - M[i], i))
	i = best[1]
	return ' '.join(map(str, makeMountain(M, i)))
input = sys.stdin.readline
(N,) = map(int, input().split())
M = [int(x) for x in input().split()]
ans = solve(N, M)
print(ans)


n = int(input())
hmax = list(map(int, input().split()))
ans = 0
seq: list
for i in range(n):
	sumi = 0
	cur = hmax[i]
	seqi = [0] * n
	for j in range(i, n):
		cur = min(cur, hmax[j])
		seqi[j] = cur
		sumi += cur
	cur = hmax[i]
	for j in range(i - 1, -1, -1):
		cur = min(cur, hmax[j])
		seqi[j] = cur
		sumi += cur
	if sumi >= ans:
		seq = seqi
		ans = sumi
print(*seq)


def findHeights(n, m):
	l = [0] * (n + 2)
	r = [0] * (n + 2)
	h = [0] * (n + 2)
	for i in range(1, n + 1):
		j = i - 1
		while j > 0:
			if m[j - 1] < m[i - 1]:
				break
			j -= 1
		if j > 0:
			l[i] = l[j] + (i - j) * m[i - 1]
		else:
			l[i] = i * m[i - 1]
	for i in range(n, 0, -1):
		j = i + 1
		while j < n:
			if m[j - 1] < m[i - 1]:
				break
			j += 1
		if j < n:
			r[i] = r[j] + (j - i) * m[i - 1]
		else:
			r[i] = (n - i + 1) * m[i - 1]
	maxVal = ans = 0
	for i in range(1, n + 1):
		tmp = l[i] + r[i] - m[i - 1]
		if tmp > maxVal:
			maxVal = tmp
			ans = i - 1
	h[ans] = m[ans]
	for i in range(ans + 1, n):
		h[i] = min(h[i - 1], m[i])
	for i in range(ans - 1, -1, -1):
		h[i] = min(h[i + 1], m[i])
	return h

def main():
	n = int(input())
	m = list(map(int, input().split()))
	ans = findHeights(n, m)
	for i in range(n):
		print(ans[i], end=' ')
main()


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 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')
numplots = int(input())
limfloors = list(map(int, input().split()))
listsums = []
answers = {}
minsofar = max(limfloors) + 1
indexminsofar = 0
for k in range(numplots):
	sum1 = 0
	answerlist = []
	max1 = limfloors[k]
	if max1 <= minsofar:
		minsofar = max1
		indexminsofar = k
	answerlist += [minsofar] * (indexminsofar + 1)
	sum1 += minsofar * (indexminsofar + 1)
	minsofar2 = max1
	templist = []
	for j in range(1, k - indexminsofar):
		if limfloors[k - j] <= minsofar2:
			sum1 += limfloors[k - j]
			minsofar2 = limfloors[k - j]
			templist.append(limfloors[k - j])
		else:
			sum1 += minsofar2
			templist.append(minsofar2)
	answerlist += templist[::-1]
	minsofar3 = max1
	for l in range(k, numplots):
		if limfloors[l] <= minsofar3:
			sum1 += limfloors[l]
			minsofar3 = limfloors[l]
			answerlist.append(limfloors[l])
		else:
			sum1 += minsofar3
			answerlist.append(minsofar3)
	if len(answerlist) > numplots:
		answerlist.pop(0)
		sum1 += -answerlist[0]
	listsums.append(sum1)
	answers[sum1] = answerlist
maximumsum = max(listsums)
realanswer = list(map(str, answers[maximumsum]))
print(' '.join(realanswer))


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


import heapq
n = int(input())
M = [int(i) for i in input().split()]

def f(X):
	H = []
	Ret = [0] * n
	tot = 0
	for (i, x) in enumerate(X):
		haba = 0
		while H:
			h = -heapq.heappop(H)
			(t, y) = (h // 10 ** 6, h % 10 ** 6)
			if t <= x:
				heapq.heappush(H, -(t * 10 ** 6 + y))
				break
			else:
				tot -= (t - x) * y
				haba += y
		heapq.heappush(H, -(x * 10 ** 6 + haba + 1))
		Ret[i] = tot + x
		tot += x
	return Ret
(A1, A2) = (f(M), f(M[::-1])[::-1])
A = [A1[i] + A2[i] - M[i] for i in range(n)]
Ans = [0] * n
ma = max(A)
for i in range(n):
	if A[i] == ma:
		m = i
Ans[m] = M[m]
for i in reversed(range(m)):
	Ans[i] = min(Ans[i + 1], M[i])
for i in range(m + 1, n):
	Ans[i] = min(Ans[i - 1], M[i])
print(*Ans)


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


n = int(input(''))
m = list(map(int, input('').split(' ')))
v = set()
for i in range(1, n - 1):
	if m[i] < m[i - 1]:
		if m[i] < m[i + 1]:
			v.add(i + 1)
		v.add(i - 1)
ans = [0] * n
ma = 0
if len(v) == 0:
	ans = m
for e in v:
	c = [0] * n
	mc = 0
	c[e] = m[e]
	mc += m[e]
	for i in range(e - 1, -1, -1):
		c[i] = min(m[i], c[i + 1])
		mc += c[i]
	for i in range(e + 1, n):
		c[i] = min(m[i], c[i - 1])
		mc += c[i]
	if mc > ma:
		ma = mc
		ans = c.copy()
for i in range(n):
	print(ans[i], end=' ')


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)


import sys
from collections import defaultdict

def get(arr, memo):
	if memo[tuple(arr)]:
		return memo[tuple(arr)]
	if len(arr) <= 2:
		return arr
	ind = -1
	x = min(arr)
	for i in range(len(arr)):
		if arr[i] == x:
			ind = i
			break
	left = get(arr[:i], memo)
	right = get(arr[i + 1:], memo)
	ans = [-1 for _ in range(len(arr))]
	(l, r) = (sum(left) + x * (len(arr) - ind), sum(right) + x * (ind + 1))
	if l > r:
		for i in range(len(left)):
			ans[i] = left[i]
		for i in range(len(left), len(ans)):
			ans[i] = x
		return ans
	j = 0
	for i in range(ind + 1, len(ans)):
		ans[i] = right[j]
		j += 1
	for i in range(ind + 1):
		ans[i] = x
	return ans
sys.setrecursionlimit(10000)
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
memo = defaultdict(list)
ans = get(arr, memo)
print(*ans)


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)


import os
import sys

def main():
	n = int(input())
	b = list(map(int, input().split()))
	val = 0
	ans = []
	for i in range(n):
		t = b[:]
		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) > val:
			val = sum(t)
			ans = t
	print(*ans)
main()


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 = list(map(int, input().split()))
l2 = [(e, i) for (i, e) in enumerate(l)]
l2.sort(reverse=True)
if l2[-1][0] == l2[0][0]:
	print(' '.join(map(str, l)))
	exit(0)
m = 0
for e in l2:
	if e[0] == l2[0][0]:
		m += 1
l4 = n * [0]
for e in l2:
	l3 = n * [0]
	l3[e[1]] = e[0]
	for i in reversed(range(e[1])):
		l3[i] = min(l3[i + 1], l[i])
	for i in range(e[1] + 1, n):
		l3[i] = min(l3[i - 1], l[i])
	if sum(l4) < sum(l3):
		l4 = l3
print(' '.join(map(str, l4)))


import os
import heapq
import sys
import math
import operator
from collections import defaultdict, deque
from bisect import bisect_right, bisect_left
from io import BytesIO, IOBase
from copy import copy

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

def inpt():
	return [int(k) for k in input().split()]

def main():
	n = int(input())
	ar = inpt()
	stack = []
	pref = []
	for i in range(n):
		while stack and ar[stack[len(stack) - 1]] > ar[i]:
			stack.pop()
		if not stack:
			pref.append(i)
		else:
			pref.append(stack[len(stack) - 1])
		stack.append(i)
	suf = [0] * n
	stack = []
	for i in range(n - 1, -1, -1):
		while stack and ar[stack[len(stack) - 1]] > ar[i]:
			stack.pop()
		if not stack:
			suf[i] = i
		else:
			suf[i] = stack[len(stack) - 1]
		stack.append(i)
	left = []
	for i in range(n):
		if pref[i] == i:
			left.append((i + 1) * ar[i])
		else:
			left.append(left[pref[i]] + (i - pref[i]) * ar[i])
	right = [0] * n
	for i in range(n - 1, -1, -1):
		if suf[i] == i:
			right[i] = (n - i) * ar[i]
		else:
			right[i] = right[suf[i]] + abs(i - suf[i]) * ar[i]
	(peak, mx) = (0, 0)
	for i in range(n):
		if mx < left[i] + right[i] - ar[i]:
			mx = left[i] + right[i] - ar[i]
			peak = i
	for i in range(peak - 1, -1, -1):
		ar[i] = min(ar[i], ar[i + 1])
	for i in range(peak + 1, n):
		ar[i] = min(ar[i], ar[i - 1])
	for i in ar:
		print(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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
arr = list(map(int, input().split()))
val = 0
ans = []
for i in range(n):
	t = arr[:]
	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) > val:
		val = sum(t)
		ans = t
print(*ans)


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


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

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

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

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	n = N()
	arr = RLL()
	res = []
	ressm = 0
	for i in range(n):
		sm = arr[i]
		mi = sm
		tres = [0] * n
		tres[i] = sm
		for j in range(i + 1, n):
			mi = min(mi, arr[j])
			tres[j] = mi
			sm += mi
		mi = sm
		for j in range(i - 1, -1, -1):
			mi = min(mi, arr[j])
			tres[j] = mi
			sm += mi
		if sm > ressm:
			res = tres.copy()
			ressm = sm
	print(*res)
main()


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


n = int(input())
l = list(map(int, input().split()))
max_floor = 0
l2 = [0] * n
l1 = [0] * n
for i in range(n):
	floor = 0
	t = l[i]
	a = i - 1
	while a >= 0:
		if l[a] <= t:
			t = l[a]
		l1[a] = t
		a -= 1
	l1[i] = l[i]
	b = i + 1
	t = l[i]
	while b < n:
		if t >= l[b]:
			t = l[b]
		l1[b] = t
		b += 1
	floor = sum(l1)
	if floor >= max_floor:
		max_floor = floor
		for m in range(n):
			l2[m] = l1[m]
for i in l2:
	print(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())
L = list(map(int, input().split()))
MAX = 0
ANS = []
for i in range(N):
	B = L[:]
	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 > MAX:
		MAX = S
		ANS = B[:]
print(*ANS)


n = int(input())
lis = list(map(int, input().split()))
summ = 0
i = 0
ans = []
while i < n:
	wor = lis.copy()
	peak = lis[i]
	for j in range(i - 1, -1, -1):
		if wor[j] > wor[j + 1]:
			wor[j] = wor[j + 1]
	for j in range(i, n - 1):
		if wor[j] < wor[j + 1]:
			wor[j + 1] = wor[j]
	if sum(wor) > summ:
		summ = sum(wor)
		ans = wor.copy()
	i += 1
print(' '.join(list(map(str, ans))))


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


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import Decimal
from decimal import *
from collections import defaultdict, deque
import heapq
from decimal import Decimal
getcontext().prec = 25
abcd = 'abcdefghijklmnopqrstuvwxyz'
ABCD = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
months = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
MOD = 1000000007
BUFSIZE = 8192
from bisect import bisect_left, bisect_right

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())
l = list(map(int, input().split(' ')))
ans = [0]
z = 0
for i in range(n):
	v = 0
	x = [l[i]]
	for j in range(i - 1, -1, -1):
		x.append(min(x[-1], l[j]))
	x = x[::-1]
	for j in range(i + 1, n):
		x.append(min(x[-1], l[j]))
	f = sum(x)
	if f > z:
		ans = x
		z = f
print(*ans)


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

def asc_pass(a):
	import bisect
	ret = [0]
	asc = []
	last = 0
	for (i, x) in enumerate(a):
		if x >= last:
			ret.append(ret[-1] + x)
			if x > last:
				asc.append((x, i))
				last = x
		else:
			b = bisect.bisect(asc, (x, i))
			j = asc[b][1]
			del asc[b:]
			asc.append((x, j))
			ret.append(ret[j] + (i - j + 1) * x)
			last = x
	return ret[1:]

def desc_pass(a):
	z = asc_pass(reversed(a))
	z.reverse()
	return z
asc_table = asc_pass(m)
desc_table = desc_pass(m)

def score(peak):
	return asc_table[peak] + desc_table[peak] - m[peak]
best = (-1, None)
for i in range(n):
	best = max(best, (score(i), i))

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


inf = float('inf')
n = int(input())
a = [-inf] + list(map(int, input().split())) + [-inf]
rans = [0] * (n + 2)
rst = [0]
lans = [0] * (n + 2)
lst = [n + 1]
for i in range(1, n + 2):
	while a[rst[-1]] > a[i]:
		rans[rst.pop()] = i
	rst.append(i)
	while a[lst[-1]] > a[n + 1 - i]:
		lans[lst.pop()] = n + 1 - i
	lst.append(n + 1 - i)
a[0] = 0
a[-1] = 0
l = [0] * (n + 2)
r = [0] * (n + 2)
l[0] = a[0]
r[-1] = a[-1]
for i in range(1, n + 2):
	if a[i] >= a[i - 1]:
		l[i] = l[i - 1] + a[i]
	else:
		l[i] = l[lans[i]] + (i - lans[i]) * a[i]
for i in range(n, 0, -1):
	if a[i] >= a[i + 1]:
		r[i] = r[i + 1] + a[i]
	else:
		r[i] = r[rans[i]] + (rans[i] - i) * a[i]
maxs = 0
maxt = 0
for t in range(len(a)):
	s = l[t] + r[t] - a[t]
	if s > maxs:
		maxs = s
		maxt = t
for i in range(maxt + 1, len(a)):
	if a[i] > a[i - 1]:
		a[i] = a[i - 1]
for i in range(maxt - 1, -1, -1):
	if a[i] > a[i + 1]:
		a[i] = a[i + 1]
print(*a[1:-1])


import sys
n = int(sys.stdin.readline())
m = [int(x) for x in sys.stdin.readline().split()]
ans = 0
arr = [0] * n
for i in range(n):
	floors = m[i]
	tmp = [0] * n
	tmp[i] = m[i]
	prev = m[i]
	for j in range(i - 1, -1, -1):
		num = min(prev, m[j])
		floors += num
		tmp[j] = num
		prev = num
	p2 = m[i]
	for k in range(i + 1, n):
		n2 = min(p2, m[k])
		floors += n2
		tmp[k] = n2
		p2 = n2
	if floors > ans:
		ans = floors
		for g in range(n):
			arr[g] = tmp[g]
for p in range(n):
	print(arr[p], end=' ')
print()


n = int(input())
x = [int(i) for i in input().split()]
s = [0 for _ in x]

def getVol(i, j) -> int:
	if i > j:
		return 0
	if i == j:
		s[i] = x[i]
		return x[i]
	min_v = min(x[i:j + 1])
	min_ind = x[i:j + 1].index(min_v) + i
	if min_ind == i:
		s[i] = min_v
		return min_v + getVol(i + 1, j)
	elif min_ind == j:
		s[j] = min_v
		return min_v + getVol(i, j - 1)
	else:
		v1 = (min_ind + 1 - i) * min_v + getVol(min_ind + 1, j)
		v2 = (j - min_ind + 1) * min_v + getVol(i, min_ind - 1)
		if v1 > v2:
			for q in range(i, min_ind + 1):
				s[q] = min_v
			return v1
		else:
			for q in range(min_ind, j + 1):
				s[q] = min_v
			return v2
i = getVol(0, len(x) - 1)
print(*s)


import sys
import math
input = sys.stdin.readline

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

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

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

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

def pr_list(a):
	print(*a, sep=' ')

def main():
	n = inp()
	floor = inlt()
	ans = 0
	g_final = []
	for high in range(0, n):
		curr = floor[high]
		c_floor = floor[high]
		curr_plan = [floor[high]]
		for i in range(high - 1, -1, -1):
			if floor[i] > c_floor:
				curr = curr + c_floor
			else:
				curr = curr + floor[i]
				c_floor = floor[i]
			curr_plan = [c_floor] + curr_plan
		c_floor = floor[high]
		for i in range(high + 1, n):
			if floor[i] > c_floor:
				curr = curr + c_floor
			else:
				curr = curr + floor[i]
				c_floor = floor[i]
			curr_plan = curr_plan + [c_floor]
		if curr > ans:
			ans = curr
			g_final = curr_plan
	pr_list(g_final)
	return
main()


n = int(input())
l = input().split()
for i in range(n):
	l[i] = int(l[i])
a = []
for j in range(n):
	if j != n - 1:
		min = l[j]
	all = l[j]
	for i in range(j + 1, n):
		if l[i] >= min:
			all += min
		else:
			min = l[i]
			all += min
	if j != 0:
		min = l[j]
	for i in range(j - 1, -1, -1):
		if l[i] >= min:
			all += min
		else:
			min = l[i]
			all += min
	a.append(all)
max = -1
for i in range(n):
	if a[i] >= max:
		max = a[i]
		ind = i
if ind != n - 1:
	min = l[ind]
	for i in range(ind + 1, n):
		if l[i] >= min:
			l[i] = min
		else:
			min = l[i]
if ind != 0:
	min = l[ind]
	for i in range(ind - 1, -1, -1):
		if l[i] >= min:
			l[i] = min
		else:
			min = l[i]
for i in l:
	print(i, end=' ')


n = int(input())
m = []
lis = input().split()
for i in lis:
	m.append(int(i))
maxind = [0]
maxx = m[0]
for i in range(1, n):
	if m[i] > maxx:
		maxind = [i]
		maxx = m[i]
	elif m[i] == maxx:
		maxind.append(i)

def find(indd):
	sum = m[indd]
	minn = m[indd]
	for i in range(indd - 1, -1, -1):
		minn = min(m[i], minn)
		sum += minn
	minn = m[indd]
	for i in range(indd + 1, n):
		minn = min(m[i], minn)
		sum += minn
	return sum
ab = 0
ind = 0
for i in range(n):
	l = find(i)
	if l > ab:
		ab = l
		ind = i
a = [0] * n
a[ind] = m[ind]
minn = m[ind]
for i in range(ind - 1, -1, -1):
	minn = min(m[i], minn)
	a[i] = minn
minn = m[ind]
for i in range(ind + 1, n):
	minn = min(m[i], minn)
	a[i] = minn
for i in a:
	print(i, end=' ')
print()


n = int(input())
l = list(map(int, input().split()))
k = []
p = -1
for i in range(n):
	mn = l[i]
	tem = [0] * n
	tem[i] = l[i]
	c = l[i]
	for j in range(i - 1, -1, -1):
		mn = min(mn, l[j])
		tem[j] = mn
		c += mn
	mn = l[i]
	for j in range(i + 1, n):
		mn = min(mn, l[j])
		tem[j] = mn
		c += mn
	if c > p:
		p = c
		k = tem
print(*k)


import sys
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 = 998244353
n = int(ri())
b = Ri()
maxx = -1
ind = -1
for i in range(0, len(b)):
	a = b[:]
	left = 0
	right = 0
	for j in range(i - 1, -1, -1):
		if a[j] < a[j + 1]:
			left += a[j]
		else:
			a[j] = a[j + 1]
			left += a[j]
	for j in range(i + 1, n):
		if a[j] < a[j - 1]:
			right += a[j]
		else:
			a[j] = a[j - 1]
			right += a[j]
	fin = left + right + b[i]
	if fin > maxx:
		maxx = fin
		ind = i
for j in range(ind - 1, -1, -1):
	if b[j] < b[j + 1]:
		left += b[j]
	else:
		b[j] = b[j + 1]
		left += a[j]
for j in range(ind + 1, n):
	if b[j] < b[j - 1]:
		right += a[j]
	else:
		b[j] = b[j - 1]
		right += b[j]
print(*b)


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)


from collections import defaultdict, Counter
import os
import sys
from io import BytesIO, IOBase
ii = lambda : int(input())
lmii = lambda : list(map(int, input().split()))
slmii = lambda : sorted(map(int, input().split()))
li = lambda : list(input())
mii = lambda : map(int, input().split())
msi = lambda : map(str, input().split())

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

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

def main():
	n = ii()
	a = lmii()
	if n <= 2:
		print(*a)
	else:
		f = 0
		for i in range(n):
			f = 0
			for j in range(i + 1, n):
				if a[j] > a[i]:
					f += 1
					break
			for j in range(i):
				if a[j] > a[i]:
					f += 1
					break
			if f == 2:
				break
		if f != 2:
			print(*a)
		else:
			ans = [0] * n
			lst = list(a)
			for i in range(n):
				a = list(lst)
				mn = a[i]
				for j in range(i + 1, n):
					mn = min(mn, a[j])
					a[j] = mn
				mn = a[i]
				for j in range(i - 1, -1, -1):
					mn = min(mn, a[j])
					a[j] = mn
				if sum(ans) < sum(a):
					ans = list(a)
			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())
l = list(map(int, input().split()))
su = 0
ans = []
for i in range(n):
	t = l[:]
	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 > su:
		su = d
		ans = t
print(*ans)


n = int(input())
l = list(map(int, input().strip().split()))
(left, right) = ([0] * n, [0] * n)
s = [0]
left[0] = l[0]
for i in range(1, n):
	while len(s) > 0 and l[s[-1]] > l[i]:
		s.pop()
	if len(s) == 0:
		left[i] = (i + 1) * l[i]
	else:
		left[i] = left[s[-1]] + (i - s[-1]) * l[i]
	s.append(i)
s = [n - 1]
right[n - 1] = l[n - 1]
for i in range(n - 2, -1, -1):
	while len(s) > 0 and l[s[-1]] > l[i]:
		s.pop()
	if len(s) == 0:
		right[i] = (n - i) * l[i]
	else:
		right[i] = right[s[-1]] + (s[-1] - i) * l[i]
	s.append(i)
(max_val, index) = (-1, -1)
for i in range(n):
	if left[i] + right[i] - l[i] > max_val:
		max_val = left[i] + right[i] - l[i]
		index = i
if index != -1:
	for i in range(index - 1, -1, -1):
		l[i] = min(l[i + 1], l[i])
	for i in range(index + 1, n):
		l[i] = min(l[i], l[i - 1])
for i in l:
	print(i, end=' ')


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


n = int(input())
m = list(map(int, input().split()))
assert len(m) == n
st = [(m[0], 0)]
left = [m[0]] + [0] * (n - 1)
for i in range(1, len(m)):
	if m[i] >= m[i - 1]:
		left[i] = left[i - 1] + m[i]
	else:
		while st and st[-1][0] > m[i]:
			st.pop()
		if st:
			left[i] = left[st[-1][1]] + (i - st[-1][1]) * m[i]
		else:
			left[i] = (i + 1) * m[i]
	st.append((m[i], i))
st = [(m[n - 1], n - 1)]
right = [0] * (n - 1) + [m[n - 1]]
for i in range(n - 2, -1, -1):
	if m[i] >= m[i + 1]:
		right[i] = right[i + 1] + m[i]
	else:
		while st and st[-1][0] > m[i]:
			st.pop()
		if st:
			right[i] = right[st[-1][1]] + (st[-1][1] - i) * m[i]
		else:
			right[i] = (n - i) * m[i]
	st.append((m[i], i))
(mx, mxi) = (0, -1)
for i in range(n):
	area = left[i] + right[i] - m[i]
	if mx < area:
		mx = area
		mxi = i
for i in range(mxi - 1, -1, -1):
	if m[i] > m[i + 1]:
		m[i] = m[i + 1]
for i in range(mxi + 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 = 0
res = None
for i in range(n):
	temp = list(arr)
	mini = arr[i]
	for j in range(i - 1, -1, -1):
		temp[j] = min(temp[j], mini)
		mini = min(mini, temp[j])
	mini = arr[i]
	for j in range(i + 1, n):
		temp[j] = min(temp[j], mini)
		mini = min(mini, temp[j])
	s = sum(temp)
	if s > ans:
		ans = s
		res = list(temp)
print(*res)


n = int(input())
M = list(map(int, input().split()))
maxsum = 0
for i in range(n):
	a = M.copy()
	Sum = a[i]
	for j in range(i + 1, n):
		a[j] = min(a[j], a[j - 1])
		Sum += a[j]
	for j in range(i - 1, -1, -1):
		a[j] = min(a[j], a[j + 1])
		Sum += a[j]
	if Sum > maxsum:
		maxsum = Sum
		array = a.copy()
for i in array:
	print(i, end=' ')


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


n = int(input())
a = list(map(int, input().split()))
maxi = 0
ans = []
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])
	summ = sum(b)
	if summ > maxi:
		maxi = summ
		ans = b[:]
print(*ans)


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


import sys

def solve():
	input = sys.stdin.readline
	N = int(input())
	M = [int(m) for m in input().split()]
	maxFloor = -1
	Ans = [-1] * N
	for i in range(N):
		tAns = [-1 for _ in range(N)]
		tAns[i] = M[i]
		totalFloor = M[i]
		for j in range(i - 1, -1, -1):
			tAns[j] = min(M[j], tAns[j + 1])
			totalFloor += tAns[j]
		for k in range(i + 1, N):
			tAns[k] = min(M[k], tAns[k - 1])
			totalFloor += tAns[k]
		if tAns[0] <= 0 or tAns[N - 1] <= 0:
			continue
		if maxFloor < totalFloor:
			maxFloor = totalFloor
			for (a, b) in enumerate(tAns):
				Ans[a] = b
	print(' '.join(map(str, Ans)))
	return 0
solve()


from typing import List
import collections
import bisect
import itertools
import functools
from collections import defaultdict
import collections
from fractions import gcd
import heapq
from math import ceil, sqrt
import sys
sys.setrecursionlimit(50000)
from collections import defaultdict
n = int(input())
M = list(map(int, input().split()))
ret = 0
retA = []
for i in range(n):
	P = M[:]
	total = P[i]
	for j in range(i - 1, -1, -1):
		P[j] = min(P[j], P[j + 1])
		total += P[j]
	for j in range(i + 1, n):
		P[j] = min(P[j], P[j - 1])
		total += P[j]
	if ret < total:
		ret = total
		retA = P
print(' '.join((str(i) for i in retA)))


n = int(input())
sp = list(map(int, input().split()))
forw = [0 for i in range(n)]
backw = [0 for i in range(n)]
stack = [(0, -1, 0)]
for i in range(n):
	while sp[i] <= stack[-1][0]:
		stack.pop()
	stack.append((sp[i], i, (i - stack[-1][1]) * sp[i] + stack[-1][2]))
	forw[i] = stack[-1][2]
revsp = sp[::-1]
stack = [(0, -1, 0)]
for i in range(n):
	while revsp[i] <= stack[-1][0]:
		stack.pop()
	stack.append((revsp[i], i, (i - stack[-1][1]) * revsp[i] + stack[-1][2]))
	backw[i] = stack[-1][2]
backw = backw[::-1]
center = 0
ans_center = 0
for i in range(n):
	if ans_center < backw[i] + forw[i] - sp[i]:
		center = i
		ans_center = backw[i] + forw[i] - sp[i]
ans = [0 for i in range(n)]
ans[center] = sp[center]
cur = sp[center]
for i in range(center - 1, -1, -1):
	cur = min(cur, sp[i])
	ans[i] = cur
cur = sp[center]
for i in range(center + 1, n, 1):
	cur = min(cur, sp[i])
	ans[i] = cur
print(*ans)


N = int(input())
B = list(map(int, input().split(' ')))
C = B.copy()
Ans = []
maxi = -1000000000.0
flag = 0
m = C[0]
n = C[-1]
for i in range(1, len(C)):
	if not C[i] <= C[i - 1]:
		flag = 1
		break
if flag == 0:
	print(*B)
else:
	flag = 0
	F = C[::-1]
	for i in range(1, len(F)):
		if not F[i] <= F[i - 1]:
			flag = 1
			break
	if flag == 0:
		print(*B)
	else:
		for i in range(len(B)):
			for j in range(i - 1, -1, -1):
				C[j] = min(C[j], C[j + 1])
			for j in range(i + 1, len(B)):
				C[j] = min(C[j], C[j - 1])
			d = sum(C)
			if d > maxi:
				maxi = d
				Ans = C.copy()
			C = B.copy()
		print(*Ans)


from collections import defaultdict, Counter
from bisect import bisect, bisect_left
from math import sqrt, gcd, ceil, factorial
from heapq import heapify, heappush, heappop
MOD = 10 ** 9 + 7
inf = float('inf')
ans_ = []

def nin():
	return int(input())

def ninf():
	return int(file.readline())

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

def stf():
	return file.readline().strip()

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

def readf():
	return list(map(int, file.readline().strip().split()))
ans_ = []

def mini(arr):
	n = len(arr)
	stk = [0]
	a = [-1] * n
	for i in range(n):
		while stk and arr[stk[-1]] >= arr[i]:
			stk.pop()
		if stk:
			a[i] = stk[-1]
		stk.append(i)
	return a

def solve():
	n = nin()
	arr = read()
	lmin = mini(arr)
	rmin = [n - i - 1 for i in mini(arr[::-1])[::-1]]
	pre = [0] * n
	suf = [0] * n
	for i in range(n):
		(x, y) = (i, n - i - 1)
		if lmin[x] == -1:
			pre[x] = (x + 1) * arr[x]
		else:
			pre[x] = pre[lmin[x]] + (x - lmin[i]) * arr[x]
		if rmin[y] == n:
			suf[y] = (n - y) * arr[y]
		else:
			suf[y] = suf[rmin[y]] + (rmin[y] - y) * arr[y]
	(mx, ind) = (0, -1)
	for i in range(n):
		if i == 0:
			tem = suf[i]
		elif i == n - 1:
			tem = pre[i]
		else:
			tem = pre[i] + suf[i] - arr[i]
		if tem > mx:
			mx = tem
			ind = i
	(ans, i) = ([0] * n, ind)
	while i != -1:
		for x in range(i, lmin[i], -1):
			ans[x] = arr[i]
		i = lmin[i]
	i = ind
	while i != n:
		for x in range(i, rmin[i]):
			ans[x] = arr[i]
		i = rmin[i]
	ans_.append(ans)
solve()
for i in ans_:
	print(*i)


def set_inc(lis6):
	lis6 = lis6[::-1]
	if len(lis6) != 0:
		count = lis6[0]
		for j1 in range(len(lis6)):
			if lis6[j1] > count:
				lis6[j1] = count
			count = lis6[j1]
	return lis6[::-1]

def set_dec(lis5):
	if len(lis5) != 0:
		count2 = lis5[0]
		for j2 in range(len(lis5)):
			if lis5[j2] > count2:
				lis5[j2] = count2
			count2 = lis5[j2]
	return lis5

def peak(lis, a1):
	lis2 = []
	a2 = max(lis)
	for i in range(a1):
		if lis[i] == a2:
			lis2.append(i)
	return lis2
n1 = int(input())
m1 = list(map(int, input().split(' ')))
a3 = max(m1)
lis4 = []
for mx in range(len(m1)):
	lis3 = set_inc(m1[0:mx]) + set_dec(m1[mx:n1])
	lis4.append(sum(lis3))
q1 = peak(lis4, len(lis4))[0]
mx2 = q1
print(' '.join(list(map(str, set_inc(m1[0:q1]) + set_dec(m1[q1:n1])))))


import sys

def solve(N, M):

	def makeMountain(i):
		m = M[i]
		curr = m
		left = []
		for j in range(i - 1, -1, -1):
			curr = min(M[j], curr)
			left.append(curr)
		curr = m
		right = []
		for j in range(i + 1, len(M)):
			curr = min(M[j], curr)
			right.append(curr)
		return left[::-1] + [m] + right
	best = tuple()
	for i in range(len(M)):
		total = sum(makeMountain(i))
		best = max(best, (total, i))
	i = best[1]
	return ' '.join(map(str, makeMountain(i)))
input = sys.stdin.readline
(N,) = map(int, input().split())
M = [int(x) for x in input().split()]
ans = solve(N, M)
print(ans)


import sys

def get_vals(n, arr, rev):
	if rev:
		arr.reverse()
	st = []
	vals = [0 for i in range(n)]
	for i in range(n):
		while len(st) != 0 and arr[st[-1]] >= arr[i]:
			st.pop()
		vals[i] = (i + 1) * arr[i] if len(st) == 0 else (i - st[-1]) * arr[i] + vals[st[-1]]
		st.append(i)
	if rev:
		arr.reverse()
		vals.reverse()
	return vals

def main():
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	lf = get_vals(n, arr, False)
	rf = get_vals(n, arr, True)
	(val, pos) = max(((lf[i] + rf[i] - arr[i], i) for i in range(n)))
	for i in range(pos - 1, -1, -1):
		arr[i] = min(arr[i], arr[i + 1])
	for i in range(pos + 1, n):
		arr[i] = min(arr[i], arr[i - 1])
	sys.stdout.write(' '.join(map(str, arr)) + '\n')
main()


inf = int(2000000000.0) + 1
n = int(input())
a = [-inf] + [int(x) for x in input().split()] + [-inf]

def minLR(n, a):
	ans = [0] * (n + 2)
	st = [0]
	for i in range(1, n + 2):
		while a[st[-1]] > a[i]:
			ans[st.pop()] = i
		st.append(i)
	return ans[1:-1]
a.reverse()
ansL = minLR(n, a)
ansL.reverse()
newL = [n - item for item in ansL]
a.reverse()
ansR = minLR(n, a)
newR = [item - 1 for item in ansR]
L = []
for i in range(n):
	if newL[i] == -1:
		L += [(i + 1) * a[i + 1]]
	else:
		L += [L[newL[i]] + (i - newL[i]) * a[i + 1]]
R = [0] * n
for i in range(n - 1, -1, -1):
	if newR[i] == n:
		R[i] = (n - i) * a[i + 1]
	else:
		R[i] = R[newR[i]] + (newR[i] - i) * a[i + 1]
m = 0
index = -1
for i in range(n):
	t = L[i] + R[i] - a[i + 1]
	if t > m:
		m = t
		index = i
b = a[1:-1]
for i in range(index + 1, n):
	if b[i] > b[i - 1]:
		b[i] = b[i - 1]
for i in range(index - 1, -1, -1):
	if b[i] > b[i + 1]:
		b[i] = b[i + 1]
for i in range(n):
	print(str(b[i]) + ' ', end='')


inf = float('inf')
n = int(input())
a = [-inf] + [int(i) for i in input().split()] + [-inf]
for x in (0, 1):
	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]
	res = [0] * (n + 2)
	for i in range(1, len(a)):
		res[i] = res[ns[i]] + a[i] * (i - ns[i]) if ns[i] else i * a[i]
	a = a[::-1]
	if x:
		ri = res[:][::-1]
	else:
		le = res[:]
su = [i + j - k for (i, j, k) in zip(le, ri, a)][1:-1]
ma = su.index(max(su))
a = a[1:-1]
for i in range(ma + 1, n):
	a[i] = min(a[i], a[i - 1])
for i in range(ma - 1, -1, -1):
	a[i] = min(a[i], a[i + 1])
print(*a)


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


n = int(input())
a = [0] + list(map(int, input().split())) + [0]
left = [0] * (n + 2)
right = [0] * (n + 2)
left_min = [0] * (n + 2)
right_min = [0] * (n + 2)
ans = [0] * n
st = [0]
for i in range(1, n + 2):
	while a[st[-1]] > a[i]:
		right_min[st.pop()] = i
	st.append(i)
for j in range(n, -1, -1):
	while a[st[-1]] > a[j]:
		left_min[st.pop()] = j
	st.append(j)
for p in range(1, n + 2):
	if a[p] >= a[p - 1]:
		left[p] = left[p - 1] + a[p]
	else:
		left[p] = left[left_min[p]] + a[p] * (p - left_min[p])
for q in range(n + 1, 0, -1):
	if a[q] <= a[q - 1]:
		right[q - 1] = right[q] + a[q - 1]
	else:
		right[q - 1] = right[right_min[q - 1]] + a[q - 1] * (right_min[q - 1] - q + 1)
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[imax - 1] = a[imax]
for m in range(imax, n):
	ans[m] = min(ans[m - 1], a[m + 1])
for r in range(imax - 2, -1, -1):
	ans[r] = min(ans[r + 1], a[r + 1])
print(' '.join(map(str, ans)))


n = int(input())
ch = input()
L4 = [int(i) for i in ch.split()]
ind_max = 0
L2 = [0] * n
L3 = [''] * n
for ind_max in range(n):
	L = L4.copy()
	for i in range(ind_max + 1, n):
		L[i] = min(L[i - 1], L[i])
	i = ind_max - 1
	while True:
		if i < 0:
			break
		L[i] = min(L[i], L[i + 1])
		i = i - 1
	L2[ind_max] = sum(L)
	L3[ind_max] = L
ind_max = -1
maxx = max(L2)
for i in range(n):
	if L2[i] == maxx:
		s = ''
		for i in L3[i]:
			s += str(i) + ' '
		print(s)
		break


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


def main():
	import sys
	input = sys.stdin.buffer.readline
	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)
main()


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 f():

	def calcLeftSum(n, M):

		def binSearch1(low, high, memo, e):
			if memo[low][0] >= e:
				return low
			else:
				while high - low > 1:
					mid = (low + high) // 2
					if memo[mid][0] >= e:
						high = mid
					else:
						low = mid
				return high
		memo = [[0, 0] for i in range(n + 1)]
		memo[-1][1] = -1
		leftSum = [0] * (n + 1)
		memo[0] = [M[0], 0]
		leftSum[0] = M[0]
		cur = 0
		for i in range(1, n):
			if M[i] > memo[cur][0]:
				cur += 1
				memo[cur][0] = M[i]
			elif M[i] < memo[cur][0]:
				p = binSearch1(0, cur, memo, M[i])
				cur = p
				memo[cur][0] = M[i]
			memo[cur][1] = i
			leftSum[i] = leftSum[memo[cur - 1][1]] + memo[cur][0] * (memo[cur][1] - memo[cur - 1][1])
		return leftSum
	leftSum = calcLeftSum(n, M)[:n]
	rightSum = calcLeftSum(n, M[::-1])[n - 1::-1]
	s = 0
	max = 0
	for i in range(n):
		x = leftSum[i] + rightSum[i] - M[i]
		if x > max:
			s = i
			max = x
	rt = [0] * n
	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)))
n = int(input())
M = [int(s) for s in input().split()]
f()


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,) = map(int, input().split())
ms = 0
for idx in range(n):
	(ans, s) = ([], 0)
	c = a[idx]
	for i in range(idx, -1, -1):
		c = min(c, a[i])
		ans.append(c)
		s += c
	ans = ans[::-1]
	c = a[idx]
	for i in range(idx + 1, n):
		c = min(c, a[i])
		ans.append(c)
		s += c
	if s > ms:
		mans = ans
		ms = s
print(*mans)


import sys
n = int(sys.stdin.readline())
inf = int(2000000000.0) + 1
m = [-inf] + [int(x) for x in sys.stdin.readline().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)
st1 = [0]
for i in range(n + 1, 0, -1):
	while m[st1[-1]] > m[i]:
		l[st1.pop()] = i
	st1.append(i)
a = [0] * (n + 2)
for i in range(1, n + 1):
	a[i] = 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):
	if m[i + 1] < m[i]:
		m[i] = m[i + 1]
q += 1
for i in range(1, q + 1):
	print(m[i])
for i in range(q + 1, n + 1):
	if m[i - 1] < m[i]:
		m[i] = m[i - 1]
	print(m[i])


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


from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
sm = 0
b = deepcopy(a)
ans = deepcopy(a)
for i in range(n):
	mn = a[i]
	b[i] = a[i]
	for j in range(i - 1, -1, -1):
		if mn < a[j]:
			b[j] = mn
		else:
			mn = a[j]
			b[j] = a[j]
	mx = a[i]
	for k in range(i + 1, n):
		if mx < a[k]:
			b[k] = mx
		else:
			mx = a[k]
			b[k] = a[k]
	if sm <= sum(b):
		ans = deepcopy(b)
		sm = sum(b)
print(*ans)


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


_DEBUG = 0
_W = 7

def debug(obj):
	if _DEBUG:
		print(f"{obj}.{' ' * (_W - len(obj))}", end='')
		obj = eval(obj)
		if type(obj) is list:
			print('\t'.join(list(map(str, obj))))
		else:
			print(obj)

def inp():
	global n, arr
	if _DEBUG:
		n = 3
		arr = [10, 6, 8]
	else:
		n = int(input())
		arr = list(map(int, input().split()))
inf = int(1000000000.0) + 1

def getRLess(arrn, n):
	arr = [-inf] + arrn
	answ = [0] * n
	st = [0]
	for i in range(1, n + 1):
		while arr[st[-1]] > arr[i]:
			answ[st.pop() - 1] = i - 1
		st.append(i)
	for i in st[1:]:
		answ[i - 1] = -1
	return answ

def getLLess(arrn, n):
	arr = arrn + [-inf]
	answ = [0] * n
	st = [n]
	for i in range(n - 1, -1, -1):
		while arr[st[-1]] > arr[i]:
			answ[st.pop()] = i
		st.append(i)
	for i in st[1:]:
		answ[i] = -1
	return answ
inp()
(ll, rl) = (getLLess(arr, n), getRLess(arr, n))
debug('arr')
debug('ll')
debug('rl')
(dpl, dpr) = ([0] * (n + 1), [0] * (n + 1))
(prevl, prevr) = ([-1] * n, [n] * n)
for i in range(1, n + 1):
	cl = ll[i - 1]
	if cl == -1:
		dpl[i] = arr[i - 1] * i
	else:
		dpl[i] = dpl[cl + 1] + arr[i - 1] * (i - cl - 1)
		prevl[i - 1] = cl
debug('dpl')
debug('prevl')
for i in range(1, n + 1):
	indEl = n - i
	indElLR = rl[indEl]
	if indElLR == -1:
		dpr[i] = i * arr[indEl]
	else:
		dpr[i] = dpr[n - indElLR] + arr[indEl] * (indElLR - indEl)
		prevr[indEl] = indElLR
debug('dpr')
debug('prevr')
mx = -1
lll = 0
for i in range(n + 1):
	if dpl[i] + dpr[n - i] >= mx:
		lll = i
		mx = dpl[i] + dpr[n - i]
debug('lll')
crl = lll - 1
resl = []
while crl != -1:
	resl.extend([str(arr[crl])] * (crl - prevl[crl]))
	crl = prevl[crl]
crr = lll
debug('crr')
resr = []
while crr != n:
	resr.extend([str(arr[crr])] * (prevr[crr] - crr))
	crr = prevr[crr]
debug('resl')
debug('resr')
print(' '.join(resl[::-1] + resr))


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


n = int(input())
a = list(map(int, input().split()))
Map = {}
ans = 0
for ind in range(n):
	b = [i for i in a]
	m = a[ind]
	i = ind - 1
	j = ind + 1
	Min = m
	s = m
	while i >= 0:
		Min = min(Min, b[i])
		b[i] = Min
		i -= 1
		s += Min
	Min = m
	while j < n:
		Min = min(Min, b[j])
		b[j] = Min
		j += 1
		s += Min
	Map[s] = b
	ans = max(ans, s)
print(*Map[ans])


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 sys
from functools import partial
from typing import List, Tuple
sys.setrecursionlimit(10000000)
n = int(input())
limits = tuple(map(int, input().split()))
order = tuple(sorted(range(n), key=limits.__getitem__))

def _solve(limits: Tuple[int], order: Tuple[int], heights: List[int], low: int, high: int) -> Tuple[int, List[int]]:
	if low == high:
		index = low
		heights[index] = limits[index]
		return (limits[index], heights)
	for index in order:
		if index >= low and index <= high:
			break
	else:
		msg = f'Could not find an appropriate index for low = {low}, high = {high}'
		raise ValueError(msg)
	if low != index:
		left_heights = heights[:]
		(left_sum, left_heights) = solve(left_heights, low, index - 1)
	else:
		left_sum = -1
	if high != index:
		right_heights = heights[:]
		(right_sum, right_heights) = solve(right_heights, index + 1, high)
	else:
		right_sum = -1
	height = limits[index]
	if left_sum + height * (high - index + 1) > right_sum + height * (index - low + 1):
		heights = left_heights
		for i in range(index, high + 1):
			heights[i] = height
	else:
		heights = right_heights
		for i in range(low, index + 1):
			heights[i] = height
	return (sum(heights[low:high + 1]), heights)
solve = partial(_solve, limits, order)
print(*solve([0] * n, 0, n - 1)[1])


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 = [int(i) for i in input().split()]
best = 0
bestl = []
for i in range(n):
	temp = [i for i in l]
	res = l[i]
	j = i - 1
	while j >= 0:
		temp[j] = min(temp[j], temp[j + 1])
		res += temp[j]
		j -= 1
	j = i + 1
	while j < n:
		temp[j] = min(temp[j], temp[j - 1])
		res += temp[j]
		j += 1
	if res > best:
		best = res
		bestl = [i for i in temp]
print(*bestl)


n = int(input())
o_a = list(map(int, input().split()))
max_a = None
max_a_val = 0
for mode in range(n):
	a = o_a.copy()
	cur_max = a[mode]
	for right in range(mode + 1, n):
		if a[right] <= cur_max:
			cur_max = a[right]
		else:
			a[right] = cur_max
	cur_max = a[mode]
	for left in range(mode - 1, -1, -1):
		if a[left] <= cur_max:
			cur_max = a[left]
		else:
			a[left] = cur_max
	a_val = sum(a)
	if a_val > max_a_val:
		max_a_val = a_val
		max_a = a
print(' '.join(map(str, max_a)))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

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

def iinp():
	return int(inp())

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

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

def lmp():
	return list(mp())

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

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

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

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

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
n = iinp()
arr = lmp()
mx = 0
ansl = l1d(n)
for i in range(n):
	j = i - 1
	c = arr[i]
	mn = arr[i]
	ansl[i] = arr[i]
	while j >= 0:
		mn = min(mn, arr[j])
		ansl[j] = mn
		c += mn
		j -= 1
	j = i + 1
	mn = arr[i]
	while j < n:
		mn = min(mn, arr[j])
		ansl[j] = mn
		c += mn
		j += 1
	if c > mx:
		ans = ansl.copy()
		mx = c
print(*ans)


n = int(input())
x = list(map(int, input().split()))
(dp, dp2, s, s2, d, d2) = ([0] * n, [0] * n, [], [], {}, {})
for i in range(n):
	if i == 0:
		dp[0] = x[0]
	elif x[i] >= x[i - 1]:
		dp[i] = dp[i - 1] + x[i]
		s.append(x[i - 1])
	else:
		while len(s) > 0 and x[i] < s[len(s) - 1]:
			s.pop()
		if len(s) == 0:
			dp[i] = x[i] * (i + 1)
		else:
			dp[i] = dp[d[s[len(s) - 1]]] + x[i] * (i - d[s[len(s) - 1]])
	d[x[i]] = i
for i in range(n - 1, -1, -1):
	if i == n - 1:
		dp2[i] = x[i]
	elif x[i] >= x[i + 1]:
		dp2[i] = dp2[i + 1] + x[i]
		s2.append(x[i + 1])
	else:
		while len(s2) > 0 and x[i] < s2[len(s2) - 1]:
			s2.pop()
		if len(s2) == 0:
			dp2[i] = x[i] * (n - i)
		else:
			dp2[i] = dp2[d2[s2[len(s2) - 1]]] + x[i] * (d2[s2[len(s2) - 1]] - i)
	d2[x[i]] = i
(ans, ma) = (0, 0)
for i in range(n):
	if dp[i] + dp2[i] - x[i] > ma:
		ma = dp[i] + dp2[i] - x[i]
		ans = i
(lim, y) = (x[ans], [0] * n)
y[ans] = x[ans]
for i in range(ans + 1, n):
	y[i] = min(x[i], lim)
	lim = y[i]
lim = x[ans]
for i in range(ans - 1, -1, -1):
	y[i] = min(lim, x[i])
	lim = y[i]
for i in range(n):
	print(y[i], end=' ')


t = int(input())
c = input().split(' ')
estado = 'nada'
for i in range(0, t):
	c[i] = int(c[i])
max = []
tot = 0
spikes = [0]
for i in range(0, t - 1):
	if c[i] > c[i + 1]:
		estado = 'bajada'
	elif c[i] < c[i + 1] and estado == 'bajada':
		spikes.append(i)
		spikes.append(i)
		estado = 'subida'
spikes.append(len(c) - 1)
totini = 0
for i in range(0, int(len(spikes) / 2)):
	tot = 0
	red = []
	for j in range(spikes[2 * i], spikes[2 * i + 1] + 1):
		red.append(c[j])
	for j in range(spikes[2 * i] - 1, -1, -1):
		if c[j] > red[0]:
			red.insert(0, red[0])
		else:
			red.insert(0, c[j])
	for j in range(spikes[2 * i + 1], t - 1):
		if red[j] >= c[j + 1]:
			red.append(c[j + 1])
		else:
			red.append(red[j])
	for j in range(0, len(red)):
		tot += red[j]
	if tot > totini:
		max = red
		totini = tot
for i in range(0, t):
	print(max[i], end=' ')


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


import os
import sys
from io import BytesIO, IOBase

def solution(arr, n):
	if n == 1:
		print(arr[0])
		return
	res = 0
	test = arr[:]
	for i in range(n):
		peak = test[i]
		for j in range(i - 1, -1, -1):
			if test[j] > test[j + 1]:
				test[j] = test[j + 1]
		for j in range(i + 1, n):
			if test[j] > test[j - 1]:
				test[j] = test[j - 1]
		if sum(test) > res:
			res = sum(test)
			res_arr = test
		test = arr[:]
	for x in res_arr:
		print(x, end=' ')

def main():
	n = int(input())
	arr = [int(x) for x in input().split()]
	solution(arr, n)
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()


input = __import__('sys').stdin.readline
print = __import__('sys').stdout.write
n = int(input())
height = list(map(int, input().split()))
l = [0 for _ in range(n)]
r = [0 for _ in range(n)]
l_min = 10 ** 9 + 1
for i in range(n):
	if height[i] <= l_min:
		l_min = height[i]
		l[i] = (i + 1) * l_min
	else:
		j = i - 1
		while 0 <= j:
			if height[j] < height[i]:
				l[i] += l[j]
				j += 1
				break
			j -= 1
		l[i] += (i - j + 1) * height[i]
l_min = 10 ** 9 + 1
for i in range(n - 1, -1, -1):
	if height[i] <= l_min:
		l_min = height[i]
		r[i] = (n - i) * l_min
	else:
		j = i + 1
		while j <= n - 1:
			if height[j] < height[i]:
				r[i] += r[j]
				break
			j += 1
		r[i] += (j - i) * height[i]
max_val = 0
max_idx = 0
for i in range(n):
	if l[i] + r[i] - height[i] > max_val:
		max_val = l[i] + r[i] - height[i]
		max_idx = i
tmp = [0 for _ in range(n)]
current_min = height[max_idx]
for i in range(max_idx - 1, -1, -1):
	current_min = min(current_min, height[i])
	tmp[i] = current_min
current_min = height[max_idx]
for i in range(max_idx + 1, n, 1):
	current_min = min(current_min, height[i])
	tmp[i] = current_min
tmp[max_idx] = height[max_idx]
for i in tmp:
	print(f'{i} ')


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)


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[:]
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)


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


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
	f = iter(open('C.txt').readlines())

	def input():
		return next(f)
else:
	input = lambda : sys.stdin.readline().strip()
fprint = lambda *args: print(*args, flush=True)
n = int(input())
a = list(map(int, input().split()))
bestSum = -1
best = []
for k in range(n):
	l = []
	r = []
	m = a[k]
	for i in reversed(a[:k]):
		m = min(m, i)
		l.append(m)
	m = a[k]
	for i in a[k + 1:]:
		m = min(m, i)
		r.append(m)
	res = list(reversed(l)) + [a[k]] + r
	if sum(res) > bestSum:
		best = res
		bestSum = sum(res)
print(' '.join(map(str, best)))


def rmin():
	global a, n
	st = [0]
	r = [0] * (n + 2)
	for i in range(1, n + 2):
		while a[st[-1]] > a[i]:
			j = st.pop()
			r[j] = i
		st.append(i)
	return r

def lmin():
	global a, n
	st = [0]
	l = [0] * (n + 2)
	j = n
	for i in range(n + 1, 0, -1):
		while a[st[-1]] > a[i]:
			j = st.pop()
			l[j] = i
		st.append(i)
	return l
inf = 10 ** 9
n = int(input())
a = [-inf] + list(map(int, input().split())) + [-inf]
rr = rmin()
ll = lmin()
l = [0] * (n + 2)
r = [0] * (n + 2)
for i in range(1, n + 1):
	if ll[i] == 0:
		l[i] = i * a[i]
	else:
		j = ll[i]
		l[i] = l[j] + (i - j) * a[i]
for i in range(n, 0, -1):
	if rr[i] == n + 1:
		r[i] = (n - i + 1) * a[i]
	else:
		j = rr[i]
		r[i] = r[j] + (j - i) * a[i]
ib = 0
mnb = 0
for i in range(1, n + 1):
	s = l[i] + r[i] - a[i]
	if s > mnb:
		ib = i
		mnb = s
ans = [0] * (n + 2)
ans[ib] = a[ib]
for i in range(ib - 1, 0, -1):
	ans[i] = min(ans[i + 1], a[i])
for i in range(ib + 1, n + 1):
	ans[i] = min(ans[i - 1], a[i])
print(*ans[1:-1])


import sys
input = sys.stdin.readline
print = sys.stdout.write
n = int(input())
s = [int(i) for i in input().split()]

def nearest_min(dir='l'):
	stack = []
	arr = [-1] * n
	if dir == 'l':
		x = range(n)
	elif dir == 'r':
		x = range(n - 1, -1, -1)
	for i in x:
		top = -1
		while stack:
			if stack[top][0] < s[i]:
				arr[i] = stack[top][1]
				break
			else:
				stack.pop()
		stack.append([s[i], i])
	return arr
rm = nearest_min('r')
lm = nearest_min('l')
max_sum = 0
for i in range(n):
	rsum = 0
	(j, k) = (i, i)
	ans2 = [0] * n
	while j < n:
		if rm[j] == -1:
			rsum += s[j] * (n - j)
			ans2[j:n] = [s[j]] * (n - j)
			break
		else:
			rsum += s[j] * (rm[j] - j)
			ans2[j:rm[j]] = [s[j]] * (rm[j] - j)
			j = rm[j]
	lsum = 0
	while k >= 0:
		if lm[k] == -1:
			lsum += s[k] * (k + 1)
			ans2[k::-1] = [s[k]] * (k + 1)
			break
		else:
			lsum += s[k] * (k - lm[k])
			ans2[k:lm[k]:-1] = [s[k]] * (k - lm[k])
			k = lm[k]
	tsum = lsum + rsum - s[i]
	if tsum > max_sum:
		max_sum = tsum
		ans = ans2
		pivot = i
print(' '.join(map(str, 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 factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

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

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

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def ctd(chr):
	return ord(chr) - ord('a')
mod = 998244353
INF = float('inf')

def main():
	n = N()
	arr = RLL()

	def c(carr):
		q = [[-1, -1]]
		sma = []
		sm = 0
		for (i, v) in enumerate(carr):
			while q and v < q[-1][0]:
				(vl, ind) = q.pop()
				sm += (ind - q[-1][1]) * v
				sm -= (ind - q[-1][1]) * vl
			sm += v
			q.append((v, i))
			sma.append(sm)
		return (sma, q)
	(arra, q1) = c(arr)
	(arrb, q2) = c(arr[::-1])
	arrb.reverse()
	res = 0
	ind = -1
	for i in range(n):
		now = arra[i] + arrb[i] - arr[i]
		if now > res:
			res = now
			ind = i
	res = [0] * n
	res[ind] = arr[ind]
	mi = arr[ind]
	for i in range(ind - 1, -1, -1):
		mi = min(mi, arr[i])
		res[i] = mi
	mi = arr[ind]
	for i in range(ind + 1, n):
		mi = min(mi, arr[i])
		res[i] = mi
	print(*res)
main()


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


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

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

def si():
	return input().rstrip()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	n = ii()
	a = li()
	ans = 0
	for i in range(n):
		x = a[i]
		c = a[i]
		for j in range(i + 1, n):
			c = min(a[j], c)
			x += c
		c = a[i]
		for j in range(i - 1, -1, -1):
			c = min(a[j], c)
			x += c
		if x > ans:
			ans = x
			pos = i
	c = a[pos]
	for i in range(pos + 1, n):
		c = min(a[i], c)
		a[i] = c
	c = a[pos]
	for i in range(pos - 1, -1, -1):
		c = min(a[i], c)
		a[i] = c
	print(*a)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


n = int(input())
arr = list(map(int, input().split()))
b = []
for i in range(n):
	b.append([arr[i], i])

def sortFirst(val):
	return val[0]
b.sort(key=sortFirst, reverse=True)
min_jog = None
index = -1
for i in range(n):
	jog = 0
	a = arr[:]
	if b[i][1] != 0 and b[i][1] != n - 1:
		for j in range(b[i][1] - 1, -1, -1):
			if a[j] > a[j + 1]:
				jog += a[j] - a[j + 1]
				a[j] = a[j + 1]
		for j in range(b[i][1] + 1, n):
			if a[j] > a[j - 1]:
				jog += a[j] - a[j - 1]
				a[j] = a[j - 1]
	elif b[i][1] == 0:
		for j in range(b[i][1] + 1, n):
			if a[j] > a[j - 1]:
				jog += a[j] - a[j - 1]
				a[j] = a[j - 1]
	else:
		for j in range(b[i][1] - 1, -1, -1):
			if a[j] > a[j + 1]:
				jog += a[j] - a[j + 1]
				a[j] = a[j + 1]
	if min_jog == None:
		min_jog = jog
		index = b[i][1]
	elif jog < min_jog:
		min_jog = jog
		index = b[i][1]
if index != 0 and index != n - 1:
	for j in range(index - 1, -1, -1):
		if arr[j] > arr[j + 1]:
			arr[j] = arr[j + 1]
	for j in range(index + 1, n):
		if arr[j] > arr[j - 1]:
			arr[j] = arr[j - 1]
elif index == 0:
	for j in range(index + 1, n):
		if arr[j] > arr[j - 1]:
			arr[j] = arr[j - 1]
else:
	for j in range(index - 1, -1, -1):
		if arr[j] > arr[j + 1]:
			arr[j] = arr[j + 1]
print(*arr, sep=' ')


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


n = int(input())
a = list(map(int, input().split()))
x = 0
y = 0
z = 0
b = list(a)
c = list(a[::-1])
l = []
for _ in range(1, len(b)):
	if b[_] > b[_ - 1]:
		b[_] = b[_ - 1]
l.append(sum(b))
for _ in range(1, len(c)):
	if c[_] > c[_ - 1]:
		c[_] = c[_ - 1]
l.append(sum(c))
c = c[::-1]
if l[0] >= l[1]:
	f = list(b)
	l.remove(l[1])
else:
	f = list(c)
	l.remove(l[0])
y = l[0]
for _ in range(1, len(a)):
	d = list(a)
	for i in range(_ - 1, -1, -1):
		if d[i] > d[i + 1]:
			d[i] = d[i + 1]
	for j in range(_ + 1, len(d)):
		if d[j] > d[j - 1]:
			d[j] = d[j - 1]
	if sum(d) >= y:
		f = list(d)
		y = sum(d)
for _ in range(len(f)):
	print(f[_], end=' ')


import heapq
n = int(input())
m = list(map(int, input().split()))
M = [m[i] for i in range(n)]
peaks = []
for i in range(0, n):
	peaks.append((i, m[i]))
floors = 0
maxf = 0
I = 0
P = 0
while peaks != []:
	(index, peak) = peaks.pop()
	floors = 0
	x = peak
	y = peak
	for i in range(n):
		if i < index:
			x = min(m[index - (i + 1)], x)
			floors += x
		if i > index:
			y = min(m[i], y)
			floors += y
		if i == index:
			floors += m[index]
	if maxf <= floors:
		maxf = floors
		I = index
		P = peak
for i in range(n):
	if i < I:
		m[I - (i + 1)] = min(m[I - i], m[I - (i + 1)])
	if i > I:
		m[i] = min(m[i], m[i - 1])
print(*m)


n = int(input())
m = list(map(int, input().split()))
ans = 0
floor = -1
for j in range(n):
	m_l = m[0:j]
	m_r = m[j:n]
	for i in range(len(m_l) - 1, 0, -1):
		if m_l[i - 1] > m_l[i]:
			m_l[i - 1] = m_l[i]
	for i in range(len(m_r) - 1):
		if m_r[i] < m_r[i + 1]:
			m_r[i + 1] = m_r[i]
	cnt = sum(m_l) + sum(m_r)
	if floor < cnt:
		floor = cnt
		ans = m_l + m_r
print(*ans)


length = int(input())
heights = list(map(int, input().split()))
result = []
max_sum = 0
for i1 in range(length):
	temp = heights.copy()
	for i2 in range(i1 + 1, length, 1):
		temp[i2] = min(temp[i2], temp[i2 - 1])
	for i2 in range(i1 - 1, -1, -1):
		temp[i2] = min(temp[i2 + 1], temp[i2])
	temp_sum = sum(temp)
	if temp_sum > max_sum:
		result = temp.copy()
		max_sum = temp_sum
print(*result)


import sys
input = sys.stdin.readline

def re1(a, i1, n):
	ref = a[i1]
	for i in range(i1, 0, -1):
		if a[i] >= a[i - 1]:
			ref = a[i - 1]
			continue
		else:
			a[i - 1] = ref
	ref = a[i1]
	for i in range(i1, n - 1):
		if a[i] >= a[i + 1]:
			ref = a[i + 1]
			continue
		else:
			a[i + 1] = ref
	return a
n = int(input())
a = list(map(int, input().split()))
f = 0
re = []
for i in range(n):
	b = a[:]
	c = re1(b, i, n)
	if sum(c) > f:
		f = sum(c)
		re = c
print(*re)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
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')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

def printPrevSmaller(arr, n):
	S = list()
	w = [-1] * n
	for i in range(n):
		while len(S) > 0 and S[-1][0] >= arr[i]:
			S.pop()
		if len(S) != 0:
			w[i] = S[-1][1]
		S.append((arr[i], i))
	return w
n = int(input())
a = list(map(int, input().split()))
ans = 0
ma = 0
left = printPrevSmaller(a, n)
right = printPrevSmaller(a[::-1], n)
right = [n - 1 - right[i] for i in range(n)]
right = right[::-1]
left1 = [0] * n
right1 = [0] * n
left1[0] = a[0]
for i in range(1, n):
	if left[i] == -1:
		r = 0
	else:
		r = left1[left[i]]
	left1[i] = r + (i - left[i]) * a[i]
right1[-1] = a[-1]
for i in range(n - 2, -1, -1):
	if right[i] == n:
		r = 0
	else:
		r = right1[right[i]]
	right1[i] = r + (right[i] - i) * a[i]
for j in range(n):
	an = -a[j]
	t = j
	if left[j] == -1:
		r = 0
	else:
		r = left1[left[j]]
	an += a[j] * (j - left[j]) + r
	j = t
	if right[j] == n:
		r = 0
	else:
		r = right1[right[j]]
	an += a[j] * (right[j] - j) + r
	j = t
	ma = max(ma, an)
	if ma == an:
		ind = j
we = [0] * n
we[ind] = a[ind]
for i in range(ind - 1, -1, -1):
	if a[i] >= we[i + 1]:
		we[i] = we[i + 1]
	else:
		we[i] = a[i]
for i in range(ind + 1, n):
	if a[i] >= we[i - 1]:
		we[i] = we[i - 1]
	else:
		we[i] = a[i]
print(*we, sep=' ')


n = int(input())
a = list(map(int, input().split()))
maxx = 0
for i in range(n):
	s = a[i]
	k = a[i]
	b = [0] * n
	b[i] = k
	for j in range(i + 1, n):
		if a[j] > k:
			b[j] = k
		else:
			b[j] = a[j]
			k = a[j]
		s += b[j]
	k = a[i]
	for j in range(i - 1, -1, -1):
		if a[j] > k:
			b[j] = k
		else:
			b[j] = a[j]
			k = a[j]
		s += b[j]
	if s > maxx:
		maxx = s
		c = b
print(' '.join((str(x) for x in c)))


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


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
can = []
maxi = 0
for i in range(n):
	tmp = a[i]
	now = a[i]
	tmp2 = []
	tmp3 = []
	for j in range(i + 1, n):
		now = min(now, a[j])
		tmp += now
		tmp2.append(now)
	now = a[i]
	for j in range(i)[::-1]:
		now = min(now, a[j])
		tmp += now
		tmp3.append(now)
	tmp3.reverse()
	tmp4 = tmp3 + [a[i]] + tmp2
	if maxi < sum(tmp4):
		maxi = sum(tmp4)
		maxj = tmp4
print(' '.join(list(map(str, maxj))))


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

def is_sorted(test_list):
	flag = False
	if all((test_list[i] <= test_list[i + 1] for i in range(len(test_list) - 1))):
		flag = True
	if not flag and all((test_list[i] >= test_list[i + 1] for i in range(len(test_list) - 1))):
		flag = True
	return flag

def par(t):
	if is_sorted(t) or len(t) < 3:
		return t
	mini = min(t)
	im = t.index(mini)
	gauche = par(t[0:im])
	droite = par(t[im + 1:len(t)])
	result1 = gauche + [mini] + [min(mini, a) for a in droite]
	result2 = [min(mini, a) for a in gauche] + [mini] + droite
	if sum(result1) > sum(result2):
		return result1
	else:
		return result2
result = par(m)
print(' '.join(map(str, result)))


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


def opt(a):
	if len(a) <= 1:
		return a
	mi = a.index(min(a))
	sl = opt(a[:mi])
	sr = opt(a[mi + 1:])
	if sum(sl) + (len(a) - mi) * a[mi] > sum(sr) + (mi + 1) * a[mi]:
		return sl + [a[mi]] * (len(a) - mi)
	else:
		return [a[mi]] * (mi + 1) + sr
n = int(input())
m = list(map(int, input().split()))
print(*opt(m))


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)


numplots = int(input())
limfloors = list(map(int, input().split()))
listsums = []
answers = {}
minsofar = max(limfloors) + 1
indexminsofar = 0
for k in range(numplots):
	sum1 = 0
	answerlist = []
	max1 = limfloors[k]
	if max1 <= minsofar:
		minsofar = max1
		indexminsofar = k
	answerlist += [minsofar] * (indexminsofar + 1)
	sum1 += minsofar * (indexminsofar + 1)
	minsofar2 = max1
	templist = []
	for j in range(1, k - indexminsofar):
		if limfloors[k - j] <= minsofar2:
			sum1 += limfloors[k - j]
			minsofar2 = limfloors[k - j]
			templist.append(limfloors[k - j])
		else:
			sum1 += minsofar2
			templist.append(minsofar2)
	answerlist += templist[::-1]
	minsofar3 = max1
	for l in range(k, numplots):
		if limfloors[l] <= minsofar3:
			sum1 += limfloors[l]
			minsofar3 = limfloors[l]
			answerlist.append(limfloors[l])
		else:
			sum1 += minsofar3
			answerlist.append(minsofar3)
	if len(answerlist) > numplots:
		answerlist.pop(0)
		sum1 += -answerlist[0]
	listsums.append(sum1)
	answers[sum1] = answerlist
maximumsum = max(listsums)
realanswer = list(map(str, answers[maximumsum]))
print(' '.join(realanswer))


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())
m = list(map(int, input().split()))

def is_sorted(test_list, begin, end):
	flag = False
	if all((test_list[i] <= test_list[i + 1] for i in range(begin, end - 1))):
		flag = True
	if not flag and all((test_list[i] >= test_list[i + 1] for i in range(begin, end - 1))):
		flag = True
	return flag

def par(t, begin, end):
	if is_sorted(t, begin, end) or end - begin < 3:
		return (sum(t[begin:end]), [], [])
	mini = t[begin]
	im = begin
	for i in range(begin, end):
		if t[i] < mini:
			mini = t[i]
			im = i
	gauche = par(t, begin, im)
	droite = par(t, im + 1, end)
	rabDroite = 0
	for i in range(im, end):
		rabDroite += min(t[i], mini)
	rabGauche = 0
	for i in range(begin, im + 1):
		rabGauche += min(t[i], mini)
	if gauche[0] + rabDroite > droite[0] + rabGauche:
		return (gauche[0] + rabDroite, gauche[1], gauche[2] + [(im, mini)])
	else:
		return (droite[0] + rabGauche, droite[1] + [(im, mini)], droite[2])
result = par(m, 0, n)
if result[1]:
	result[1].sort(reverse=True)
	pos = 0
	for i in range(result[1][0][0], -1, -1):
		if pos < len(result[1]) - 1 and i == result[1][pos + 1][0]:
			pos = pos + 1
		if m[i] > result[1][pos][1]:
			m[i] = result[1][pos][1]
if result[2]:
	result[2].sort()
	pos = 0
	for i in range(result[2][0][0], len(m)):
		if pos < len(result[2]) - 1 and i == result[2][pos + 1][0]:
			pos = pos + 1
		if m[i] > result[2][pos][1]:
			m[i] = result[2][pos][1]
print(' '.join(map(str, m)))


import sys
import itertools

def find_floors(peak, ms):
	score = 0
	current_height = ms[peak]
	for m in reversed(ms[:peak]):
		current_height = min(m, current_height)
		score += current_height
	current_height = ms[peak]
	for m in ms[peak:]:
		current_height = min(m, current_height)
		score += current_height
	return score

def print_soln(peak, ms):
	output = [-1 for i in range(len(ms))]
	current_height = ms[peak]
	for (i, m) in enumerate(reversed(ms[:peak])):
		current_height = min(m, current_height)
		output[peak - 1 - i] = current_height
	current_height = ms[peak]
	for (i, m) in enumerate(ms[peak:]):
		current_height = min(m, current_height)
		output[peak + i] = current_height
	for i in output:
		print(i)

def main():
	lines = []
	for l in sys.stdin:
		lines.append(l)
	ms = [int(i) for i in lines[1].split()]
	winner = 0
	winner_index = 0
	for i in range(len(ms)):
		v = find_floors(i, ms)
		if v > winner:
			winner_index = i
			winner = v
	print_soln(winner_index, ms)
main()


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


n = int(input())
a = list(map(int, input().split()))
all = [a[i] for i in range(n)]
peaks = []
p = []
l = [0 for i in range(n)]
r = [0 for i in range(n)]
st = []
for i in range(0, n):
	while st != [] and a[i] <= a[st[-1]]:
		st.pop()
	if st == []:
		l[i] = a[i] * (i + 1)
	else:
		l[i] = l[st[-1]] + a[i] * (i - st[-1])
	st.append(i)
while st != []:
	st.pop()
for i in range(0, n):
	j = n - i - 1
	while st != [] and a[j] <= a[st[-1]]:
		st.pop()
	if st == []:
		r[j] = a[j] * (i + 1)
	else:
		r[j] = r[st[-1]] + a[j] * (st[-1] - j)
	st.append(j)
maxf = 0
I = 0
for i in range(n):
	if l[i] + r[i] - a[i] >= maxf:
		maxf = l[i] + r[i] - a[i]
		I = i
for i in range(n):
	if i < I:
		a[I - (i + 1)] = min(a[I - i], a[I - (i + 1)])
	if i > I:
		a[i] = min(a[i], a[i - 1])
print(*a)


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())
B = list(map(int, input().split()))
ans = 0
ansL = []
for i in range(n):
	cB = B.copy()
	if i >= 1:
		for j in range(i - 1, -1, -1):
			cB[j] = min(cB[j + 1], cB[j])
	if i <= n - 2:
		for k in range(i + 1, n):
			cB[k] = min(cB[k - 1], cB[k])
	sumb = sum(cB)
	if ans <= sumb:
		ans = sumb
		ansL = cB[:]
print(*ansL)


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())
m = list(map(int, input().split()))
btm = list()
peak = list()
if n > 1:
	if m[0] >= m[1]:
		peak.append(0)
	if m[-1] >= m[-2]:
		peak.append(n - 1)
for i in range(n - 2):
	if m[i] >= m[i + 1] and m[i + 2] >= m[i + 1]:
		btm.append(i + 1)
	elif m[i] <= m[i + 1] and m[i + 2] <= m[i + 1]:
		peak.append(i + 1)
ans = 0
ansls = list()
for h in peak:
	ans2 = 0
	ansls2 = [0] * n
	limit = 1001001001
	for i in reversed(range(h)):
		if i in btm:
			limit = min(limit, m[i])
		ans2 += min(limit, m[i])
		ansls2[i] = min(limit, m[i])
	limit = 1001001001
	for i in range(h, n):
		if i in btm:
			limit = min(limit, m[i])
		ans2 += min(limit, m[i])
		ansls2[i] = min(limit, m[i])
	if ans < ans2:
		ans = ans2
		ansls = ansls2
if ansls:
	print(*ansls)
else:
	print(*m)


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)


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 leftincreasing(l):
	l.append(0)
	d = deque()
	d.append(-1)
	dp = []
	for i in range(len(l) - 1):
		while l[d[-1]] >= l[i]:
			d.pop()
		if i and l[i] >= l[i - 1]:
			dp.append(dp[-1] + l[i])
		elif d[-1] == -1:
			dp.append(l[i] * (i + 1))
		else:
			dp.append(dp[d[-1]] + l[i] * (i - d[-1]))
		d.append(i)
	return dp

def giveans(i, l):
	l1 = []
	l2 = []
	temp = i
	i -= 1
	while i >= 0:
		if not l1:
			l1.append(min(l[i], l[temp]))
		else:
			l1.append(min(l[i], l1[-1]))
		i -= 1
	i = temp + 1
	while i < len(l):
		if not l2:
			l2.append(min(l[i], l[temp]))
		else:
			l2.append(min(l[i], l2[-1]))
		i += 1
	return l1[::-1] + [l[temp]] + l2
n = val()
l = li()
ldec = leftincreasing(l[:])
linc = leftincreasing(l[:][::-1])[::-1]
ans = 0
currans = -float('inf')
for i in range(n):
	if ldec[i] + linc[i] - l[i] > currans:
		currans = ldec[i] + linc[i] - l[i]
		ans = i
	elif currans == ldec[i] + linc[i] - l[i] and l[ans] < l[i]:
		ans = i
print(*giveans(ans, l))


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)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
le = [l[0]]
r = [l[-1]]
an = 0
d = idx = -1
k = [0]
for i in range(1, n):
	if l[i - 1] <= l[i]:
		le.append(le[i - 1] + l[i])
	else:
		while k and l[k[-1]] >= l[i]:
			k.pop()
		if k:
			j = k[-1]
			le.append(l[i] * (i - j) + le[j])
		else:
			le.append((i + 1) * l[i])
	k.append(i)
k = [0]
p = list(l[::-1])
for i in range(1, n):
	if p[i - 1] <= p[i]:
		r.append(r[i - 1] + p[i])
	else:
		while k and p[k[-1]] >= p[i]:
			k.pop()
		if k:
			j = k[-1]
			r.append(p[i] * (i - j) + r[j])
		else:
			r.append((i + 1) * p[i])
	k.append(i)
for i in range(n):
	x = le[i] + r[n - i - 1] - l[i]
	if x >= an:
		idx = i
		an = x
for i in range(idx + 1, n):
	l[i] = min(l[i], l[i - 1])
for i in range(idx - 1, -1, -1):
	l[i] = min(l[i], l[i + 1])
print(*l)


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)


def max_tower(n, a):
	if n <= 2:
		return (sum(a), a)
	minn = 0
	islin = True
	isrev = True
	for i in range(1, n):
		if a[i] < a[i - 1]:
			islin = False
		elif a[i] > a[i - 1]:
			isrev = False
		if a[i] < a[minn]:
			minn = i
	if islin or isrev:
		return (sum(a), a)
	if minn == 0:
		(summ, tows) = max_tower(n - 1, a[1:])
		return (summ + a[0], [a[0]] + tows)
	if minn == n - 1:
		(summ, tows) = max_tower(n - 1, a[:-1])
		return (summ + a[-1], tows + [a[-1]])
	amin = a[minn]
	(sum1, tow1) = max_tower(minn, a[:minn])
	(sum2, tow2) = max_tower(n - minn - 1, a[minn + 1:])
	sum1 += amin * (n - minn)
	sum2 += amin * (minn + 1)
	if sum1 > sum2:
		return (sum1, tow1 + [amin] * (n - minn))
	return (sum2, [amin] * (minn + 1) + tow2)
nn = int(input())
m = list(map(int, input().split()))
s = ''
for el in max_tower(nn, m)[1]:
	s += str(el) + ' '
print(s.strip(' '))


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


from math import log2

class Sparse:

	def __init__(self, a):
		self.a = a
		self.n = len(a)
		self.k = int(log2(self.n)) + 1
		self.lookup = [[0] * self.k for x in range(self.n)]
		self.process()

	def process(self):
		for i in range(self.n):
			self.lookup[i][0] = self.a[i]
		j = 1
		while 1 << j <= self.n:
			i = 0
			while i + (1 << j) - 1 < self.n:
				self.lookup[i][j] = min(self.lookup[i][j - 1], self.lookup[i + (1 << j - 1)][j - 1])
				i += 1
			j += 1

	def query(self, a, b):
		x = int(log2(b - a + 1))
		return min(self.lookup[a][x], self.lookup[b - (1 << x) + 1][x])
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())
a = list(map(int, input().split()))
if n == 1:
	print(a[0])
	quit()

def increase_till(a, ind):
	bi = Sparse(a[:ind + 1])
	b = []
	for i in range(ind + 1):
		b += [bi.query(i, ind)]
	current = b[-1] if b else a[0]
	increasing = True
	for i in range(ind + 1, n):
		if a[i] >= current and increasing:
			current = a[i]
			b += [a[i]]
		elif increasing:
			increasing = False
			current = a[i]
			b += [a[i]]
		else:
			current = min(current, a[i])
			b += [current]
	return (sum(b), list(b))
ar = []
ans = 0
for q in range(n):
	(an, arr) = increase_till(a, q)
	if an >= ans:
		ans = an
		ar = list(arr)
print(' '.join((str(k) for k in ar)))


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 sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations

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

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

def Lstr():
	return list(sys.stdin.readline().strip())

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

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

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

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.add(i)
	if n > 1:
		factors.add(n)
	print(factors)
	if len(factors) == 2:
		return True
	else:
		return False

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

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

def minimum_integer_not_in_list(a):
	b = max(a)
	if b < 1:
		return 1
	A = set(a)
	B = set(range(1, b + 1))
	D = B - A
	if len(D) == 0:
		return b + 1
	else:
		return min(D)

def solution():
	n = Int()
	a = List()
	temp = copy.copy(a)
	res = [0]
	for i in range(n):
		a = list(temp)
		for j in range(i + 1, n):
			a[j] = min(a[j - 1], a[j])
		for j in range(i - 1, -1, -1):
			a[j] = min(a[j + 1], a[j])
		if sum(a) >= sum(res):
			res = list(a)
	print(*res)
solution()


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())
M = list(map(int, input().split()))
s = 0
ans = [0] * n
for i in range(n):
	temp = 0
	cur = M[i]
	m = [0] * n
	for j in reversed(range(i)):
		if M[j] >= cur:
			temp += cur
			m[j] = cur
		else:
			temp += M[j]
			cur = M[j]
			m[j] = M[j]
	cur = M[i]
	for j in range(i, n):
		if M[j] >= cur:
			temp += cur
			m[j] = cur
		else:
			temp += M[j]
			cur = M[j]
			m[j] = M[j]
	if s <= temp:
		ans = m
		s = temp
print(*ans)


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


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


from sys import *

def solve(a, n):
	x = []
	mi = 0
	for i in range(n):
		index = i
		b = [a[index]] * n
		s = a[index]
		for i in range(index + 1, n):
			b[i] = min(a[i], b[i - 1])
			s += b[i]
		for i in range(index - 1, -1, -1):
			b[i] = min(a[i], b[i + 1])
			s += b[i]
		if s > mi:
			mi = s
			x = b
	return x
test = 1
for t in range(0, test):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = solve(arr, n)
	for a in ans:
		print(a, end=' ')


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
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')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(10)]
pp = [0] * 10

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

def binarySearch(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = arr[n - 1]
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] >= key:
			res = arr[mid]
			right = mid - 1
		else:
			left = mid + 1
	return res

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = arr[0]
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] > key:
			right = mid - 1
		else:
			res = arr[mid]
			left = mid + 1
	return res
n = int(input())
a = list(map(int, input().split()))
m = a[0]
l = [0] * n
stack = []
for i in range(n):
	m = min(m, a[i])
	if m == a[i]:
		l[i] = i * a[i]
		stack.append(i)
	else:
		while a[stack[-1]] > a[i]:
			stack.pop()
		l[i] = a[i] * (i - 1 - stack[-1]) + a[stack[-1]] + l[stack[-1]]
		stack.append(i)
r = [0] * n
stack = []
res = 0
m = a[-1]
ind = 0
for i in range(n - 1, -1, -1):
	m = min(m, a[i])
	if m == a[i]:
		r[i] = (n - 1 - i) * a[i]
		stack.append(i)
	else:
		while a[stack[-1]] > a[i]:
			stack.pop()
		r[i] = abs(stack[-1] - i - 1) * a[i] + r[stack[-1]] + a[stack[-1]]
		stack.append(i)
	k = l[i] + r[i] + a[i]
	if k > res:
		res = k
		ind = i
ans = [0] * n
ans[ind] = a[ind]
for i in range(ind + 1, n):
	ans[i] = min(ans[i - 1], a[i])
for i in range(ind - 1, -1, -1):
	ans[i] = min(ans[i + 1], a[i])
print(*ans)