#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int swapp(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}
int main(){

ios_base::sync_with_stdio(0);
cin.tie(0);
int kraj = -1;

int n;
cin >> n;
vector<int>v(n);
cin >> v[0];
for (int i = 1; i < n; i++)
{
    cin >> v[i];
    if(kraj == -1 && v[i] >= v[i-1] )
        kraj = i;

}
if(kraj == -1)
    swapp(v[v.size()-2],v[v.size()-1]);
else
    {
    int maxi = kraj;
    for (int i = kraj; i < n; i++)
    {
        if ( v[i] >= v[maxi])
            maxi = i;
    }
    vector<int>::iterator up = upper_bound(v.begin(), v.begin()+kraj, v[maxi],greater<int>());
    int pos = up - v.begin();
    int j = pos;
    while(v[j] >= v[maxi] && j < maxi)
        j++;
    swapp(v[j],v[maxi]);

}
for(int i = 0; i < n; i++)
{
    cout << v[i] << " " ;
}



return 0;
}