#include <iostream>
using namespace std;
int main() {
    int n;
    while (cin >> n) {
        int a[n];
        for (int i=0;i<n;i++) cin>>a[i];
        int max_num=0;
        int term1=a[0];
        int min_num=0;
        int term2=a[0];
        for (int i=0;i<n;i++)
        {           
            if(a[i]>term1)
            {
                term1=a[i];
                max_num=i;
            }
            if(a[i]<term2)
            {
                term2=a[i];
                min_num=i;
            }
        }
        a[max_num]=term2;
        a[min_num]=term1;
        for (int i=0;i<n;i++) cout<<a[i]<<" ";
    }
}