#include <iostream>
using namespace std;

int main()
{
    int N;
    cin >> N;
    int maxindex=0;
    int maximum=0;
    int brojac = 0;
    short A[N];
    int t;
    int i;
    bool tadija = true;
    cin >> A[0];
    for(i=1; i<N; i++)
    {
        cin >>A[i];
       if ((A[i]<=A[i-1]) && (tadija)) brojac++;
       else tadija=false;
       if((!tadija) && (maximum<=A[i]))
       {

        maximum =A[i];
        maxindex=i;
       }
    }


    for(i=0; i<=brojac; i++)
    {

        if(A[i]<=A[maxindex])
    {
        t=A[i];
        A[i]=A[maxindex];
        A[maxindex]=t;
        break;

    }
    }
    if(brojac==N-1)
    {
        t=A[brojac];
        A[brojac]=A[brojac-1];
        A[brojac-1]=t;
    }

for(i=0; i<N;i++)
    cout << A[i] << " ";
    cout << endl;

return 0;
}