#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

int ti;

#define mid ((l+r)>>1)
const int maxn=200100;
const int INF=1e9;
void chkmin(int &x,int y){
	x=x<y?x:y;
}
int Min(int x,int y){
	return x<y?x:y;
}
struct node{
	int l,r;
}b[maxn];
bool cmp(const node &A,const node B){
	return A.r<B.r;
}
int n,m;
int a[maxn],cnt[maxn];
int f[maxn],g[maxn];
int tr[maxn<<2];
void Build_tree(int h,int l,int r){
	tr[h]=INF;
	if(l==r)return;
	Build_tree(h<<1,l,mid);
	Build_tree(h<<1|1,mid+1,r);
}
int Query(int h,int l,int r,int p){
	if(l==r) return tr[h];
	if(p<=mid)
		return Min(Query(h<<1,l,mid,p),tr[h]);
	else
		return Min(Query(h<<1|1,mid+1,r,p),tr[h]);
}
void updata(int h,int l,int r,int s,int t,int v){
	if(s<=l&&r<=t){
		chkmin(tr[h],v);
		return;
	}
	if(t<=mid)
		updata(h<<1,l,mid,s,t,v);
	else if(s>mid)
		updata(h<<1|1,mid+1,r,s,t,v);
	else{
		updata(h<<1,l,mid,s,mid,v);
		updata(h<<1|1,mid+1,r,mid+1,t,v);
	}
}
void solve(){
	for(int i=1;i<=n;i++)
		f[i]=g[i]=INF;
	for(int i=1;i<=m;i++)
		f[i]=g[i]=INF;
	Build_tree(1,1,n);
	int ps=1;
	for(int i=1;i<=m;i++){
		while(ps<=b[i].r){
			chkmin(g[ps],g[ps-1]+a[ps]);
			ps++;
		}
		ti=ps-1;
		f[i]=Query(1,1,n,b[i].l)+cnt[b[i].r];
		chkmin(f[i],g[b[i].l-1]+cnt[b[i].r]-cnt[b[i].l-1]);
		updata(1,1,n,b[i].l,b[i].r,f[i]-cnt[b[i].r]);

		chkmin(g[b[i].r],f[i]);
	}
	while(ps<=n){
		chkmin(g[ps],g[ps-1]+a[ps]);
		ps++;
	}
}
int main(){
//	freopen("hamming.in","r",stdin);
//	freopen("hamming.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		cnt[i]=cnt[i-1]+(a[i]==0);

	scanf("%d",&m);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].l,&b[i].r);
	sort(b+1,b+m+1,cmp);

	solve();
	printf("%d\n",g[n]);
	return 0;
}

### Prompt

Please create a solution in Cpp to the following problem:

You are given a sequence a = \\{a_1, ..., a_N\\} with all zeros, and a sequence b = \\{b_1, ..., b_N\\} consisting of 0 and 1. The length of both is N.

You can perform Q kinds of operations. The i-th operation is as follows:

* Replace each of a_{l_i}, a_{l_i + 1}, ..., a_{r_i} with 1.



Minimize the hamming distance between a and b, that is, the number of i such that a_i \neq b_i, by performing some of the Q operations.

Constraints

* 1 \leq N \leq 200,000
* b consists of 0 and 1.
* 1 \leq Q \leq 200,000
* 1 \leq l_i \leq r_i \leq N
* If i \neq j, either l_i \neq l_j or r_i \neq r_j.

Input

Input is given from Standard Input in the following format:


N
b_1 b_2 ... b_N
Q
l_1 r_1
l_2 r_2
:
l_Q r_Q


Output

Print the minimum possible hamming distance.

Examples

Input

3
1 0 1
1
1 3


Output

1


Input

3
1 0 1
2
1 1
3 3


Output

0


Input

3
1 0 1
2
1 1
2 3


Output

1


Input

5
0 1 0 1 0
1
1 5


Output

2


Input

9
0 1 0 1 1 1 0 1 0
3
1 4
5 8
6 7


Output

3


Input

15
1 1 0 0 0 0 0 0 1 0 1 1 1 0 0
9
4 10
13 14
1 7
4 14
9 11
2 6
7 8
3 12
7 13


Output

5


Input

10
0 0 0 1 0 0 1 1 1 0
7
1 4
2 5
1 3
6 7
9 9
1 5
7 9


Output

1

### Response

```cpp
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

int ti;

#define mid ((l+r)>>1)
const int maxn=200100;
const int INF=1e9;
void chkmin(int &x,int y){
	x=x<y?x:y;
}
int Min(int x,int y){
	return x<y?x:y;
}
struct node{
	int l,r;
}b[maxn];
bool cmp(const node &A,const node B){
	return A.r<B.r;
}
int n,m;
int a[maxn],cnt[maxn];
int f[maxn],g[maxn];
int tr[maxn<<2];
void Build_tree(int h,int l,int r){
	tr[h]=INF;
	if(l==r)return;
	Build_tree(h<<1,l,mid);
	Build_tree(h<<1|1,mid+1,r);
}
int Query(int h,int l,int r,int p){
	if(l==r) return tr[h];
	if(p<=mid)
		return Min(Query(h<<1,l,mid,p),tr[h]);
	else
		return Min(Query(h<<1|1,mid+1,r,p),tr[h]);
}
void updata(int h,int l,int r,int s,int t,int v){
	if(s<=l&&r<=t){
		chkmin(tr[h],v);
		return;
	}
	if(t<=mid)
		updata(h<<1,l,mid,s,t,v);
	else if(s>mid)
		updata(h<<1|1,mid+1,r,s,t,v);
	else{
		updata(h<<1,l,mid,s,mid,v);
		updata(h<<1|1,mid+1,r,mid+1,t,v);
	}
}
void solve(){
	for(int i=1;i<=n;i++)
		f[i]=g[i]=INF;
	for(int i=1;i<=m;i++)
		f[i]=g[i]=INF;
	Build_tree(1,1,n);
	int ps=1;
	for(int i=1;i<=m;i++){
		while(ps<=b[i].r){
			chkmin(g[ps],g[ps-1]+a[ps]);
			ps++;
		}
		ti=ps-1;
		f[i]=Query(1,1,n,b[i].l)+cnt[b[i].r];
		chkmin(f[i],g[b[i].l-1]+cnt[b[i].r]-cnt[b[i].l-1]);
		updata(1,1,n,b[i].l,b[i].r,f[i]-cnt[b[i].r]);

		chkmin(g[b[i].r],f[i]);
	}
	while(ps<=n){
		chkmin(g[ps],g[ps-1]+a[ps]);
		ps++;
	}
}
int main(){
//	freopen("hamming.in","r",stdin);
//	freopen("hamming.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		cnt[i]=cnt[i-1]+(a[i]==0);

	scanf("%d",&m);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].l,&b[i].r);
	sort(b+1,b+m+1,cmp);

	solve();
	printf("%d\n",g[n]);
	return 0;
}

```