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

#include<bits/stdc++.h>

#define INF 0x7fffffff

const int N=700010;

using namespace std;

 

inline int read(){

	char ch=getchar();int x=0,f=1;	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

	return x*f;

} 

 

struct edge{

	int v,nxt,w;

}e[N*20];

int head[N<<4],cnt;

long long inf;

int n,m,s;

 

void add(int u,int v,int w){

	e[++cnt].v=v;

	e[cnt].w=w;

	e[cnt].nxt=head[u];

	head[u]=cnt;

}

 

int tin[N<<4],tout[N<<4],temp;

long long dis[N<<4];

 

void build(int k,int l,int r){

	if(l==r){

		tin[k]=tout[k]=l;

		return ;

	}

	int mid=(l+r)>>1;

	build(k<<1,l,mid);

	build(k<<1|1,mid+1,r);

	tin[k]=++temp;tout[k]=++temp;

	add(tout[k<<1],tout[k],0);

	add(tout[k<<1|1],tout[k],0);

	add(tin[k],tin[k<<1],0);

	add(tin[k],tin[k<<1|1],0);

}

 

void update1(int k,int l,int r,int L,int R,int u,int w){

	if(L<=l&&r<=R){

		add(u,tin[k],w);

		return ;

	}

	int mid=(l+r)>>1;

	if(mid>=L){

		update1(k<<1,l,mid,L,R,u,w);

	}

	if(mid<R){

		update1(k<<1|1,mid+1,r,L,R,u,w);

	}

}

 

void update2(int k,int l,int r,int L,int R,int u,int w){

		if(L<=l&&r<=R){

		add(tout[k],u,w);

		return ;

	}

	int mid=(l+r)>>1;

	if(mid>=L){

		update2(k<<1,l,mid,L,R,u,w);

	}

	if(mid<R){

		update2(k<<1|1,mid+1,r,L,R,u,w);

	}

}

 

struct node{

	int u;

	long long d;

	bool operator<(const node&h)const{return d>h.d;};

};

 

void dij(){

	memset(dis,0x3f,sizeof(dis));

	inf=dis[1];

	priority_queue<node> q;

	dis[s]=0;

	q.push((node){s,0});

	while(!q.empty()){

		node now=q.top();

		int u=now.u;

		long long d=now.d;

		q.pop();

		if(d!=dis[u])continue;

		for(int i=head[u];i;i=e[i].nxt){

			int v=e[i].v,w=e[i].w;

			if(dis[v]>dis[u]+w){

				dis[v]=dis[u]+w;

				q.push((node){v,dis[v]});

			}

		}

	}

}

 

int main(){

	n=read(),m=read(),s=1;

	temp=n+m; 

	build(1,1,n+m);

	for(int i=1;i<=m;i++){

		int l=read(),r=read(),w=read();

		update1(1,1,n+m,l,r,n+i,0);

		update2(1,1,n+m,l,r,n+i,w);

	}

	dij(); 

	if(dis[n]!=inf){

		printf("%I64d ",dis[n]);

	}

	else printf("-1 ");

	return 0;

}