#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

int main()
{
    int N;
    scanf("%d", &N);

    int* s = new int[1000000];
    for (int i = 0; i < N; ++i)
         scanf("%d", s+i);

    pair<int, int>* a = new pair<int, int>[1000000];

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

    sort(a, a+N);

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

    if (j == -1 && i == N)
            swap(s[N-1], s[N-2]);

    for (i = 0; i < N; ++i)
        printf("%d ", s[i]);

    return 0;
}