#include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, p, q; cin >> n >> p >> q; p--; q--; vector a(n), pos(n); for(int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } next_permutation(a.begin(), a.end()); for(int i = 0; i < n; i++) pos[a[i]] = i; if(pos[p] < pos[q]){ for(int x : a) cout << x << ' '; cout << endl; return 0; } set st; for(int i = n-1; i > pos[q]; i--) st.insert(a[i]); for(int i = pos[q]; i >= 0; i--){ st.insert(a[i]); auto ok = [&]() -> bool{ auto p = st.end(); p--; if((*p) == a[i]) return false; if(st.size() == 1) return false; if((*p) != q) return true; p--; if((*p) == a[i]) return false; return true; }; if(ok()){ vector v; for(int j = n-1; j >= i; j--) v.push_back(a[j]); sort(v.begin(), v.end()); int idx = -1; for(int j = 0; j < v.size(); j++) { if(v[j] == a[i]) idx = j; } int nx = [&]() -> int { if(v[idx+1] != q) return v[idx+1]; return v[idx+2]; }(); for(int j = 0; j < i; j++) cout << a[j]+1 << ' '; cout << nx+1 << ' '; if(p < q){ for(int x : v){ if(x != nx) cout << x+1 << ' '; } cout << endl; return 0; } bool f = false; for(int x : v){ if(x == nx) continue; if(x == p || x == q) continue; if(x > p && !f){ f = true; cout << p+1 << ' ' << q+1 << ' '; } cout << x+1 << ' '; } cout << endl; return 0; } } cout << -1 << endl; }