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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class segtree{
public:
	static const int N=1<<19;
	int dp[1<<20];
	int lazy[1<<20];
	segtree(){
		memset(dp,0,sizeof(dp));
	}

	void lazy_eval(int k){
		dp[k]+=lazy[k];
		if(k<N-1){
			lazy[k*2+1]+=lazy[k];
			lazy[k*2+2]+=lazy[k];
		}
		lazy[k]=0;
	}
	void lazy_upd(int k){
		dp[k]=max(dp[k*2+1],dp[k*2+2]);
	}
	void update(int a,int b,int v,int k=0,int l=0,int r=N){
		lazy_eval(k);
		if(b<=l || r<=a)return;
		if(a<=l && r<=b){
			lazy[k]+=v;
			lazy_eval(k);
			return;
		}
		int mid=(l+r)/2;
		update(a,b,v,k*2+1,l,mid);
		update(a,b,v,k*2+2,mid,r);
		lazy_upd(k);
		return;
	}

	int query(int a,int b,int k=0,int l=0,int r=N){
		lazy_eval(k);
		if(b<=l || r<=a)return -214514;
		if(a<=l && r<=b)return dp[k];
		int mid=(l+r)/2;
		int vl=query(a,b,k*2+1,l,mid);
		int vr=query(a,b,k*2+2,mid,r);
		lazy_upd(k);
		return max(vl,vr);
	}
};

segtree seg;

int n,q;
int b[200002];
int sum[200002];
P p[200002];

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&b[i]);
		sum[i+1]+=sum[i];
		sum[i+1]+=b[i];
		seg.update(i+1,n+1,-b[i]);
	}
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d%d",&p[i].first,&p[i].second);
		p[i].first--;
	}
	sort(p,p+q);
	int now=0;
	for(int i=0;i<n;i++){
		while(now<q && p[now].first==i){
			int v=seg.query(0,p[now].second);
			int prev=seg.query(p[now].second,p[now].second+1)+sum[p[now].second]-sum[i];
			seg.update(p[now].second,p[now].second+1,max(v+sum[p[now].second]-sum[p[now].first]-prev,0));
			now++;
		}
		int prev=seg.query(i+1,i+2)+b[i];
		if(b[i]==0){
			seg.update(i+1,i+2,max(seg.query(i,i+1)-prev+1,0));
		}else{
			seg.update(i+1,i+2,max(seg.query(i,i+1)-prev,0));
		}
		seg.update(i+1,n+1,b[i]);
	}
	printf("%d\n",n-seg.query(n,n+1));
	return 0;
}