import java.awt.*;
import java.io.*;
import java.io.IOException;
import java.util.*;
import java.text.DecimalFormat;

public class Exam {

	public static long mod = (long)Math.pow(10, 9)+7 ;
	public static double epsilon=0.00000000008854;//value of epsilon
	public static InputReader sc = new InputReader(System.in);
	public static PrintWriter pw = new PrintWriter(System.out);
	public static long gcd(long a,long b){
		while(b>0){
			long t=b;
			b=a%b;
			a=t;
		}
		return a;
    }

	static void dfs(Point p[],int v,boolean vis[]){
        if (vis[v])return;
        vis[v]=true;
        for (int i=0;i<p.length;i++){
            if (p[i].x==p[v].x || p[i].y==p[v].y){
                dfs(p,i,vis);
            }
        }
	}
	public static long bSearch(Pair[]d, int l, int r, int a) {
		if(d[l].d > a)
			return 0;
		if(l == r)
			return d[l].g;
		int m = (l + r + 1) / 2;
		if(d[m].d > a)
			return bSearch(d, l, m - 1, a);
		return bSearch(d, m, r, a);
	}
	static class Pair implements Comparable<Pair> {
		int d, i;
		long g;
		public Pair(int D, long G, int I) {
			d = D;
			g = G;
			i = I;
		}
		public int compareTo(Pair p) {
			if(d == p.d)
				return i - p.i;
			return d - p.d;
		}
	 }

	public static long pow(long x,long y,long mod){
		long ans=1;
		while(y>0){
			if((y&1)==1){
				ans=(ans*x)%mod;
			}
			y=y>>1;
			x=(x*x)%mod;
		}
		return ans;
	}
	public static boolean isPalindrome(String s1,String s2){
		s2=reverseString(s2);

		if(s1.equals(s2)){
		return true;
		}
		else
		return false;

		/*
		boolean f=true;
		for(int i=0;i<s.length();i++){
			if(i>s.length()-i-1)
			break;
			if(s.charAt(i)!=s.charAt(s.length()-i-1)){
				f=false;
				break;
			}
		}
		return f;*/
	}

	public static HashSet<Long> query(int root,int l,int r,int st,int en,ArrayList<HashSet<Long>>s){
		if(l>=st&&r<=en){
			return s.get(root);
		}
		else if(r<st||l>en)
		return new HashSet<Long>();
		else{
			HashSet<Long> h1=query(2*root+1, l, (l+r)/2, st, en, s);
		HashSet<Long> h2=query(2*root+2, (l+r)/2+1, r, st, en, s);
		HashSet<Long> h=new HashSet<>();
		h.addAll(h1);h.addAll(h2);
		return h;
		}
	}

	public static void main(String[] args) {
		// code starts..
		int n=sc.nextInt();
		int a[]=scanArray(n);
		Stack<ob> st=new Stack<>();
		//st.add(new ob(-1,0));
		long l[]=new long[n];
		long r[]=new long[n];
		for(int i=0;i<n;i++){
			while(st.size()>0){
				if(a[st.peek().i]>=a[i])
				st.pop();
				else break;
			}
			int k=0;
			long s=0;
			if(st.size()>0){
				k=i-st.peek().i;
				s+=st.peek().s;
			}
			s+=(long)k*a[i];
			l[i]=s;
			st.add(new ob(i,s));

		}
		st=new Stack<>();

		for(int i=n-1;i>=0;i--){
			while(st.size()>0){
				if(a[st.peek().i]>=a[i])
				st.pop();
				else break;
			}
			int k=0;
			long s=0;
			if(st.size()>0){
				k=st.peek().i-i+1;
				s+=st.peek().s;
			}
			s+=(long)k*a[i];
			r[i]=s;
			st.add(new ob(i,s));
		}
		int in=0;long max=0;
		for(int i=0;i<n;i++){
			if(l[i]+r[i]+a[i]>max){
				max=l[i]+r[i]+a[i];
				in=i;
			}
		}
		for(int i=in+1;i<n;i++){
			if(a[i]>a[i-1]){
				a[i]=a[i-1];
			}
		}
		for(int i=in-1;i>0;i--){
			if(a[i]>a[i+1]){
				a[i]=a[i+1];
			}
		}
		for(int i=0;i<n;i++){
			pw.print(a[i]+" ");
		}

		// Code ends...
		pw.flush();
		pw.close();
	}
	static class tripletL implements Comparable<tripletL> {
		Long x, y, z;

		tripletL(long x, long y, long z) {
			this.x = x;
			this.y = y;
			this.z = z;
		}

		public int compareTo(tripletL o) {
			int result = x.compareTo(o.x);
			if (result == 0)
				result = y.compareTo(o.y);
			if (result == 0)
				result = z.compareTo(o.z);

			return result;
		}

		public boolean equlas(Object o) {
			if (o instanceof tripletL) {
				tripletL p = (tripletL) o;
				return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0);
			}
			return false;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		public int hashCode() {
			return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode();
		}
	}
	public static String Doubleformate(double a,int n){
		String s="";
		while(n-->0){
			s+='0';
		}
		DecimalFormat f =new DecimalFormat("#0."+s);
		return f.format(a);
	}

	public static Comparator<Long[]> column(int i){
		return
		new Comparator<Long[]>() {
			@Override
			public int compare(Long[] o1, Long[] o2) {
				return o1[i].compareTo(o2[i]);//for ascending
				//return o2[i].compareTo(o1[i]);//for descending
			}
		};
	}
	public static Comparator<Long[]> column(){
		return
		new Comparator<Long[]>() {
			@Override
			public int compare(Long[] o1, Long[] o2) {
				if(o1[1]!=o2[1])
				return o1[1].compareTo(o2[1]);//for ascending
				//return o2[i].compareTo(o1[i]);//for descending
				else return o1[0].compareTo(o2[0]);
			}
		};
	}
	public static Comparator<Long[]> pair(){
		return
		new Comparator<Long[]>() {
			@Override
			public int compare(Long[] o1, Long[] o2) {
				int result=o1[0].compareTo(o2[0]);
				if(result==0)
				result=o1[1].compareTo(o2[1]);
				return result;
			}
		};
	}
	public static Comparator<Long[]> Triplet(){
		return
		new Comparator<Long[]>() {
			@Override
			public int compare(Long[] o1, Long[] o2) {

					for(int i=0;i<3;i++){
						for(int j=i+1;j<3;j++){
							for(int k=0;k<3;k++){
								for(int p=k+1;p<3;p++){
									if((o1[i]==o2[k]&&o1[j]==o2[p])||(o1[j]==o2[k]&&o1[i]==o2[p])){

									}
								}
							}
						}
					}
					int result=o1[0].compareTo(o2[0]);
					if(result==0)
					result=o1[1].compareTo(o2[1]);
					if(result==0)
					result=o1[2].compareTo(o2[2]);
					return result;
			}
		};
	}

	public static String reverseString(String s){
		StringBuilder input1 = new StringBuilder();
        input1.append(s);
		input1 = input1.reverse();
		return input1.toString();
	}
	public static int[] scanArray(int n){
		int a[]=new int [n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextInt();

		return a;
	}
	public static long[] scanLongArray(int n){
		long a[]=new long [n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextLong();

		return a;
	}
	public static String [] scanStrings(int n){
		String a[]=new String [n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextLine();

		return a;
	}
	static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

		public String readString() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

}
class Bit{//1...n
	long a[];
	Bit(int n){
		a=new long[n+1];
	}
	void update(long f,long delta){
		int i=(int)f;
		while(i<a.length){
			a[i]+=delta;
			i+=i&(-i);
		}
	}
	long query(long f){
		int  i=(int)f;
		long sum=0;
		while(i>0){
			sum+=a[i];
			i-=i&(-i);
		}
		return sum;
	}
}
class ob{
	int i;
	long s=0;
	ob(int i,long s){
		this.i=i;
		this.s=s;
	}
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

import org.omg.Messaging.SyncScopeHelper;

public class tr1 {
	static PrintWriter out;
	static StringBuilder sb;
	static int inf = (int) 1e9;
	static long mod = (long) 1000000007;
	static int[] si;
	static ArrayList<Integer> primes;
	static HashSet<Integer> pr;
	static int n, k, m;
	static int[] in;
	static HashMap<Integer, Integer> factors;
	static HashSet<Integer> f;
	static int[] fac, a, b;
	static int[] l, r;
	static int[][] memo1, memo2;
	static int[] numc;
	static ArrayList<Integer>[] ad;
	static HashMap<Integer, Integer> hm;
	static pair[] ed;
	static TreeSet<Integer>[] np;
	static TreeMap<pair1, Integer> tm;
	static long[] c1, c2, c3;
	static int[] col;
	static pair[] mm;
	static boolean[] vis, touch;
	static int max, c;
	static HashSet<Integer> hs;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		int n = sc.nextInt();
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		if (n == 1) {
			System.out.println(a[0]);
			return;
		}
		int[] lf = new int[n];
		int[] rf = new int[n];
		Stack<Integer> s = new Stack<>();
		Arrays.fill(lf, -1);
		Arrays.fill(rf, -1);
		for (int i = 0; i < n; i++) {
			if (s.isEmpty()) {
				s.push(i);
			} else
				while (!s.isEmpty()) {
					if (a[s.peek()] > a[i])
						s.pop();
					else {
						if (!s.isEmpty())
							lf[i] = s.peek();
						s.push(i);
						break;
					}
				}
		}
		s = new Stack<>();
		for (int i = n - 1; i >= 0; i--) {
			if (s.isEmpty()) {
				s.push(i);
			} else
				while (!s.isEmpty()) {
					if (a[s.peek()] > a[i])
						s.pop();
					else {
						if (!s.isEmpty())
							rf[i] = s.peek();
						s.push(i);
						break;
					}
				}
		}
		long[] rr = new long[n];
		long[] ll = new long[n];
		for (int i = 0; i < n; i++) {
			if (lf[i] == -1) {
				ll[i] = a[i] * ((long) (i + 1));
			} else {
				ll[i] = a[i] * ((long) (i - lf[i])) + ll[lf[i]];
			}
		}
		for (int i = n-1; i >= 0; i--) {
			if (rf[i] == -1) {
				rr[i] = a[i] * ((long) (n - i));
			} else {
				rr[i] = a[i] * ((long) (rf[i]-i)) + rr[rf[i]];
			}
		}
		long max=0;
		int id=0;
		long []print=new long [n];
		for(int i=0;i<n;i++) {
			long tem=0;
			tem+=ll[i]+rr[i]-a[i];
			if(tem>max) {
				max=tem;
				id=i;
			}
		}
		print[id]=a[id];
		long min=a[id];
		for(int i=id+1;i<n;i++)
		{
			print[i]=Math.min(print[i-1],a[i]);
		}
		for(int i=id-1;i>=0;i--)
		{
			print[i]=Math.min(print[i+1],a[i]);
		}
		for(int i=0;i<n;i++)
			out.print(print[i]+" ");
		out.flush();
	}

	static class pair implements Comparable<pair> {
		int p;
		int c;
		int id;

		pair(int p11, int cc, int i) {
			p = p11;
			c = cc;
			id = i;
		}

		public String toString() {
			return p + " " + c + " " + id;
		}

		@Override
		public int compareTo(pair o) {
			if (c == o.c)
				return id - o.id;
			return p - o.p;
		}
	}

	static class pair1 implements Comparable<pair1> {
		int x;
		int y;
		int z;

		pair1(int f, int t, int u) {
			x = f;
			y = t;
			z = u;
			/// i = n;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		@Override
		public int compareTo(pair1 o) {
			if (x == o.x)
				if (y == o.y)
					return z - o.z;
				else
					return y - o.y;
			else
				return x - o.x;
		}
	}

	static long gcd(long a, long b) {

		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public int[] merge(int[] d, int st, int e) {
		if (st > e)
			return null;
		if (e == st) {
			int[] ans = { d[e] };
			return ans;
		}
		int mid = (st + e) / 2;
		int[] ans = new int[e - st + 1];
		int[] ll = merge(d, st, mid);
		int[] rr = merge(d, mid + 1, e);
		if (ll == null)
			return rr;
		if (rr == null)
			return ll;
		int iver = 0;
		int idxl = 0;
		int idxr = 0;
		for (int i = st; i <= e; i++) {
			if (ll[idxl] < rr[idxr]) {

			}
		}
		return ans;
	}

	public static class pair2 implements Comparable<pair2> {
		int a;
		int idx;

		pair2(int a, int i) {
			this.a = a;
			idx = i;
		}

		public String toString() {
			return a + " " + idx;
		}

		@Override
		public int compareTo(pair2 o) {
			// TODO Auto-generated method stub
			return idx - o.idx;
		}
	}

	static long inver(long x) {
		int a = (int) x;
		long e = (mod - 2);
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1) {
				// System.out.println(res*a);
				res = (int) ((1l * res * a) % mod);
			}
			a = (int) ((1l * a * a) % mod);
			e >>= 1;
		}
		// out.println(res+" "+x);
		return res % mod;
	}

	static int atMostSum(int arr[], int n, int k) {
		int sum = 0;
		int cnt = 0, maxcnt = 0;

		for (int i = 0; i < n; i++) {

			// If adding current element doesn't
			// cross limit add it to current window
			if ((sum + arr[i]) <= k) {
				sum += arr[i];
				cnt++;
			}

			// Else, remove first element of current
			// window and add the current element
			else if (sum != 0) {
				sum = sum - arr[i - cnt] + arr[i];
			}

			// keep track of max length.
			maxcnt = Math.max(cnt, maxcnt);
		}
		return maxcnt;
	}

	public static int[] longestSubarray(int[] inp) {
		// array containing prefix sums up to a certain index i
		int[] p = new int[inp.length];
		p[0] = inp[0];
		for (int i = 1; i < inp.length; i++) {
			p[i] = p[i - 1] + inp[i];
		}

		// array Q from the description below
		int[] q = new int[inp.length];
		q[inp.length - 1] = p[inp.length - 1];
		for (int i = inp.length - 2; i >= 0; i--) {
			q[i] = Math.max(q[i + 1], p[i]);
		}
		int a = 0;
		int b = 0;
		int maxLen = 0;
		int curr;
		int[] res = new int[] { -1, -1 };
		while (b < inp.length) {
			curr = a > 0 ? q[b] - p[a - 1] : q[b];
			if (curr >= 0) {
				if (b - a > maxLen) {
					maxLen = b - a;
					res = new int[] { a, b };
				}
				b++;
			} else {
				a++;
			}
		}
		return res;
	}

	static void factor(int n) {
		if (si[n] == n) {
			f.add(n);
			return;
		}
		f.add(si[n]);
		factor(n / si[n]);
	}

	static void seive() {
		si = new int[100001];
		primes = new ArrayList<>();
		int N = 100001;
		si[1] = 1;
		for (int i = 2; i < N; i++) {
			if (si[i] == 0) {
				si[i] = i;
				primes.add(i);
			}
			for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
				si[primes.get(j) * i] = primes.get(j);

		}
	}

	static class unionfind {
		int[] p;
		int[] size;
		int jum;
		int[] colors;

		unionfind(int n, int[] c) {
			p = new int[n];
			size = new int[n];
			colors = c.clone();
			for (int i = 0; i < n; i++) {
				p[i] = i;
			}
			jum = n;
			Arrays.fill(size, 1);
		}

		int findSet(int v) {
			if (v == p[v])
				return v;
			return p[v] = findSet(p[v]);
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		void combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return;
			jum--;
			if (size[a] > size[b]) {
				p[b] = a;
				size[a] += size[b];
				colors[b] = -1;
			} else {
				p[a] = b;
				size[b] += size[a];
				colors[a] = -1;
			}
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

import org.omg.Messaging.SyncScopeHelper;

public class tr1 {
	static PrintWriter out;
	static StringBuilder sb;
	static int inf = (int) 1e9;
	static long mod = (long) 1000000007;
	static int[] si;
	static ArrayList<Integer> primes;
	static HashSet<Integer> pr;
	static int n, k, m;
	static int[] in;
	static HashMap<Integer, Integer> factors;
	static HashSet<Integer> f;
	static int[] fac, a, b;
	static int[] l, r;
	static int[][] memo1, memo2;
	static int[] numc;
	static ArrayList<Integer>[] ad;
	static HashMap<Integer, Integer> hm;
	static pair[] ed;
	static TreeSet<Integer>[] np;
	static TreeMap<pair1, Integer> tm;
	static long[] c1, c2, c3;
	static int[] col;
	static pair[] mm;
	static boolean[] vis, touch;
	static int max, c;
	static HashSet<Integer> hs;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		int n = sc.nextInt();
		long ans1 = 0;
		long ans2 = 0;
		long[] ans11 = new long[n];
		long[] ans22 = new long[n];
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		long min = -1;
		long last = -1;
		for (int i = 0; i < n; i++) {
			if (last != -1) {
				if(min==-1) {
	                  if(a[i]<last) {
	                	  min=a[i];
	                  }
			          ans1+=a[i];
			          ans11[i]=a[i];
				}
				else {
	                  if(a[i]<last) {
	                	  min=Math.min(a[i], min);
	                  }
					long x=Math.min(min, a[i]);
					ans1+=x;
					ans11[i]=x;
				}
			} else {
				ans1 += a[i];
				ans11[i]=a[i];
			}
			last = a[i];
		}
		min=-1;
		last=-1;
		for (int i = 0; i < n; i++) {
			if (last != -1) {
				if(min==-1) {
	                  if(a[i]<last) {
	                	  min=a[i];
	                  }
			          ans2+=a[i];
			          ans22[i]=a[i];
				}
				else {
	                  if(a[i]<last) {
	                	  min=Math.min(a[i], min);
	                  }
					long x=Math.min(min, a[i]);
					ans2+=x;
					ans22[i]=x;
				}
			} else {
				ans2 += a[i];
				ans22[i]=a[i];
			}
			last = a[i];
		}
		if(ans1>ans2)
		for (int i = 0; i < n; i++)
			out.print(ans11[i] + " ");
		else
			for (int i = 0; i < n; i++)
				out.print(ans22[i] + " ");
		out.flush();
	}

	static void dfs(int u) {
		vis[u] = true;
		if (u == n - 1)
			touch[u] = true;
		for (int v : ad[u])
			if (!vis[v]) {
				dfs(v);
				touch[u] |= touch[v];
			}
	}

	static class pair implements Comparable<pair> {
		int p;
		int c;
		int id;

		pair(int p11, int cc, int i) {
			p = p11;
			c = cc;
			id = i;
		}

		public String toString() {
			return p + " " + c + " " + id;
		}

		@Override
		public int compareTo(pair o) {
			if (c == o.c)
				return id - o.id;
			return p - o.p;
		}
	}

	static class pair1 implements Comparable<pair1> {
		int x;
		int y;
		int z;

		pair1(int f, int t, int u) {
			x = f;
			y = t;
			z = u;
			/// i = n;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		@Override
		public int compareTo(pair1 o) {
			if (x == o.x)
				if (y == o.y)
					return z - o.z;
				else
					return y - o.y;
			else
				return x - o.x;
		}
	}

	static long gcd(long a, long b) {

		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public int[] merge(int[] d, int st, int e) {
		if (st > e)
			return null;
		if (e == st) {
			int[] ans = { d[e] };
			return ans;
		}
		int mid = (st + e) / 2;
		int[] ans = new int[e - st + 1];
		int[] ll = merge(d, st, mid);
		int[] rr = merge(d, mid + 1, e);
		if (ll == null)
			return rr;
		if (rr == null)
			return ll;
		int iver = 0;
		int idxl = 0;
		int idxr = 0;
		for (int i = st; i <= e; i++) {
			if (ll[idxl] < rr[idxr]) {

			}
		}
		return ans;
	}

	public static class pair2 implements Comparable<pair2> {
		int a;
		int idx;

		pair2(int a, int i) {
			this.a = a;
			idx = i;
		}

		public String toString() {
			return a + " " + idx;
		}

		@Override
		public int compareTo(pair2 o) {
			// TODO Auto-generated method stub
			return idx - o.idx;
		}
	}

	static long inver(long x) {
		int a = (int) x;
		long e = (mod - 2);
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1) {
				// System.out.println(res*a);
				res = (int) ((1l * res * a) % mod);
			}
			a = (int) ((1l * a * a) % mod);
			e >>= 1;
		}
		// out.println(res+" "+x);
		return res % mod;
	}

	static int atMostSum(int arr[], int n, int k) {
		int sum = 0;
		int cnt = 0, maxcnt = 0;

		for (int i = 0; i < n; i++) {

			// If adding current element doesn't
			// cross limit add it to current window
			if ((sum + arr[i]) <= k) {
				sum += arr[i];
				cnt++;
			}

			// Else, remove first element of current
			// window and add the current element
			else if (sum != 0) {
				sum = sum - arr[i - cnt] + arr[i];
			}

			// keep track of max length.
			maxcnt = Math.max(cnt, maxcnt);
		}
		return maxcnt;
	}

	public static int[] longestSubarray(int[] inp) {
		// array containing prefix sums up to a certain index i
		int[] p = new int[inp.length];
		p[0] = inp[0];
		for (int i = 1; i < inp.length; i++) {
			p[i] = p[i - 1] + inp[i];
		}

		// array Q from the description below
		int[] q = new int[inp.length];
		q[inp.length - 1] = p[inp.length - 1];
		for (int i = inp.length - 2; i >= 0; i--) {
			q[i] = Math.max(q[i + 1], p[i]);
		}
		int a = 0;
		int b = 0;
		int maxLen = 0;
		int curr;
		int[] res = new int[] { -1, -1 };
		while (b < inp.length) {
			curr = a > 0 ? q[b] - p[a - 1] : q[b];
			if (curr >= 0) {
				if (b - a > maxLen) {
					maxLen = b - a;
					res = new int[] { a, b };
				}
				b++;
			} else {
				a++;
			}
		}
		return res;
	}

	static void factor(int n) {
		if (si[n] == n) {
			f.add(n);
			return;
		}
		f.add(si[n]);
		factor(n / si[n]);
	}

	static void seive() {
		si = new int[100001];
		primes = new ArrayList<>();
		int N = 100001;
		si[1] = 1;
		for (int i = 2; i < N; i++) {
			if (si[i] == 0) {
				si[i] = i;
				primes.add(i);
			}
			for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
				si[primes.get(j) * i] = primes.get(j);

		}
	}

	static class unionfind {
		int[] p;
		int[] size;
		int jum;
		int[] colors;

		unionfind(int n, int[] c) {
			p = new int[n];
			size = new int[n];
			colors = c.clone();
			for (int i = 0; i < n; i++) {
				p[i] = i;
			}
			jum = n;
			Arrays.fill(size, 1);
		}

		int findSet(int v) {
			if (v == p[v])
				return v;
			return p[v] = findSet(p[v]);
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		void combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return;
			jum--;
			if (size[a] > size[b]) {
				p[b] = a;
				size[a] += size[b];
				colors[b] = -1;
			} else {
				p[a] = b;
				size[b] += size[a];
				colors[a] = -1;
			}
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

import org.omg.Messaging.SyncScopeHelper;

public class tr1 {
	static PrintWriter out;
	static StringBuilder sb;
	static int inf = (int) 1e9;
	static long mod = (long) 1000000007;
	static int[] si;
	static ArrayList<Integer> primes;
	static HashSet<Integer> pr;
	static int n, k, m;
	static int[] in;
	static HashMap<Integer, Integer> factors;
	static HashSet<Integer> f;
	static int[] fac, a, b;
	static int[] l, r;
	static int[][] memo1, memo2;
	static int[] numc;
	static ArrayList<Integer>[] ad;
	static HashMap<Integer, Integer> hm;
	static pair[] ed;
	static TreeSet<Integer>[] np;
	static TreeMap<pair1, Integer> tm;
	static long[] c1, c2, c3;
	static int[] col;
	static pair[] mm;
	static boolean[] vis, touch;
	static int max, c;
	static HashSet<Integer> hs;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		int n = sc.nextInt();
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		if (n == 1) {
			System.out.println(a[0]);
			return;
		}
		int[] lf = new int[n];
		int[] rf = new int[n];
		Stack<Integer> s = new Stack<>();
		Arrays.fill(lf, -1);
		Arrays.fill(rf, -1);
		for (int i = 0; i < n; i++) {
	//		if (s.isEmpty()) {
		//		s.push(i);
			//} else
				while (!s.isEmpty()) {
					if (a[s.peek()] >= a[i])
						s.pop();
					else {
						if (!s.isEmpty())
							lf[i] = s.peek();
				//		s.push(i);
						break;
					}
				}
				s.push(i);
		}
		s = new Stack<>();
		for (int i = n - 1; i >= 0; i--) {
			//if (s.isEmpty()) {
			//	s.push(i);
		//	} else
				while (!s.isEmpty()) {
					if (a[s.peek()] >= a[i])
						s.pop();
					else {
						if (!s.isEmpty())
							rf[i] = s.peek();
				//		s.push(i);
						break;
					}
				}
			s.push(i);
		}
		long[] rr = new long[n];
		long[] ll = new long[n];
		for (int i = 0; i < n; i++) {
			if (lf[i] == -1) {
				ll[i] = a[i] * ((long) (i + 1));
			} else {
				ll[i] = a[i] * ((long) (i - lf[i])) + ll[lf[i]];
			}
		}
		for (int i = n-1; i >= 0; i--) {
			if (rf[i] == -1) {
				rr[i] = a[i] * ((long) (n - i));
			} else {
				rr[i] = a[i] * ((long) (rf[i]-i)) + rr[rf[i]];
			}
		}
		long max=0;
		int id=0;
		long []print=new long [n];
		for(int i=0;i<n;i++) {
			long tem=0;
			tem+=ll[i]+rr[i]-a[i];
			if(tem>max) {
				max=tem;
				id=i;
			}
		}
	//	System.out.println(Arrays.toString(lf));
		//System.out.println(Arrays.toString(rf));
		print[id]=a[id];
		long min=a[id];
		for(int i=id+1;i<n;i++)
		{
			print[i]=Math.min(print[i-1],a[i]);
		}
		for(int i=id-1;i>=0;i--)
		{
			print[i]=Math.min(print[i+1],a[i]);
		}
		for(int i=0;i<n;i++)
			out.print(print[i]+" ");
		out.flush();
	}

	static class pair implements Comparable<pair> {
		int p;
		int c;
		int id;

		pair(int p11, int cc, int i) {
			p = p11;
			c = cc;
			id = i;
		}

		public String toString() {
			return p + " " + c + " " + id;
		}

		@Override
		public int compareTo(pair o) {
			if (c == o.c)
				return id - o.id;
			return p - o.p;
		}
	}

	static class pair1 implements Comparable<pair1> {
		int x;
		int y;
		int z;

		pair1(int f, int t, int u) {
			x = f;
			y = t;
			z = u;
			/// i = n;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		@Override
		public int compareTo(pair1 o) {
			if (x == o.x)
				if (y == o.y)
					return z - o.z;
				else
					return y - o.y;
			else
				return x - o.x;
		}
	}

	static long gcd(long a, long b) {

		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public int[] merge(int[] d, int st, int e) {
		if (st > e)
			return null;
		if (e == st) {
			int[] ans = { d[e] };
			return ans;
		}
		int mid = (st + e) / 2;
		int[] ans = new int[e - st + 1];
		int[] ll = merge(d, st, mid);
		int[] rr = merge(d, mid + 1, e);
		if (ll == null)
			return rr;
		if (rr == null)
			return ll;
		int iver = 0;
		int idxl = 0;
		int idxr = 0;
		for (int i = st; i <= e; i++) {
			if (ll[idxl] < rr[idxr]) {

			}
		}
		return ans;
	}

	public static class pair2 implements Comparable<pair2> {
		int a;
		int idx;

		pair2(int a, int i) {
			this.a = a;
			idx = i;
		}

		public String toString() {
			return a + " " + idx;
		}

		@Override
		public int compareTo(pair2 o) {
			// TODO Auto-generated method stub
			return idx - o.idx;
		}
	}

	static long inver(long x) {
		int a = (int) x;
		long e = (mod - 2);
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1) {
				// System.out.println(res*a);
				res = (int) ((1l * res * a) % mod);
			}
			a = (int) ((1l * a * a) % mod);
			e >>= 1;
		}
		// out.println(res+" "+x);
		return res % mod;
	}

	static int atMostSum(int arr[], int n, int k) {
		int sum = 0;
		int cnt = 0, maxcnt = 0;

		for (int i = 0; i < n; i++) {

			// If adding current element doesn't
			// cross limit add it to current window
			if ((sum + arr[i]) <= k) {
				sum += arr[i];
				cnt++;
			}

			// Else, remove first element of current
			// window and add the current element
			else if (sum != 0) {
				sum = sum - arr[i - cnt] + arr[i];
			}

			// keep track of max length.
			maxcnt = Math.max(cnt, maxcnt);
		}
		return maxcnt;
	}

	public static int[] longestSubarray(int[] inp) {
		// array containing prefix sums up to a certain index i
		int[] p = new int[inp.length];
		p[0] = inp[0];
		for (int i = 1; i < inp.length; i++) {
			p[i] = p[i - 1] + inp[i];
		}

		// array Q from the description below
		int[] q = new int[inp.length];
		q[inp.length - 1] = p[inp.length - 1];
		for (int i = inp.length - 2; i >= 0; i--) {
			q[i] = Math.max(q[i + 1], p[i]);
		}
		int a = 0;
		int b = 0;
		int maxLen = 0;
		int curr;
		int[] res = new int[] { -1, -1 };
		while (b < inp.length) {
			curr = a > 0 ? q[b] - p[a - 1] : q[b];
			if (curr >= 0) {
				if (b - a > maxLen) {
					maxLen = b - a;
					res = new int[] { a, b };
				}
				b++;
			} else {
				a++;
			}
		}
		return res;
	}

	static void factor(int n) {
		if (si[n] == n) {
			f.add(n);
			return;
		}
		f.add(si[n]);
		factor(n / si[n]);
	}

	static void seive() {
		si = new int[100001];
		primes = new ArrayList<>();
		int N = 100001;
		si[1] = 1;
		for (int i = 2; i < N; i++) {
			if (si[i] == 0) {
				si[i] = i;
				primes.add(i);
			}
			for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
				si[primes.get(j) * i] = primes.get(j);

		}
	}

	static class unionfind {
		int[] p;
		int[] size;
		int jum;
		int[] colors;

		unionfind(int n, int[] c) {
			p = new int[n];
			size = new int[n];
			colors = c.clone();
			for (int i = 0; i < n; i++) {
				p[i] = i;
			}
			jum = n;
			Arrays.fill(size, 1);
		}

		int findSet(int v) {
			if (v == p[v])
				return v;
			return p[v] = findSet(p[v]);
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		void combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return;
			jum--;
			if (size[a] > size[b]) {
				p[b] = a;
				size[a] += size[b];
				colors[b] = -1;
			} else {
				p[a] = b;
				size[b] += size[a];
				colors[a] = -1;
			}
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

import org.omg.Messaging.SyncScopeHelper;

public class tr1 {
	static PrintWriter out;
	static StringBuilder sb;
	static int inf = (int) 1e9;
	static long mod = (long) 1000000007;
	static int[] si;
	static ArrayList<Integer> primes;
	static HashSet<Integer> pr;
	static int n, k, m;
	static int[] in;
	static HashMap<Integer, Integer> factors;
	static HashSet<Integer> f;
	static int[] fac, a, b;
	static int[] l, r;
	static int[][] memo1, memo2;
	static int[] numc;
	static ArrayList<Integer>[] ad;
	static HashMap<Integer, Integer> hm;
	static pair[] ed;
	static TreeSet<Integer>[] np;
	static TreeMap<pair1, Integer> tm;
	static long[] c1, c2, c3;
	static int[] col;
	static pair[] mm;
	static boolean[] vis, touch;
	static int max, c;
	static HashSet<Integer> hs;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		int n = sc.nextInt();
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		if (n == 1) {
			System.out.println(a[0]);
			return;
		}
		int[] lf = new int[n];
		int[] rf = new int[n];
		Stack<Integer> s = new Stack<>();
		Arrays.fill(lf, -1);
		Arrays.fill(rf, -1);
		for (int i = 0; i < n; i++) {
	//		if (s.isEmpty()) {
		//		s.push(i);
			//} else
				while (!s.isEmpty()) {
					if (a[s.peek()] >= a[i])
						s.pop();
					else {
						if (!s.isEmpty())
							lf[i] = s.peek();
				//		s.push(i);
						break;
					}
				}
		}
		s = new Stack<>();
		for (int i = n - 1; i >= 0; i--) {
			//if (s.isEmpty()) {
			//	s.push(i);
		//	} else
				while (!s.isEmpty()) {
					if (a[s.peek()] >= a[i])
						s.pop();
					else {
						if (!s.isEmpty())
							rf[i] = s.peek();
				//		s.push(i);
						break;
					}
				}
			s.push(i);
		}
		long[] rr = new long[n];
		long[] ll = new long[n];
		for (int i = 0; i < n; i++) {
			if (lf[i] == -1) {
				ll[i] = a[i] * ((long) (i + 1));
			} else {
				ll[i] = a[i] * ((long) (i - lf[i])) + ll[lf[i]];
			}
		}
		for (int i = n-1; i >= 0; i--) {
			if (rf[i] == -1) {
				rr[i] = a[i] * ((long) (n - i));
			} else {
				rr[i] = a[i] * ((long) (rf[i]-i)) + rr[rf[i]];
			}
		}
		long max=0;
		int id=0;
		long []print=new long [n];
		for(int i=0;i<n;i++) {
			long tem=0;
			tem+=ll[i]+rr[i]-a[i];
			if(tem>max) {
				max=tem;
				id=i;
			}
		}
		///System.out.println(Arrays.toString(lf));
	///	System.out.println(Arrays.toString(rf));
		print[id]=a[id];
		long min=a[id];
		for(int i=id+1;i<n;i++)
		{
			print[i]=Math.min(print[i-1],a[i]);
		}
		for(int i=id-1;i>=0;i--)
		{
			print[i]=Math.min(print[i+1],a[i]);
		}
		for(int i=0;i<n;i++)
			out.print(print[i]+" ");
		out.flush();
	}

	static class pair implements Comparable<pair> {
		int p;
		int c;
		int id;

		pair(int p11, int cc, int i) {
			p = p11;
			c = cc;
			id = i;
		}

		public String toString() {
			return p + " " + c + " " + id;
		}

		@Override
		public int compareTo(pair o) {
			if (c == o.c)
				return id - o.id;
			return p - o.p;
		}
	}

	static class pair1 implements Comparable<pair1> {
		int x;
		int y;
		int z;

		pair1(int f, int t, int u) {
			x = f;
			y = t;
			z = u;
			/// i = n;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		@Override
		public int compareTo(pair1 o) {
			if (x == o.x)
				if (y == o.y)
					return z - o.z;
				else
					return y - o.y;
			else
				return x - o.x;
		}
	}

	static long gcd(long a, long b) {

		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public int[] merge(int[] d, int st, int e) {
		if (st > e)
			return null;
		if (e == st) {
			int[] ans = { d[e] };
			return ans;
		}
		int mid = (st + e) / 2;
		int[] ans = new int[e - st + 1];
		int[] ll = merge(d, st, mid);
		int[] rr = merge(d, mid + 1, e);
		if (ll == null)
			return rr;
		if (rr == null)
			return ll;
		int iver = 0;
		int idxl = 0;
		int idxr = 0;
		for (int i = st; i <= e; i++) {
			if (ll[idxl] < rr[idxr]) {

			}
		}
		return ans;
	}

	public static class pair2 implements Comparable<pair2> {
		int a;
		int idx;

		pair2(int a, int i) {
			this.a = a;
			idx = i;
		}

		public String toString() {
			return a + " " + idx;
		}

		@Override
		public int compareTo(pair2 o) {
			// TODO Auto-generated method stub
			return idx - o.idx;
		}
	}

	static long inver(long x) {
		int a = (int) x;
		long e = (mod - 2);
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1) {
				// System.out.println(res*a);
				res = (int) ((1l * res * a) % mod);
			}
			a = (int) ((1l * a * a) % mod);
			e >>= 1;
		}
		// out.println(res+" "+x);
		return res % mod;
	}

	static int atMostSum(int arr[], int n, int k) {
		int sum = 0;
		int cnt = 0, maxcnt = 0;

		for (int i = 0; i < n; i++) {

			// If adding current element doesn't
			// cross limit add it to current window
			if ((sum + arr[i]) <= k) {
				sum += arr[i];
				cnt++;
			}

			// Else, remove first element of current
			// window and add the current element
			else if (sum != 0) {
				sum = sum - arr[i - cnt] + arr[i];
			}

			// keep track of max length.
			maxcnt = Math.max(cnt, maxcnt);
		}
		return maxcnt;
	}

	public static int[] longestSubarray(int[] inp) {
		// array containing prefix sums up to a certain index i
		int[] p = new int[inp.length];
		p[0] = inp[0];
		for (int i = 1; i < inp.length; i++) {
			p[i] = p[i - 1] + inp[i];
		}

		// array Q from the description below
		int[] q = new int[inp.length];
		q[inp.length - 1] = p[inp.length - 1];
		for (int i = inp.length - 2; i >= 0; i--) {
			q[i] = Math.max(q[i + 1], p[i]);
		}
		int a = 0;
		int b = 0;
		int maxLen = 0;
		int curr;
		int[] res = new int[] { -1, -1 };
		while (b < inp.length) {
			curr = a > 0 ? q[b] - p[a - 1] : q[b];
			if (curr >= 0) {
				if (b - a > maxLen) {
					maxLen = b - a;
					res = new int[] { a, b };
				}
				b++;
			} else {
				a++;
			}
		}
		return res;
	}

	static void factor(int n) {
		if (si[n] == n) {
			f.add(n);
			return;
		}
		f.add(si[n]);
		factor(n / si[n]);
	}

	static void seive() {
		si = new int[100001];
		primes = new ArrayList<>();
		int N = 100001;
		si[1] = 1;
		for (int i = 2; i < N; i++) {
			if (si[i] == 0) {
				si[i] = i;
				primes.add(i);
			}
			for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
				si[primes.get(j) * i] = primes.get(j);

		}
	}

	static class unionfind {
		int[] p;
		int[] size;
		int jum;
		int[] colors;

		unionfind(int n, int[] c) {
			p = new int[n];
			size = new int[n];
			colors = c.clone();
			for (int i = 0; i < n; i++) {
				p[i] = i;
			}
			jum = n;
			Arrays.fill(size, 1);
		}

		int findSet(int v) {
			if (v == p[v])
				return v;
			return p[v] = findSet(p[v]);
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		void combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return;
			jum--;
			if (size[a] > size[b]) {
				p[b] = a;
				size[a] += size[b];
				colors[b] = -1;
			} else {
				p[a] = b;
				size[b] += size[a];
				colors[a] = -1;
			}
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

import org.omg.Messaging.SyncScopeHelper;

public class tr1 {
	static PrintWriter out;
	static StringBuilder sb;
	static int inf = (int) 1e9;
	static long mod = (long) 1000000007;
	static int[] si;
	static ArrayList<Integer> primes;
	static HashSet<Integer> pr;
	static int n, k, m;
	static int[] in;
	static HashMap<Integer, Integer> factors;
	static HashSet<Integer> f;
	static int[] fac, a, b;
	static int[] l, r;
	static int[][] memo1, memo2;
	static int[] numc;
	static ArrayList<Integer>[] ad;
	static HashMap<Integer, Integer> hm;
	static pair[] ed;
	static TreeSet<Integer>[] np;
	static TreeMap<pair1, Integer> tm;
	static long[] c1, c2, c3;
	static int[] col;
	static pair[] mm;
	static boolean[] vis, touch;
	static int max, c;
	static HashSet<Integer> hs;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		int n = sc.nextInt();
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		if(n==1) {
			System.out.println(a[0]);
			return;
		}
		long []minsuff=new long [n];
		long []minpre=new long [n];
		minpre[0]=a[0];
		for(int i=1;i<n;i++)
			minpre[i]=Math.min(a[i], minpre[i-1]);
		minsuff[n-1]=a[n-1];
		for(int i=n-2;i>=0;i--)
			minsuff[i]=Math.min(a[i], minsuff[i+1]);
		long []sumsuff=new long [n];
		long []sumpre=new long [n];
		sumpre[0]=minpre[0];
		for(int i=1;i<n;i++)
			sumpre[i]=minpre[i]+sumpre[i-1];
		sumsuff[n-1]=minsuff[n-1];
		for(int i=n-2;i>=0;i--)
			sumsuff[i]=minsuff[i]+sumsuff[i+1];
		long ans=0;
		int id=0;
		for(int i=0;i<n;i++) {
			long tem=0;
			if(i==0) {
				tem+=a[i];
				tem+=sumsuff[i+1];
			}
			else if(i==n-1) {
				tem+=a[i];
				tem+=sumpre[i-1];
			}
			else {
				tem+=a[i];
				tem+=sumsuff[i+1];
				tem+=sumpre[i-1];
			}
			if(tem>ans) {
				ans=tem;
				id=i;
			}
		}
	//	System.out.println(id);
		//System.out.println(Arrays.toString(minpre));
		//System.out.println(Arrays.toString(minsuff));
		long []print=new long [n];
		print[id]=a[id];
		long min=a[id];
		for(int i=id+1;i<n;i++) {
			print[i]=Math.min(a[i], min);
		  min=Math.min(min, a[i]);
		}
		min=a[id];
		for(int i=id-1;i>=0;i--)
		{
			print[i]=Math.min(a[i], min);
			  min=Math.min(min, a[i]);
		}
		for(int i=0;i<n;i++)
			out.print(print[i]+" ");
		out.flush();
	}

	static void dfs(int u) {
		vis[u] = true;
		if (u == n - 1)
			touch[u] = true;
		for (int v : ad[u])
			if (!vis[v]) {
				dfs(v);
				touch[u] |= touch[v];
			}
	}

	static class pair implements Comparable<pair> {
		int p;
		int c;
		int id;

		pair(int p11, int cc, int i) {
			p = p11;
			c = cc;
			id = i;
		}

		public String toString() {
			return p + " " + c + " " + id;
		}

		@Override
		public int compareTo(pair o) {
			if (c == o.c)
				return id - o.id;
			return p - o.p;
		}
	}

	static class pair1 implements Comparable<pair1> {
		int x;
		int y;
		int z;

		pair1(int f, int t, int u) {
			x = f;
			y = t;
			z = u;
			/// i = n;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		@Override
		public int compareTo(pair1 o) {
			if (x == o.x)
				if (y == o.y)
					return z - o.z;
				else
					return y - o.y;
			else
				return x - o.x;
		}
	}

	static long gcd(long a, long b) {

		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public int[] merge(int[] d, int st, int e) {
		if (st > e)
			return null;
		if (e == st) {
			int[] ans = { d[e] };
			return ans;
		}
		int mid = (st + e) / 2;
		int[] ans = new int[e - st + 1];
		int[] ll = merge(d, st, mid);
		int[] rr = merge(d, mid + 1, e);
		if (ll == null)
			return rr;
		if (rr == null)
			return ll;
		int iver = 0;
		int idxl = 0;
		int idxr = 0;
		for (int i = st; i <= e; i++) {
			if (ll[idxl] < rr[idxr]) {

			}
		}
		return ans;
	}

	public static class pair2 implements Comparable<pair2> {
		int a;
		int idx;

		pair2(int a, int i) {
			this.a = a;
			idx = i;
		}

		public String toString() {
			return a + " " + idx;
		}

		@Override
		public int compareTo(pair2 o) {
			// TODO Auto-generated method stub
			return idx - o.idx;
		}
	}

	static long inver(long x) {
		int a = (int) x;
		long e = (mod - 2);
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1) {
				// System.out.println(res*a);
				res = (int) ((1l * res * a) % mod);
			}
			a = (int) ((1l * a * a) % mod);
			e >>= 1;
		}
		// out.println(res+" "+x);
		return res % mod;
	}

	static int atMostSum(int arr[], int n, int k) {
		int sum = 0;
		int cnt = 0, maxcnt = 0;

		for (int i = 0; i < n; i++) {

			// If adding current element doesn't
			// cross limit add it to current window
			if ((sum + arr[i]) <= k) {
				sum += arr[i];
				cnt++;
			}

			// Else, remove first element of current
			// window and add the current element
			else if (sum != 0) {
				sum = sum - arr[i - cnt] + arr[i];
			}

			// keep track of max length.
			maxcnt = Math.max(cnt, maxcnt);
		}
		return maxcnt;
	}

	public static int[] longestSubarray(int[] inp) {
		// array containing prefix sums up to a certain index i
		int[] p = new int[inp.length];
		p[0] = inp[0];
		for (int i = 1; i < inp.length; i++) {
			p[i] = p[i - 1] + inp[i];
		}

		// array Q from the description below
		int[] q = new int[inp.length];
		q[inp.length - 1] = p[inp.length - 1];
		for (int i = inp.length - 2; i >= 0; i--) {
			q[i] = Math.max(q[i + 1], p[i]);
		}
		int a = 0;
		int b = 0;
		int maxLen = 0;
		int curr;
		int[] res = new int[] { -1, -1 };
		while (b < inp.length) {
			curr = a > 0 ? q[b] - p[a - 1] : q[b];
			if (curr >= 0) {
				if (b - a > maxLen) {
					maxLen = b - a;
					res = new int[] { a, b };
				}
				b++;
			} else {
				a++;
			}
		}
		return res;
	}

	static void factor(int n) {
		if (si[n] == n) {
			f.add(n);
			return;
		}
		f.add(si[n]);
		factor(n / si[n]);
	}

	static void seive() {
		si = new int[100001];
		primes = new ArrayList<>();
		int N = 100001;
		si[1] = 1;
		for (int i = 2; i < N; i++) {
			if (si[i] == 0) {
				si[i] = i;
				primes.add(i);
			}
			for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
				si[primes.get(j) * i] = primes.get(j);

		}
	}

	static class unionfind {
		int[] p;
		int[] size;
		int jum;
		int[] colors;

		unionfind(int n, int[] c) {
			p = new int[n];
			size = new int[n];
			colors = c.clone();
			for (int i = 0; i < n; i++) {
				p[i] = i;
			}
			jum = n;
			Arrays.fill(size, 1);
		}

		int findSet(int v) {
			if (v == p[v])
				return v;
			return p[v] = findSet(p[v]);
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		void combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return;
			jum--;
			if (size[a] > size[b]) {
				p[b] = a;
				size[a] += size[b];
				colors[b] = -1;
			} else {
				p[a] = b;
				size[b] += size[a];
				colors[a] = -1;
			}
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


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

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.util.Comparator.*;

public class Main {
    FastScanner in;
    PrintWriter out;
    ArrayList<Integer>[] graph;
    ArrayList<GraphPair>[] weightedGraph;
    long mod = 998_244_353L; // (long) 1e9 + 7 || (long) 1e9 + 9
    boolean multitests = false;

    private void solve() throws IOException {
        // solveA();
        // solveB();
        solveC();
    }

    private void solveA() throws IOException {
        int[] a = in.nextIntArray(3);
        sort(a);

        int ans = 0;
        for (int i = 0; i < 3; i++) {
            if (a[i] > 0) {
                a[i]--;
                ans++;
            }
        }

        for (int i = 0; i < 3; i++) {
            if (a[(i + 1) % 3] > 0 && a[(i + 2) % 3] > 0) {
                a[(i + 1) % 3]--;
                a[(i + 2) % 3]--;
                ans++;
            }
        }

        if (a[0] > 0 && a[1] > 0 && a[2] > 0)
            ans++;

        out.println(ans);
    }

    private void solveB() throws IOException {
        int n = in.nextInt(), x = in.nextInt(), y = in.nextInt();
        out.println((n - min(x - 1, max(0, n - y - 1)) - min(y - 1, max(0, n - x - 1)) - min(n - x - min(y - 1, max(0, n - x - 1)), n - y - min(x - 1, max(0, n - y - 1))))
                + " " +
                (1 + min(x - 1, n - y) + min(y - 1, n - x) + min(x - 1 - min(x - 1, n - y), y - 1 - min(y - 1, n - x))));
    }

    private void solveC() throws IOException {
        int n = in.nextInt();
        long[] m = in.nextLongArray(n);

        int[] st = new int[n];
        int ss = 0;

        int[] prefParent = new int[n];
        long[] prefSum = new long[n];
        for (int i = 0; i < n; i++) {
            while (ss > 0 && m[st[ss - 1]] > m[i])
                ss--;
            prefParent[i] = ss == 0 ? -1 : st[ss - 1];
            prefSum[i] = ss == 0 ? (i + 1) * m[i] : prefSum[st[ss - 1]] + (i - st[ss - 1]) * m[i];
            st[ss++] = i;
        }

        ss = 0;

        int[] suffParent = new int[n];
        long[] suffSum = new long[n];
        for (int i = n - 1; i >= 0; i--) {
            while (ss > 0 && m[st[ss - 1]] > m[i])
                ss--;
            suffParent[i] = ss == 0 ? n : st[ss - 1];
            suffSum[i] = ss == 0 ? (n - i) * m[i] : suffSum[st[ss - 1]] + (st[ss - 1] - i) * m[i];
            st[ss++] = i;
        }

        int maxId = 0;
        for (int i = 1; i < n; i++)
            if (prefSum[i] + suffSum[i] - m[i] > prefSum[maxId] + suffSum[maxId] - m[maxId])
                maxId = i;

        long[] ans = new long[n];
        long cur = (int) 1e9 + 1;
        for (int i = maxId, next = maxId; i < n; i++) {
            if (i == next) {
                cur = m[i];
                next = suffParent[i];
            }
            ans[i] = cur;
        }

        cur = (int) 1e9 + 1;
        for (int i = maxId, next = maxId; i >= 0; i--) {
            if (i == next) {
                cur = m[i];
                next = prefParent[i];
            }
            ans[i] = cur;
        }

        for (int i = 0; i < n; i++) {
            out.print(ans[i] + " ");
        }
    }

    int binSearch(int[] a, int x) {
        int l = -1, mid, r = a.length;
        while (l + 1 < r) {
            mid = (l + r) / 2;
            if (a[mid] >= x)
                r = mid;
            else
                l = mid;
        }
        return r;
    }

    void shuffleInt(int[] a) {
        Random random = new Random();
        for (int i = a.length - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            int swap = a[j];
            a[j] = a[i];
            a[i] = swap;
        }
    }

    void shuffleLong(long[] a) {
        Random random = new Random();
        for (int i = a.length - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            long swap = a[j];
            a[j] = a[i];
            a[i] = swap;
        }
    }

    void reverseInt(int[] a) {
        for (int i = 0, j = a.length - 1; i < j; i++, j--) {
            int swap = a[i];
            a[i] = a[j];
            a[j] = swap;
        }
    }

    void reverseLong(long[] a) {
        for (int i = 0, j = a.length - 1; i < j; i++, j--) {
            long swap = a[i];
            a[i] = a[j];
            a[j] = swap;
        }
    }

    int maxInt(int[] a) {
        int max = a[0];
        for (int i = 1; i < a.length; i++)
            if (max < a[i])
                max = a[i];
        return max;
    }

    long maxLong(long[] a) {
        long max = a[0];
        for (int i = 1; i < a.length; i++)
            if (max < a[i])
                max = a[i];
        return max;
    }

    int minInt(int[] a) {
        int min = a[0];
        for (int i = 1; i < a.length; i++)
            if (min > a[i])
                min = a[i];
        return min;
    }

    long minLong(long[] a) {
        long min = a[0];
        for (int i = 1; i < a.length; i++)
            if (min > a[i])
                min = a[i];
        return min;
    }

    long sumInt(int[] a) {
        long s = 0;
        for (int i = 0; i < a.length; i++)
            s += a[i];
        return s;
    }

    long sumLong(long[] a) {
        long s = 0;
        for (int i = 0; i < a.length; i++)
            s += a[i];
        return s;
    }

    long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    long binpowmod(long a, long n) {
        long res = 1;
        a %= mod;
        n %= mod - 1;
        while (n > 0) {
            if (n % 2 == 1)
                res = (res * a) % mod;
            a = (a * a) % mod;
            n /= 2;
        }
        return res;
    }

    class GraphPair implements Comparable<GraphPair> {
        int v;
        long w;

        GraphPair(int v, long w) {
            this.v = v;
            this.w = w;
        }

        public int compareTo(GraphPair o) {
            return w != o.w ? Long.compare(w, o.w) : Integer.compare(v, o.v);
        }
    }

    ArrayList<Integer>[] createGraph(int n) {
        ArrayList<Integer>[] graph = new ArrayList[n];
        for (int i = 0; i < n; i++)
            graph[i] = new ArrayList<>();
        return graph;
    }

    ArrayList<GraphPair>[] createWeightedGraph(int n) {
        ArrayList<GraphPair>[] graph = new ArrayList[n];
        for (int i = 0; i < n; i++)
            graph[i] = new ArrayList<>();
        return graph;
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s), 32768);
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

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

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

        int[][] nextIntTable(int n, int m) throws IOException {
            int[][] a = new int[n][m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    a[i][j] = in.nextInt();
            return a;
        }

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

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

        long[][] nextLongTable(int n, int m) throws IOException {
            long[][] a = new long[n][m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    a[i][j] = in.nextLong();
            return a;
        }

        ArrayList<Integer>[] nextGraph(int n, int m, boolean directed) throws IOException {
            ArrayList<Integer>[] graph = createGraph(n);
            for (int i = 0; i < m; i++) {
                int v = in.nextInt() - 1, u = in.nextInt() - 1;
                graph[v].add(u);
                if (!directed)
                    graph[u].add(v);
            }
            return graph;
        }

        ArrayList<GraphPair>[] nextWeightedGraph(int n, int m, boolean directed) throws IOException {
            ArrayList<GraphPair>[] graph = createWeightedGraph(n);
            for (int i = 0; i < m; i++) {
                int v = in.nextInt() - 1, u = in.nextInt() - 1;
                long w = in.nextLong();
                graph[v].add(new GraphPair(u, w));
                if (!directed)
                    graph[u].add(new GraphPair(v, w));
            }
            return graph;
        }

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

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        for (int t = multitests ? in.nextInt() : 1; t-- > 0; )
            solve();

        out.flush();
        out.close();
    }

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


//created by Whiplash99
import java.io.*;
import java.util.*;
public class A
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i,N;

        N=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().trim().split(" ");
        int a[]=new int[N];
        for(i=0;i<N;i++) a[i]=Integer.parseInt(s[i]);

        long[] l=new long[N];
        long[] r=new long[N];

        ArrayDeque<Integer> stack=new ArrayDeque<>();
        int[] next=new int[N];
        for(i=0;i<N;i++)
        {
            while (!stack.isEmpty()&&a[stack.peek()]>a[i])
                next[stack.pop()]=i;
            stack.push(i);
        }
        while (!stack.isEmpty()) next[stack.pop()]=N;

        int[] prev=new int[N];
        for(i=N-1;i>=0;i--)
        {
            while (!stack.isEmpty()&&a[stack.peek()]>a[i])
                prev[stack.pop()]=i;
            stack.push(i);
        }
        while (!stack.isEmpty()) prev[stack.pop()]=-1;

        for(i=0;i<N;i++)
        {
            int j=prev[i];

            if(j>=0) l[i]+=l[j];
            l[i]+=(long)a[i]*(i-j);
        }

        for(i=N-1;i>=0;i--)
        {
            int j=next[i];

            if(j<N) r[i]+=r[j];
            r[i]+=(long)a[i]*(j-i);
        }

        long ans=0; int pos=-1;
        for(i=0;i<N;i++)
        {
            long tmp=l[i]+r[i]-a[i];
            if(tmp>ans)
            {
                ans=tmp;
                pos=i;
            }
        }

        ArrayDeque<Integer> DQ=new ArrayDeque<>();
        DQ.add(a[pos]);

        int tmp=a[pos];
        for(i=pos-1;i>=0;i--)
        {
            tmp=Math.min(tmp,a[i]);
            DQ.addFirst(tmp);
        }

        tmp=a[pos];
        for(i=pos+1;i<N;i++)
        {
            tmp=Math.min(tmp,a[i]);
            DQ.addLast(tmp);
        }

        StringBuilder sb=new StringBuilder();
        for(int x:DQ) sb.append(x).append(" ");

        System.out.println(sb);
    }
}


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

public class MainClass
{
    static long[] ans;
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        ans = new long[n];
        long[] A = new long[n];
        for (int i=0;i<n;i++)   A[i] = in.nextLong();
        Stack<Integer> stack = new Stack<>();
        int[] left = new int[n];
        int[] right = new int[n];
        for (int i=n - 1;i>=0;i--)
        {
            long x = A[i];
            while (!stack.empty() && A[stack.peek()] > x)
            {
                left[stack.pop()] = i;
            }
            stack.push(i);
        }
        while (!stack.empty())
            left[stack.pop()] = -1;
        for (int i=0;i< n;i++)
        {
            long x = A[i];
            while (!stack.empty() && A[stack.peek()] > x)
            {
                right[stack.pop()] = i;
            }
            stack.push(i);
        }
        while (!stack.empty())
            right[stack.pop()] = n;
        long[] dp1 = new long[n];
        long[] dp2 = new long[n];
        for (int i=0;i<n;i++)
        {
            int smallToLeft = left[i];
            dp1[i] = ((smallToLeft > -1)?dp1[smallToLeft]:0) + (i - smallToLeft) * A[i];
        }
        for (int i=n - 1;i>=0;i--)
        {
            int smallToRight = right[i];
            dp2[i] = ((smallToRight < n)?dp2[smallToRight]:0) + (smallToRight - i) * A[i];
        }

        long max = 0L;
        int idx = -1;
        for (int i=0;i<n;i++)
        {
            long res = dp1[i] + dp2[i] - A[i];
            if (res > max)
            {
                max = res;
                idx = i;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        ans[idx] = A[idx];
        long mm = A[idx];
        for (int i=idx - 1;i>=0;i--)
        {
            mm = Math.min(mm, A[i]);
            ans[i] = mm;
        }
        mm = A[idx];
        for (int i=idx + 1;i<n;i++)
        {
            mm = Math.min(mm, A[i]);
            ans[i] = mm;
        }
        for (int i=0;i<n;i++)
            stringBuilder.append(ans[i]).append(" ");
        System.out.println(stringBuilder);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}
class BIT
{
    int size;
    long[] table;
    public BIT(int size)
    {
        table = new long[size];
        this.size = size;
    }

    void update(int i, long delta)
    {
        while (i > 0)
        {
            table[i] += delta;
            i -= Integer.lowestOneBit(i);
        }
    }

    long sum(int i)
    {
        long sum = 0L;
        while (i < size)
        {
            sum += table[i];
            i += Integer.lowestOneBit(i);
        }
        return sum;
    }
}


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

public class MainClass
{
    static long[] ans;
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        ans = new long[n];
        long[] A = new long[n];
        for (int i=0;i<n;i++)   A[i] = in.nextLong();
        StringBuilder stringBuilder = new StringBuilder();
        BIT BIT1 = new BIT(n + 10);
        BIT BIT2 = new BIT(n + 10);
        long[] left = new long[n];
        long[] right = new long[n];
        left[0] = A[0];
        BIT1.update((int)A[0], A[0]);
        BIT2.update((int)A[0], 1);
        for (int i=1;i<n;i++)
        {
            long sum = BIT1.sum((int)A[i] + 1);
            long count = BIT2.sum((int)A[i] + 1);
            left[i] = left[i - 1] + A[i] - sum + count * A[i];
            BIT1.update((int)A[i], A[i]);
            BIT2.update((int)A[i], 1);
        }
        right[n - 1] = A[n - 1];
        BIT BIT3 = new BIT(n + 10);
        BIT BIT4 = new BIT(n + 10);
        BIT3.update((int)A[n - 1], A[n - 1]);
        BIT4.update((int)A[n - 1], 1);
        for (int i=n - 2;i>=0;i--)
        {
            long sum = BIT3.sum((int)A[i] + 1);
            long count = BIT4.sum((int)A[i] + 1);
            right[i] = right[i + 1] + A[i] - sum + count * A[i];
            BIT3.update((int)A[i], A[i]);
            BIT4.update((int)A[i], 1);
        }

        long max = 0L;
        int idx = -1;
        for (int i=0;i<n ;i++)
        {
            long res = left[i] + right[i] - A[i];
            if (res > max)
            {
                max = res;
                idx = i;
            }
        }
        long[] ans = new long[n];
        ans[idx] = A[idx];
        long mm = A[idx];
        for (int i=idx -1;i>=0;i--)
        {
            ans[i] = Math.min(A[i], mm);
            mm = Math.min(A[i], mm);
        }
        mm = A[idx];
        for (int i=idx +1;i< n;i++)
        {
            ans[i] = Math.min(A[i], mm);
            mm = Math.min(A[i], mm);
        }
        for (int i=0;i<n;i++)
            stringBuilder.append(ans[i]).append(" ");
        System.out.println(stringBuilder);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}
class BIT
{
    int size;
    long[] table;
    public BIT(int size)
    {
        table = new long[size];
        this.size = size;
    }

    void update(int i, long delta)
    {
        while (i > 0)
        {
            table[i] += delta;
            i -= Integer.lowestOneBit(i);
        }
    }

    long sum(int i)
    {
        long sum = 0L;
        while (i < size)
        {
            sum += table[i];
            i += Integer.lowestOneBit(i);
        }
        return sum;
    }
}


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

public class MainClass
{
    static long[] ans;
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        ans = new long[n];
        int[] A = new int[n];
        long[] B = new long[n];
        long[] C = new long[n];
        for (int i=0;i<n;i++)
        {
            C[i] = B[i] = in.nextLong();
        }
        new MergeSortLong().sort(B, 0, n - 1);
        for (int i=0;i<n;i++)
            A[i] = Arrays.binarySearch(B, C[i]) + 1;
        StringBuilder stringBuilder = new StringBuilder();
        BIT BIT1 = new BIT(n + 10);
        BIT BIT2 = new BIT(n + 10);
        long[] left = new long[n];
        long[] right = new long[n];
        left[0] = B[A[0] - 1];
        BIT1.update(A[0], B[A[0] - 1]);
        BIT2.update(A[0], 1);
        long s = B[A[0] - 1];
        for (int i=1;i<n;i++)
        {
            long sum = BIT1.sum(A[i] + 1);
            long count = BIT2.sum(A[i] + 1);
            left[i] = s + B[A[i] - 1] - sum + count * B[A[i] - 1];
            BIT1.update(A[i], B[A[i] - 1]);
            BIT2.update(A[i], 1);
            s += B[A[i] - 1];
        }
        right[n - 1] = B[A[n - 1] - 1];
        BIT BIT3 = new BIT(n + 10);
        BIT BIT4 = new BIT(n + 10);
        BIT3.update(A[n - 1], B[A[n - 1] - 1]);
        BIT4.update(A[n - 1], 1);
        s = B[A[n - 1] - 1];
        for (int i=n - 2;i>=0;i--)
        {
            long sum = BIT3.sum(A[i] + 1);
            long count = BIT4.sum(A[i] + 1);
            right[i] = s + B[A[i] - 1] - sum + count * B[A[i] - 1];
            BIT3.update(A[i], B[A[i] - 1]);
            BIT4.update(A[i], 1);
            s += B[A[i] - 1];
        }

        long max = 0L;
        int idx = -1;
        for (int i=0;i<n ;i++)
        {
            long res = left[i] + right[i] - B[i];
            if (res > max)
            {
                max = res;
                idx = i;
            }
        }
        long[] ans = new long[n];
        ans[idx] = C[idx];
        long mm = C[idx];
        for (int i=idx -1;i>=0;i--)
        {
            ans[i] = Math.min(C[i], mm);
            mm = Math.min(C[i], mm);
        }
        mm = C[idx];
        for (int i=idx +1;i< n;i++)
        {
            ans[i] = Math.min(C[i], mm);
            mm = Math.min(C[i], mm);
        }
        for (int i=0;i<n;i++)
            stringBuilder.append(ans[i]).append(" ");
        System.out.println(stringBuilder);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}
class BIT
{
    int size;
    long[] table;
    public BIT(int size)
    {
        table = new long[size];
        this.size = size;
    }

    void update(int i, long delta)
    {
        while (i > 0)
        {
            table[i] += delta;
            i -= Integer.lowestOneBit(i);
        }
    }

    long sum(int i)
    {
        long sum = 0L;
        while (i < size)
        {
            sum += table[i];
            i += Integer.lowestOneBit(i);
        }
        return sum;
    }
}
class MergeSortLong
{
    // Merges two subarrays of arr[].
    // First subarray is arr[l..m]
    // Second subarray is arr[m+1..r]
    void merge(long arr[], int l, int m, int r) {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        long L[] = new long[n1];
        long R[] = new long[n2];

        /*Copy data to temp arrays*/
        for (int i = 0; i < n1; ++i)
            L[i] = arr[l + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[m + 1 + j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
    void sort(long arr[], int l, int r) {
        if (l < r) {
            // Find the middle point
            int m = (l + r) / 2;

            // Sort first and second halves
            sort(arr, l, m);
            sort(arr, m + 1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }
}


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

public class MainClass
{
    static long[] ans;
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        ans = new long[n];
        int[] A = new int[n];
        long[] B = new long[n];
        long[] C = new long[n];
        for (int i=0;i<n;i++)
        {
            C[i] = B[i] = in.nextLong();
        }
        new MergeSortLong().sort(B, 0, n - 1);
        for (int i=0;i<n;i++)
            A[i] = Arrays.binarySearch(B, C[i]) + 1;
        StringBuilder stringBuilder = new StringBuilder();
        BIT BIT1 = new BIT(n + 10);
        BIT BIT2 = new BIT(n + 10);
        long[] left = new long[n];
        long[] right = new long[n];
        left[0] = B[A[0] - 1];
        BIT1.update(A[0], B[A[0] - 1]);
        BIT2.update(A[0], 1);
        long s = B[A[0] - 1];
        for (int i=1;i<n;i++)
        {
            long sum = BIT1.sum(A[i] + 1);
            long count = BIT2.sum(A[i] + 1);
            left[i] = s + B[A[i] - 1] - sum + count * B[A[i] - 1];
            BIT1.update(A[i], B[A[i] - 1]);
            BIT2.update(A[i], 1);
            s += B[A[i] - 1];
        }
        right[n - 1] = B[A[n - 1] - 1];
        BIT BIT3 = new BIT(n + 10);
        BIT BIT4 = new BIT(n + 10);
        BIT3.update(A[n - 1], B[A[n - 1] - 1]);
        BIT4.update(A[n - 1], 1);
        s = B[A[n - 1] - 1];
        for (int i=n - 2;i>=0;i--)
        {
            long sum = BIT3.sum(A[i] + 1);
            long count = BIT4.sum(A[i] + 1);
            right[i] = s + B[A[i] - 1] - sum + count * B[A[i] - 1];
            BIT3.update(A[i], B[A[i] - 1]);
            BIT4.update(A[i], 1);
            s += B[A[i] - 1];
        }

        long max = 0L;
        int idx = -1;
        for (int i=0;i<n ;i++)
        {
            long res = left[i] + right[i] - B[i];
            if (res > max)
            {
                max = res;
                idx = i;
            }
        }
        long[] ans = new long[n];
        ans[idx] = C[idx];
        long mm = C[idx];
        for (int i=idx -1;i>=0;i--)
        {
            ans[i] = Math.min(C[i], mm);
            mm = Math.min(C[i], mm);
        }
        mm = C[idx];
        for (int i=idx +1;i< n;i++)
        {
            ans[i] = Math.min(C[i], mm);
            mm = Math.min(C[i], mm);
        }
        for (int i=0;i<n;i++)
            stringBuilder.append(ans[i]).append(" ");
        System.out.println(stringBuilder);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}


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

public class MainClass
{
    static long[] ans;
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        ans = new long[n];
        int[] A = new int[n];
        long[] B = new long[n];
        long[] C = new long[n];
        for (int i=0;i<n;i++)
        {
            C[i] = B[i] = in.nextLong();
        }
        new MergeSortLong().sort(B, 0, n - 1);
        for (int i=0;i<n;i++)
            A[i] = Arrays.binarySearch(B, C[i]) + 1;
        StringBuilder stringBuilder = new StringBuilder();
        BIT BIT1 = new BIT(n + 10);
        BIT BIT2 = new BIT(n + 10);
        long[] left = new long[n];
        long[] right = new long[n];
        left[0] = B[A[0] - 1];
        BIT1.update(A[0], B[A[0] - 1]);
        BIT2.update(A[0], 1);
        long s = B[A[0] - 1];
        for (int i=1;i<n;i++)
        {
            long sum = BIT1.sum(A[i] + 1);
            long count = BIT2.sum(A[i] + 1);
            left[i] = s + B[A[i] - 1] - sum + count * B[A[i] - 1];
            BIT1.update(A[i], B[A[i] - 1]);
            BIT2.update(A[i], 1);
            s += B[A[i] - 1];
        }
        right[n - 1] = B[A[n - 1] - 1];
        BIT BIT3 = new BIT(n + 10);
        BIT BIT4 = new BIT(n + 10);
        BIT3.update(A[n - 1], B[A[n - 1] - 1]);
        BIT4.update(A[n - 1], 1);
        s = B[A[n - 1] - 1];
        for (int i=n - 2;i>=0;i--)
        {
            long sum = BIT3.sum(A[i] + 1);
            long count = BIT4.sum(A[i] + 1);
            right[i] = s + B[A[i] - 1] - sum + count * B[A[i] - 1];
            BIT3.update(A[i], B[A[i] - 1]);
            BIT4.update(A[i], 1);
            s += B[A[i] - 1];
        }

        long max = 0L;
        int idx = -1;
        for (int i=0;i<n ;i++)
        {
            long res = left[i] + right[i] - B[i];
            if (res > max)
            {
                max = res;
                idx = i;
            }
        }
        long[] ans = new long[n];
        ans[idx] = C[idx];
        long mm = C[idx];
        for (int i=idx -1;i>=0;i--)
        {
            ans[i] = Math.min(C[i], mm);
            mm = Math.min(C[i], mm);
        }
        mm = C[idx];
        for (int i=idx +1;i< n;i++)
        {
            ans[i] = Math.min(C[i], mm);
            mm = Math.min(C[i], mm);
        }
        for (int i=0;i<n;i++)
            stringBuilder.append(ans[i]).append(" ");
        System.out.println(stringBuilder);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}
class BIT
{
    int size;
    long[] table;
    public BIT(int size)
    {
        table = new long[size];
        this.size = size;
    }

    void update(int i, long delta)
    {
        while (i > 0)
        {
            table[i] += delta;
            i -= Integer.lowestOneBit(i);
        }
    }

    long sum(int i)
    {
        long sum = 0L;
        while (i < size)
        {
            sum += table[i];
            i += Integer.lowestOneBit(i);
        }
        return sum;
    }
}


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

public class MainClass
{
    static long[] ans;
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        ans = new long[n];
        long[] A = new long[n];
        for (int i=0;i<n;i++)   A[i] = in.nextLong();
        StringBuilder stringBuilder = new StringBuilder();
        BIT BIT1 = new BIT(n + 10);
        BIT BIT2 = new BIT(n + 10);
        long[] left = new long[n];
        long[] right = new long[n];
        left[0] = A[0];
        BIT1.update((int)A[0], A[0]);
        BIT2.update((int)A[0], 1);
        for (int i=1;i<n;i++)
        {
            long sum = BIT1.sum((int)A[i] + 1);
            long count = BIT2.sum((int)A[i] + 1);
            left[i] = left[i - 1] + A[i] - sum + count * A[i];
            BIT1.update((int)A[i], A[i]);
            BIT2.update((int)A[i], 1);
        }
        right[n - 1] = A[n - 1];
        BIT BIT3 = new BIT(n + 10);
        BIT BIT4 = new BIT(n + 10);
        BIT3.update((int)A[n - 1], A[n - 1]);
        BIT4.update((int)A[n - 1], 1);
        for (int i=n - 2;i>=0;i--)
        {
            long sum = BIT3.sum((int)A[i] + 1);
            long count = BIT4.sum((int)A[i] + 1);
            right[i] = right[i + 1] + A[i] - sum + count * A[i];
            BIT3.update((int)A[i], A[i]);
            BIT4.update((int)A[i], 1);
        }

        long max = 0L;
        int idx = -1;
        for (int i=0;i<n ;i++)
        {
            long res = left[i] + right[i] - A[i];
            if (res > max)
            {
                max = res;
                idx = i;
            }
        }
        long[] ans = new long[n];
        ans[idx] = A[idx];
        long mm = A[idx];
        for (int i=idx -1;i>=0;i--)
        {
            ans[i] = Math.min(A[i], mm);
            mm = Math.min(A[i], mm);
        }
        mm = A[idx];
        for (int i=idx +1;i< n;i++)
        {
            ans[i] = Math.min(A[i], mm);
            mm = Math.min(A[i], mm);
        }
        for (int i=0;i<n;i++)
            stringBuilder.append(ans[i]).append(" ");
        System.out.println(stringBuilder);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}