#include #include #include #include void fail() { std::cout << -1 << std::endl; std::exit(0); } void answer(const std::vector& xs) { for (auto x : xs) std::cout << x << " "; std::cout << std::endl; std::exit(0); } void answer(const std::vector& fs, const std::vector& bs) { for (auto x : fs) std::cout << x << " "; for (auto x : bs) std::cout << x << " "; std::cout << std::endl; std::exit(0); } int pos(const std::vector& xs, int x) { return std::find(xs.begin(), xs.end(), x) - xs.begin(); } void solve() { int n, p, q; std::cin >> n >> p >> q; std::vector xs(n); for (auto& x : xs) std::cin >> x; if (!std::next_permutation(xs.begin(), xs.end())) fail(); if (pos(xs, p) < pos(xs, q)) answer(xs); int qi = pos(xs, q); std::vector fs(xs.begin(), xs.begin() + qi), bs(xs.begin() + qi, xs.end()); int m = bs.size(); if (q > p) { int min = n + 1; for (int i = 0; i < m; ++i) { if (bs[i] > q) min = std::min(min, bs[i]); } if (min > n) { if (!std::next_permutation(fs.begin(), fs.end())) fail(); std::sort(bs.begin(), bs.end()); answer(fs, bs); } auto ri = pos(bs, min); std::swap(bs.front(), bs[ri]); std::sort(bs.begin() + 1, bs.end()); answer(fs, bs); } else { int min = n + 1; for (int i = 0; i < m; ++i) { if (bs[i] > q) min = std::min(min, bs[i]); } assert(min <= n); auto ri = pos(bs, min); std::swap(bs.front(), bs[ri]); std::sort(bs.begin() + 1, bs.end()); if (pos(bs, p) < pos(bs, q)) answer(fs, bs); auto pi = pos(bs, p), qi = pos(bs, q); while (pi > qi) { std::swap(bs[qi], bs[qi + 1]); ++qi; } answer(fs, bs); } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }