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

inline int* largestNum(int* num_in_str, int n)
{
    int max_digit = -1;
    int max_digit_indx = -1;
    int l_indx = -1;
    int r_indx = -1;
    for (int i = n - 1; i >= 0; i--) {
        if (num_in_str[i] > max_digit) {
            max_digit = num_in_str[i];
            max_digit_indx = i;
            continue;
        }
        if (num_in_str[i] < max_digit) {
            l_indx = i;
            r_indx = max_digit_indx;
            //break;
        }
    }

    // check for is number already in order 
    if (l_indx == -1)
    {
        int i, j;
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                if(num_in_str[i] == num_in_str[j] && i != j) return num_in_str;
            }
        }
        swap(num_in_str[n - 1], num_in_str[n - 2]);
        return num_in_str;
    }

    swap(num_in_str[l_indx], num_in_str[r_indx]);

    return num_in_str;
}

// driver function 
int main()
{
    int n;
    cin >> n;
    int* num = (int*)malloc(n * sizeof(int));
    for(int i = 0; i < n; i++) cin >> num[i];
    int* num1 = largestNum(num, n);
    for(int i  = 0; i < n; i++) cout << num1[i] << " ";
    cout << endl;
    return 0;
}