Language: C++

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define mset(a, b) memset(a, b, sizeof(a))
#define DBG(x) cout << "[" << #x << "]: " << x << endl
using ll = long long;
using pii = pair<int, int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif

template<class Ty> Ty randint(Ty a, Ty b) { return uniform_int_distribution<Ty>(a, b)(rng); }
template<class num> inline void rd(num& x) {
	char c, neg = 0; while(isspace(c = getchar_unlocked()));
	if(!isdigit(c)) neg = (c == '-'), x = 0;
	else x = c - '0';
	while(isdigit(c = getchar_unlocked())) x = (x << 3) + (x << 1) + c - '0';
	x = neg ? -x : x; }
template <class... Args> inline void rd(Args&... args) { (rd(args), ...); }

const int MAXN = 2e5 + 5, INF = 0x3f3f3f3f;

int cntA[MAXN];
int cntB[MAXN];

struct CMP {
	bool operator() (pii const& a, pii const& b) {
		return mk(a.se, a.fi) > mk(b.se, b.fi);
	}
};

int main() {
	int n; rd(n);
	vector<int> a(n);
	for (int& i : a) rd(i);
	vector<int> b(n);
	for (int& i : b) rd(i);

	set<pii, CMP> A, B;
	for (int i : a) cntA[i]++;
	for (int i : b) cntB[i]++;
	for (int i = 1; i <= n; i++) if (cntA[i]) A.emplace(i, cntA[i]);
	for (int i = 1; i <= n; i++) if (cntB[i]) B.emplace(i, cntB[i]);

	vector<vector<int>> edges(n+1);

	for (auto& pa : A) {
		int el = pa.fi, cn = pa.se;
		for (int i = 0; i < cn; i++) {
			auto it = prev(B.end());
			while(it != B.begin() && it->fi == el) it = prev(it);

			if (it->fi == el) {
				puts("No");
				return 0;
			}


			edges[el].push_back(it->fi);
			pii newValue = *it;
			B.erase(it);
			if (newValue.second > 1) {
				newValue.second--;
				B.insert(newValue);
			}
		}
	}

	bool ok = true;
	for (int i = 0; i <= n; i++) {
		ok &= (int) edges[i].size() == cntA[i];
	}

	if (!ok) {
		puts("No");
	}
	else {
		puts("Yes");
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j < cntA[i]; j++) {
				printf("%d ", edges[i][j]);
			}
		}
		puts("");
	}


}

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = CMP; _Alloc = std::allocator<std::pair<int, int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = CMP; _Alloc = std::allocator<std::pair<int, int> >; key_type = std::pair<int, int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2433:15:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_unique(_Args&& ...) [with _Args = {int&, int&}; _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = CMP; _Alloc = std::allocator<std::pair<int, int> >]'
 2433 |         auto __res = _M_get_insert_unique_pos(__z._M_key());
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:465:33:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::emplace(_Args&& ...) [with _Args = {int&, int&}; _Key = std::pair<int, int>; _Compare = CMP; _Alloc = std::allocator<std::pair<int, int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, CMP, std::allocator<std::pair<int, int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::rebind<std::pair<int, int> >; typename _Alloc::value_type = std::pair<int, int>]'
  465 |         { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
      |                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:48:53:   required from here
   48 |         for (int i = 1; i <= n; i++) if (cntA[i]) A.emplace(i, cntA[i]);
      |                                                   ~~~~~~~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const CMP&, const std::pair<int, int>&, const std::pair<int, int>&>' evaluates to false