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

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

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

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

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

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(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;
        }

        public String next() {
            return readString();
        }

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

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

    long modPow(long a, long p, long m) {
        if (a == 1) return 1;
        long ans = 1;
        while (p > 0) {
            if (p % 2 == 1) ans = (ans * a) % m;
            a = (a * a) % m;
            p >>= 1;
        }
        return ans;
    }

    long modInv(long a, long m) {
        return modPow(a, m - 2, m);
    }

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

    class DSU {
        int par[], size[], connectedComponent;

        DSU(int n) {
            par = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i++) {
                size[i] = 1;
                par[i] = i;
            }
            connectedComponent = n;
        }

        int root(int n) {
            if (n == par[n]) return n;
            return par[n] = root(par[n]);
        }

        boolean union(int u, int v) {
            return f_union(root(u), root(v));
        }

        boolean f_union(int u, int v) {
            if (u == v) return false;
            if (size[u] > size[v]) {
                size[u] += size[v];
                par[v] = u;
            } else {
                size[v] += size[u];
                par[u] = v;
            }
            connectedComponent--;
            return true;
        }

        DSU clone_DSU() {
            DSU t = new DSU(par.length);
            t.connectedComponent = connectedComponent;
            for (int i = 0; i < par.length; i++) {
                t.par[i] = par[i];
                t.size[i] = size[i];
            }
            return t;
        }
    }

    //taken from KharYusuf
    static class LCA {
        public int[][] par;
        public int[] lvl;
        public final int LOG;
        public final int n;
        public ArrayList<Integer>[] a;

        public LCA(int n, ArrayList<Integer>[] aa) {
            a = aa;
            this.n = n;
            LOG = log2(n) + 1;
            par = new int[n][LOG];
            lvl = new int[n];
        }

        public void build(int u) {
            dfs(u, -1, 0);
            for (int j = 1; j < LOG; j++)
                for (int i = 0; i < n; i++) {
                    par[i][j] = -1;
                    if (par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1];
                }
        }

        public void dfs(int cur, int p, int l) {
            par[cur][0] = p;
            lvl[cur] = l;
            for (int i : a[cur]) if (i != p) dfs(i, cur, l + 1);
        }

        public int query(int u, int v) {
            if (lvl[u] < lvl[v]) {
                int t = u;
                u = v;
                v = t;
            }
            int dis = lvl[u] - lvl[v];
            while (dis > 0) {
                int raise = log2(dis);
                u = par[u][raise];
                dis ^= 1 << raise;
            }
            if (u == v) return u;
            for (int i = LOG - 1; i >= 0; i--) {
                if (par[u][i] != -1 && par[u][i] != par[v][i]) {
                    u = par[u][i];
                    v = par[v][i];
                }
            }
            return par[u][0];
        }

        public int dist(int u, int v) {
            return lvl[u] + lvl[v] - (lvl[query(u, v)] << 1);
        }

        public int log2(int i) {
            int cnt = -1;
            while (i > 0) {
                i >>= 1;
                cnt++;
            }
            return cnt;
        }

    }

    PrintWriter out;
    InputReader sc;

    public void run() {
        sc = new InputReader(System.in);
        // Scanner sc=new Scanner(System.in);
        //  Random sc=new Random();
        out = new PrintWriter(System.out);

        int n=sc.nextInt();
        int a[]=new int[n];

        for (int i = 0; i <n ; i++) {
            a[i]=sc.nextInt();
        }

        long pre[]=new long[n];
        long suf[]=new long[n];
        Stack<Node> prefixStack=new Stack<>();
        Stack<Node> suffixStack=new Stack<>();

        f(prefixStack,pre,a,true);
        f(suffixStack,suf,a,false);

//        out.println(Arrays.toString(pre));
//        out.println(Arrays.toString(suf));

        long ans=0;int ind=-1;
        for (int i = 0; i <n ; i++) {
            if(ans<pre[i]+suf[i]-a[i]){
                ans=pre[i]+suf[i]-a[i];
                ind=i;
            }
        }

        //out.println(ind);

        int arr[]=new int[n];
        int min=a[ind];
        for (int i = ind; i <n ; i++) {
            min=Math.min(a[i],min);
            arr[i]=min;

        }

        min=a[ind];

        for (int i = ind; i >=0 ; i--) {
            min=Math.min(a[i],min);
            arr[i]=min;

        }

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

    void f(Stack<Node> st,long dp[],int a[],boolean isPrefix){
        int n=dp.length;
        if(isPrefix) {
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    dp[i] = a[i];
                    st.push(new Node(a[i], a[i],i));
                } else {

                    while (!st.isEmpty() && st.peek().num > a[i]) {
                        Node u=st.pop();
                    }
                    dp[i]=(!st.isEmpty()?1l*(i-(st.peek().ind+1)+1)*a[i]+st.peek().sum:1l*(i+1)*a[i]);
                    st.push(new Node(a[i],dp[i],i));
                }
            }
        }
        else{
            for (int i = n-1; i >=0; i--) {
                if (i == n-1) {
                    dp[i] = a[i];
                    st.push(new Node(a[i], a[i],i));
                } else {
                    while (!st.isEmpty() && st.peek().num > a[i]) {
                        Node u=st.pop();
                    }

                    dp[i]=(!st.isEmpty()?1l*(-i+(st.peek().ind-1)+1)*a[i]+st.peek().sum:1l*(n-1-(i)+1)*a[i]);
                   // out.println(i+" "+dp[i]+" "+st.size());
                    st.push(new Node(a[i],dp[i],i));
                }
            }
        }
    }

    class Node{
        int num;
        long sum;
        int ind;

        public Node(int num, long sum,int fre) {
            this.num = num;
            this.sum = sum;
            this.ind=fre;
        }
    }

}


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

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

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

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

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

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(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;
        }

        public String next() {
            return readString();
        }

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

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

    long modPow(long a, long p, long m) {
        if (a == 1) return 1;
        long ans = 1;
        while (p > 0) {
            if (p % 2 == 1) ans = (ans * a) % m;
            a = (a * a) % m;
            p >>= 1;
        }
        return ans;
    }

    long modInv(long a, long m) {
        return modPow(a, m - 2, m);
    }

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

    class DSU {
        int par[], size[], connectedComponent;

        DSU(int n) {
            par = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i++) {
                size[i] = 1;
                par[i] = i;
            }
            connectedComponent = n;
        }

        int root(int n) {
            if (n == par[n]) return n;
            return par[n] = root(par[n]);
        }

        boolean union(int u, int v) {
            return f_union(root(u), root(v));
        }

        boolean f_union(int u, int v) {
            if (u == v) return false;
            if (size[u] > size[v]) {
                size[u] += size[v];
                par[v] = u;
            } else {
                size[v] += size[u];
                par[u] = v;
            }
            connectedComponent--;
            return true;
        }

        DSU clone_DSU() {
            DSU t = new DSU(par.length);
            t.connectedComponent = connectedComponent;
            for (int i = 0; i < par.length; i++) {
                t.par[i] = par[i];
                t.size[i] = size[i];
            }
            return t;
        }
    }

    //taken from KharYusuf
    static class LCA {
        public int[][] par;
        public int[] lvl;
        public final int LOG;
        public final int n;
        public ArrayList<Integer>[] a;

        public LCA(int n, ArrayList<Integer>[] aa) {
            a = aa;
            this.n = n;
            LOG = log2(n) + 1;
            par = new int[n][LOG];
            lvl = new int[n];
        }

        public void build(int u) {
            dfs(u, -1, 0);
            for (int j = 1; j < LOG; j++)
                for (int i = 0; i < n; i++) {
                    par[i][j] = -1;
                    if (par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1];
                }
        }

        public void dfs(int cur, int p, int l) {
            par[cur][0] = p;
            lvl[cur] = l;
            for (int i : a[cur]) if (i != p) dfs(i, cur, l + 1);
        }

        public int query(int u, int v) {
            if (lvl[u] < lvl[v]) {
                int t = u;
                u = v;
                v = t;
            }
            int dis = lvl[u] - lvl[v];
            while (dis > 0) {
                int raise = log2(dis);
                u = par[u][raise];
                dis ^= 1 << raise;
            }
            if (u == v) return u;
            for (int i = LOG - 1; i >= 0; i--) {
                if (par[u][i] != -1 && par[u][i] != par[v][i]) {
                    u = par[u][i];
                    v = par[v][i];
                }
            }
            return par[u][0];
        }

        public int dist(int u, int v) {
            return lvl[u] + lvl[v] - (lvl[query(u, v)] << 1);
        }

        public int log2(int i) {
            int cnt = -1;
            while (i > 0) {
                i >>= 1;
                cnt++;
            }
            return cnt;
        }

    }

    PrintWriter out;
    InputReader sc;

    public void run() {
        sc = new InputReader(System.in);
        // Scanner sc=new Scanner(System.in);
        //  Random sc=new Random();
        out = new PrintWriter(System.out);

        int n=sc.nextInt();
        int a[]=new int[n];

        for (int i = 0; i <n ; i++) {
            a[i]=sc.nextInt();
        }

        long pre[]=new long[n];
        long suf[]=new long[n];
        Stack<Node> prefixStack=new Stack<>();
        Stack<Node> suffixStack=new Stack<>();

        f(prefixStack,pre,a,true);
        f(prefixStack,suf,a,false);

//

        long ans=0;int ind=-1;
        for (int i = 0; i <n ; i++) {
            if(ans<pre[i]+suf[i]-a[i]){
                ans=pre[i]+suf[i]-a[i];
                ind=i;
            }
        }

        int arr[]=new int[n];
        int min=a[ind];
        for (int i = ind; i <n ; i++) {
            min=Math.min(a[i],min);
            arr[i]=min;

        }

        min=a[ind];

        for (int i = ind; i >=0 ; i--) {
            min=Math.min(a[i],min);
            arr[i]=min;

        }

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

    void f(Stack<Node> st,long dp[],int a[],boolean isPrefix){
        int n=dp.length;
        if(isPrefix) {
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    dp[i] = a[i];
                    st.push(new Node(a[i], a[i],i));
                } else {

                    while (!st.isEmpty() && st.peek().num > a[i]) {
                        Node u=st.pop();
                    }
                    dp[i]=(!st.isEmpty()?(i-(st.peek().ind+1)+1)*a[i]+st.peek().sum:(i+1)*a[i]);
                    st.push(new Node(a[i],dp[i],i));
                }
            }
        }
        else{
            for (int i = n-1; i >=0; i--) {
                if (i == n-1) {
                    dp[i] = a[i];
                    st.push(new Node(a[i], a[i],i));
                } else {
                    while (!st.isEmpty() && st.peek().num > a[i]) {
                        Node u=st.pop();
                    }
                    dp[i]=(!st.isEmpty()?(-i+(st.peek().ind-1)+1)*a[i]+st.peek().sum:(n-1-(i)+1)*a[i]);
                    st.push(new Node(a[i],dp[i],i));
                }
            }
        }
    }

    class Node{
        int num;
        long sum;
        int ind;

        public Node(int num, long sum,int fre) {
            this.num = num;
            this.sum = sum;
            this.ind=fre;
        }
    }

}


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

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

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

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

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

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(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;
        }

        public String next() {
            return readString();
        }

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

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

    long modPow(long a, long p, long m) {
        if (a == 1) return 1;
        long ans = 1;
        while (p > 0) {
            if (p % 2 == 1) ans = (ans * a) % m;
            a = (a * a) % m;
            p >>= 1;
        }
        return ans;
    }

    long modInv(long a, long m) {
        return modPow(a, m - 2, m);
    }

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

    class DSU {
        int par[], size[], connectedComponent;

        DSU(int n) {
            par = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i++) {
                size[i] = 1;
                par[i] = i;
            }
            connectedComponent = n;
        }

        int root(int n) {
            if (n == par[n]) return n;
            return par[n] = root(par[n]);
        }

        boolean union(int u, int v) {
            return f_union(root(u), root(v));
        }

        boolean f_union(int u, int v) {
            if (u == v) return false;
            if (size[u] > size[v]) {
                size[u] += size[v];
                par[v] = u;
            } else {
                size[v] += size[u];
                par[u] = v;
            }
            connectedComponent--;
            return true;
        }

        DSU clone_DSU() {
            DSU t = new DSU(par.length);
            t.connectedComponent = connectedComponent;
            for (int i = 0; i < par.length; i++) {
                t.par[i] = par[i];
                t.size[i] = size[i];
            }
            return t;
        }
    }

    //taken from KharYusuf
    static class LCA {
        public int[][] par;
        public int[] lvl;
        public final int LOG;
        public final int n;
        public ArrayList<Integer>[] a;

        public LCA(int n, ArrayList<Integer>[] aa) {
            a = aa;
            this.n = n;
            LOG = log2(n) + 1;
            par = new int[n][LOG];
            lvl = new int[n];
        }

        public void build(int u) {
            dfs(u, -1, 0);
            for (int j = 1; j < LOG; j++)
                for (int i = 0; i < n; i++) {
                    par[i][j] = -1;
                    if (par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1];
                }
        }

        public void dfs(int cur, int p, int l) {
            par[cur][0] = p;
            lvl[cur] = l;
            for (int i : a[cur]) if (i != p) dfs(i, cur, l + 1);
        }

        public int query(int u, int v) {
            if (lvl[u] < lvl[v]) {
                int t = u;
                u = v;
                v = t;
            }
            int dis = lvl[u] - lvl[v];
            while (dis > 0) {
                int raise = log2(dis);
                u = par[u][raise];
                dis ^= 1 << raise;
            }
            if (u == v) return u;
            for (int i = LOG - 1; i >= 0; i--) {
                if (par[u][i] != -1 && par[u][i] != par[v][i]) {
                    u = par[u][i];
                    v = par[v][i];
                }
            }
            return par[u][0];
        }

        public int dist(int u, int v) {
            return lvl[u] + lvl[v] - (lvl[query(u, v)] << 1);
        }

        public int log2(int i) {
            int cnt = -1;
            while (i > 0) {
                i >>= 1;
                cnt++;
            }
            return cnt;
        }

    }

    PrintWriter out;
    InputReader sc;

    public void run() {
        sc = new InputReader(System.in);
        // Scanner sc=new Scanner(System.in);
        //  Random sc=new Random();
        out = new PrintWriter(System.out);

        int n=sc.nextInt();
        int a[]=new int[n];

        for (int i = 0; i <n ; i++) {
            a[i]=sc.nextInt();
        }

        long pre[]=new long[n];
        long suf[]=new long[n];
        Stack<Node> prefixStack=new Stack<>();
        Stack<Node> suffixStack=new Stack<>();

        f(prefixStack,pre,a,true);
        f(prefixStack,suf,a,false);

//

        long ans=0;int ind=-1;
        for (int i = 0; i <n ; i++) {
            if(ans<pre[i]+suf[i]-a[i]){
                ans=pre[i]+suf[i]-a[i];
                ind=i;
            }
        }

        int arr[]=new int[n];
        int min=a[ind];
        for (int i = ind; i <n ; i++) {
            min=Math.min(a[i],min);
            arr[i]=min;

        }

        min=a[ind];

        for (int i = ind; i >=0 ; i--) {
            min=Math.min(a[i],min);
            arr[i]=min;

        }

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

    void f(Stack<Node> st,long dp[],int a[],boolean isPrefix){
        int n=dp.length;
        if(isPrefix) {
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    dp[i] = a[i];
                    st.push(new Node(a[i], a[i],i));
                } else {

                    while (!st.isEmpty() && st.peek().num > a[i]) {
                        Node u=st.pop();
                    }
                    dp[i]=(!st.isEmpty()?1l*(i-(st.peek().ind+1)+1)*a[i]+st.peek().sum:1l*(i+1)*a[i]);
                    st.push(new Node(a[i],dp[i],i));
                }
            }
        }
        else{
            for (int i = n-1; i >=0; i--) {
                if (i == n-1) {
                    dp[i] = a[i];
                    st.push(new Node(a[i], a[i],i));
                } else {
                    while (!st.isEmpty() && st.peek().num > a[i]) {
                        Node u=st.pop();
                    }
                    dp[i]=(!st.isEmpty()?1l*(-i+(st.peek().ind-1)+1)*a[i]+st.peek().sum:1l*(n-1-(i)+1)*a[i]);
                    st.push(new Node(a[i],dp[i],i));
                }
            }
        }
    }

    class Node{
        int num;
        long sum;
        int ind;

        public Node(int num, long sum,int fre) {
            this.num = num;
            this.sum = sum;
            this.ind=fre;
        }
    }

}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            for(int i=1;i<n;i++)
            {
                if(arr[i]>=arr[i-1])
                prr[i]=arr[i]+prr[i-1];
                else
                {
                    int x=bs(0,i-1,seg,n,arr[i]);
                    i--;
                      i++;
                    if(x==-1)
                    {
                        long z=i+1L;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            for(int i=n-2;i>=0;i--)
            {
                if(arr[i]>=arr[i+1])
                srr[i]=arr[i]+srr[i+1];
                else
                {
                    int x=bs(i+1,n-1,seg,n,arr[i]);
                    i++;n--;

                    i--;n++;
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Long.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            Stack<Integer> stk=new Stack();
            long[] prr=new long[n];
            prr[0]=arr[0];
            stk.add(0);
            for(int i=1;i<n;i++)
            {
               while(!stk.isEmpty()&&arr[stk.peek()]>=arr[i])
               stk.pop();
               {
                    int x=-1;
                    if(stk.size()!=0)
                    x=stk.peek();
                    stk.push(i);
                    if(x==-1)
                    {
                        long z=i+1L;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            stk.clear();
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            stk.push(n-1);
            for(int i=n-2;i>=0;i--)
            {
                while(!stk.isEmpty()&&arr[stk.peek()]>=arr[i])
               stk.pop();
                {
                     int x=-1;
                    if(stk.size()!=0)
                    x=stk.peek();
                    stk.push(i);
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            for(int i=1;i<n;i++)
            {
                if(arr[i]>=arr[i-1])
                prr[i]=arr[i]+prr[i-1];
                else
                {
                    int x=bs(0,i-1,seg,n,arr[i]);
                    if(x==-1)
                    {
                        long z=i;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            long[] srr=new long[n];
            prr[n-1]=srr[n-1];
            for(int i=n-2;i>=0;i--)
            {
                if(arr[i]>=arr[i+1])
                srr[i]=arr[i]+srr[i+1];
                else
                {
                    int x=bs(i+1,n-1,seg,n,arr[i]);
                    if(x==-1)
                    {
                        long z=n-i;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        prr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=srr[0];
                int p=0;
                if(prr[n-1]>tmp)
                {
                    tmp=prr[n-1];
                    p=n-1;
                }
                for(int i=1;i<n-1;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Integer.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<=x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            Stack<Integer> stk=new Stack();
            long[] prr=new long[n];
            prr[0]=arr[0];
            stk.add(0);
            for(int i=1;i<n;i++)
            {
               while(!stk.isEmpty()&&arr[stk.peek()]>=arr[i])
               stk.pop();
               {
                    int x=-1;
                    if(stk.size()!=0)
                    x=stk.peek();
                    stk.push(i);
                    if(x==-1)
                    {
                        long z=i+1L;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            stk.clear();
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            stk.push(n-1);
            for(int i=n-2;i>=0;i--)
            {
                while(!stk.isEmpty()&&arr[stk.peek()]>=arr[i])
               stk.pop();
                {
                     int x=-1;
                    if(stk.size()!=0)
                    x=stk.peek();
                    stk.push(i);
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            for(int i=1;i<n;i++)
            {
                if(arr[i]>=arr[i-1])
                prr[i]=arr[i]+prr[i-1];
                else
                {
                    int x=bs(0,i-1,seg,n,arr[i]);
                    i--;
                      i++;
                    if(x==-1)
                    {
                        long z=i+1L;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            for(int i=n-2;i>=0;i--)
            {
                if(arr[i]>=arr[i+1])
                srr[i]=arr[i]+srr[i+1];
                else
                {
                    int x=bs1(i+1,n-1,seg,n,arr[i]);
                    i++;n--;

                    i--;n++;
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Long.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
      public static int bs1(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<x)
            {
                ans=m;
                r=m-1;
            }
            else
            l=m+1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            for(int i=1;i<n;i++)
            {
                if(arr[i]>=arr[i-1])
                prr[i]=arr[i]+prr[i-1];
                else
                {
                    int x=bs(0,i-1,seg,n,arr[i]);
                    if(x==-1)
                    {
                        long z=i;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            for(int i=n-2;i>=0;i--)
            {
                if(arr[i]>=arr[i+1])
                srr[i]=arr[i]+srr[i+1];
                else
                {
                    int x=bs(i+1,n-1,seg,n,arr[i]);
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=srr[0];
                int p=0;
                if(prr[n-1]>tmp)
                {
                    tmp=prr[n-1];
                    p=n-1;
                }
                for(int i=1;i<n-1;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Integer.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<=x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            for(int i=1;i<n;i++)
            {
                if(arr[i]>=arr[i-1])
                prr[i]=arr[i]+prr[i-1];
                else
                {
                    int x=bs(0,i-1,seg,n,arr[i]);
                    i--;
                      i++;
                    if(x==-1)
                    {
                        long z=i+1;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            for(int i=n-2;i>=0;i--)
            {
                if(arr[i]>=arr[i+1])
                srr[i]=arr[i]+srr[i+1];
                else
                {
                    int x=bs(i+1,n-1,seg,n,arr[i]);
                    i++;n--;

                    i--;n++;
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Long.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<=x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            Stack<Integer> stk=new Stack();
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            stk.add(0);
            for(int i=1;i<n;i++)
            {
               while(!stk.isEmpty()&&arr[stk.peek()]>=arr[i])
               stk.pop();
               {
                    int x=-1;
                    if(stk.size()!=0)
                    x=stk.peek();
                    stk.push(i);
                    if(x==-1)
                    {
                        long z=i+1L;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            stk.clear();
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            stk.push(n-1);
            for(int i=n-2;i>=0;i--)
            {
                while(!stk.isEmpty()&&arr[stk.peek()]>=arr[i])
               stk.pop();
                {
                     int x=-1;
                    if(stk.size()!=0)
                    x=stk.peek();
                    stk.push(i);
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Long.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
      public static int bs1(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,l,m,seg);
            if(p<x)
            {
                ans=m;
                r=m-1;
            }
            else
            l=m+1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread  {
    boolean[] prime;
    FastScanner sc;
    PrintWriter pw;
    long startTime = System.currentTimeMillis();
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public long nlo() {
            return Long.parseLong(next());
        }

        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(next());
        }

        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }

        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public Main(ThreadGroup t,Runnable r,String s,long d )
    {
        super(t,r,s,d);
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public static void main(String[] args)
    {
        new Main(null,null,"",1<<26).start();
    }

    /////////////------------------------------------//////////////
    ////////////------------------Main-Logic--------//////////////
    ///////////-------------------------------------//////////////

 public static int[][] srr,prr;
    public void solve() {
        int t = 1;
        while(t-->0)
        {
            int n=sc.ni();
            long[] arr=new long[n];
            for(int i=0;i<n;i++)
            arr[i]=sc.ni();
            long[] seg=new long[4*n+1];
            build(arr,0,n-1,1,seg);
            long[] prr=new long[n];
            prr[0]=arr[0];
            for(int i=1;i<n;i++)
            {
                if(arr[i]>=arr[i-1])
                prr[i]=arr[i]+prr[i-1];
                else
                {
                    int x=bs(0,i-1,seg,n,arr[i]);
                    i--;
                      i++;
                    if(x==-1)
                    {
                        long z=i+1L;
                        prr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=i-x;
                        prr[i]=arr[i]*z+prr[x];
                    }
                }
            }
            long[] srr=new long[n];
            srr[n-1]=arr[n-1];
            for(int i=n-2;i>=0;i--)
            {
                if(arr[i]>=arr[i+1])
                srr[i]=arr[i]+srr[i+1];
                else
                {
                    int x=bs1(i+1,n-1,seg,n,arr[i]);
                    i++;n--;

                    i--;n++;
                    if(x==-1)
                    {
                        long z=n-i;
                        srr[i]=z*arr[i];
                    }
                    else
                    {
                        long z=x-i;
                        srr[i]=arr[i]*z+srr[x];
                    }
                }
            }
            if(n==1)
            {
                pw.println(arr[0]);
            }
            else
            {
                long tmp=-1;
                int p=0;
                for(int i=0;i<n;i++)
                {
                    long z=prr[i]+srr[i]-arr[i];
                    if(z>tmp)
                    {
                        tmp=z;
                        p=i;
                    }
                }
                for(int i=p-1;i>=0;i--)
                arr[i]=Math.min(arr[i],arr[i+1]);
                for(int i=p+1;i<n;i++)
                arr[i]=Math.min(arr[i],arr[i-1]);
                for(int i=0;i<n;i++)
                pw.print(arr[i]+" ");
            }
        }
    }
      public static void build(long[] arr,int start,int end,int ind,long[] seg)
    {
        if(start==end)
        {
            seg[ind]=arr[start];
            return;
        }
        build(arr,start,(start+end)/2,2*ind,seg);
        build(arr,(start+end)/2+1,end,2*ind+1,seg);
        seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        return;
    }
    public static long query(int s,int e,int ind,int qs,int qe,long[] seg)
    {
        // no overlapp
        if(qe<s||qs>e)
        return Long.MAX_VALUE;

        // complete overlapp
        if(qs<=s&&qe>=e)
        return seg[ind];

        // partial overlapp

        int mid=(s+e)/2;
        long x=query(s,mid,2*ind,qs,qe,seg);
        long y=query(mid+1,e,2*ind+1,qs,qe,seg);
        return Math.min(x,y);
    }
    public static int bs(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,m,r,seg);
            if(p<x)
            {
                ans=m;
                l=m+1;
            }
            else
            r=m-1;
        }
        return ans;
    }
      public static int bs1(int l,int r,long[] seg,int n,long x)
    {
        int ans=-1;
        while(l<=r)
        {
            int m=(l+r)/2;
            long p=query(0,n-1,1,l,m,seg);
            if(p<x)
            {
                ans=m;
                r=m-1;
            }
            else
            l=m+1;
        }
        return ans;
    }
}


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.text.*;
import java.math.*;
import java.util.*;

public class Main implements Runnable {

	class Pair{
		public long value;
		public int count;
	}

	Stack<Pair> stack= new Stack<Pair>();

	public void solve() throws Exception {

		int n=iread();
		int []m= new int[n+1];
		for(int i=1;i<=n;i++)
			m[i]= iread();

		long []curve1= new long[n+2];
		long []curve2= new long[n+2];

		stack.clear();
		curve1[0]=0;
		for(int i=1;i<=n;i++){
			curve1[i]= processStack(curve1[i-1], m[i]);
		}

		stack.clear();
		curve2[n+1]=0;
		for(int i=n;i>=1;i--){
			curve2[i]= processStack(curve2[i+1], m[i]);
		}

		int peak=-1;
		long max=-1;
		for(int i=1;i<=n;i++){
			long x= curve1[i]+curve2[i]-m[i];
			if(x>max){
				max=x;
				peak=i;
			}
		}

		long result[]= new long[n+2];
		result[peak]= m[peak];

		for(int i=peak-1;i>=1;i--){
			result[i]= Math.min(result[i+1], m[i]);
		}
		for(int i=peak+1;i<=n;i++){
			result[i]= Math.min(result[i-1], m[i]);
		}

		for(int i=1;i<=n;i++){
			System.out.print(result[i]+" ");
		}
	}

	private long processStack(long curve1, long value) {
		int count=0;
		while(!stack.isEmpty() && stack.peek().value>=value){
			Pair p= stack.pop();
			count+=p.count;
			curve1 -= (p.value * p.count);
			curve1+=(value * p.count);
		}
		long sum=curve1+value;
		count++;
		Pair newP= new Pair();
		newP.count= count;
		newP.value= value;
		stack.push(newP);

		return sum;
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new BufferedWriter(new OutputStreamWriter(System.out));
			// in = new BufferedReader(new FileReader(filename+".in"));
			// out = new BufferedWriter(new FileWriter(filename+".out"));
			solve();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public int iread() throws Exception {
		return Integer.parseInt(readword());
	}

	public double dread() throws Exception {
		return Double.parseDouble(readword());
	}

	public long lread() throws Exception {
		return Long.parseLong(readword());
	}

	BufferedReader in;

	BufferedWriter out;

	public String readword() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) {

		try {
			Locale.setDefault(Locale.US);
		} catch (Exception e) {

		}
		// new Thread(new Main()).start();
		new Thread(null, new Main(), "1", 1 << 25).start();
	}
}


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

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

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

	SegmentTreeRMQPos sg;

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

		int pos = sg.minx(l, r + 1)[1];

		long rv = arr[pos];

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

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

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

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

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

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

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

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

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

		public int minpos;
		public int minval;

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

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

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

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

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

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

	class FastReader {
		InputStream is;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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