#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;

const int INF = 1000000000;

#define fr first
#define sc second

struct SEG{
	int siz = 1<<18;
	int s[1<<19],t[1<<19];
	void init(){
		for(int i = 0 ; i < siz*2-1 ; i ++){
			s[i] = INF;
			t[i] = 0;
		}
	}
	void add(int a,int b,int x,int k,int l,int r){
		if(b <= l || r <= a)return;
		if(a <= l && r <= b){
			t[k] ++;
			return;
		}
		add(a,b,x,2*k+1,l,(l+r)/2);
		add(a,b,x,2*k+2,(l+r)/2,r);
		s[k] = min( s[2*k+1]+t[2*k+1] , s[2*k+2]+t[2*k+2] );
	}
	void chmin(int a, int x){
		int b = a+siz-1;
		x -= t[b];
		while(b > 0){
			b = (b-1)/2;
			x -= t[b];
		}
		a += siz-1;
		s[a] = min( s[a] , x );
		while(a > 0){
			x += t[a];
			a = (a-1)/2;
			s[a] = min( s[a] , x );
		}
	}
	int query(int a,int b,int k,int l,int r){
		if(b <= l || r <= a)return INF;
		if(a <= l && r <= b)return s[k]+t[k];
		int chl = query(a,b,2*k+1,l,(l+r)/2);
		int chr = query(a,b,2*k+2,(l+r)/2,r);
		return min(chl,chr)+t[k];
	}
}dp;

int main(){
	static int n;
	static int b[200010];
	static int q;
	static int l[200010],r[200010];
	scanf("%d",&n);
	for(int i = 1 ; i <= n ; i ++){
		scanf("%d",&b[i]);
	}
	scanf("%d",&q);
	for(int i = 1 ; i <= q ; i ++){
		scanf("%d%d",&l[i],&r[i]);
	}
	
	vector<P> vec;
	for(int i = 1 ; i <= q ; i ++){
		vec.push_back(P(l[i],r[i]));
	}
	sort(vec.begin(),vec.end());
	
	dp.init();
	dp.chmin(0,0);
	int loc = 0;
	for(int i = 1 ; i <= n ; i ++){
		while(loc < vec.size() && vec[loc].fr == i){
			dp.chmin(vec[loc].sc,dp.query(0,vec[loc].sc+1,0,0,dp.siz));
			loc ++;
		}
		if(b[i] == 0){
			dp.add(i,dp.siz,1,0,0,dp.siz);
		}
		else {
			dp.add(0,i,1,0,0,dp.siz);
		}
	}
	cout << dp.query(0,dp.siz,0,0,dp.siz) << endl;
}


### Prompt

Create a solution in Cpp for 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 <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;

const int INF = 1000000000;

#define fr first
#define sc second

struct SEG{
	int siz = 1<<18;
	int s[1<<19],t[1<<19];
	void init(){
		for(int i = 0 ; i < siz*2-1 ; i ++){
			s[i] = INF;
			t[i] = 0;
		}
	}
	void add(int a,int b,int x,int k,int l,int r){
		if(b <= l || r <= a)return;
		if(a <= l && r <= b){
			t[k] ++;
			return;
		}
		add(a,b,x,2*k+1,l,(l+r)/2);
		add(a,b,x,2*k+2,(l+r)/2,r);
		s[k] = min( s[2*k+1]+t[2*k+1] , s[2*k+2]+t[2*k+2] );
	}
	void chmin(int a, int x){
		int b = a+siz-1;
		x -= t[b];
		while(b > 0){
			b = (b-1)/2;
			x -= t[b];
		}
		a += siz-1;
		s[a] = min( s[a] , x );
		while(a > 0){
			x += t[a];
			a = (a-1)/2;
			s[a] = min( s[a] , x );
		}
	}
	int query(int a,int b,int k,int l,int r){
		if(b <= l || r <= a)return INF;
		if(a <= l && r <= b)return s[k]+t[k];
		int chl = query(a,b,2*k+1,l,(l+r)/2);
		int chr = query(a,b,2*k+2,(l+r)/2,r);
		return min(chl,chr)+t[k];
	}
}dp;

int main(){
	static int n;
	static int b[200010];
	static int q;
	static int l[200010],r[200010];
	scanf("%d",&n);
	for(int i = 1 ; i <= n ; i ++){
		scanf("%d",&b[i]);
	}
	scanf("%d",&q);
	for(int i = 1 ; i <= q ; i ++){
		scanf("%d%d",&l[i],&r[i]);
	}
	
	vector<P> vec;
	for(int i = 1 ; i <= q ; i ++){
		vec.push_back(P(l[i],r[i]));
	}
	sort(vec.begin(),vec.end());
	
	dp.init();
	dp.chmin(0,0);
	int loc = 0;
	for(int i = 1 ; i <= n ; i ++){
		while(loc < vec.size() && vec[loc].fr == i){
			dp.chmin(vec[loc].sc,dp.query(0,vec[loc].sc+1,0,0,dp.siz));
			loc ++;
		}
		if(b[i] == 0){
			dp.add(i,dp.siz,1,0,0,dp.siz);
		}
		else {
			dp.add(0,i,1,0,0,dp.siz);
		}
	}
	cout << dp.query(0,dp.siz,0,0,dp.siz) << endl;
}


```