#include <bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
#define ll long long
#define s second
#define f first

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 mt(seed);

const int MAX_N=10e6+5;
int t,n,a[MAX_N],b[MAX_N],c[MAX_N];
double mx=0,start_time,end_time,duration;

double GetTime(){
  return (double)clock()/CLOCKS_PER_SEC;
}

void Start(){
    start_time=GetTime();
}

void Stop(){
    end_time=GetTime();
    duration=end_time-start_time;
}

void Print(){
 for(int i=0;i<n;++i){
    cout << a[i] << " ";
 }
 cout << endl;
}

bool Compare(){
 for(int i=0;i<n;++i){
    if(b[i]==a[i]){continue;}
    if(a[i]>b[i]){return false;}
    if(a[i]<b[i]){return true;}
 }
 return false;
}

bool cmp(int a,int b){
 return a>b;
}

void MakeTestCase(){
  n=1000000;
  for(int i=0;i<n;++i){
    a[i]=mt()%10;
    b[i]=a[i];
  }
}

void Test(){
   for(int i=0;i<n;++i){
    for(int j=i+1;j<n;++j){
        swap(b[i],b[j]);
        if(Compare()){
            cout << "):";
            system("pause");
        }
        swap(b[i],b[j]);
     }
   }
   cout << "(:" << endl;
}

void Solve(){
  bool issorted=is_sorted(a,a+n,cmp);
  if(issorted){
     for(int i=0;i<n-1;++i){
        if(a[i]==a[i+1]){
            Print();
            return;
        }
     }
     swap(a[n-2],a[n-1]);
     Print();
     return;
  }
  c[n-1]=-1;
  int mx=a[n-1],idx=n-1,lst=-1;
  for(int i=n-2;i>=0;--i){
    if(mx>a[i]){c[i]=idx;lst=i;}
    if(mx<a[i]){mx=a[i];idx=i;c[i]=-1;}
    if(mx==a[i]){c[i]=-1;}
  }
  swap(a[lst],a[c[lst]]);
  Print();
}

int main(){
  cin >> n;
  for(int i=0;i<n;++i){
    cin >> a[i];
  }
  Solve();
  return 0;
}