#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    int n,j,q;std::cin >> n;
    bool waschanged = false;
    std::vector <int> v(n);
    std::vector <int> t;
    for(auto &i:v) std::cin >> i;
    t = v;
    std::sort(t.begin(),t.end(),[](int n,int n1){return n > n1;});
    for (int i = 0;i < int(t.size());++i){
        if(t[i] != v[i]){
            waschanged = true;
            j = std::distance(v.begin(),std::find(v.begin(),v.end(),t[i]));
            q = v[j];
            v[j] = v[i];
            v[i] = q;
            break;
        }
    }
    if(waschanged){
       for(auto i:v) std::cout << i << " ";
    }else{
    j = v.size();
    q = v[j-1];
    v[j-1] = v[j-2];
    v[j-2] = q;
        for(auto i:v) std::cout << i << " ";
    }
    return 0;
}