//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} #define N 200100 //head int n, p, q; int a[N], pl[N], _[N]; bitset check; void print() { rep(i, n) cout << a[i]+1 << (i == n-1?'\n':' '); return; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> p >> q; rep(i, n) cin >> a[i]; rep(i, n) a[i]--; if(!next_permutation(a, a+n)) { cout << -1 << endl; return 0; } rep(i, n) pl[a[i]] = i; if(pl[--p] < pl[--q]) { print(); return 0; } int u = -1; for(int i = q+1; i < n; i++) { if(pl[q] < pl[i]) { u = i; break; } } if(u == -1) { for(int i = pl[q]-1; i >= 0; i--) { if(i != pl[q]-1 and a[i] > a[i+1]) break; check.set(a[i]); for(int j = a[i]+1; j < n and !check.test(j); j++) { check.set(j); if(pl[j] > pl[q]) { swap(a[pl[j]], a[i]); sort(a+i+1, a+n); goto OUT; } } } copy(a, a+n, _); if(!next_permutation(a, a+pl[q])) { prev_permutation(a, a+pl[q]); for(int i = pl[q]-1; i >= 0; i--) { for(int j = a[i]+1; j < (i == 0?n:a[i-1]); j++) { if(j == q) continue; swap(a[i], a[pl[j]]); sort(a+i+1, a+n); goto OUT; } } cout << -1 << endl; return 0; } int z = -1; rep(i, pl[q]) if(_[i] != a[i]) { z = i; break; } sort(a+z+1, a+n); print(); return 0; } swap(a[pl[q]], a[pl[u]]); sort(a+pl[q]+1, a+n); OUT:; rep(i, n) pl[a[i]] = i; if(pl[p] < pl[q]) { print(); return 0; } rotate(a+pl[q], a+pl[q]+1, a+pl[p]+1); print(); }