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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	
	ll n;
	cin >> n;
	ll a[n], b[n];
	P pa[n], pb[n];
	zep(i, 0, n){cin >> a[i]; pa[i] = P(a[i], i);}
	zep(i, 0, n){cin >> b[i]; pb[i] = P(b[i], i);}
	
	bool ans = true;
	ll cnt = 0;
	zep(i, 0, n){
		if(a[i] > b[i]){
			cnt++;
		}
	}
	if(cnt > n-2){
		ans = false;
	}
	
	sort(pa, pa+n);
	sort(pb, pb+n);
	zep(i, 0, n){
		if(pa[i].first > pb[i].first){
			ans = false;
		}
	}
	
	if(ans){
		ans = false;
	
		ll rpa[n], rpb[n];
		zep(i, 0, n){
			rpa[pa[i].second] = i;
			rpb[pb[i].second] = i;
		}
		/*rrep(i, n-2, 0){
			if(pa[i].first == pa[i+1].first){
				rpa[pa[i].second] = rpa[pa[i+1].second];
			}
		}
		zep(i, 1, n){
			if(pb[i].first == pb[i-1].first){
				rpb[pb[i].second] = rpb[pb[i-1].second];
			}
		}*/
		
		ll cum[n];
		memset(cum, 0, sizeof(cum));
		zep(i, 1, n){
			cum[i] += cum[i-1];
			if(pa[i].first <= pb[i-1].first){
				cum[i] += 1;
			}
		}
		
		zep(i, 0, n){
			//cout << rpa[i] << " " << rpb[i] << endl;
			if(a[i] <= b[i] && (rpa[i] > rpb[i] || cum[rpb[i]]-cum[rpa[i]] == rpb[i]-rpa[i])){
				ans = true;
			}
		}
	}
	print((ans)? "Yes" : "No")
	return 0;
}