
import java.util.Scanner;

 class Main {

    static class Elem{
        int val;
        int index;

        public Elem(int val, int index) {
            this.val = val;
            this.index = index;
        }
    }

    public static void heapify(Elem[] heap, int index, int n){
        int left=index*2+1;
        int right = index*2+2;

        int max=index;
        if(left < n && heap[left].val >= heap[max].val){
            max=left;
        }
        if(right<n && heap[right].val >= heap[max].val){
            max=right;
        }
        if(max!=index){
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        }

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();

        Elem[] heap =new Elem[n];

        for(int i=0;i<n;i++){
            heap[i]=new Elem(a[i],i);
        }

        for(int i=n/2;i>=0;i--){
            heapify(heap, i,n);
        }
//        for(int i=0;i<n;i++){
//            System.out.println(heap[i].val + " " + heap[i].index);
//        }

        int[] ans = new int[n];
        for(int i=0;i<n;i++){
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        }
        for(int i=0;i<n;i++){
            System.out.println(ans[i]);
        }

    }
} import java.util.Scanner; Scanner java.util class Main {

    static class Elem{
        int val;
        int index;

        public Elem(int val, int index) {
            this.val = val;
            this.index = index;
        }
    }

    public static void heapify(Elem[] heap, int index, int n){
        int left=index*2+1;
        int right = index*2+2;

        int max=index;
        if(left < n && heap[left].val >= heap[max].val){
            max=left;
        }
        if(right<n && heap[right].val >= heap[max].val){
            max=right;
        }
        if(max!=index){
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        }

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();

        Elem[] heap =new Elem[n];

        for(int i=0;i<n;i++){
            heap[i]=new Elem(a[i],i);
        }

        for(int i=n/2;i>=0;i--){
            heapify(heap, i,n);
        }
//        for(int i=0;i<n;i++){
//            System.out.println(heap[i].val + " " + heap[i].index);
//        }

        int[] ans = new int[n];
        for(int i=0;i<n;i++){
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        }
        for(int i=0;i<n;i++){
            System.out.println(ans[i]);
        }

    }
} Main public static void heapify(Elem[] heap, int index, int n){
        int left=index*2+1;
        int right = index*2+2;

        int max=index;
        if(left < n && heap[left].val >= heap[max].val){
            max=left;
        }
        if(right<n && heap[right].val >= heap[max].val){
            max=right;
        }
        if(max!=index){
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        }

    } heapify {
        int left=index*2+1;
        int right = index*2+2;

        int max=index;
        if(left < n && heap[left].val >= heap[max].val){
            max=left;
        }
        if(right<n && heap[right].val >= heap[max].val){
            max=right;
        }
        if(max!=index){
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        }

    } int left=index*2+1; left index*2+1 index*2 index 2 1 int right = index*2+2; right index*2+2 index*2 index 2 2 int max=index; max index if(left < n && heap[left].val >= heap[max].val){
            max=left;
        } left < n && heap[left].val >= heap[max].val left < n left n heap[left].val >= heap[max].val heap[left].val heap[left] heap left heap[left].val heap[max].val heap[max] heap max heap[max].val {
            max=left;
        } max=left max left if(right<n && heap[right].val >= heap[max].val){
            max=right;
        } right<n && heap[right].val >= heap[max].val right<n right n heap[right].val >= heap[max].val heap[right].val heap[right] heap right heap[right].val heap[max].val heap[max] heap max heap[max].val {
            max=right;
        } max=right max right if(max!=index){
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        } max!=index max index {
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        } Elem temp = heap[index]; temp heap[index] heap index heap[index]=heap[max] heap[index] heap index heap[max] heap max heap[max]=temp heap[max] heap max temp heapify(heap, max, n) heapify heap max n Elem[] heap heap int index index int n n public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();

        Elem[] heap =new Elem[n];

        for(int i=0;i<n;i++){
            heap[i]=new Elem(a[i],i);
        }

        for(int i=n/2;i>=0;i--){
            heapify(heap, i,n);
        }
//        for(int i=0;i<n;i++){
//            System.out.println(heap[i].val + " " + heap[i].index);
//        }

        int[] ans = new int[n];
        for(int i=0;i<n;i++){
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        }
        for(int i=0;i<n;i++){
            System.out.println(ans[i]);
        }

    } main {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();

        Elem[] heap =new Elem[n];

        for(int i=0;i<n;i++){
            heap[i]=new Elem(a[i],i);
        }

        for(int i=n/2;i>=0;i--){
            heapify(heap, i,n);
        }
//        for(int i=0;i<n;i++){
//            System.out.println(heap[i].val + " " + heap[i].index);
//        }

        int[] ans = new int[n];
        for(int i=0;i<n;i++){
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        }
        for(int i=0;i<n;i++){
            System.out.println(ans[i]);
        }

    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int n= in.nextInt(); n in.nextInt() in.nextInt in int[] a = new int[n]; a new int[n] n for(int i=0;i<n;i++)
            a[i]=in.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=in.nextInt(); a[i]=in.nextInt() a[i] a i in.nextInt() in.nextInt in Elem[] heap =new Elem[n]; heap new Elem[n] n for(int i=0;i<n;i++){
            heap[i]=new Elem(a[i],i);
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            heap[i]=new Elem(a[i],i);
        } {
            heap[i]=new Elem(a[i],i);
        } heap[i]=new Elem(a[i],i) heap[i] heap i new Elem(a[i],i) for(int i=n/2;i>=0;i--){
            heapify(heap, i,n);
        } int i=n/2; int i=n/2; i n/2 n 2 i>=0 i 0 i-- i-- i {
            heapify(heap, i,n);
        } {
            heapify(heap, i,n);
        } heapify(heap, i,n) heapify heap i n int[] ans = new int[n]; ans new int[n] n for(int i=0;i<n;i++){
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        } {
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        } if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            } heap[0].index!=i heap[0].index heap[0] heap 0 heap[0].index i {
                ans[i]=heap[0].val;
            } ans[i]=heap[0].val ans[i] ans i heap[0].val heap[0] heap 0 heap[0].val {
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            } if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    } n>=3 n 3 {
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                } ans[i]=Math.max(heap[1].val, heap[2].val) ans[i] ans i Math.max(heap[1].val, heap[2].val) Math.max Math heap[1].val heap[1] heap 1 heap[1].val heap[2].val heap[2] heap 2 heap[2].val if(n==2){
                    ans[i]=heap[1].val;
                    } n==2 n 2 {
                    ans[i]=heap[1].val;
                    } ans[i]=heap[1].val ans[i] ans i heap[1].val heap[1] heap 1 heap[1].val for(int i=0;i<n;i++){
            System.out.println(ans[i]);
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            System.out.println(ans[i]);
        } {
            System.out.println(ans[i]);
        } System.out.println(ans[i]) System.out.println System.out System System.out ans[i] ans i String[] args args static class Elem{
        int val;
        int index;

        public Elem(int val, int index) {
            this.val = val;
            this.index = index;
        }
    } Elem int val; val int index; index public Elem(int val, int index) {
            this.val = val;
            this.index = index;
        } Elem {
            this.val = val;
            this.index = index;
        } this.val = val this.val this this.val val this.index = index this.index this this.index index int val val int index index

import java.util.Scanner;

 class Main {

    static class Elem{
        int val;
        int index;

        public Elem(int val, int index) {
            this.val = val;
            this.index = index;
        }
    }

    public static void heapify(Elem[] heap, int index, int n){
        int left=index*2+1;
        int right = index*2+2;

        int max=index;
        if(left < n && heap[left].val >= heap[max].val){
            max=left;
        }
        if(right<n && heap[right].val >= heap[max].val){
            max=right;
        }
        if(max!=index){
            Elem temp = heap[index];
            heap[index]=heap[max];
            heap[max]=temp;
            heapify(heap, max, n);
        }

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();

        Elem[] heap =new Elem[n];

        for(int i=0;i<n;i++){
            heap[i]=new Elem(a[i],i);
        }

        for(int i=n/2;i>=0;i--){
            heapify(heap, i,n);
        }
//        for(int i=0;i<n;i++){
//            System.out.println(heap[i].val + " " + heap[i].index);
//        }

        int[] ans = new int[n];
        for(int i=0;i<n;i++){
            if(heap[0].index!=i){
                ans[i]=heap[0].val;
            }
            else{
                if(n>=3){
                    ans[i]=Math.max(heap[1].val, heap[2].val);
                }
                else
                    if(n==2){
                    ans[i]=heap[1].val;
                    }
            }
        }
        for(int i=0;i<n;i++){
            System.out.println(ans[i]);
        }

    }
}
