#include <iostream>
#include <set>
#include <iterator>
using namespace std;

#define ll long long
#define loop(i,a,b) for(int i = a; i < b; i++)
#define loop_n(i,a,b) for(int i = a; i > b; i--)

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	ll N;
	cin >> N;

	int arr[N];
	multiset<int> s;
	
	bool copies=false;
	int prev;

    //unos niza i seta
	loop(i, 0, N){
		int temp;
		cin >> temp;

        if(i==0 || prev!=temp) prev=temp;
        else{
            copies=true;
        }

		arr[i]=temp;
		s.insert(temp);
	} //arr: 8 2 5, set: 2 5 8

	

    ll index = 0; //arr: 8 2 5
	set<int>::reverse_iterator it = s.rbegin(); //set: 8 5 2
	
	int temp; bool found = false;
	
    
	
	while(it != s.rend() || index<N){ 
	    
	    if(arr[index]==*it){
	      it++;
	      index++;
	      continue;  
	    } 
	    
	    // arr.index->2, set.iterator->5
	    
	    found = true;
	    
	    temp = arr[index]; //2
	    arr[index] = *it; //5
	    
	    break;
	}
	
	
	
	if(found){
	    loop_n(i, N, index){ //od kraja
            if(arr[i]==*it){ //if arr[3] = 5 jeste set.iterator=5
                arr[i] = temp; //arr[3] = 2
                break;
            }
        }
	}else if(!found && !copies){
	    int temp2 = arr[N-1];
	    arr[N-1] = arr[N-2];
	    arr[N-2] = temp2;
	}
	
	

    loop(i, 0, N){
        cout << arr[i];
        if(i<N-1) cout << " ";
    }

	return 0;
}