Language: C++

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

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
const int N = 1e6 + 7, M = 1000000007, inf = INT_MAX;

int freq[N];
struct mycomp {
	bool operator()(int a, int b)
	{
		if ( freq[a] != freq[b] ) return freq[a] > freq[b];
		else return a < b;
	}
};

void solve()
{
	int n;
	cin >> n;

	int arr[n], brr[n], res[n];
	lop(i, 0, n) cin >> arr[i];
	lop(i, 0, n) cin >> brr[i], freq[ brr[i] ]++;
	set<int, mycomp> st;
	lop(i, 0, N) if ( freq[i] ) st.insert(i);
	// cout << st.size() << endl;
	int i = 0;
	while ( i < n )
	{
		int x = arr[i];

		auto it = st.begin();
		int rem = -1;
		if ( *it == x  ) rem = x, st.erase(rem);
		// cout << x << " :: " << " " << st.size() << endl;
		if ( rem != -1 ) it = st.begin();
		if ( it == st.end() ) { cout << "No" << endl; return; }
		else
		{
			res[i] = *it;
			int val = *it;
			st.erase(val);
			freq[val]--;
			if (freq[val]) st.insert(val);
		}
		// cout << res[i] << " " << " " << rem << " ";
		// if ( rem != -1 ) cout << freq[rem]; cout << endl;
		if ( rem != -1 ) st.insert(rem);
		i++;
	}
	cout << "Yes" << endl;
	lop(i, 0, n) cout << res[i] << " "; cout << endl;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);   cin.tie(0);   cout.tie(0);

	int test = 1;
	// cin >> test;
	while (test--)solve();


}






















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 = long long int; _Val = long long int; _KeyOfValue = std::_Identity<long long int>; _Compare = mycomp; _Alloc = std::allocator<long long int>; _Const_Link_type = const std::_Rb_tree_node<long long 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 = long long int; _Val = long long int; _KeyOfValue = std::_Identity<long long int>; _Compare = mycomp; _Alloc = std::allocator<long long int>; key_type = long long int]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const long long int&; _Key = long long int; _Val = long long int; _KeyOfValue = std::_Identity<long long int>; _Compare = mycomp; _Alloc = std::allocator<long long int>]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   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>::insert(const value_type&) [with _Key = long long int; _Compare = mycomp; _Alloc = std::allocator<long long 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<long long int, long long int, std::_Identity<long long int>, mycomp, std::allocator<long long int> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<long long int>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::rebind<long long int>; typename _Alloc::value_type = long long int; value_type = long long int]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:26:39:   required from here
   26 |         lop(i, 0, N) if ( freq[i] ) st.insert(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 mycomp&, const long long int&, const long long int&>' evaluates to false