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

int main()
{

    ll n; cin >> n;
    ll a[n];

    priority_queue<pair<ll, ll>> pq;

    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        pq.push(make_pair(a[i], i));
    }

    ll i = 0;
    again:

    ll m = pq.top().first;
    ll gr = pq.top().second;

    for (i; i <= gr; ++i) {

        if (a[i] < m) {
            swap(a[i], a[gr]);
            goto ende;
        }
    }
    i = pq.top().second;
    pq.pop(); // try for next max

    // last condition (all decreasing)
    if (pq.empty()) {
        if (a[0] == a[1]) {
            swap(a[0], a[1]);
            goto ende;
        }
        swap(a[n-2], a[n-1]);
        goto ende;
    }

    goto again;

    // Print
    ende:
    for (ll i = 0; i < n; ++i)
        cout << a[i] << " ";


    return 0;
}