import java.util.*;
import java.io.*;
public class Main{
    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);
        }

    }
    static class OutputWriter {
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream) {
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer) {
			this.writer = new PrintWriter(writer);
		}

		public void print(Object...objects) {
			for (int i = 0; i < objects.length; i++) {
				if (i != 0)
					writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void printLine(Object...objects) {
			print(objects);
			writer.println();
		}

		public void close() {
			writer.close();
		}

		public void flush() {
			writer.flush();
		}

		}

    static class IOUtils {

		public static int[] readIntArray(InputReader in, int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
                array[i] = in.nextInt();
            return array;
        }

    }
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
    static int mod = (int)(1e9+7);
    public static long pow(long a,long b)
    {
        long ans = 1;
        while(b> 0)
        {
            if((b & 1)==1){
                ans = (ans*a) % mod;
            }
            a = (a*a) % mod;
            b = b>>1;
        }
        return ans;
    }

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        OutputWriter out = new OutputWriter(System.out);
        //IOUtils io = new IOUtils();
        int n = in.nextInt();
        long[] arr = new long[n+2];
        for(int i=1;i<=n;i++)
        {
            arr[i] = in.nextLong();
        }
        Deque<Integer> stack = new LinkedList<>();
        long[] left = new long[n+2];
        stack.push(0);
        for(int i=1;i<=n;i++)
        {
            while(!stack.isEmpty() && arr[stack.peek()]>arr[i])
                stack.pop();
            left[i] = left[stack.peek()] + (i-stack.peek())*arr[i];
            stack.push(i);
        }
        stack.clear();
        stack.push(n+1);
        long[] right = new long[n+2];
        for(int i=n;i>=1;i--)
        {
            while(!stack.isEmpty() && arr[stack.peek()]>arr[i])
                stack.pop();
            right[i] = right[stack.peek()] + (stack.peek()-i)*arr[i];
            stack.push(i);
        }
        int maxIdx = -1;
        long max = 0;
        for(int i=1;i<=n;i++)
        {
            if((left[i]+right[i]-arr[i])>=max)
            {
                max = left[i]+right[i]-arr[i];
                maxIdx = i;
            }
        }
        long[] ans = new long[n+2];
        max = Long.MAX_VALUE;
        for(int i=maxIdx;i<=n;i++)
        {
            max = Math.min(max,arr[i]);
            ans[i] = max;
        }
        max = Long.MAX_VALUE;
        for(int i=maxIdx;i>=1;i--)
        {
            max = Math.min(max,arr[i]);
            ans[i] = max;
        }
        for(int i=1;i<=n;i++)
        {
            out.print(ans[i] + " ");
        }
        out.flush();
        out.close();
    }
}


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

public class CTask {
    private static final String QUICK_ANSWER = "NO";
    private final Input in;
    private final StringBuilder out;

    public CTask(Input in, StringBuilder out) {
        this.in = in;
        this.out = out;
    }

    public void solve() throws QuickAnswer {
        int n = nextInt();
        int[] m = nextInts(n);

        long[] forward = new long[n];
        int[] prev = new int[n];

        forward[0] = m[0];
        prev[0] = -1;
        for (int i = 1; i < n; i++) {
            int p = i - 1;
            while(p >= 0 && m[p] >= m[i]) p = prev[p];
            prev[i] = p;
            if(p == -1) forward[i] = (long) m[i] * (i + 1);
            else forward[i] = forward[p] + (long)m[i] * (i - p);
        }

        long[] backward = new long[n];
        int[] next = new int[n];

        backward[n-1] = m[n-1];
        next[n-1] = n;
        for (int i = n-2; i >= 0; i--) {
            int p = i + 1;
            while(p < n && m[p] >= m[i]) p = next[p];
            next[i] = p;
            if(p == n) backward[i] = (long) m[i] * (n - i);
            else backward[i] = backward[p] + (long)m[i] * (p - i);
        }

        long max = 0;
        int maxIndex = 0;
        for (int i = 0; i < n; i++) {
            long v = forward[i] + backward[i] - m[i];
            if(v <= max) continue;
            max = v;
            maxIndex = i;
        }
        int[] res = new int[n];
        int i = maxIndex;
        do {
            int p = prev[i];
            for(int j = i; j > p; --j) {
                res[j] = m[i];
            }
            i = p;
        } while (i >= 0);
        i = maxIndex;
        do {
            int p = next[i];
            for (int j = i; j < p; j++) {
                res[j] = m[i];
            }
            i = p;
        } while (i < n);

        String prefix = "";
        for(int val : res) {
            out.append(prefix).append(val);
            prefix = " ";
        }
    }

    // Common functions

    private static class Input {
        private final BufferedReader in;
        private StringTokenizer tokenizer = null;

        public Input(BufferedReader in) {
            this.in = in;
        }

        String nextToken() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return tokenizer.nextToken();
        }

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

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

        String nextLine() {
            try {
                return in.readLine();
            } catch (IOException e) {
                return "";
            }
        }
    }

    void quickAnswer(String answer) throws QuickAnswer {
        throw new QuickAnswer(answer);
    }

    void quickAnswer() throws QuickAnswer {
        quickAnswer(QUICK_ANSWER);
    }

    static class QuickAnswer extends Exception {
        private String answer;

        public QuickAnswer(String answer) {
            this.answer = answer;
        }
    }

    void print(Object... args) {
        String prefix = "";
        for (Object arg : args) {
            out.append(prefix);
            out.append(arg);
            prefix = " ";
        }
    }

    void println(Object... args) {
        print(args);
        out.append("\n");
    }

    void printsp(Object... args) {
        print(args);
        out.append(" ");
    }

    int nextInt() {
        return in.nextInt();
    }

    long nextLong() {
        return in.nextLong();
    }

    String nextString() {
        return in.nextLine();
    }

    int[] nextInts(int count) {
        int[] res = new int[count];
        for (int i = 0; i < count; ++i) {
            res[i] = nextInt();
        }
        return res;
    }

    int[][] nextInts(int count, int n) {
        int[][] res = new int[n][count];
        for (int i = 0; i < count; ++i) {
            for (int j = 0; j < n; j++) {
                res[j][i] = nextInt();
            }
        }
        return res;
    }

    long[] nextLongs(int count) {
        long[] res = new long[count];
        for (int i = 0; i < count; ++i) {
            res[i] = nextLong();
        }
        return res;
    }

    long[][] nextLongs(int count, int n) {
        long[][] res = new long[n][count];
        for (int i = 0; i < count; ++i) {
            for (int j = 0; j < n; j++) {
                res[j][i] = nextLong();
            }
        }
        return res;
    }

    public static void main(String[] args) {
        doMain(System.in, System.out);
    }

    static void doMain(InputStream inStream, PrintStream outStream) {
        Input in = new Input(new BufferedReader(new InputStreamReader(inStream)));
        StringBuilder totalOut = new StringBuilder();
        int count = 1;
        //count = in.nextInt();
        while (count-- > 0) {
            try {
                StringBuilder out = new StringBuilder();
                new CTask(in, out).solve();
                totalOut.append(out.toString());
            } catch (QuickAnswer e) {
                totalOut.append(e.answer);
            }
            if (count > 0) {
                totalOut.append("\n");
            }
        }
        outStream.print(totalOut.toString());
    }
}


//package algorithm.div622;

import java.util.Scanner;

/**
 * Created with: IntelliJ IDEA
 * Function:
 * User: zhangzhe041@ke.com
 * Date: 2020-03-07 17:22:40
 */
public class Skyscrapers {

    long[] a;
    long[] b;
    int n;
    int MAX = (int)(1e9) + 7;
    TreeNode[] tree;

    public Skyscrapers(){
        Scanner in = new Scanner(System.in);
        n = Integer.parseInt(in.nextLine());
        String[] strs = in.nextLine().split(" ");
        a = new long[n + 1];
        b = new long[n + 1];
        tree = new TreeNode[4 * n];
        for(int i = 1; i <= n; i++){
            //todo
            a[i] = Long.parseLong(strs[i - 1]);
        }
        build_tree(1, n, 1);
        solve(1, n);
        for(int i = 1; i <= n; i++){
            System.out.print(b[i] + " ");
        }
//        System.out.println();
    }

    public long solve(int l, int r){
        if(l > r) return 0;
        if(l == r){
            b[l] = a[l];
            return a[l];
        }
        Pair p = query(l, r, 1);
        long sum1 = solve(l, p.pos - 1);
        long sum2 = solve(p.pos + 1, r);
        if((p.pos - 1 - l + 1) * p.height+ p .height + sum2 < (r - p.pos - 1 + 1) * p.height + p.height + sum1){
            for(int i = p.pos; i <= r; i++){
                b[i] = p.height;
            }
            return (r - p.pos + 1) * p.height + sum1;
        }else{
            for(int i = l; i <= p.pos; i++){
                b[i] = p.height;
            }
            return (p.pos - l + 1) * p.height + sum2;
        }

    }

    public Pair query(int ql ,int qr, int id){
        int l = tree[id].l;
        int r = tree[id].r;
        int mid = (l + r) >> 1;
        if(ql <= l && r <= qr) return tree[id].pair;
        Pair tmp = new Pair(MAX, MAX);
        if(ql <= mid){
            Pair left = query(ql, qr, 2 * id);
            if(left.height < tmp.height) tmp = left;
        }
        if(qr > mid){
            Pair right = query(ql, qr, 2 * id + 1);
            if(right.height < tmp.height) tmp = right;
        }
        return tmp;
    }

    public void build_tree(int l, int r, int id){
        tree[id] = new TreeNode();
        tree[id].l = l;
        tree[id].r = r;
        tree[id].pair = new Pair();
        int mid = (l + r) >> 1;
        if(l == r){
            tree[id].pair.height = a[l];
            tree[id].pair.pos = l;
        }else{
            build_tree(l, mid, 2 * id);
            build_tree(mid + 1, r, 2 * id + 1);
            push_up(id);
        }
    }

    public void push_up(int id){
        if(tree[2 * id].pair.height == 0) tree[2 * id].pair = new Pair(MAX,MAX);
        if(tree[2 * id + 1].pair.height == 0) tree[2 * id + 1].pair = new Pair(MAX,MAX);
        if(tree[2 * id].pair.height < tree[2 * id + 1].pair.height){
            tree[id].pair = tree[2 * id].pair;
        }else tree[id].pair = tree[2 * id + 1].pair;
    }

    class Pair{
        long height;
        int pos;

        public Pair(long height, int pos){
            this.height = height;
            this.pos = pos;
        }
        Pair(){};
    }

    class TreeNode{
        int l;
        int r;
        Pair pair;
    }

    public static void main(String[] args) {
        new Skyscrapers();
    }

}


//package algorithm.div622;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Created with: IntelliJ IDEA
 * Function:
 * User: zhangzhe041@ke.com
 * Date: 2020-03-07 17:22:40
 */
public class Skyscrapers {

    long[] a;
    long[] b;
    int n;
    int MAX = (int)(1e9) + 7;
    TreeNode[] tree;
    PrintWriter out = new PrintWriter(System.out);

    public Skyscrapers() throws IOException {
//        Scanner in = new Scanner(System.in);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(in.readLine());
        String[] strs = in.readLine().split(" ");
        a = new long[n + 1];
        b = new long[n + 1];
        tree = new TreeNode[4 * n];
        for(int i = 1; i <= n; i++){
            a[i] = Long.parseLong(strs[i - 1]);
        }
        build_tree(1, n, 1);
        solve(1, n);
        for(int i = 1; i <= n; i++){
//            System.out.print(b[i] + " ");
            out.print(b[i] + " ");
        }
//        System.out.println();
        out.close();
    }

    public long solve(int l, int r){
        if(l > r) return 0;
        if(l == r){
            b[l] = a[l];
            return a[l];
        }
        Pair p = query(l, r, 1);
        long sum1 = solve(l, p.pos - 1);
        long sum2 = solve(p.pos + 1, r);
        if((p.pos - 1 - l + 1) * p.height+ p .height + sum2 < (r - p.pos - 1 + 1) * p.height + p.height + sum1){
            for(int i = p.pos; i <= r; i++){
                b[i] = p.height;
            }
            return (r - p.pos + 1) * p.height + sum1;
        }else{
            for(int i = l; i <= p.pos; i++){
                b[i] = p.height;
            }
            return (p.pos - l + 1) * p.height + sum2;
        }

    }

    public Pair query(int ql ,int qr, int id){
        int l = tree[id].l;
        int r = tree[id].r;
        int mid = (l + r) >> 1;
        if(ql <= l && r <= qr) return tree[id].pair;
        Pair tmp = new Pair(MAX, MAX);
        if(ql <= mid){
            Pair left = query(ql, qr, 2 * id);
            if(left.height < tmp.height) tmp = left;
        }
        if(qr > mid){
            Pair right = query(ql, qr, 2 * id + 1);
            if(right.height < tmp.height) tmp = right;
        }
        return tmp;
    }

    public void build_tree(int l, int r, int id){
        tree[id] = new TreeNode();
        tree[id].l = l;
        tree[id].r = r;
        tree[id].pair = new Pair();
        int mid = (l + r) >> 1;
        if(l == r){
            tree[id].pair.height = a[l];
            tree[id].pair.pos = l;
        }else{
            build_tree(l, mid, 2 * id);
            build_tree(mid + 1, r, 2 * id + 1);
            push_up(id);
        }
    }

    public void push_up(int id){
//        if(tree[2 * id].pair.height == 0) tree[2 * id].pair = new Pair(MAX,MAX);
//        if(tree[2 * id + 1].pair.height == 0) tree[2 * id + 1].pair = new Pair(MAX,MAX);
        if(tree[2 * id].pair.height < tree[2 * id + 1].pair.height){
            tree[id].pair = tree[2 * id].pair;
        }else tree[id].pair = tree[2 * id + 1].pair;
    }

    class Pair{
        long height;
        int pos;

        public Pair(long height, int pos){
            this.height = height;
            this.pos = pos;
        }
        Pair(){};
    }

    class TreeNode{
        int l;
        int r;
        Pair pair;
    }

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

}


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

public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        //Scanner sc = new Scanner();
        Reader in = new Reader();
        Main solver = new Main();
        solver.solve(out, in);
        out.flush();
        out.close();

    }

    static int INF = 2*(int)1e9;
    static int maxn = 5*(int)1e6+5;
    static int mod= 1000000321 ;
    static int n,m,k,t,q,d,p,s;

    void solve(PrintWriter out, Reader in) throws IOException{
        n = in.nextInt();

        int[] arr = new int[n+1];
        for (int i = 1; i <= n; i++) arr[i] = in.nextInt();

        int[][] pos = new int[n+2][2];
        long[] pre = new long[n+2];
        long[] suf = new long[n+2];

        TreeSet<Integer> set = new TreeSet<Integer>();
        ArrayList<Node> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) list.add(new Node(arr[i], i));

        set.add(0); set.add(n+1);
        Collections.sort(list);
        Node e;
        for (int i = 0; i < n; i++) {
            e = list.get(i);
            pos[e.id][0] = set.lower(e.id);
            pos[e.id][1] = set.higher(e.id);
            set.add(e.id);
        }

        suf[n] = arr[n];
        for (int i = n-1; i >= 1; i--) {
            suf[i] = (long)arr[i]*(long)(pos[i][1]-i)+suf[pos[i][1]];
        }
        suf[0] = suf[1];

        pre[1] = arr[1];
        for (int i = 2; i <= n; i++) {
            pre[i] = (long)arr[i]*(long)(i-pos[i][0])+pre[pos[i][0]];
        }
        pre[n+1] = pre[n];

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

        int[] res = new int[n+1];
        res[idx] = arr[idx];
        for (int i = idx-1; i >=1; i--) {
            res[i] = Math.min(arr[i], res[i+1]);
        }
        idx++;
        res[idx] = arr[idx];
        for (int i = idx+1; i <= n; i++) {
            res[i] = Math.min(arr[i], res[i-1]);
        }

        for (int i = 1; i <= n; i++) out.print(res[i]+" ");
        out.println();
    }

    static class Node implements Comparable<Node>{
        int id, val;

        Node (int val, int id) {
            this.id = id;
            this.val = val;
        }

        public int compareTo(Node o) {
            return this.val - o.val;
        }
    }

    static class Reader {

    private InputStream mIs;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public Reader() {
        this(System.in);
    }

    public Reader(InputStream is) {
        mIs = is;
    }

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

    }
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = mIs.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0) {
                return -1;
            }
        }
        return buf[curChar++];
    }

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

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

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

    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 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 boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

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

    }
}


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

public class Test {

    private static int r = 1000_000_007;

    public static void main(String[] args) {
        FastScanner in = new FastScanner();
        int n = in.nextInt();
        int[] a = new int[n + 2];
        for (int i = 1; i <= n; i++) {
            a[i] = in.nextInt();
        }

        Stack<Node> s = new Stack<>();
        long[] b = new long[n + 2];
        int x;
        for (int i = 1; i <= n; i++) {
            x = 1;
            while (!s.isEmpty() && s.peek().h >= a[i]) {
                x += s.pop().i;
            }
            s.push(new Node(a[i], x));
            b[i] = (long) x * a[i] + b[i - x];
        }

        s.clear();
        long[] c = new long[n + 2];
        for (int i = n; i > 0; i--) {
            x = 1;
            while (!s.isEmpty() && s.peek().h >= a[i]) {
                x += s.pop().i;
            }
            s.push(new Node(a[i], x));
            c[i] = (long) x * a[i] + c[i + x];
        }
        long max = -1;
        int maxi = -1;
        for (int i = 1; i <= n + 1; i++) {
            if ((b[i - 1] + c[i]) > max) {
                max = b[i - 1] + c[i];
                maxi = i - 1;
            }
        }
        x = a[maxi];
        for (int i = maxi; i > 0; i--) {
            if (a[i] < x) {
                x = a[i];
            }
            a[i] = x;
        }

        x = a[maxi+1];
        for (int i = maxi+1; i <= n; i++) {
            if (a[i] < x) {
                x = a[i];
            }
            a[i] = x;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= n; i++) {
            sb.append(a[i] + " ");
        }
        System.out.println(sb);
    }
}

class Node {

    int h, i;

    public Node(int h, int i) {
        this.h = h;
        this.i = i;
    }

}

class FastScanner {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");

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

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

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

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


import java.util.*;
import java.io.*;
public class EdB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int n = Integer.parseInt(bf.readLine());
 		int[] plots = new int[n];
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 		for(int i = 0;i<n;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();

 			plots[i] = Integer.parseInt(st.nextToken());

 		}
 		long[] rightsme = new long[n];
 		Stack<Integer> sme = new Stack<Integer>();
 		sme.add(0);
 		for(int i = 1;i<n;i++){
 			while (!sme.isEmpty() && plots[sme.peek()] > plots[i])
 				rightsme[sme.pop()] = i;
			sme.push(i);
 		}
 		while(!sme.isEmpty())
 			rightsme[sme.pop()] = -1;

 		long[] leftsme = new long[n];
 		sme.add(n-1);
 		for(int i = n-2;i>=0;i--){
 			while (!sme.isEmpty() && plots[sme.peek()] > plots[i])
 				leftsme[sme.pop()] = i;
			sme.push(i);
 		}
 		while(!sme.isEmpty())
 			leftsme[sme.pop()] = -1;

 		long[] left = new long[n];
 		long[] right = new long[n];
 		left[0] = plots[0];
 		long min = plots[0];
 		for(int i = 1;i<n;i++){
 			min = Math.min(plots[i],  min);
 			if (plots[i] == min)
 				left[i] = (i+1)*plots[i];
 			else
 				left[i] = left[(int)(leftsme[i])] + (i-leftsme[i])*plots[i];

 		}
 		right[n-1] = plots[n-1];
 		long min1 = plots[n-1];
 		for(int i = n-2;i>=0;i--){

 			min1 = Math.min(plots[i],  min1);
 			if (plots[i] == min1)
 				right[i] = (n-i)*plots[i];
 			else
 				right[i] = right[(int)(rightsme[i])]  + (rightsme[i] - i)*plots[i];

 		}

 		long[] dp = new long[n];
 		for(int i = 0;i<n;i++){
 			dp[i] = left[i] + right[i] - plots[i];

 		}

 		int maxindex = 0;
 		long max = 0;
 		for(int i = 0;i<n;i++){
 			if (dp[i] > max){
 				max = dp[i];
 				maxindex = i;
 			}
 		}
 		int[] answer = new int[n];
 		answer[maxindex] = plots[maxindex];
 		for(int j = maxindex+1;j<n;j++){
 			answer[j] = Math.min(answer[j-1], plots[j]);
 		}
 		for(int j = maxindex-1;j>=0;j--){
 			answer[j] = Math.min(answer[j+1], plots[j]);
 		}
 		for(int i = 0;i<n;i++){
 			out.print(answer[i] + " ");
 		}

 		out.close();

 	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class EdB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int n = Integer.parseInt(bf.readLine());
 		int[] plots = new int[n];
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 		for(int i = 0;i<n;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();

 			plots[i] = Integer.parseInt(st.nextToken());

 		}
 		long[] rightsme = new long[n];
 		Stack<Integer> sme = new Stack<Integer>();
 		sme.add(0);
 		for(int i = 1;i<n;i++){
 			while (!sme.isEmpty() && plots[sme.peek()] > plots[i])
 				rightsme[sme.pop()] = i;
			sme.push(i);
 		}
 		while(!sme.isEmpty())
 			rightsme[sme.pop()] = -1;

 		long[] leftsme = new long[n];
 		sme.add(n-1);
 		for(int i = n-2;i>=0;i--){
 			while (!sme.isEmpty() && plots[sme.peek()] > plots[i])
 				leftsme[sme.pop()] = i;
			sme.push(i);
 		}
 		while(!sme.isEmpty())
 			leftsme[sme.pop()] = -1;

 		long[] left = new long[n];
 		long[] right = new long[n];
 		left[0] = plots[0];
 		long min = plots[0];
 		for(int i = 1;i<n;i++){
 			min = Math.min(plots[i],  min);
 			if (plots[i] == min)
 				left[i] = (i+1)*plots[i];
 			else
 				left[i] = left[(int)(leftsme[i])] + (i-leftsme[i])*plots[i];
 		}

 		right[n-1] = plots[n-1];
 		long min1 = plots[n-1];
 		for(int i = n-2;i>=0;i--){
 			min1 = Math.min(plots[i],  min1);
 			if (plots[i] == min1)
 				right[i] = (i+1)*plots[i];
 			else
 				right[i] = right[(int)(rightsme[i])] + (rightsme[i] - i)*plots[i];
 		}

 		long[] dp = new long[n];
 		for(int i = 0;i<n;i++){
 			dp[i] = left[i] + right[i] - plots[i];

 		}

 		int maxindex = 0;
 		long max = 0;
 		for(int i = 0;i<n;i++){
 			if (dp[i] > max){
 				max = dp[i];
 				maxindex = i;
 			}
 		}
 		int[] answer = new int[n];
 		answer[maxindex] = plots[maxindex];
 		for(int j = maxindex+1;j<n;j++){
 			answer[j] = Math.min(answer[j-1], plots[j]);
 		}
 		for(int j = maxindex-1;j>=0;j--){
 			answer[j] = Math.min(answer[j+1], plots[j]);
 		}
 		for(int i = 0;i<n;i++){
 			out.print(answer[i] + " ");
 		}

 		out.close();

 	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class EdB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int n = Integer.parseInt(bf.readLine());
 		int[] plots = new int[n];
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 		for(int i = 0;i<n;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();

 			plots[i] = Integer.parseInt(st.nextToken());

 		}

 		long[] dp = new long[n];
 		long sum = plots[0];
 		long[] temp = new long[n];
 		temp[0] = plots[0];
 		for(int i = 1;i<n;i++){
 			sum+=Math.min(plots[i], temp[i-1]);
 			temp[i] = Math.min(plots[i], temp[i-1]);
 		}
 		dp[0] = sum;

 		for(int i = 1;i<n;i++){
 			if (plots[i] > plots[i-1]){
 				long sum1 = 0;
 				long min = plots[i] - plots[i-1];
 				for(int j = i;j<n;j++){
 					if (plots[j] > plots[i-1]){
 						sum1+=Math.min(min, plots[j]-plots[i-1]);
 						min = Math.min(min,  plots[j] - plots[i-1]);
 					}
 					else
 						break;
 				}
 				dp[i] = dp[i-1] + sum1;
 			}
 			else if (plots[i] < plots[i-1]){
 				long sum1 = 0;
 				long min = plots[i-1] - plots[i];
 				for(int j = i-1;j>=0;j--){
 					if (plots[j] > plots[i]){
 						sum1+=Math.min(min, plots[j]-plots[i]);
 						min = Math.min(min,  plots[j] - plots[i]);
 					}
 					else
 						break;
 				}
 				dp[i] = dp[i-1] - sum1;
 			}
 		}
 		int maxindex = 0;
 		long max = 0;
 		for(int i = 0;i<n;i++){
 			if (dp[i] > max){
 				max = dp[i];
 				maxindex = i;
 			}
 		}
 		int[] answer = new int[n];
 		answer[maxindex] = plots[maxindex];
 		for(int j = maxindex+1;j<n;j++){
 			answer[j] = Math.min(answer[j-1], plots[j]);
 		}
 		for(int j = maxindex-1;j>=0;j--){
 			answer[j] = Math.min(answer[j+1], plots[j]);
 		}
 		for(int i = 0;i<n;i++){
 			out.print(answer[i] + " ");
 		}

 		out.close();

 	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;
import java.io.*;
public class EdB {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int n = Integer.parseInt(bf.readLine());
 		int[] plots = new int[n];
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 		for(int i = 0;i<n;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();

 			plots[i] = Integer.parseInt(st.nextToken());

 		}

 		long[] dp = new long[n];
 		long sum = plots[0];
 		long[] temp = new long[n];
 		temp[0] = plots[0];
 		for(int i = 1;i<n;i++){
 			sum+=Math.min(plots[i], temp[i-1]);
 			temp[i] = Math.min(plots[i], temp[i-1]);
 		}
 		dp[0] = sum;

 		for(int i = 1;i<n;i++){
 			if (plots[i] > plots[i-1]){
 				long sum1 = 0;
 				long min = plots[i] - plots[i-1];
 				for(int j = i;j<n;j++){
 					if (plots[j] > plots[i-1]){
 						sum1+=Math.min(min, plots[j]-plots[i-1]);
 						min = Math.min(min,  plots[j] - plots[i-1]);
 					}
 					else
 						break;
 				}
 				dp[i] = dp[i-1] + sum1;
 			}
 			else if (plots[i] < plots[i-1]){
 				long sum1 = 0;
 				long min = plots[i-1] - plots[i];
 				for(int j = i-1;j>=0;j--){
 					if (plots[j] > plots[i]){
 						sum1+=Math.min(min, plots[j]-plots[i]);
 						min = Math.min(min,  plots[j] - plots[i]);
 					}
 					else
 						break;
 				}
 				dp[i] = dp[i-1] - sum1;
 			}
 			else{
 				dp[i] = dp[i-1];
 			}
 		}
 		int maxindex = 0;
 		long max = 0;
 		for(int i = 0;i<n;i++){
 			if (dp[i] > max){
 				max = dp[i];
 				maxindex = i;
 			}
 		}
 		int[] answer = new int[n];
 		answer[maxindex] = plots[maxindex];
 		for(int j = maxindex+1;j<n;j++){
 			answer[j] = Math.min(answer[j-1], plots[j]);
 		}
 		for(int j = maxindex-1;j>=0;j--){
 			answer[j] = Math.min(answer[j+1], plots[j]);
 		}
 		for(int i = 0;i<n;i++){
 			out.print(answer[i] + " ");
 		}

 		out.close();

 	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] a = new int[n];
        for (int i=0;i<n;i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        long[] arr_left = new long[n];
        int tmp_left = a[0];
        ArrayList<int[]> left = new ArrayList<int[]>();
        int[] first_left = {a[0], 1};
        left.add(first_left);
        long sum_left = 0L;
        sum_left += a[0];
        arr_left[0] = a[0];
        for (int i=1;i<n;i++) {
            int add_cnt = 1;
            if (a[i]<left.get(left.size()-1)[0]) {
                while (left.size()-1>=0 &&  a[i]<left.get(left.size()-1)[0]) {
                    sum_left-=(long)left.get(left.size()-1)[0]*left.get(left.size()-1)[1];
                    add_cnt+=left.get(left.size()-1)[1];
                    left.remove(left.size()-1);
                }
            }
            int[] add = {a[i], add_cnt};
            sum_left += (long)a[i]*add_cnt;
            left.add(add);
            arr_left[i] = sum_left;
        }

        long[] arr_right = new long[n];
        int tmp_right = a[n-1];
        ArrayList<int[]> right = new ArrayList<int[]>();
        int[] first_right = {a[n-1], 1};
        right.add(first_right);
        long sum_right = 0L;
        sum_right += a[n-1];
        arr_right[n-1] = a[n-1];
        for (int i=n-2;i>=0;i--) {
            int add_cnt = 1;
            if (a[i]<right.get(right.size()-1)[0]) {
                while (right.size()-1>=0 &&  a[i]<right.get(right.size()-1)[0]) {
                    sum_right-=(long)right.get(right.size()-1)[0]*right.get(right.size()-1)[1];
                    add_cnt+=right.get(right.size()-1)[1];
                    right.remove(right.size()-1);
                }
            }
            int[] add = {a[i], add_cnt};
            sum_right += (long)a[i]*add_cnt;
            right.add(add);
            arr_right[i] = sum_right;
        }

        // System.out.println(Arrays.toString(arr_left));
        // System.out.println(Arrays.toString(arr_right));

        int max_index = -1;
        long max_sum = 0L;
        for (int i=0;i<n;i++) {
            if (max_sum<arr_left[i]+arr_right[i]-a[i]) {
                max_sum = arr_left[i]+arr_right[i]-a[i];
                max_index = i;
            }
        }
        // System.out.println(max_index);

        int[] ans_arr = new int[n];
        int ans_left = a[max_index];
        for (int j=max_index-1;j>=0;j--) {
            ans_left = Math.min(ans_left, a[j]);
            ans_arr[j] = ans_left;
        }
        ans_arr[max_index] = a[max_index];
        int ans_right = a[max_index];
        for (int j=max_index+1;j<n;j++) {
            ans_right = Math.min(ans_right, a[j]);
            ans_arr[j] = ans_right;
        }

        StringBuilder sb = new StringBuilder();
        for (int i=0;i<n;i++) {
            sb.append(ans_arr[i]);
            if (i!=n-1) sb.append(" ");
        }
        System.out.println(sb);
        // System.out.println(Arrays.toString(ans_arr));
    }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        long[] arr= new long[n];
        for(int i=0;i<n;i++) arr[i]=sc.nextLong();
        int[] right= new int[n];
        right[n-1]=n;
        for(int i=n-2;i>=0;i--){
                int j=i+1;
                while(j<n && arr[j]>=arr[i]) j=right[j];
                right[i]=j;
        }
        int[] left=new int[n];
        left[0]=-1;
        for(int i=1;i<n;i++){
            int j=i-1;
            while(j>=0 && arr[j]>=arr[i]) j=left[j];
            left[i]=j;
        }
        long[] rsum=new long[n];
        rsum[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            int k=right[i];
            rsum[i]=(k-i)*arr[i];
            if(k<n) rsum[i]=rsum[i]+rsum[k];
        }
        long[] lsum=new long[n];
        lsum[0]=arr[0];
        for(int i=1;i<n;i++){
            int k=left[i];
            lsum[i]=(i-k)*arr[i];
            if(k>=0) lsum[i]+=lsum[k];
        }
        long max=0;int peak=-1;
        for(int i=0;i<n;i++){
            long sum=rsum[i]+lsum[i]-arr[i];
            if(sum>max){
                max=sum;
                peak=i;
            }
        }
        for(int i=peak+1;i<n;i++){
               arr[i]=Math.min(arr[i],arr[i-1]);
           }
           for(int i=peak-1;i>=0;i--){
               arr[i]=Math.min(arr[i+1],arr[i]);
           }
           for(int i=0;i<n;i++){
               System.out.print(arr[i]+" ");
           }
           System.out.println();
    }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr= new int[n];
        for(int i=0;i<n;i++) arr[i]=sc.nextInt();
        int[] right= new int[n];
        right[n-1]=n;
        for(int i=n-2;i>=0;i--){
                int j=i+1;
                while(j<n && arr[j]>=arr[i]) j=right[j];
                right[i]=j;
        }
        int[] left=new int[n];
        left[0]=-1;
        for(int i=1;i<n;i++){
            int j=i-1;
            while(j>=0 && arr[j]>=arr[i]) j=left[j];
            left[i]=j;
        }
        long[] rsum=new long[n];
        rsum[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            int k=right[i];
            rsum[i]=(k-i)*arr[i];
            if(k<n) rsum[i]=rsum[i]+rsum[k];
        }
        long[] lsum=new long[n];
        lsum[0]=arr[0];
        for(int i=1;i<n;i++){
            int k=left[i];
            lsum[i]=(i-k)*arr[i];
            if(k>=0) lsum[i]+=lsum[k];
        }
        long max=0;int peak=-1;
        for(int i=0;i<n;i++){
            long sum=rsum[i]+lsum[i]-arr[i];
            if(sum>max){
                max=sum;
                peak=i;
            }
        }
        for(int i=peak+1;i<n;i++){
               arr[i]=Math.min(arr[i],arr[i-1]);
           }
           for(int i=peak-1;i>=0;i--){
               arr[i]=Math.min(arr[i+1],arr[i]);
           }
           for(int i=0;i<n;i++){
               System.out.print(arr[i]+" ");
           }
           System.out.println();
    }
}
Click to see test details

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: Jun/06/2020 18:10:50UTC+5.5 (f2).
Desktop version, switch to mobile version.
Privacy Policy
Supported by
Telegram ИТМО


import java.util.*;
public class Main{
   public static void main(String[] args){
       Scanner sc=new Scanner(System.in);
           int n=sc.nextInt();
           int[] arr=new int[n];
           for(int i=0;i<n;i++){
               arr[i]=sc.nextInt();
           }
           long max=0;int peak=-1;
           for(int i=0;i<n;i++){
               int p =arr[i];
               long lmax=p;
               int j1=i+1,j2=i-1;
               int prev=arr[i];
               while(j1<n){
                   prev=Math.min(prev,arr[j1]);
                   lmax+=prev;
                   j1++;
               }
               prev=arr[i];
               while(j2>=0){
                   prev=Math.min(prev,arr[j2]);
                   lmax+=prev;
                   j2--;
               }
               if(lmax>max){
                   max=lmax;
                   peak=i;
               }
           }
           for(int i=peak+1;i<n;i++){
               arr[i]=Math.min(arr[i],arr[i-1]);
           }
           for(int i=peak-1;i>=0;i--){
               arr[i]=Math.min(arr[i+1],arr[i]);
           }
           for(int i=0;i<n;i++){
               System.out.print(arr[i]+" ");
           }
           System.out.println();

   }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr= new int[n];
        for(int i=0;i<n;i++) arr[i]=sc.nextInt();
        int[] right= new int[n];
        right[n-1]=n;
        for(int i=n-2;i>=0;i--){
                int j=i+1;
                while(j<n && arr[j]>=arr[i]) j=right[j];
                right[i]=j;
        }
        int[] left=new int[n];
        left[0]=-1;
        for(int i=1;i<n;i++){
            int j=i-1;
            while(j>=0 && arr[j]>=arr[i]) j=left[j];
            left[i]=j;
        }
        long[] rsum=new long[n];
        rsum[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            int k=right[i];
            rsum[i]=(k-i)*arr[i];
            if(k<n) rsum[i]=rsum[i]+rsum[k];
        }
        long[] lsum=new long[n];
        lsum[0]=arr[0];
        for(int i=1;i<n;i++){
            int k=left[i];
            lsum[i]=(i-k)*arr[i];
            if(k>=0) lsum[i]+=lsum[k];
        }
        long max=0;int peak=-1;
        for(int i=0;i<n;i++){
            long sum=rsum[i]+lsum[i]-arr[i];
            if(sum>max){
                max=sum;
                peak=i;
            }
        }
        for(int i=peak+1;i<n;i++){
               arr[i]=Math.min(arr[i],arr[i-1]);
           }
           for(int i=peak-1;i>=0;i--){
               arr[i]=Math.min(arr[i+1],arr[i]);
           }
           for(int i=0;i<n;i++){
               System.out.print(arr[i]+" ");
           }
           System.out.println();
    }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr= new int[n];
        for(int i=0;i<n;i++) arr[i]=sc.nextInt();
        int[] right= new int[n];
        right[n-1]=n;
        for(int i=n-2;i>=0;i--){
                int j=i+1;
                while(j<n && arr[j]>=arr[i]) j=right[j];
                right[i]=j;
        }
        int[] left=new int[n];
        left[0]=-1;
        for(int i=1;i<n;i++){
            int j=i-1;
            while(j>=0 && arr[j]>=arr[i]) j=left[j];
            left[i]=j;
        }
        long[] rsum=new long[n];
        rsum[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            int k=right[i];
            rsum[i]=(k-i)*arr[i]+rsum[k];
        }
        long[] lsum=new long[n];
        lsum[0]=arr[0];
        for(int i=1;i<n;i++){
            int k=left[i];
            lsum[i]=(i-k)*arr[i]+lsum[k];
        }
        long max=0;int peak=-1;
        for(int i=0;i<n;i++){
            long sum=rsum[i]+lsum[i]-arr[i];
            if(sum>max){
                max=sum;
                peak=i;
            }
        }
        for(int i=peak+1;i<n;i++){
               arr[i]=Math.min(arr[i],arr[i-1]);
           }
           for(int i=peak-1;i>=0;i--){
               arr[i]=Math.min(arr[i+1],arr[i]);
           }
           for(int i=0;i<n;i++){
               System.out.print(arr[i]+" ");
           }
           System.out.println();
    }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr= new int[n];
        for(int i=0;i<n;i++) arr[i]=sc.nextInt();
        int[] right= new int[n];
        right[n-1]=n;
        for(int i=n-2;i>=0;i--){
                int j=i+1;
                while(j<n && arr[j]>arr[i]) j=right[j];
                right[i]=j;
        }
        int[] left=new int[n];
        left[0]=-1;
        for(int i=1;i<n;i++){
            int j=i-1;
            while(j>=0 && arr[j]>arr[i]) j=left[j];
            left[i]=j;
        }
        long[] rsum=new long[n];
        rsum[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            int k=right[i];
            rsum[i]=(k-i)*arr[i];
            if(k<n) rsum[i]=rsum[i]+rsum[k];
        }
        long[] lsum=new long[n];
        lsum[0]=arr[0];
        for(int i=1;i<n;i++){
            int k=left[i];
            lsum[i]=(i-k)*arr[i];
            if(k>=0) lsum[i]+=lsum[k];
        }
        long max=0;int peak=-1;
        for(int i=0;i<n;i++){
            long sum=rsum[i]+lsum[i]-arr[i];
            if(sum>max){
                max=sum;
                peak=i;
            }
        }
        for(int i=peak+1;i<n;i++){
               arr[i]=Math.min(arr[i],arr[i-1]);
           }
           for(int i=peak-1;i>=0;i--){
               arr[i]=Math.min(arr[i+1],arr[i]);
           }
           for(int i=0;i<n;i++){
               System.out.print(arr[i]+" ");
           }
           System.out.println();
    }
}


//make sure to make new file!
import java.io.*;
import java.util.*;
//should work on C1 and C2
public class C1313{

   public static void main(String[] args)throws IOException{
      BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
      PrintWriter out = new PrintWriter(System.out);

      int n = Integer.parseInt(f.readLine());

      StringTokenizer st = new StringTokenizer(f.readLine());
      long[] array = new long[n];
      for(int k = 0; k < n; k++){
         array[k] = Long.parseLong(st.nextToken());
      }

      int[] lastmin = new int[n];
      Arrays.fill(lastmin,-1);

      Stack<Pair> stk = new Stack<Pair>();
      stk.add(new Pair(array[0],0));

      for(int k = 1; k < n; k++){

         while(!stk.isEmpty() && stk.peek().v > array[k]){
            stk.pop();
         }

         if(stk.isEmpty()){
            lastmin[k] = -1;
         } else {
            lastmin[k] = stk.peek().i;
         }

         stk.add(new Pair(array[k],k));
      }

      long[] dpleft = new long[n];
      dpleft[0] = array[0];

      for(int k = 1; k < n; k++){
         if(lastmin[k] == -1){
            dpleft[k] = array[k]*(k+1);
         } else {
            dpleft[k] = dpleft[lastmin[k]] + array[k]*(k-lastmin[k]);
         }
         //out.println(dpleft[k]);
      }

      //same as before but backward
      int[] firstmin = new int[n];
      Arrays.fill(firstmin,-1);

      stk = new Stack<Pair>();
      stk.add(new Pair(array[n-1],n-1));

      for(int k = n-2; k >= 0; k--){

         while(!stk.isEmpty() && stk.peek().v > array[k]){
            stk.pop();
         }

         if(stk.isEmpty()){
            firstmin[k] = -1;
         } else {
            firstmin[k] = stk.peek().i;
         }

         stk.add(new Pair(array[k],k));
      }

      long[] dpright = new long[n];
      dpright[n-1] = array[n-1];

      for(int k = n-2; k >= 0; k--){
         if(firstmin[k] == -1){
            dpright[k] = array[k]*(n-k);
         } else {
            dpright[k] = dpright[firstmin[k]] + array[k]*(firstmin[k]-k);
         }
         //out.println(dpright[k]);
      }

      long max = 0;
      int maxindex = 0;

      for(int k = 0; k < n; k++){
         if(dpleft[k] + dpright[k]-array[k] >= max){
            max = dpleft[k] + dpright[k] - array[k];
            maxindex = k;
         }
      }

      ArrayList<Long> left = new ArrayList<Long>();
      ArrayList<Long> right = new ArrayList<Long>();

      left.add(array[maxindex]);

      for(int k = maxindex-1; k >= 0; k--){
         left.add(Math.min(array[k],left.get(left.size()-1)));
      }

      right.add(array[maxindex]);
      for(int k = maxindex+1; k < n; k++){
         right.add(Math.min(array[k],right.get(right.size()-1)));
      }

      StringJoiner sj = new StringJoiner(" ");
      for(int k = left.size()-1; k >= 0; k--){
         sj.add("" + left.get(k));
      }
      for(int k = 1; k < right.size(); k++){
         sj.add("" + right.get(k));
      }

      out.println(sj.toString());

      out.close();
   }

   public static class Pair{
      long v;
      int i;
      public Pair(long a, int b){
         v = a;
         i = b;
      }
   }

}


/*	/ ﾌﾌ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ム
	/ )\⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀  Y
	(⠀⠀| ( ͡° ͜ʖ ͡°）⠀⌒(⠀ ノ
	(⠀ ﾉ⌒ Y ⌒ヽ-く __／
	| _⠀｡ノ| ノ｡ |/
	(⠀ー '_人`ー ﾉ
	⠀|\ ￣ _人'彡ﾉ
	⠀ )\⠀⠀ ｡⠀⠀ /
	⠀⠀(\⠀ #⠀ /
	⠀/⠀⠀⠀/ὣ====================D-
	/⠀⠀⠀/⠀ \ \⠀⠀\
	( (⠀)⠀⠀⠀⠀ ) ).⠀)
	(⠀⠀)⠀⠀⠀⠀⠀( | /
	|⠀ /⠀⠀⠀⠀⠀⠀ | /
	[_] ⠀⠀⠀⠀⠀[___]                     */
// Main Code at the Bottom
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
	//Fast IO class
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
        	boolean env=System.getProperty("ONLINE_JUDGE") != null;
        	if(!env) {
        		try {
					br=new BufferedReader(new FileReader("src\\input.txt"));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
        	}
        	else br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    static long MOD=1000000000+7;
    //Euclidean Algorithm
    static long gcd(long A,long B){
    	return (B==0)?A:gcd(B,A%B);
    }
    //Modular Exponentiation
    static long fastExpo(long x,long n){
        if(n==0) return 1;
        if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD;
        return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD;
    }
    //Modular Inverse
    static long inverse(long x) {
    	return fastExpo(x,MOD-2);
    }
    //Prime Number Algorithm
    static boolean isPrime(long n){
        if(n<=1) return false;
        if(n<=3) return true;
        if(n%2==0 || n%3==0) return false;
        for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false;
        return true;
    }
    //Reverse an array
    static void reverse(int arr[],int l,int r){
    	while(l<r) {
    		int tmp=arr[l];
    		arr[l++]=arr[r];
    		arr[r--]=tmp;
    	}
    }
    //Print array
    static void print1d(int arr[]) {
    	out.println(Arrays.toString(arr));
    }
    static void print2d(int arr[][]) {
    	for(int a[]: arr) out.println(Arrays.toString(a));
    }
    // Pair
    static class pair{
    	int x,y;
    	pair(int a,int b){
    		this.x=a;
    		this.y=b;
    	}
    	public boolean equals(Object obj) {
    		if(obj == null || obj.getClass()!= this.getClass()) return false;
            pair p = (pair) obj;
            return (this.x==p.x && this.y==p.y);
        }
    	public int hashCode() {
            return Objects.hash(x,y);
        }
    }
    static FastReader sc=new FastReader();
    static PrintWriter out=new PrintWriter(System.out);
    //Main function(The main code starts from here)
    static int seg[],N;
    static long a[],ans[];
    static void init(int n) {
    	N=n;
    	a=new long[n];
    	ans=new long[n];
    	seg=new int[4*n];
    }
    static void build(int root,int l,int r) {
    	if(l==r) {
    		seg[root]=l;
    		return;
    	}
    	int mid=l+(r-l)/2;
    	build(2*root+1,l,mid);
    	build(2*root+2,mid+1,r);
    	if(a[seg[2*root+1]]<a[seg[2*root+2]]) seg[root]=seg[2*root+1];
    	else seg[root]=seg[2*root+2];
    }
    static int query(int root,int l,int r,int x,int y) {
    	if(x<=l && r<=y) return seg[root];
    	if(y<l || r<x) return -1;
    	int mid=l+(r-l)/2;
    	int val1=query(2*root+1,l,mid,x,y);
    	int val2=query(2*root+2,mid+1,r,x,y);
    	if(val1==-1) return val2;
    	if(val2==-1) return val1;
    	if(a[val1]<a[val2]) return val1;
    	return val2;
    }
    static long solve(int l,int r) {
    	if(r-l==0) return ans[l]=a[l];
    	int pivot=query(0,0,N-1,l,r);
    	long val1=(pivot-l+1)*a[pivot]+(pivot+1<=r?solve(pivot+1,r):0);
    	long val2=(r-pivot+1)*a[pivot]+(pivot-1<l?0:solve(l,pivot-1));
    	if(val1>val2) for(int i=l;i<=pivot;i++) ans[i]=a[pivot];
    	else for(int i=pivot;i<=r;i++) ans[i]=a[pivot];
    	return Math.max(val1, val2);
    }
    public static void main (String[] args) throws java.lang.Exception {
    	int test;
    	test=1;
    	//test=sc.nextInt();
    	while(test-->0){
    		int n=sc.nextInt();
    		init(n);
    		for(int i=0;i<n;i++) a[i]=sc.nextLong();
    		build(0,0,n-1);
    		solve(0,n-1);
    		for(Long x: ans) out.print(x+" ");
    	}
        out.flush();
        out.close();
    }
}


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

public class GFG {
    public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		//int t=Integer.parseInt(br.readLine().trim());
		//while(t--!=0){
			int n=Integer.parseInt(br.readLine().trim());
    		String s=br.readLine().trim();
    		String ss[]=s.split("\\s+");
			int a[]=new int[n];
			for(int i=0;i<n;i++)
				a[i]=Integer.parseInt(ss[i]);
			long min=Integer.MAX_VALUE;
			int ind=-1;
			long l[]=new long[n];
			long r[]=new long[n];
			for(int i=0;i<n;i++){
			    if(min>=a[i]){
					min=a[i];
					l[i]=(i+1)*a[i];
					continue;
				}
				int j=i-1;
				for(j=i-1;j>=0;j--){
					if(a[j]<=a[i])
						break;
				}
				l[i]=l[j]+(i-j)*a[i];
			}
			min=Integer.MAX_VALUE;
			for(int i=n-1;i>=0;i--){
			    if(min>=a[i]){
					min=a[i];
					r[i]=(n-i)*a[i];
					continue;
				}
				int j=i+1;
				for(j=i+1;j<n;j++){
					if(a[j]<=a[i])
						break;
				}
				r[i]=r[j]+(j-i)*a[i];
			}
			long max=Integer.MIN_VALUE;
			for(int i=0;i<n;i++){
				long sum=l[i]+r[i]-a[i];
				if(max<sum){
					max=sum;
					ind=i;
				}
			}
			for(int j=ind-1;j>=0;j--){
				if(a[j]>a[j+1])
					a[j]=a[j+1];
			}
			for(int j=ind+1;j<n;j++){
				if(a[j]>a[j-1])
					a[j]=a[j-1];
			}
			System.out.print(a[0]);
			for(int i=1;i<n;i++)
			    System.out.print(" "+a[i]);
			System.out.println();
		//}
	}
}


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

public class GFG {
    public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		//int t=Integer.parseInt(br.readLine().trim());
		//while(t--!=0){
			int n=Integer.parseInt(br.readLine().trim());
    		String s=br.readLine().trim();
    		String ss[]=s.split("\\s+");
			int a[]=new int[n];
			for(int i=0;i<n;i++)
				a[i]=Integer.parseInt(ss[i]);
			long min=Integer.MAX_VALUE;
			int ind=-1;
			long l[]=new long[n];
			long r[]=new long[n];
			for(int i=0;i<n;i++){
			    if(min>=a[i]){
					min=a[i];
					l[i]=1l*(i+1)*a[i];
					continue;
				}
				int j=i-1;
				for(j=i-1;j>=0;j--){
					if(a[j]<=a[i])
						break;
				}
				l[i]=l[j]+1l*(i-j)*a[i];
			}
			min=Integer.MAX_VALUE;
			for(int i=n-1;i>=0;i--){
			    if(min>=a[i]){
					min=a[i];
					r[i]=1l*(n-i)*a[i];
					continue;
				}
				int j=i+1;
				for(j=i+1;j<n;j++){
					if(a[j]<=a[i])
						break;
				}
				r[i]=r[j]+1l*(j-i)*a[i];
			}
			long max=Integer.MIN_VALUE;
			for(int i=0;i<n;i++){
				long sum=l[i]+r[i]-a[i];
				if(max<sum){
					max=sum;
					ind=i;
				}
			}
			for(int j=ind-1;j>=0;j--){
				if(a[j]>a[j+1])
					a[j]=a[j+1];
			}
			for(int j=ind+1;j<n;j++){
				if(a[j]>a[j-1])
					a[j]=a[j-1];
			}
			System.out.print(a[0]);
			for(int i=1;i<n;i++)
			    System.out.print(" "+a[i]);
			System.out.println();
		//}
	}
}


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

public class GFG {
    public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		//int t=Integer.parseInt(br.readLine().trim());
		//while(t--!=0){
			int n=Integer.parseInt(br.readLine().trim());
    		String s=br.readLine().trim();
    		String ss[]=s.split("\\s+");
			int a[]=new int[n];
			for(int i=0;i<n;i++)
				a[i]=Integer.parseInt(ss[i]);
			long min=Integer.MAX_VALUE;
			int ind=-1;
			long l[]=new long[n];
			long r[]=new long[n];
			int min_l[]=new int[n];
			int min_r[]=new int[n];
			Arrays.fill(min_l,-1);
			Arrays.fill(min_r,-1);
			for(int i=1;i<n;i++){
				if(a[i-1]<=a[i]){
					min_l[i]=i-1;
					continue;
				}
				int j=i-1;
				while(min_l[j]!=-1&&a[min_l[j]]>a[i])
					j=min_l[j];
				min_l[i]=min_l[j];
			}
			for(int i=n-2;i>=0;i--){
				if(a[i+1]<=a[i]){
					min_r[i]=i+1;
					continue;
				}
				int j=i+1;
				while(min_r[j]!=-1&&a[min_r[j]]>a[i])
					j=min_r[j];
				min_r[i]=min_r[j];
			}
			for(int i=0;i<n;i++){
			    if(min>=a[i]){
					min=a[i];
					l[i]=1l*(i+1)*a[i];
					continue;
				}
				int j=min_l[i];
				l[i]=l[j]+1l*(i-j)*a[i];
			}
			min=Integer.MAX_VALUE;
			for(int i=n-1;i>=0;i--){
			    if(min>=a[i]){
					min=a[i];
					r[i]=1l*(n-i)*a[i];
					continue;
				}
				int j=min_r[i];
				r[i]=r[j]+1l*(j-i)*a[i];
			}
			long max=Integer.MIN_VALUE;
			for(int i=0;i<n;i++){
				long sum=l[i]+r[i]-a[i];
				if(max<sum){
					max=sum;
					ind=i;
				}
			}
			for(int j=ind-1;j>=0;j--){
				if(a[j]>a[j+1])
					a[j]=a[j+1];
			}
			for(int j=ind+1;j<n;j++){
				if(a[j]>a[j-1])
					a[j]=a[j-1];
			}
			System.out.print(a[0]);
			for(int i=1;i<n;i++)
			    System.out.print(" "+a[i]);
			System.out.println();
		//}
	}
}


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

public class GFG {
    public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		//int t=Integer.parseInt(br.readLine().trim());
		//while(t--!=0){
			int n=Integer.parseInt(br.readLine().trim());
    		String s=br.readLine().trim();
    		String ss[]=s.split("\\s+");
			int b[]=new int[n];
			for(int i=0;i<n;i++)
				b[i]=Integer.parseInt(ss[i]);
			long max=Integer.MIN_VALUE;
			int ind=-1;
			for(int i=0;i<n;i++){
			    int a[]=new int[n];
			    for(int j=0;j<n;j++)
			        a[j]=b[j];
				long cnt=a[i];
				for(int j=i-1;j>=0;j--){
					if(a[j]>a[j+1])
					    a[j]=a[j+1];
					cnt+=a[j];
				}
				for(int j=i+1;j<n;j++){
					if(a[j]>a[j-1])
					    a[j]=a[j-1];
					cnt+=a[j];
				}
				if(max<cnt){
					max=cnt;
					ind=i;
				}
			}
			for(int j=ind-1;j>=0;j--){
				if(b[j]>b[j+1])
					b[j]=b[j+1];
			}
			for(int j=ind+1;j<n;j++){
				if(b[j]>b[j-1])
					b[j]=b[j-1];
			}
			System.out.print(b[0]);
			for(int i=1;i<n;i++)
			    System.out.print(" "+b[i]);
			System.out.println();
		//}
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Washoum
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        inputClass in = new inputClass(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C2SkyscrapersHardVersion solver = new C2SkyscrapersHardVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class C2SkyscrapersHardVersion {
        public void solve(int testNumber, inputClass sc, PrintWriter out) {
            int n = sc.nextInt();
            int[] tab = new int[n];
            int min = 0;
            for (int i = 0; i < n; i++) {
                tab[i] = sc.nextInt();
                if (tab[i] > tab[min]) {
                }
            }
            int[] left = new int[n];
            Arrays.fill(left, -1);
            for (int i = 1; i < n; i++) {
                int j = i - 1;
                while (j >= 0 && tab[j] >= tab[i]) {
                    j = left[j];
                }
                left[i] = j;
            }
            int[] right = new int[n];
            Arrays.fill(right, -1);
            for (int i = n - 2; i >= 0; i--) {
                int j = i + 1;
                while (j < n && j != -1 && tab[j] >= tab[i]) {
                    j = right[j];
                }
                right[i] = j;
            }
            long[] ansl = new long[n];
            for (int i = 0; i < n; i++) {
                if (left[i] == -1) {
                    ansl[i] = tab[i] * i;
                } else {
                    ansl[i] = ansl[left[i]] + tab[left[i]] + (i - left[i] - 1) * tab[i];
                }
            }
            long[] ansr = new long[n];
            for (int i = n - 1; i >= 0; i--) {
                if (right[i] == -1) {
                    ansr[i] = tab[i] * (n - i - 1);
                } else {
                    ansr[i] = ansr[right[i]] + tab[right[i]] + (right[i] - i - 1) * tab[i];
                }
            }
            int idx = -1;
            long best = 0;
            for (int i = 0; i < n; i++) {
                if (ansr[i] + ansl[i] + tab[i] > best) {
                    idx = i;
                    best = ansr[i] + ansl[i] + tab[i];
                }
            }
            int[] ans = new int[n];
            ans[idx] = tab[idx];
            for (int i = idx + 1; i < n; i++) {
                ans[i] = Math.min(ans[i - 1], tab[i]);
            }
            for (int i = idx - 1; i >= 0; i--) {
                ans[i] = Math.min(ans[i + 1], tab[i]);
            }
            for (int i : ans) {
                out.print(i + " ");
            }
        }

    }

    static class inputClass {
        BufferedReader br;
        StringTokenizer st;

        public inputClass(InputStream in) {

            br = new BufferedReader(new InputStreamReader(in));
        }

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

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

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Washoum
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        inputClass in = new inputClass(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C2SkyscrapersHardVersion solver = new C2SkyscrapersHardVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class C2SkyscrapersHardVersion {
        public void solve(int testNumber, inputClass sc, PrintWriter out) {
            int n = sc.nextInt();
            long[] tab = new long[n];
            int min = 0;
            for (int i = 0; i < n; i++) {
                tab[i] = sc.nextInt();
                if (tab[i] > tab[min]) {
                }
            }
            int[] left = new int[n];
            Arrays.fill(left, -1);
            for (int i = 1; i < n; i++) {
                int j = i - 1;
                while (j >= 0 && tab[j] >= tab[i]) {
                    j = left[j];
                }
                left[i] = j;
            }
            int[] right = new int[n];
            Arrays.fill(right, -1);
            for (int i = n - 2; i >= 0; i--) {
                int j = i + 1;
                while (j < n && j != -1 && tab[j] >= tab[i]) {
                    j = right[j];
                }
                right[i] = j;
            }
            long[] ansl = new long[n];
            for (int i = 0; i < n; i++) {
                if (left[i] == -1) {
                    ansl[i] = tab[i] * i;
                } else {
                    ansl[i] = ansl[left[i]] + tab[left[i]] + (i - left[i] - 1) * tab[i];
                }
            }
            long[] ansr = new long[n];
            for (int i = n - 1; i >= 0; i--) {
                if (right[i] == -1) {
                    ansr[i] = tab[i] * (n - i - 1);
                } else {
                    ansr[i] = ansr[right[i]] + tab[right[i]] + (right[i] - i - 1) * tab[i];
                }
            }
            int idx = -1;
            long best = 0;
            for (int i = 0; i < n; i++) {
                if (ansr[i] + ansl[i] + tab[i] > best) {
                    idx = i;
                    best = ansr[i] + ansl[i] + tab[i];
                }
            }
            long[] ans = new long[n];
            ans[idx] = tab[idx];
            for (int i = idx + 1; i < n; i++) {
                ans[i] = Math.min(ans[i - 1], tab[i]);
            }
            for (int i = idx - 1; i >= 0; i--) {
                ans[i] = Math.min(ans[i + 1], tab[i]);
            }
            for (long i : ans) {
                out.print(i + " ");
            }
        }

    }

    static class inputClass {
        BufferedReader br;
        StringTokenizer st;

        public inputClass(InputStream in) {

            br = new BufferedReader(new InputStreamReader(in));
        }

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

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

    }
}


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=i;
			long s=0;
			if(st.size()>0){
				k=i-st.peek().i-1;
				s+=st.peek().s;
			}
			s+=((long)k)*a[i];
			l[i]=s;
			st.add(new ob(i,s+a[i]));

		}
		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=n-1-i;
			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+a[i]));
		}
		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.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=i;
			long s=0;
			if(st.size()>0){
				k=i-st.peek().i-1;
				s+=st.peek().s;
			}
			s+=((long)k)*a[i];
			l[i]=s;
			st.add(new ob(i,s+a[i]));

		}
		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=i;
			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+a[i]));
		}
		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.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-1;
				s+=st.peek().s;
			}
			s+=(long)k*a[i];
			l[i]=s;
			st.add(new ob(i,s+a[i]));

		}
		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+a[i]));
		}
		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;
	}
}