#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    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]) {
            int p = a[i];
            a[i] = z.f;
            a[z.s] = p;
            break;
        }
    }
    for(int i=0;i<n;i++)
        cout << a[i] << " ";
    return 0;
}