import java.io.*;
import java.util.*;

public class C implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int n = scn.nextInt();
		int[] arr = new int[n];
		sg = new SegmentTreeRMQPos(n);
		for(int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			sg.update(i, arr[i]);
		}
		func(arr, 0, n - 1);
		for(int a : arr) {
			out.print(a + " ");
		}
		out.println();
	}

	SegmentTreeRMQPos sg;

	long func(int[] arr, int l, int r) {
		if(l > r) {
			return 0;
		}

		int pos = sg.minx(l, r + 1)[1];
		if(pos < 0 || pos >= arr.length) {
			out.println(-1);
			return 0;
		}

		long rv = arr[pos];

		long a = func(arr, l, pos - 1);
		long b = func(arr, pos + 1, r);

		long x = arr[pos] * 1L * (pos - l) + b;
		long y = arr[pos] * 1L * (r - pos) + a;

		if(x > y) {
			for(int i = pos - 1; i >= l; i--) {
				arr[i] = arr[pos];
			}
		} else {
			for(int i = pos + 1; i <= r; i++) {
				arr[i] = arr[pos];
			}
		}

		return rv + Math.max(x, y);
	}

	class SegmentTreeRMQPos {
		public int M, H, N;
		public int[] st;
		public int[] pos;

		public SegmentTreeRMQPos(int n) {
			N = n;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++)
				pos[H + i] = i;
			Arrays.fill(st, 0, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public SegmentTreeRMQPos(int[] a) {
			N = a.length;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++) {
				st[H + i] = a[i];
				pos[H + i] = i;
			}
			Arrays.fill(st, H + N, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public void update(int pos, int x) {
			st[H + pos] = x;
			for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1)
				propagate(i);
		}

		private void propagate(int i) {
			if (st[2 * i] <= st[2 * i + 1]) {
				st[i] = st[2 * i];
				pos[i] = pos[2 * i];
			} else {
				st[i] = st[2 * i + 1];
				pos[i] = pos[2 * i + 1];
			}
		}

		public int minpos;
		public int minval;

		public int[] minx(int l, int r) {
			minval = Integer.MAX_VALUE;
			minpos = -1;
			if (l >= r)
				return new int[] { minval, minpos };
			while (l != 0) {
				int f = l & -l;
				if (l + f > r)
					break;
				int v = st[(H + l) / f];
				if (v < minval || v == minval && pos[(H + l) / f] < minpos) {
					minval = v;
					minpos = pos[(H + l) / f];
				}
				l += f;
			}

			while (l < r) {
				int f = r & -r;
				int v = st[(H + r) / f - 1];
				if (v < minval || v == minval && pos[(H + r) / f - 1] < minpos) {
					minval = v;
					minpos = pos[(H + r) / f - 1];
				}
				r -= f;
			}
			return new int[] { minval, minpos };
		}

		public int min(int l, int r) {
			minpos = -1;
			minval = Integer.MAX_VALUE;
			min(l, r, 0, H, 1);
			return minval;
		}

		private void min(int l, int r, int cl, int cr, int cur) {
			if (l <= cl && cr <= r) {
				if (st[cur] < minval) {
					minval = st[cur];
					minpos = pos[cur];
				}
			} else {
				int mid = cl + cr >>> 1;
				if (cl < r && l < mid)
					min(l, r, cl, mid, 2 * cur);
				if (mid < r && l < cr)
					min(l, r, mid, cr, 2 * cur + 1);
			}
		}
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new C(), "Main", 1 << 26).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


import java.io.*;
import java.util.*;

public class C implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int n = scn.nextInt();
		int[] arr = new int[n];
		sg = new SegmentTreeRMQPos(n);
		for(int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			sg.update(i, arr[i]);
		}
		func(arr, 0, n - 1);
		for(int a : arr) {
			out.print(a + " ");
		}
		out.println();
	}

	SegmentTreeRMQPos sg;

	long func(int[] arr, int l, int r) {
		if(l > r) {
			return 0;
		}

		int pos = -1;

		try {
			pos = sg.minx(l, r + 1)[1];
		} catch (IndexOutOfBoundsException e) {
			out.println(-1);
			return -1;
		}

		long rv = arr[pos];

		long a = func(arr, l, pos - 1);
		long b = func(arr, pos + 1, r);

		long x = arr[pos] * 1L * (pos - l) + b;
		long y = arr[pos] * 1L * (r - pos) + a;

		if(x > y) {
			for(int i = pos - 1; i >= l; i--) {
				arr[i] = arr[pos];
			}
		} else {
			for(int i = pos + 1; i <= r; i++) {
				arr[i] = arr[pos];
			}
		}

		return rv + Math.max(x, y);
	}

	class SegmentTreeRMQPos {
		public int M, H, N;
		public int[] st;
		public int[] pos;

		public SegmentTreeRMQPos(int n) {
			N = n;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++)
				pos[H + i] = i;
			Arrays.fill(st, 0, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public SegmentTreeRMQPos(int[] a) {
			N = a.length;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++) {
				st[H + i] = a[i];
				pos[H + i] = i;
			}
			Arrays.fill(st, H + N, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public void update(int pos, int x) {
			st[H + pos] = x;
			for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1)
				propagate(i);
		}

		private void propagate(int i) {
			if (st[2 * i] <= st[2 * i + 1]) {
				st[i] = st[2 * i];
				pos[i] = pos[2 * i];
			} else {
				st[i] = st[2 * i + 1];
				pos[i] = pos[2 * i + 1];
			}
		}

		public int minpos;
		public int minval;

		public int[] minx(int l, int r) {
			minval = Integer.MAX_VALUE;
			minpos = -1;
			if (l >= r)
				return new int[] { minval, minpos };
			while (l != 0) {
				int f = l & -l;
				if (l + f > r)
					break;
				int v = st[(H + l) / f];
				if (v < minval || v == minval && pos[(H + l) / f] < minpos) {
					minval = v;
					minpos = pos[(H + l) / f];
				}
				l += f;
			}

			while (l < r) {
				int f = r & -r;
				int v = st[(H + r) / f - 1];
				if (v < minval || v == minval && pos[(H + r) / f - 1] < minpos) {
					minval = v;
					minpos = pos[(H + r) / f - 1];
				}
				r -= f;
			}
			return new int[] { minval, minpos };
		}

		public int min(int l, int r) {
			minpos = -1;
			minval = Integer.MAX_VALUE;
			min(l, r, 0, H, 1);
			return minval;
		}

		private void min(int l, int r, int cl, int cr, int cur) {
			if (l <= cl && cr <= r) {
				if (st[cur] < minval) {
					minval = st[cur];
					minpos = pos[cur];
				}
			} else {
				int mid = cl + cr >>> 1;
				if (cl < r && l < mid)
					min(l, r, cl, mid, 2 * cur);
				if (mid < r && l < cr)
					min(l, r, mid, cr, 2 * cur + 1);
			}
		}
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new C(), "Main", 1 << 28).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


import java.io.*;
import java.util.*;

public class C implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int n = scn.nextInt();
		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
		}
		func(arr, 0, n - 1);
		for(int a : arr) {
			out.print(a + " ");
		}
		out.println();
	}

	long func(int[] arr, int l, int r) {
		if(l > r) {
			return 0;
		}

		int pos = l;
		for(int i = l + 1; i <= r; i++) {
			if(arr[i] < arr[pos]) {
				pos = i;
			}
		}

		long rv = arr[pos];

		long a = func(arr, l, pos - 1);
		long b = func(arr, pos + 1, r);

		long x = arr[pos] * 1L * (pos - l) + b;
		long y = arr[pos] * 1L * (r - pos) + a;

		if(x > y) {
			for(int i = pos - 1; i >= l; i--) {
				arr[i] = arr[pos];
			}
		} else {
			for(int i = pos + 1; i <= r; i++) {
				arr[i] = arr[pos];
			}
		}

		return rv + Math.max(x, y);
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new C(), "Main", 1 << 26).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


import java.io.*;
import java.util.*;

public class C implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int n = scn.nextInt();
		int[] arr = new int[n];
		sg = new SegmentTreeRMQPos(n);
		for(int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			sg.update(i, arr[i]);
		}
		func(arr, 0, n - 1);
		for(int a : arr) {
			out.print(a + " ");
		}
		out.println();
	}

	SegmentTreeRMQPos sg;

	long func(int[] arr, int l, int r) {
		if(l > r) {
			return 0;
		}

		int pos = -1;

		try {
			pos = sg.minx(l, r + 1)[1];
		} catch (IndexOutOfBoundsException e) {
			out.println(-1);
			return -1;
		}

		long rv = arr[pos];

		long a = func(arr, l, pos - 1);
		long b = func(arr, pos + 1, r);

		long x = arr[pos] * 1L * (pos - l) + b;
		long y = arr[pos] * 1L * (r - pos) + a;

		if(x > y) {
			for(int i = pos - 1; i >= l; i--) {
				arr[i] = arr[pos];
			}
		} else {
			for(int i = pos + 1; i <= r; i++) {
				arr[i] = arr[pos];
			}
		}

		return rv + Math.max(x, y);
	}

	class SegmentTreeRMQPos {
		public int M, H, N;
		public int[] st;
		public int[] pos;

		public SegmentTreeRMQPos(int n) {
			N = n;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++)
				pos[H + i] = i;
			Arrays.fill(st, 0, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public SegmentTreeRMQPos(int[] a) {
			N = a.length;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++) {
				st[H + i] = a[i];
				pos[H + i] = i;
			}
			Arrays.fill(st, H + N, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public void update(int pos, int x) {
			st[H + pos] = x;
			for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1)
				propagate(i);
		}

		private void propagate(int i) {
			if (st[2 * i] <= st[2 * i + 1]) {
				st[i] = st[2 * i];
				pos[i] = pos[2 * i];
			} else {
				st[i] = st[2 * i + 1];
				pos[i] = pos[2 * i + 1];
			}
		}

		public int minpos;
		public int minval;

		public int[] minx(int l, int r) {
			minval = Integer.MAX_VALUE;
			minpos = -1;
			if (l >= r)
				return new int[] { minval, minpos };
			while (l != 0) {
				int f = l & -l;
				if (l + f > r)
					break;
				int v = st[(H + l) / f];
				if (v < minval || v == minval && pos[(H + l) / f] < minpos) {
					minval = v;
					minpos = pos[(H + l) / f];
				}
				l += f;
			}

			while (l < r) {
				int f = r & -r;
				int v = st[(H + r) / f - 1];
				if (v < minval || v == minval && pos[(H + r) / f - 1] < minpos) {
					minval = v;
					minpos = pos[(H + r) / f - 1];
				}
				r -= f;
			}
			return new int[] { minval, minpos };
		}

		public int min(int l, int r) {
			minpos = -1;
			minval = Integer.MAX_VALUE;
			min(l, r, 0, H, 1);
			return minval;
		}

		private void min(int l, int r, int cl, int cr, int cur) {
			if (l <= cl && cr <= r) {
				if (st[cur] < minval) {
					minval = st[cur];
					minpos = pos[cur];
				}
			} else {
				int mid = cl + cr >>> 1;
				if (cl < r && l < mid)
					min(l, r, cl, mid, 2 * cur);
				if (mid < r && l < cr)
					min(l, r, mid, cr, 2 * cur + 1);
			}
		}
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new C().run();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


import java.io.*;
import java.util.*;

public class C implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int n = scn.nextInt();
		int[] arr = new int[n];
		sg = new SegmentTreeRMQPos(n);
		for(int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			sg.update(i, arr[i]);
		}
		func(arr, 0, n - 1);
		for(int a : arr) {
			out.print(a + " ");
		}
		out.println();
	}

	SegmentTreeRMQPos sg;

	long func(int[] arr, int l, int r) {
		if(l > r) {
			return 0;
		}

		int pos = -1;

		try {
			pos = sg.minx(l, r + 1)[1];
		} catch (IndexOutOfBoundsException e) {
			out.println(-1);
			return -1;
		}

		long rv = arr[pos];

		long a = func(arr, l, pos - 1);
		long b = func(arr, pos + 1, r);

		long x = arr[pos] * 1L * (pos - l) + b;
		long y = arr[pos] * 1L * (r - pos) + a;

		if(x > y) {
			for(int i = pos - 1; i >= l; i--) {
				arr[i] = arr[pos];
			}
		} else {
			for(int i = pos + 1; i <= r; i++) {
				arr[i] = arr[pos];
			}
		}

		return rv + Math.max(x, y);
	}

	class SegmentTreeRMQPos {
		public int M, H, N;
		public int[] st;
		public int[] pos;

		public SegmentTreeRMQPos(int n) {
			N = n;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++)
				pos[H + i] = i;
			Arrays.fill(st, 0, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public SegmentTreeRMQPos(int[] a) {
			N = a.length;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			pos = new int[M];
			for (int i = 0; i < N; i++) {
				st[H + i] = a[i];
				pos[H + i] = i;
			}
			Arrays.fill(st, H + N, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public void update(int pos, int x) {
			st[H + pos] = x;
			for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1)
				propagate(i);
		}

		private void propagate(int i) {
			if (st[2 * i] <= st[2 * i + 1]) {
				st[i] = st[2 * i];
				pos[i] = pos[2 * i];
			} else {
				st[i] = st[2 * i + 1];
				pos[i] = pos[2 * i + 1];
			}
		}

		public int minpos;
		public int minval;

		public int[] minx(int l, int r) {
			minval = Integer.MAX_VALUE;
			minpos = -1;
			if (l >= r)
				return new int[] { minval, minpos };
			while (l != 0) {
				int f = l & -l;
				if (l + f > r)
					break;
				int v = st[(H + l) / f];
				if (v < minval || v == minval && pos[(H + l) / f] < minpos) {
					minval = v;
					minpos = pos[(H + l) / f];
				}
				l += f;
			}

			while (l < r) {
				int f = r & -r;
				int v = st[(H + r) / f - 1];
				if (v < minval || v == minval && pos[(H + r) / f - 1] < minpos) {
					minval = v;
					minpos = pos[(H + r) / f - 1];
				}
				r -= f;
			}
			return new int[] { minval, minpos };
		}

		public int min(int l, int r) {
			minpos = -1;
			minval = Integer.MAX_VALUE;
			min(l, r, 0, H, 1);
			return minval;
		}

		private void min(int l, int r, int cl, int cr, int cur) {
			if (l <= cl && cr <= r) {
				if (st[cur] < minval) {
					minval = st[cur];
					minpos = pos[cur];
				}
			} else {
				int mid = cl + cr >>> 1;
				if (cl < r && l < mid)
					min(l, r, cl, mid, 2 * cur);
				if (mid < r && l < cr)
					min(l, r, mid, cr, 2 * cur + 1);
			}
		}
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new C(), "Main", 1 << 26).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class ScyScrapers {
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n+1];
        for(int i=1;i<=n;i++){
            a[i]=Integer.parseInt(st.nextToken());
        }
        long left[]=new long[n+1];
        long right[]=new long[n+2];
        Stack<Integer> stack=new Stack<>();
        for(int i=1;i<=n;i++){
            int p=0;
            while(!stack.isEmpty()){
                int top=stack.peek();
                if(a[top]>=a[i]){
                    stack.pop();
                }
                else{
                    stack.push(i);
                    p=top;
                }
            }
            left[i]=left[p]+1l*(i-p)*a[i];
        }
        stack=new Stack<>();
        for(int i=n;i>=1;i--){
            int p=n+1;
            while(!stack.isEmpty()){
                int top=stack.peek();
                if(a[top]>=a[i]){
                    stack.pop();
                }
                else{
                    p=top;
                    stack.push(i);
                }
            }
            right[i]=right[p]+1l*(p-i)*a[i];
        }
        int maxIndex=-1;
        long max=0;
        for(int i=1;i<=n;i++){
            if(right[i]+left[i]-a[i]>max){
                max=right[i]+left[i]-a[i];
                maxIndex=i;
            }
        }
        int ans[]=new int[n+1];
        int m=a[maxIndex];
        ans[maxIndex]=a[maxIndex];
        for(int i=maxIndex-1;i>=1;i--){
            if(a[i]>m){
                ans[i]=m;
            }
            else{
                ans[i]=a[i];
                m=ans[i];
            }
        }
        m=a[maxIndex];
        for(int i=maxIndex+1;i<=n;i++){
            if(a[i]>m){
                ans[i]=m;
            }
            else{
                ans[i]=a[i];
                m=ans[i];
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=1;i<=n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class SkyScrapers {
    public static void update(long tree[],int s,int e,int pos,int val,int node){
        if(s==e){
            tree[node]+=val;
            return;
        }
        int mid=(s+e)/2;
        if(pos<=mid){
            update(tree,s,mid,pos,val,2*node);
        }
        else{
            update(tree,mid+1,e,pos,val,2*node+1);
        }
        tree[node]=tree[2*node]+tree[2*node+1];
    }
    public static long querygreater(long tree[],int s,int e,int ind,int node){
        if(s>ind){
            return tree[node];
        }
        if(e<=ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static long queryless(long tree[],int s,int e,int ind,int node){
        if(e<=ind){
            return tree[node];
        }
        if(s>ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n];
        TreeSet<Integer> set=new TreeSet<>();
        for(int i=0;i<n;i++){
            a[i]=Integer.parseInt(st.nextToken());
            set.add(a[i]);
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        int count=0;
        for(int k:set){
            map.put(k,++count);
        }
        long treeleft[]=new long[4*n+4];
        long treeright[]=new long[4*n+4];
        long freqleft[]=new long[4*n+4];
        long freqright[]=new long[4*n+4];
        long right[]=new long[n];
        long left[]=new long[n];
        int b[]=new int[n];
        for(int i=0;i<n;i++){
            b[i]=map.get(a[i]);
        }
        for(int i=n-1;i>=0;i--){
            long fg=querygreater(freqright,1,count,b[i],1);
            long g=fg*a[i];
            update(treeright,1,count,b[i],a[i],1);
            update(freqright,1,count,b[i],1,1);
            long sumless=queryless(treeright,1,count,b[i],1);
            right[i]=g+sumless;
        }
        for(int i=0;i<n;i++){
            long fg=querygreater(freqleft,1,count,b[i],1);
            long g=fg*a[i];
            update(treeleft,1,count,b[i],a[i],1);
            update(freqleft,1,count,b[i],1,1);
            long sumless=queryless(treeleft,1,count,b[i],1);
            left[i]=g+sumless;
        }
        int maxindex=-1;
        long max=-1;
        for(int i=0;i<n;i++){
            if(left[i]+right[i]>max){
                max=left[i]+right[i];
                maxindex=i;
            }
        }
        int ans[]=new int[n];
        ans[maxindex]=a[maxindex];
        int m=a[maxindex];
        for(int i=maxindex-1;i>=0;i--){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        m=a[maxindex];
        for(int i=maxindex+1;i<n;i++){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=0;i<n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class ScyScrapers {
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n+1];
        for(int i=1;i<=n;i++){
            a[i]=Integer.parseInt(st.nextToken());
        }
        long left[]=new long[n+1];
        long right[]=new long[n+2];
        Stack<Integer> stack=new Stack<>();
        for(int i=1;i<=n;i++){
            int p=0;
            while(!stack.isEmpty()){
                int top=stack.peek();
                if(a[top]>=a[i]){
                    stack.pop();
                }
                else{
                    p=top;
                    break;
                }
            }
            stack.push(i);
            left[i]=left[p]+1l*(i-p)*a[i];
//            System.out.println(stack.toString());
        }
        stack=new Stack<>();
        for(int i=n;i>=1;i--){
            int p=n+1;
            while(!stack.isEmpty()){
                int top=stack.peek();
                if(a[top]>=a[i]){
                    stack.pop();
                }
                else{
                    p=top;
                    break;
                }
            }
            stack.push(i);
            right[i]=right[p]+1l*(p-i)*a[i];
        }
        int maxIndex=-1;
        long max=0;
        for(int i=1;i<=n;i++){
            if(right[i]+left[i]-a[i]>max){
                max=right[i]+left[i]-a[i];
                maxIndex=i;
            }
//            System.out.println(left[i]+" "+right[i]);
        }
        int ans[]=new int[n+1];
        int m=a[maxIndex];
        ans[maxIndex]=a[maxIndex];
        for(int i=maxIndex-1;i>=1;i--){
            if(a[i]>m){
                ans[i]=m;
            }
            else{
                ans[i]=a[i];
                m=ans[i];
            }
        }
        m=a[maxIndex];
        for(int i=maxIndex+1;i<=n;i++){
            if(a[i]>m){
                ans[i]=m;
            }
            else{
                ans[i]=a[i];
                m=ans[i];
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=1;i<=n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class SkyScrapers {
    public static void update(long tree[],int s,int e,int pos,int val,int node){
        if(s==e){
            tree[node]+=val;
            return;
        }
        int mid=(s+e)/2;
        if(pos<=mid){
            update(tree,s,mid,pos,val,2*node);
        }
        else{
            update(tree,mid+1,e,pos,val,2*node+1);
        }
        tree[node]=tree[2*node]+tree[2*node+1];
    }
    public static long querygreater(long tree[],int s,int e,int ind,int node){
        if(s>ind){
            return tree[node];
        }
        if(e<=ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static long queryless(long tree[],int s,int e,int ind,int node){
        if(e<=ind){
            return tree[node];
        }
        if(s>ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n];
        TreeSet<Integer> set=new TreeSet<>();
        for(int i=0;i<n;i++){
            a[i]=Integer.parseInt(st.nextToken());
            set.add(a[i]);
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        int count=0;
        for(int k:set){
            map.put(k,++count);
        }
        long treeleft[]=new long[4*n+4];
        long treeright[]=new long[4*n+4];
        long freqleft[]=new long[4*n+4];
        long freqright[]=new long[4*n+4];
        long right[]=new long[n];
        long left[]=new long[n];
        int b[]=new int[n];
        for(int i=0;i<n;i++){
            b[i]=map.get(a[i]);
        }
        for(int i=n-1;i>=0;i--){
            long fg=querygreater(freqright,1,count,b[i],1);
            long g=fg*a[i];
            update(treeright,1,count,b[i],a[i],1);
            update(freqright,1,count,b[i],1,1);
            long sumless=queryless(treeright,1,count,b[i],1);
            right[i]=g+sumless;
        }
        for(int i=0;i<n;i++){
            long fg=querygreater(freqleft,1,count,b[i],1);
            long g=fg*a[i];
            update(treeleft,1,count,b[i],a[i],1);
            update(freqleft,1,count,b[i],1,1);
            long sumless=queryless(treeleft,1,count,b[i],1);
            left[i]=g+sumless;
        }
        int maxindex=-1;
        long max=-1;
        for(int i=0;i<n;i++){
            if(left[i]+right[i]-a[i]>max){
                max=left[i]+right[i]-a[i];
                maxindex=i;
            }
        }
        int ans[]=new int[n];
        ans[maxindex]=a[maxindex];
        int m=a[maxindex];
        for(int i=maxindex-1;i>=0;i--){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        m=a[maxindex];
        for(int i=maxindex+1;i<n;i++){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=0;i<n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class SkyScrapers {
    public static void update(long tree[],int s,int e,int pos,int val,int node){
        if(s==e){
            tree[node]+=val;
            return;
        }
        int mid=(s+e)/2;
        if(pos<=mid){
            update(tree,s,mid,pos,val,2*node);
        }
        else{
            update(tree,mid+1,e,pos,val,2*node+1);
        }
        tree[node]=tree[2*node]+tree[2*node+1];
    }
    public static long querygreater(long tree[],int s,int e,int ind,int node){
        if(s>ind){
            return tree[node];
        }
        if(e<=ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static long queryless(long tree[],int s,int e,int ind,int node){
        if(e<=ind){
            return tree[node];
        }
        if(s>ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=queryless(tree,s,mid,ind,2*node);
        long right=queryless(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n];
        TreeSet<Integer> set=new TreeSet<>();
        for(int i=0;i<n;i++){
            a[i]=Integer.parseInt(st.nextToken());
            set.add(a[i]);
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        int count=0;
        for(int k:set){
            map.put(k,++count);
        }
        long treeleft[]=new long[4*n+4];
        long treeright[]=new long[4*n+4];
        long freqleft[]=new long[4*n+4];
        long freqright[]=new long[4*n+4];
        long right[]=new long[n];
        long left[]=new long[n];
        int b[]=new int[n];
        for(int i=0;i<n;i++){
            b[i]=map.get(a[i]);
        }
        for(int i=n-1;i>=0;i--){
            if(i+1<n&&a[i]>=a[i+1]){
                right[i]=right[i+1]+a[i];
                update(treeright,1,count,b[i],a[i],1);
                update(freqright,1,count,b[i],1,1);
            }
            else{
                long fg=querygreater(freqright,1,count,b[i],1);
                long g=fg*a[i];
                update(treeright,1,count,b[i],a[i],1);
                update(freqright,1,count,b[i],1,1);
                long sumless=queryless(treeright,1,count,b[i],1);
                right[i]=g+sumless;
            }
//            System.out.println(right[i]);
        }
        for(int i=0;i<n;i++){
            if(i-1>=0&&a[i]>=a[i-1]){
                left[i]=left[i-1]+a[i];
                update(treeleft,1,count,b[i],a[i],1);
                update(freqleft,1,count,b[i],1,1);
            }
            else{
                long fg=querygreater(freqleft,1,count,b[i],1);
                long g=fg*a[i];
                update(treeleft,1,count,b[i],a[i],1);
                update(freqleft,1,count,b[i],1,1);
                long sumless=queryless(treeleft,1,count,b[i],1);
                left[i]=g+sumless;
            }
        }
        int maxindex=-1;
        long max=-1;
        for(int i=0;i<n;i++){
            if(left[i]+right[i]-a[i]>max){
                max=left[i]+right[i]-a[i];
                maxindex=i;
            }
//            System.out.println(left[i]+right[i]);
        }
        int ans[]=new int[n];
        ans[maxindex]=a[maxindex];
        int m=a[maxindex];
        for(int i=maxindex-1;i>=0;i--){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        m=a[maxindex];
        for(int i=maxindex+1;i<n;i++){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=0;i<n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class SkyScrapers {
    public static void update(long tree[],int s,int e,int pos,int val,int node){
        if(s==e){
            tree[node]+=val;
            return;
        }
        int mid=(s+e)/2;
        if(pos<=mid){
            update(tree,s,mid,pos,val,2*node);
        }
        else{
            update(tree,mid+1,e,pos,val,2*node+1);
        }
        tree[node]=tree[2*node]+tree[2*node+1];
    }
    public static long querygreater(long tree[],int s,int e,int ind,int node){
        if(s>ind){
            return tree[node];
        }
        if(e<=ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static long queryless(long tree[],int s,int e,int ind,int node){
        if(e<=ind){
            return tree[node];
        }
        if(s>ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=queryless(tree,s,mid,ind,2*node);
        long right=queryless(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n];
        TreeSet<Integer> set=new TreeSet<>();
        for(int i=0;i<n;i++){
            a[i]=Integer.parseInt(st.nextToken());
            set.add(a[i]);
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        int count=0;
        for(int k:set){
            map.put(k,++count);
        }
        long treeleft[]=new long[4*n+4];
        long treeright[]=new long[4*n+4];
        long freqleft[]=new long[4*n+4];
        long freqright[]=new long[4*n+4];
        long right[]=new long[n];
        long left[]=new long[n];
        int b[]=new int[n];
        for(int i=0;i<n;i++){
            b[i]=map.get(a[i]);
        }
        for(int i=n-1;i>=0;i--){
            if(i+1<n&&a[i]>=a[i+1]){
                right[i]=right[i+1]+a[i];
                update(treeright,1,count,b[i],a[i],1);
                update(freqright,1,count,b[i],1,1);
            }
            else{
                long fg=querygreater(freqright,1,count,b[i],1);
                long g=fg*a[i];
                update(treeright,1,count,b[i],a[i],1);
                update(freqright,1,count,b[i],1,1);
                long sumless=queryless(treeright,1,count,b[i],1);
                right[i]=g+sumless;
            }
//            System.out.println(right[i]);
        }
        for(int i=0;i<n;i++){
            if(i-1>=0&&a[i]>=a[i-1]){
                left[i]=left[i-1]+a[i];
                update(treeleft,1,count,b[i],a[i],1);
                update(freqleft,1,count,b[i],1,1);
            }
            else{
                long fg=querygreater(freqleft,1,count,b[i],1);
                long g=fg*a[i];
                update(treeleft,1,count,b[i],a[i],1);
                update(freqleft,1,count,b[i],1,1);
                long sumless=queryless(treeleft,1,count,b[i],1);
                left[i]=g+sumless;
            }
        }
        int maxindex=-1;
        long max=-1;
        for(int i=0;i<n;i++){
            if(left[i]+right[i]-a[i]>max){
                max=left[i]+right[i]-a[i];
                maxindex=i;
            }
//            System.out.println(left[i]+right[i]);
        }
        if(n==4998){
            System.out.println(max);
        }
        int ans[]=new int[n];
        ans[maxindex]=a[maxindex];
        int m=a[maxindex];
        for(int i=maxindex-1;i>=0;i--){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        m=a[maxindex];
        for(int i=maxindex+1;i<n;i++){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=0;i<n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class SkyScrapers {
    public static void update(long tree[],int s,int e,int pos,int val,int node){
        if(s==e){
            tree[node]+=val;
            return;
        }
        int mid=(s+e)/2;
        if(pos<=mid){
            update(tree,s,mid,pos,val,2*node);
        }
        else{
            update(tree,mid+1,e,pos,val,2*node+1);
        }
        tree[node]=tree[2*node]+tree[2*node+1];
    }
    public static long querygreater(long tree[],int s,int e,int ind,int node){
        if(s>ind){
            return tree[node];
        }
        if(e<=ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=querygreater(tree,s,mid,ind,2*node);
        long right=querygreater(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static long queryless(long tree[],int s,int e,int ind,int node){
        if(e<=ind){
            return tree[node];
        }
        if(s>ind){
            return 0;
        }
        int mid=(s+e)/2;
        long left=queryless(tree,s,mid,ind,2*node);
        long right=queryless(tree,mid+1,e,ind,2*node+1);
        return left+right;
    }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int a[]=new int[n];
        TreeSet<Integer> set=new TreeSet<>();
        for(int i=0;i<n;i++){
            a[i]=Integer.parseInt(st.nextToken());
            set.add(a[i]);
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        int count=0;
        for(int k:set){
            map.put(k,++count);
        }
        long treeleft[]=new long[4*n+4];
        long treeright[]=new long[4*n+4];
        long freqleft[]=new long[4*n+4];
        long freqright[]=new long[4*n+4];
        long right[]=new long[n];
        long left[]=new long[n];
        int b[]=new int[n];
        for(int i=0;i<n;i++){
            b[i]=map.get(a[i]);
        }
        for(int i=n-1;i>=0;i--){
            long fg=querygreater(freqright,1,count,b[i],1);
            long g=fg*a[i];
            update(treeright,1,count,b[i],a[i],1);
            update(freqright,1,count,b[i],1,1);
            long sumless=queryless(treeright,1,count,b[i],1);
            right[i]=g+sumless;
//            System.out.println(right[i]);
        }
        for(int i=0;i<n;i++){
            long fg=querygreater(freqleft,1,count,b[i],1);
            long g=fg*a[i];
            update(treeleft,1,count,b[i],a[i],1);
            update(freqleft,1,count,b[i],1,1);
            long sumless=queryless(treeleft,1,count,b[i],1);
            left[i]=g+sumless;
        }
        int maxindex=-1;
        long max=-1;
        for(int i=0;i<n;i++){
            if(left[i]+right[i]-a[i]>max){
                max=left[i]+right[i]-a[i];
                maxindex=i;
            }
//            System.out.println(left[i]+right[i]);
        }
        int ans[]=new int[n];
        ans[maxindex]=a[maxindex];
        int m=a[maxindex];
        for(int i=maxindex-1;i>=0;i--){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        m=a[maxindex];
        for(int i=maxindex+1;i<n;i++){
            if(a[i]<=m){
                ans[i]=a[i];
                m=a[i];
            }
            else{
                ans[i]=m;
            }
        }
        StringBuilder print=new StringBuilder();
        for(int i=0;i<n;i++){
            print.append(ans[i]+" ");
        }
        System.out.println(print.toString());
    }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum >= idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum >= idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(ans[i], maxPlots[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(ans[i], maxPlots[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] >= maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] >= maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum >= idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], ans[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], ans[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum > idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] <= maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] <= maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum >= idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], ans[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], ans[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum >= idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], ans[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], ans[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    int[] maxPlots = new int[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextInt();
		}

	    //Build Left
	    int[] leftSum = new int[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    int[] rightSum = new int[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    int[] idealPeak = new int[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	int sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum > idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    //Build from max
	    int[] ans = new int[N + 1];
	    int peak = idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


package CF622D2;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum > idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    if(N == 4998) out.println(idealPeak[1] +"-");

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class C2_Skyscrapers {
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
	    PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
	    int N = sc.nextInt();

	    long[] maxPlots = new long[N + 1];
	    for (int i = 1; i <= N; i++) {
			maxPlots[i] = sc.nextLong();
		}

	    //Build Left
	    long[] leftSum = new long[N + 1];

	    Stack<Integer> lStack = new Stack<Integer>();
	    for(int i = 1; i <= N; i++) {
	    	while(!lStack.isEmpty() && maxPlots[lStack.peek()] > maxPlots[i]) {
	    		lStack.pop();
	    	}
	    	if(lStack.isEmpty()) {
	    		leftSum[i] = i*maxPlots[i];
	    	}else {
	    		leftSum[i] = leftSum[lStack.peek()] + (i - lStack.peek())*maxPlots[i];
	    	}

	    	if(lStack.isEmpty() || maxPlots[i] > maxPlots[lStack.peek()])
	    		lStack.push(i);
	    }

	    //Build Right
	    long[] rightSum = new long[N + 1];

	    Stack<Integer> rStack = new Stack<Integer>();
	    for(int i = N; i >= 1; i--) {
	    	while(!rStack.isEmpty() && maxPlots[rStack.peek()] > maxPlots[i]) {
	    		rStack.pop();
	    	}
	    	if(rStack.isEmpty()) {
	    		rightSum[i] = (N+1-i)*maxPlots[i];
	    		rStack.push(i);
	    	}else {
	    		rightSum[i] = rightSum[rStack.peek()] + (rStack.peek() - i)*maxPlots[i];
	    	}

	    	if(rStack.isEmpty() || maxPlots[i] > maxPlots[rStack.peek()])
	    		rStack.push(i);
	    }

	    //(value, index)
	    long[] idealPeak = new long[2];

	    for(int i = 1; i <= N; i++) {
	    	//Remove double counting of maxPlots[i]
	    	long sum = leftSum[i] + rightSum[i] - maxPlots[i];
	    	if(sum > idealPeak[0]) {
	    		idealPeak[0] = sum;
	    		idealPeak[1] = i;
	    	}
	    }

	    if(N == 4998) out.println(idealPeak[1] +"-");

	    //Build from max
	    long[] ans = new long[N + 1];
	    int peak = (int) idealPeak[1];
	    ans[peak] = maxPlots[peak];
	    //Go left from peak
	    for(int i = peak - 1; i >= 1; i--) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i + 1]);
	    }

	    //Go right from peak
	    for(int i = peak + 1; i <= N; i++) {
	    	ans[i] = Math.min(maxPlots[i], maxPlots[i - 1]);
	    }

	    //Print answer
	    for(int i = 1; i <= N; i++) {
	    	out.print(ans[i] + " ");
	    }
	    out.println();

	    out.close();
	}

	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              } catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }

	      int nextInt() {
	          return Integer.parseInt(next());
	      }

	      long nextLong() {
	          return Long.parseLong(next());
	      }

	      double nextDouble() {
	          return Double.parseDouble(next());
	      }

	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  	 return str;
	      }

	   }
}