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: Runtime Error)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<climits>
using namespace std;

#define int long long

const int CN = 2e5 + 5;

int read(){
    int s = 0,ne = 1; char c = getchar();
    while(c < '0' || c > '9') ne = c == '-' ? -1 : 1, c = getchar();
    while(c >= '0' && c <= '9') s = (s << 1) + (s << 3) + c - '0', c = getchar();
    return s * ne;
}

struct Splay{
    #define ls ch[o][0]
    #define rs ch[o][1]
    int val[CN], fa[CN], ch[CN][2], cnt[CN], sz[CN], rt, tot;
    inline int get(int o){ return o == ch[fa[o]][1]; }
    inline void maintain(int o){ sz[o] = sz[ls] + sz[rs] + cnt[o]; }
    inline void make(int f, int v){ val[++tot] = v;cnt[tot] = sz[tot] = 1;fa[tot] = f;if(f) ch[f][v > val[f]] = tot; maintain(f); }
    void rotate(int o){
        int p = fa[o], gp = fa[fa[o]], chk = get(o);
        ch[p][chk] = ch[o][chk^1]; fa[ch[o][chk^1]] = p;
        ch[o][chk^1] = p;fa[p] = o;
        fa[o] = gp; if(gp) ch[gp][p == ch[gp][1]] = o;
        maintain(p);maintain(o);
    }
    void splay(int o, int tgp = 0){
        for(int f = fa[o];f = fa[o], f != tgp;rotate(o))  if(fa[f] != tgp) rotate(get(f) == get(o) ? f : o);
        if(tgp == 0) rt = o;
    }
    void ins(int o, int f, int v){
        if(!o) return (void)(make(f, v), rt = tot);
        if(val[o] == v) return (void)(sz[o] ++, cnt[o]++, rt = o);
        ins(v < val[o] ? ls : rs, o, v); maintain(o);
    }
    void ins(int v){ ins(rt, 0, v);splay(rt); }
    int rank(int o, int v){  return (v == val[o] ? (rt = o, sz[ls] + 1) : (v < val[o] ? rank(ls, v) : rank(rs, v) + sz[ls] + cnt[o])); }
    int rank(int v){ ins(v);  int t = rank(rt, v); splay(rt);erase(v);return t - 1; }
    int select(int o, int v){ return (sz[ls] + 1 <= v && v <= sz[ls] + cnt[o] ? rt = o, val[o] : (v <= sz[ls] ? select(ls, v) : select(rs, v - sz[ls] - cnt[o]))) ;}
    int select(int v){ int t = select(rt, v + 1);splay(rt);return t; }
    int succ(int v){ ins(v);int o = ch[rt][1];while(ls)o = ls; splay(o); int t = val[rt]; erase(v); return t; }
    int pred(int v){ ins(v);int o = ch[rt][0];while(rs)o = rs; splay(o); int t = val[rt]; erase(v); return t; }
    void erase(int L, int R){
        int pre, suc;
        rank(rt, L); splay(rt); pre = ch[rt][0]; while(ch[pre][1])pre = ch[pre][1];
        rank(rt, R); splay(rt); suc = ch[rt][1]; while(ch[suc][0])suc = ch[suc][0];
        splay(pre);  splay(suc, rt);
		int &tgt = ch[ch[rt][1]][0]; 
		if(cnt[tgt] > 1) cnt[tgt]--, sz[tgt]--;
		else tgt = 0;
		maintain(ch[rt][1]);
		maintain(rt);
    }
    void erase (int v){ erase(v, v); }
    Splay() {ins(LLONG_MAX) ;ins(LLONG_MIN); } 
} t;

int n, a[CN], b[CN]; int cnt[CN];

set<int, greater<int> > S; 

bool RECK(){
    for(int i = 1; i <= n; i++){
        int now = a[i];
        int k = t.succ(now);
        if(k != LLONG_MAX) b[i] = k, t.erase(k);
        else {
            k = t.pred(now);
            if(k != LLONG_MIN) b[i] = k, t.erase(k);
            else return false;
        }
    }
    return true;
}

signed main()
{
    // freopen("_in.in", "r", stdin);

    n = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    for(int i = 1; i <= n; i++) b[i] = read(), S.insert(b[i]), t.ins(b[i]), cnt[ b[i] ]++;

    bool flag = true;
    for(int i = 1; i <= n && flag; i++){
        bool has = false;
        for(set<int> :: iterator it = S.begin(); it != S.end(); it++){
            int val = *it;
            if(val ^ a[i]){
                b[i] = val, has = true, cnt[val]--; 
                if(!cnt[val]) S.erase(it); break;
            }
        }
        flag &= has;
    }

    if(!flag && !RECK()) puts("No");
    else{
        puts("Yes");
        for(int i = 1; i <= n; i++) printf("%d ", b[i]);
    }
}