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;

int n, ds[100100];
vector<pair<int, int>> a, b;

int get(int x)
{
  return x == ds[x] ? x : ds[x] = get(ds[x]);
}

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
  {
    int x;
    cin >> x;
    a.push_back({x, i});
    ds[i] = i;
  }
  for (int i = 0; i < n; i++)
  {
    int x;
    cin >> x;
    b.push_back({x, i});
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  int swap = 0;
  vector<int> whereA(n), whereB(n);
  for (int i = 0; i < n; i++)
  {
    if (a[i].first > b[i].first)
    {
      cout << "No" << endl;
      return 0;
    }
    whereA[a[i].second] = whereB[b[i].second] = i;

    int x = get(a[i].second), y = get(b[i].second);
    if (x != y)
    {
      swap++;
      ds[x] = y;
    }
  }

  if (swap <= n - 2)
  {
    cout << "Yes" << endl;
    return 0;
  }

  set<int> goodValues;
  for (int i = 0; i < n; i++)
  {
    int idA = a[i].second, idB = b[i].second;
    assert(idA != idB);
    if (a[idB].first <= b[i].first && a[i].first <= b[idB].first)
    {
      cout << "Yes" << endl;
      return 0;
    }
    if (a[i].first <= b[idA].first && a[idA].first <= b[i].first)
    {
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;
}