#include<bits/stdc++.h>

using namespace std;

int n;
vector<int> raspored;
vector<int> sortirani;
vector<int> ponavljanja;

bool cmp(int a, int b)
{
    return a>b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    raspored.resize(n);
    for(int i=0;i<n;i++)
        cin >> raspored[i];
    sortirani=raspored;
    sort(sortirani.begin(),sortirani.end(),cmp);
    int cnt=0;
    while(cnt<n && raspored[cnt]==sortirani[cnt])
        cnt++;
    if(cnt == n)
    {
        ponavljanja.resize(10);
        for(int i=0;i<n;i++)
        ponavljanja[raspored[i]]++;
        bool da=false;
        for(int i=0;i<10;i++)
        {
            if(ponavljanja[i]>1)
                da=true;
        }
        if(da)
        {
            for(int i=0;i<n;i++)
                cout << raspored[i] << " ";
        }
        else
        {
            for(int i=0;i<n-2;i++)
                cout << raspored[i] << " ";
            cout << raspored[n-1] << " " << raspored[n-2] << endl;
        }
    }
    else
    {
       int pok=n-1;
       for(int i=pok;i>cnt;i++)
       {
           if(raspored[i]==sortirani[cnt])
           {
               swap(raspored[i],raspored[cnt]);
               break;
           }
       }
       for(int i=0;i<n;i++)
            cout << raspored[i] << " ";
    }
}