#include <bits/stdc++.h>
using namespace std;
    int n,niz[1000008],niz2[1000008],x,y;
bool cmp(int a,int b){
if(a>b)return true;
else return false;
}
int main(){
    cin >> n;
    for(int i=0;i<n;i++){
         cin>>niz[i];
         niz2[i]=niz[i];
    }
    sort(niz2,niz2+n,cmp);
    for(int i=0;i<n-2;i++){
         if (niz[i]!=niz2[i]){cout<<niz2[i]<<" ";
                              x=niz[i];y=niz2[i];
                              i++;
                              while(i<n){
                              if(niz[i]!=y)cout<<niz[i]<<" ";
                              else cout<<x<<" ";i++;}

                              return 0;

         }
         else cout<<niz[i]<<" ";
    }
    cout<<niz[n-1]<<" "<<niz[n-2];

return 0;

}