#include <bits/stdc++.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

    int a[1000];
    for (int i = 0; i < n; ++i)
        cin >> a[i];

    pair<short, int> b[1000];

    for (int i = 0; i < n; ++i)
    {
        b[i].first  = a[i];
        b[i].second = i;
    }

    sort(b, b+n);

    int i,j;
    bool uspesno(false);
    for (i = 0 ; i < n; ++i)
    {
        for (j = n-1; j >= 0; --j)
            if ((a[i] < b[j].first) && (i < b[j].second)) { swap(a[i], a[b[j].second]); uspesno = true; break; }
        if (uspesno) break;
    }

    if (j == -1 && i == n)
            swap(a[n-1], a[n-2]);

    for (int i = 0; i < n; ++i)
        cout << a[i] << " ";

    return 0;
}