#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int N;
	cin >> N;
	vector<int> v;
	int max_broj = -1, max_broj_index = -1; 
    int levi_index = -1, desni_index = -1; 
    int temp;
	for(int i = 0; i < N; i++){
		cin >> temp;
		v.push_back(temp);
	}
	
	for(int i = N-1; i >= 0; i--){
		if(v[i] > max_broj){
			max_broj = v[i];
			max_broj_index = i;
			continue;
		}
		if (v[i] < max_broj) { 
            levi_index = i; 
            desni_index = max_broj_index; 
        }
	}
	if(levi_index == -1){
		swap(v[N-1], v[N-2]);
	}
	swap(v[levi_index], v[desni_index]);
	for(int i : v){
		cout << i << " ";
	}
	
	return 0;
}