#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

const int N=300005;

struct ST{
	int l,r,mx;
}t[2][N<<2];
struct node{
	int l,r;
}q[N];
int n,cnt,ans,inf,m,x,y;
int b[N],dp[N],s[N];

void update(int tp,int k){
	t[tp][k].mx=max(t[tp][k<<1].mx,t[tp][k<<1|1].mx);
}

void build(int tp,int k,int l,int r){
	t[tp][k].l=l; t[tp][k].r=r;
	t[tp][k].mx=inf;
	if (l==r) return;
	int mid=(l+r)>>1;
	build(tp,k<<1,l,mid);
	build(tp,k<<1|1,mid+1,r);
	update(tp,k);
}

int query(int tp,int k,int L,int R){
	if (L<=t[tp][k].l&&t[tp][k].r<=R) return t[tp][k].mx;
	int mid=(t[tp][k].l+t[tp][k].r)>>1;
	if (R<=mid) return query(tp,k<<1,L,R);
	else if (L>mid) return query(tp,k<<1|1,L,R);
	else return max(query(tp,k<<1,L,R),query(tp,k<<1|1,L,R));
}

void modify(int tp,int k,int p,int w){
	if (t[tp][k].l==t[tp][k].r){
		t[tp][k].mx=max(t[tp][k].mx,w);
		return;
	}
	int mid=(t[tp][k].l+t[tp][k].r)>>1;
	if (p<=mid) modify(tp,k<<1,p,w);
	else modify(tp,k<<1|1,p,w);
	update(tp,k);
}

bool cmp(node a,node b){
	return a.l<b.l||(a.l==b.l&&a.r<b.r);
}

int main(){
	int i;
	scanf("%d",&n);
	for (i=1;i<=n;i++){
		scanf("%d",&b[i]);
		if (b[i]==0) cnt++,b[i]=-1;
		s[i]=s[i-1]+b[i];
	}
	scanf("%d",&m);
	for (i=1;i<=m;i++) scanf("%d%d",&q[i].l,&q[i].r);
	sort(q+1,q+1+m,cmp);
	memset(dp,-0x3f,sizeof(dp)); inf=dp[0];
	build(0,1,1,n);
	build(1,1,1,n);
	ans=0;
	for (i=1;i<=m;i++){
		x=q[i].l; y=q[i].r;
		dp[i]=s[y]-s[x-1];
		if (x>1) dp[i]=max(dp[i],query(0,1,1,x-1)+s[y]-s[x-1]);
		if (y>x) dp[i]=max(dp[i],query(1,1,x,y-1)+s[y]);
		modify(0,1,y,dp[i]);
		modify(1,1,y,dp[i]-s[y]);
		ans=max(ans,dp[i]);
	}
	printf("%d\n",n-cnt-ans);
	return 0;
}
### Prompt

Generate a Cpp solution 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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

const int N=300005;

struct ST{
	int l,r,mx;
}t[2][N<<2];
struct node{
	int l,r;
}q[N];
int n,cnt,ans,inf,m,x,y;
int b[N],dp[N],s[N];

void update(int tp,int k){
	t[tp][k].mx=max(t[tp][k<<1].mx,t[tp][k<<1|1].mx);
}

void build(int tp,int k,int l,int r){
	t[tp][k].l=l; t[tp][k].r=r;
	t[tp][k].mx=inf;
	if (l==r) return;
	int mid=(l+r)>>1;
	build(tp,k<<1,l,mid);
	build(tp,k<<1|1,mid+1,r);
	update(tp,k);
}

int query(int tp,int k,int L,int R){
	if (L<=t[tp][k].l&&t[tp][k].r<=R) return t[tp][k].mx;
	int mid=(t[tp][k].l+t[tp][k].r)>>1;
	if (R<=mid) return query(tp,k<<1,L,R);
	else if (L>mid) return query(tp,k<<1|1,L,R);
	else return max(query(tp,k<<1,L,R),query(tp,k<<1|1,L,R));
}

void modify(int tp,int k,int p,int w){
	if (t[tp][k].l==t[tp][k].r){
		t[tp][k].mx=max(t[tp][k].mx,w);
		return;
	}
	int mid=(t[tp][k].l+t[tp][k].r)>>1;
	if (p<=mid) modify(tp,k<<1,p,w);
	else modify(tp,k<<1|1,p,w);
	update(tp,k);
}

bool cmp(node a,node b){
	return a.l<b.l||(a.l==b.l&&a.r<b.r);
}

int main(){
	int i;
	scanf("%d",&n);
	for (i=1;i<=n;i++){
		scanf("%d",&b[i]);
		if (b[i]==0) cnt++,b[i]=-1;
		s[i]=s[i-1]+b[i];
	}
	scanf("%d",&m);
	for (i=1;i<=m;i++) scanf("%d%d",&q[i].l,&q[i].r);
	sort(q+1,q+1+m,cmp);
	memset(dp,-0x3f,sizeof(dp)); inf=dp[0];
	build(0,1,1,n);
	build(1,1,1,n);
	ans=0;
	for (i=1;i<=m;i++){
		x=q[i].l; y=q[i].r;
		dp[i]=s[y]-s[x-1];
		if (x>1) dp[i]=max(dp[i],query(0,1,1,x-1)+s[y]-s[x-1]);
		if (y>x) dp[i]=max(dp[i],query(1,1,x,y-1)+s[y]);
		modify(0,1,y,dp[i]);
		modify(1,1,y,dp[i]-s[y]);
		ans=max(ans,dp[i]);
	}
	printf("%d\n",n-cnt-ans);
	return 0;
}
```