#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define all(a) (a).begin(),(a).end()

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    int n, p; cin >> n;
    bool f = false;
    vector<int> a(n);
    set<pair<int,int>> x;
    for(int i=0;i<n;i++)
        cin >> a[i], x.emplace(mp(a[i],i));
    for(int i=0;i<n-1;i++) {
        x.erase(mp(a[i],i));
        pair<int,int> z = *x.rbegin();
        if(z.f>a[i]) {
            p = a[i];
            a[i] = z.f;
            a[z.s] = p;
            f = true;
            break;
        }
    }
    for(int i=0;i<=9;i++)
        if(count(all(a),i)>1) f=true;
    if(!f) p=a[n-2], a[n-2]=a[n-1], a[n-1]=p;
    for(int i=0;i<n;i++)
        cout << a[i] << " ";
    return 0;
}