#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define mid(l, r) ((l)+(r))/2
#define len(a) (a).length()
#define sz(a) (a).size()
#define xx first
#define yy second
#define inf int(2e9)
#define ff(i, a, b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for(int (i) = (a); (i) >= (b); --(i))


using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

template<class T>
void print(const T niz[], const int siz)
{
    for(int i=0;i<siz;i++)
        cout << niz[i] << " ";
    cout << endl;
}

int n;
int niz[1000005];
int maks[1000005];
int poj[20];
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    ff(i,0,n - 1){
        cin >> niz[i];
        poj[niz[i]]++;
    }
    fb(i,n - 1,0){
        maks[i] = max(maks[i + 1], niz[i]);
    }
    ff(i,0,n - 1){
        if(niz[i] < maks[i]){
            fb(j,n - 1, 0){
                if(niz[j] == maks[i]){
                    niz[j] = niz[i];
                    break;
                }
            }
            niz[i] = maks[i];
            ff(i,0,n - 1)cout << niz[i] << " ";
            cout << endl;
            return 0;
        }
    }
    ff(i,0,9){
        if(poj[i] >= 2){
            ff(i,0,n - 1)cout << niz[i] << " ";
            cout << endl;
            return 0;
        }
    }
    swap(niz[n - 1], niz[n - 2]);
    ff(i,0,n - 1)cout << niz[i] << " ";
    cout << endl;
    return 0;
}