#include<bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple

using namespace std;

const int N = 1e6;

int niz[N+10];
int last[10];
int cnt[10];

int nadji(int poz, int x)
{
    for(int i = 9; i > x; i--)
        if(last[i] != -1 && last[i] > poz) return last[i];
    return -1;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    bool ok = false;
    bool same = false;

    for(int i = 0; i <= 9; i++) last[i] = -1;
    for(int i = 1; i <= n; i++)
    {
        cin >> niz[i];
        cnt[niz[i]]++;
        if(cnt[niz[i]] > 1) same = true;
    }
    for(int i = n; i > 0; i--)
        last[niz[i]] = max(last[niz[i]], i);

    for(int i = 1; i <= n; i++)
    {
        int poz = nadji(i, niz[i]);
        if(poz != -1)
        {
            int pom = niz[i];
            niz[i] = niz[poz];
            niz[poz] = pom;
            ok = true;
            break;
        }
    }
    if(ok || same)
    {
        for(int i = 1; i <= n; i++) cout << niz[i] << " ";
        return 0;
    }
    int pom = niz[n];
    niz[n] = niz[n-1];
    niz[n-1] = pom;
    for(int i = 1; i <= n; i++) cout << niz[i] << " ";

    return 0;
}