Problem:
Score : 600 points

Problem Statement

Given are two sequences A and B, both of length N.
A and B are each sorted in the ascending order.
Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it.

Constraints

1\leq N \leq 2 \times 10^5

1\leq A_i,B_i \leq N

A and B are each sorted in the ascending order.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

Output

If there exist no reorderings that satisfy the condition, print No.

If there exists a reordering that satisfies the condition, print Yes on the first line.
After that, print a reordering of B on the second line, separating terms with a whitespace.

If there are multiple reorderings that satisfy the condition, you can print any of them.

Sample Input 1

6
1 1 1 2 2 3
1 1 1 2 2 3

Sample Output 1

Yes
2 2 3 1 1 1

Sample Input 2

3
1 1 2
1 1 3

Sample Output 2

No

Sample Input 3

4
1 1 2 3
1 2 3 3

Sample Output 3

Yes
3 3 1 2

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int n; scanf("%d", &n); std::vector<int> a(n), b(n);
	for(auto& v: a) scanf("%d", &v);
	for(auto& v: b) scanf("%d", &v);

	std::vector<int> A(2e5 + 1), B(2e5 + 1);
	for(auto v: a) A[v]++;
	for(auto v: b) B[v]++;

	for(int i = 0; i < n; i++) {
		if(n - A[i] >= B[i]) continue;
		printf("No\n");
		return 0;
	}

	auto solverX = [&]() -> std::vector<int> {
		using P = std::pair<int, int>;
		std::priority_queue<P, std::vector<P>, std::greater<P>> qu;
		for(int i = 0; i < n; i++) {
			if(!A[i]) continue;
			qu.push({A[i], i});
		}
		
		std::vector<std::pair<int, int>> vec;
		for(int i = 0; i < n; i++) {
			if(!B[i]) continue;
			vec.push_back({n - A[i] - B[i], i});
		}
		sort(begin(vec), end(vec));

		std::vector<std::vector<int>> ans(2e5 + 1);
		for(auto [_, i]: vec) {
			P p{-1, -1};
			while(B[i] and !qu.empty()) {
				auto [_, idx] = qu.top(); qu.pop();
				if(idx == i) {
					p = P{_, idx};
					continue;
				}
				while(A[idx] and B[i]) {
					ans[idx].push_back(i);
					A[idx]--;
					B[i]--;
				}
				if(A[idx]) qu.push({A[idx], idx});
			}
			if(p.first != -1) qu.push(p);
		}
		for(auto v: B) {
			if(!v) continue;
			return {};
		}

		std::vector<int> ret;
		for(int i = 0; i < ans.size(); i++) {
			for(auto v: ans[i]) ret.push_back(v);
		}
		return ret;
	};
	auto X = solverX();
	if(!X.empty()) {
		printf("Yes\n");
		for(auto v: X) printf("%d ", v);
		printf("\n");
		return 0;
	}

	A.assign(2e5 + 1, 0);
	B.assign(2e5 + 1, 0);
	for(auto v: a) A[v]++;
	for(auto v: b) B[v]++;
	auto solverY = [&]() -> std::vector<int> {
		using P = std::pair<int, int>;
		std::priority_queue<P, std::vector<P>, std::greater<P>> qu;
		for(int i = 0; i < n; i++) {
			if(!A[i]) continue;
			qu.push({A[i], i});
		}
		
		std::vector<std::pair<int, int>> vec;
		for(int i = 0; i < n; i++) {
			if(!B[i]) continue;
			vec.push_back({B[i], i});
		}
		sort(begin(vec), end(vec));

		std::vector<std::vector<int>> ans(2e5 + 1);
		for(auto [_, i]: vec) {
			P p{-1, -1};
			while(B[i] and !qu.empty()) {
				auto [_, idx] = qu.top(); qu.pop();
				if(idx == i) {
					p = P{_, idx};
					continue;
				}
				while(A[idx] and B[i]) {
					ans[idx].push_back(i);
					A[idx]--;
					B[i]--;
				}
				if(A[idx]) qu.push({A[idx], idx});
			}
			if(p.first != -1) qu.push(p);
		}
		for(auto v: B) {
			if(!v) continue;
			return {};
		}

		std::vector<int> ret;
		for(int i = 0; i < ans.size(); i++) {
			for(auto v: ans[i]) ret.push_back(v);
		}
		return ret;
	};

	X = solverY();
	if(!X.empty()) {
		printf("Yes\n");
		for(auto v: X) printf("%d ", v);
		printf("\n");
		return 0;
	}
	
	A.assign(2e5 + 1, 0);
	B.assign(2e5 + 1, 0);
	for(auto v: a) A[v]++;
	for(auto v: b) B[v]++;
	auto solverZ = [&]() -> std::vector<int> {
		std::vector<int> ret(b);
		reverse(begin(ret), end(ret));
		for(int i = 0; i < n; i++) {
			if(a[i] != ret[i]) continue;
			return {};
		}
		return ret;
	};
	
	X = solverZ();
	if(!X.empty()) {
		printf("Yes\n");
		for(auto v: X) printf("%d ", v);
		printf("\n");
		return 0;
	}

	std::random_device seed_gen;
	std::mt19937 engine(seed_gen());
	auto solverW = [&]() -> std::vector<int> {
		std::vector<int> ret(b);
		std::shuffle(begin(ret), end(ret), engine);

		std::vector<int> T;
		for(int i = 0; i < n; i++) if(a[i] == ret[i]) T.push_back(i);
		for(int k = 0; k < 10000; k++) {
			if(T.empty()) return ret;
			
			int x = T.back();
			int y = engine() % n;
			if(a[x] != ret[y] and a[y] != ret[x]) {
				std::swap(ret[x], ret[y]);
				T.pop_back();
			}
		}

		return ret;
	};

	for(int i = 0; i < 100; i++) {
		X = solverW();
		if(X.empty()) continue;

		printf("Yes\n");
		for(auto v: X) printf("%d ", v);
		printf("\n");
		return 0;
	}

	printf("No\n");
	return 0;
}