#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;

bool sortfunc (const pair<int,long> &a,const pair<int,long> &b)
{
    return (a.first>b.first);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long n;
cin >> n;
int *br=(int*)calloc(n,sizeof(int));
vector< pair <int,long> > a,b;
for (long i=0;i<n;i++)
{
    int x;
    cin >> x;
    a.push_back({x,i});
    b.push_back({x,i});
}
sort(a.begin(),a.end(),sortfunc);

long brojac=0;
int op =0;

for (int i=0;i<n;i++)
{
    if (b[i].first < a[brojac].first)
    {
        int pom;
        pom = b[i].first;
        b[i].first = b[a[brojac].second].first;
        b[a[brojac].second].first=pom;

         pom = b[i].second;
        b[i].second = b[a[brojac].second].second;
        b[a[brojac].second].second=pom;
       op=1;
        break;
    }
    br[i]=1;
    while (br[brojac]==1)
    {
        if (br[brojac]==1)
        brojac ++;
    }
}
if (!op)
{
     int pom;
        pom = b[n-2].first;
        b[n-2].first = b[n-1].first;
        b[n-1].first=pom;

         pom = b[n-2].second;
        b[n-2].second = b[n-1].second;
        b[n-1].second=pom;
}


for (int i=0;i<n;i++)
{
    cout << b[i].first << " ";
}


return 0;
}