Language: C++

#include <bits/stdc++.h>

using namespace std;

int cnt[200005];

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n,t;
	vector <int> a,b;
	vector <pair<int,int>> v;
	cin >> n;

	for(int i=0;i<n;i++)
	{
		cin >> t;
		a.push_back(t);
	}

	for(int i=0;i<n;i++)
	{
		cin >> t;
		b.push_back(t);
		cnt[t]++;
	}

	for(int i=1;i<=n;i++)
	{
		if(cnt[i] > 0)
		{
			v.push_back(make_pair(cnt[i],i));
		}
	}

	sort(v.rbegin(),v.rend());

	vector <int> res;
	int idx = 0;
	for(int i=0;i<v.size();i++)
	{
		int num = v[i].first;
		int val = v[i].second;
		if(val==a[idx])
		{
			if(i==v.size()-1)
			{
				break;
			}
			int temp1 = v[i+1].first;
			v[i+1].first = v[i].first;
			v[i].first = temp1;
			int temp2 = v[i+1].second;
			v[i+1].second = v[i].second;
			v[i].second = temp2;
		}
		num = v[i].first;
		val = v[i].second;
		for(int j=idx;j<idx+num;j++)
		{
			res.push_back(val);
		}
		idx += num;
	}

	sort(v.begin(),v.end());

	vector <int> res2;
	idx = 0;
	for(int i=0;i<v.size();i++)
	{
		int num = v[i].first;
		int val = v[i].second;
		if(val==a[idx])
		{
			if(i==v.size()-1)
			{
				break;
			}
			int temp1 = v[i+1].first;
			v[i+1].first = v[i].first;
			v[i].first = temp1;
			int temp2 = v[i+1].second;
			v[i+1].second = v[i].second;
			v[i].second = temp2;
		}
		num = v[i].first;
		val = v[i].second;
		for(int j=idx;j<idx+num;j++)
		{
			res2.push_back(val);
		}
		idx += num;
	}

	vector <int> res3;
	int idx = 0;
	for(int i=0;i<v.size();i++)
	{
		int num = v[i].first;
		int val = v[i].second;
		if(val==a[idx])
		{
			if(i==v.size()-1)
			{
				break;
			}
			int temp1 = v[v.size()-1].first;
			v[v.size()-1].first = v[i].first;
			v[i].first = temp1;
			int temp2 = v[v.size()-1].second;
			v[v.size()-1].second = v[i].second;
			v[i].second = temp2;
		}
		num = v[i].first;
		val = v[i].second;
		for(int j=idx;j<idx+num;j++)
		{
			res3.push_back(val);
		}
		idx += num;
	}

	sort(v.begin(),v.end());

	vector <int> res4;
	idx = 0;
	for(int i=0;i<v.size();i++)
	{
		int num = v[i].first;
		int val = v[i].second;
		if(val==a[idx])
		{
			if(i==v.size()-1)
			{
				break;
			}
			int temp1 = v[v.size()-1].first;
			v[v.size()-1].first = v[i].first;
			v[i].first = temp1;
			int temp2 = v[v.size()-1].second;
			v[v.size()-1].second = v[i].second;
			v[i].second = temp2;
		}
		num = v[i].first;
		val = v[i].second;
		for(int j=idx;j<idx+num;j++)
		{
			res4.push_back(val);
		}
		idx += num;
	}


	if(res.size() == n)
	{
		cout << "Yes" << '\n';
		for(int i=0;i<res.size();i++)
		{
			cout << res[i] << ' ';
		}
		cout << '\n';
	}
	else if(res2.size() == n)
	{
		cout << "Yes" << '\n';
		for(int i=0;i<res2.size();i++)
		{
			cout << res2[i] << ' ';
		}
		cout << '\n';		
	}
	else if(res3.size() == n)
	{
		cout << "Yes" << '\n';
		for(int i=0;i<res3.size();i++)
		{
			cout << res3[i] << ' ';
		}
		cout << '\n';		
	}	
	else if(res4.size() == n)
	{
		cout << "Yes" << '\n';
		for(int i=0;i<res4.size();i++)
		{
			cout << res4[i] << ' ';
		}
		cout << '\n';		
	}
	else
	{
		cout << "No" << '\n';
	}
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:99:13: error: redeclaration of 'int idx'
   99 |         int idx = 0;
      |             ^~~
a.cc:41:13: note: 'int idx' previously declared here
   41 |         int idx = 0;
      |             ^~~