#include <iostream>
#include <string>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int cifre[N] = {0};
    int index1 = N;
    int index2 = N;
    string output = "";

    for(int i = 0; i < N; i++){
        cin >> cifre[i];
        for(int x = 0; x < i; x++){
            if(cifre[x] < cifre[i] && x < index1 && cifre[index2] < cifre[i]){
                index1 = x;
                index2 = i;
                break;
            }
        }
    }
    if(index1 == index2){
        index1 = N-1;
        index2 = N-2;
    }
    swap(cifre[index1], cifre[index2]);

    for(int i : cifre) output += to_string(i) + " ";

    cout << output;

    return 0;
}