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

public class Main {

    static String br = System.getProperty("line.separator");

    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    }

    static void println(Object o){
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    }
   static void print(Object o){
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    }
} import java.io.PrintWriter; PrintWriter java.io import java.util.*; util.* java public class Main {

    static String br = System.getProperty("line.separator");

    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    }

    static void println(Object o){
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    }
   static void print(Object o){
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    }
} Main static String br = System.getProperty("line.separator"); br System.getProperty("line.separator") System.getProperty System "line.separator" public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    } main {
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    } Scanner sc  = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] A = new int[N]; A new int[N] N int max = 0; max 0 int max2 = 0; max2 0 List<Integer> maxIndex = new ArrayList<>(); maxIndex new ArrayList<>() for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        } {
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        } int num = Integer.parseInt(sc.next()); num Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc A[i] = num A[i] A i num if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            } max < num max num {
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            } max2 = max max2 max max = num max num if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1); maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max maxIndex.size() != 0 maxIndex.size() maxIndex.size maxIndex 0 maxIndex.get(maxIndex.size()-1) < max maxIndex.get(maxIndex.size()-1) maxIndex.get maxIndex maxIndex.size()-1 maxIndex.size() maxIndex.size maxIndex 1 max maxIndex.remove(maxIndex.size()-1) maxIndex.remove maxIndex maxIndex.size()-1 maxIndex.size() maxIndex.size maxIndex 1 maxIndex.add(i) maxIndex.add maxIndex i {
                max2 = Math.max(A[i], max2);
            } max2 = Math.max(A[i], max2) max2 Math.max(A[i], max2) Math.max Math A[i] A i max2 int[] B = new int[N]; B new int[N] N Arrays.fill(B, max) Arrays.fill Arrays B max for(int index : maxIndex){
            B[index] = max2;
        } int index maxIndex {
            B[index] = max2;
        } {
            B[index] = max2;
        } B[index] = max2 B[index] B index max2 StringBuffer str = new StringBuffer(); str new StringBuffer() for(int i=0; i<N; i++){
            str.append(B[i] + br);
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            str.append(B[i] + br);
        } {
            str.append(B[i] + br);
        } str.append(B[i] + br) str.append str B[i] + br B[i] B i br print(str.toString()) print str.toString() str.toString str String[] args args static void println(Object o){
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    } println {
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    } PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) out.println(o) out.println out o out.flush() out.flush out Object o o static void print(Object o){
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    } print {
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    } PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) out.print(o) out.print out o out.flush() out.flush out Object o o public class Main {

    static String br = System.getProperty("line.separator");

    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    }

    static void println(Object o){
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    }
   static void print(Object o){
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    }
} public class Main {

    static String br = System.getProperty("line.separator");

    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    }

    static void println(Object o){
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    }
   static void print(Object o){
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    }
} Main
import java.io.PrintWriter;
import java.util.*;

public class Main {

    static String br = System.getProperty("line.separator");

    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N];

        int max = 0;
        int max2 = 0;
        List<Integer> maxIndex = new ArrayList<>();
        for(int i=0; i<N; i++){
            int num = Integer.parseInt(sc.next());
            A[i] = num;
            if(max < num){
                max2 = max;
                max = num;
                if(maxIndex.size() != 0 && maxIndex.get(maxIndex.size()-1) < max) maxIndex.remove(maxIndex.size()-1);
                maxIndex.add(i);
            }else{
                max2 = Math.max(A[i], max2);
            }
        }

        int[] B = new int[N];
        Arrays.fill(B, max);

        for(int index : maxIndex){
            B[index] = max2;
        }


        StringBuffer str = new StringBuffer();
        for(int i=0; i<N; i++){
            str.append(B[i] + br);
        }

        print(str.toString());
    }

    static void println(Object o){
        PrintWriter out = new PrintWriter(System.out);
        out.println(o);
        out.flush();
    }
   static void print(Object o){
       PrintWriter out = new PrintWriter(System.out);
       out.print(o);
       out.flush();
    }
}
