#include <bits/stdc++.h>
using namespace std;

signed main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int n; cin >> n;
    vector<int> a(n);

    for (auto &x : a)
        cin >> x;
    
    vector<int> b(n);
    b = a;
    
    sort(b.begin(), b.end(), greater<int>());

    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            for (int j = i+1; j < n; j++) {
                if (a[j] == b[i]) {
                    swap(a[i], a[j]);
                    for (auto x : a)
                        cout << x << ' ';
                    return 0;
                }
            }
            assert(false);
        }
    }

    swap(a[n-1], a[n-2]);
    for (auto x : a)
        cout << x << ' ';
}