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

int n;
int arr[1000005];
pair<int, int> maxi[1000005];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
    }

    maxi[n - 1] = {arr[n - 1], n - 1};
    for(int i = n - 2; i >= 0; --i){
        if(arr[i] > maxi[i + 1].first)
            maxi[i] = {arr[i], i};
        else
            maxi[i] = maxi[i + 1];
    }

    for(int i = 0; i < n - 1; ++i){
        if(maxi[i + 1].first > arr[i]){
            swap(arr[i], arr[maxi[i + 1].second]);
            break;
        }
        else if(i == n - 2){
            swap(arr[i], arr[i + 1]);
        }
    }

    for(int i = 0; i < n; ++i){
        cout << arr[i] << " ";
    }
    cout << "\n";

    return 0;
}