Problem:
Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N.
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

2 \leq N \leq 10^5

1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.

Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

6
3 1 2 6 3 4
2 2 8 3 4 3

Sample Output 3

Yes

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

#include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
#define For(i,A,B) for(i=A;i<=(B);++i)
using namespace std;
const int N=100050;
int a[N],b[N],ta[N],tb[N],x,y,n;
bool val[N],sum[N<<2];
struct node{
	int a,b;
}tmp[N];
struct cmp{
	inline bool operator ()(int x,int y)const{return tmp[x].a>tmp[y].a;}
};
priority_queue<int,vector<int>,cmp> Q;
set<int> S;
void build(int o,int L,int R){
	if(L==R)sum[o]=val[L];
	else{
		int lc=o<<1,rc=lc|1,M=L+R>>1;
		build(lc,L,M);build(rc,M+1,R);
		sum[o]=sum[lc]&&sum[rc];
	}
}
bool ask(int o,int L,int R){
	if(x<=L&&y>=R)return sum[o];
	else{
		int lc=o<<1,rc=lc|1,M=L+R>>1;
		bool ans=1;
		if(x<=M)ans=ans&&ask(lc,L,M);
		if(y>M)ans=ans&&ask(rc,M+1,R);
		return ans;
	}
}
inline bool check2(){
	int i;
	For(i,1,n){
		if(S.find(a[i])!=S.end())return 1;
		S.insert(a[i]);
	}
	return 0;
}
inline bool cmp2(node a,node b){return a.b<b.b||a.b==b.b&&a.a<b.a;}
inline bool check(){
	int i,x;
	For(i,1,n){tmp[i].a=a[i];tmp[i].b=b[i];}
	sort(tmp+1,tmp+n+1,cmp2);
	For(i,2,n)Q.push(i);
	For(i,1,n-1){
		for(x=Q.top(),Q.pop();x<=i;x=Q.top(),Q.pop());
		if(tmp[i].a<=tmp[x].a)return 1;
		swap(tmp[i].a,tmp[x].a);
		Q.push(x);
	}
	return 0;
}
int main(){
	int i;
	scanf("%d",&n);
	For(i,1,n){
		scanf("%d",a+i);
		ta[i]=a[i];
	}
	For(i,1,n){
		scanf("%d",b+i);
		tb[i]=b[i];
	}
	sort(ta+1,ta+n+1);sort(tb+1,tb+n+1);
	For(i,1,n)if(ta[i]>tb[i]){
		puts("No");
		return 0;
	}
	if(check2()||check()){
		puts("Yes");
		return 0;
	}
	For(i,2,n)val[i]=ta[i]<=tb[i-1];
	build(1,2,n);
	For(i,1,n)if(a[i]<=b[i])if((x=lower_bound(ta+1,ta+n+1,a[i])-ta)>=(y=lower_bound(tb+1,tb+n+1,b[i])-tb)){
		puts("Yes");
		return 0;
	}else{
		++x;
		if(ask(1,2,n)){
			puts("Yes");
			return 0;
		}
	}
	puts("No");
	return 0;
}