#include <bits/stdc++.h>

//#define cin fin
//#define cout fout
#define ld long double 
#define pb push_back
#define F first
#define S second 
#define ll long long

using namespace std;

const int N = 2e5 + 1e3 , SQ = 440 ; 

int a[N] , ps[N] ; 

vector<int> v[N] ; 

int dp[N] ; 

int add[N] , mx[N] ; 

void rebuild(int id)
{
	for(int i = id * SQ ; i < (id+1) * SQ ; i ++ ) 
		dp[i] = max(dp[i]+add[id],mx[id]) ; 
	add[id] = 0 ; 
	mx[id] = 0 ;
}

void upd(int l , int r , int x)
{
	rebuild(l/SQ) ; 
	rebuild(r/SQ) ;
	for( ; l % SQ != 0 && l < r ; l ++ ) 
		dp[l] = max(dp[l],x) ; 
	if(l==r) return ; 
	for( ; r % SQ != 0 ; r -- ) 
		dp[r-1] = max(dp[r-1],x) ; 
	for(int id = l / SQ ; id <= r / SQ ; id ++ ) 
		mx[id] = max(mx[id],x) ; 
}

void upd(int l , int r)
{
	rebuild(l/SQ) ; 
	rebuild(r/SQ) ;
	for( ; l % SQ != 0 && l < r ; l ++ ) 
		dp[l] ++  ; 
	if(l==r) return ; 
	for( ; r % SQ != 0 ; r -- ) 
		dp[r-1] ++  ; 
	for(int id = l / SQ ; id < r / SQ ; id ++ ) 
		mx[id] ++ , add[id] ++  ; 
}

int get(int i)
{
	rebuild(i/SQ) ; 
	return dp[i] ; 
}

int32_t main()
{
	ios::sync_with_stdio(0) ; cin.tie(0) ; 
	int n ; 
	cin >> n ; 
	for(int i = 0 ; i < n ; i ++ ) cin >> a[i] , ps[i+1] = ps[i] + a[i] ; 
	int m ; 
	cin >> m ; 
	for(int i = 0 ; i < m ; i ++ ) 
	{
		int l , r ; 
		cin >> l >> r ; 
		l -- , r -- ; 
		v[r].pb(l) ; 
	}
	
	upd(1,2,(a[0]==0)) ; 
	int ans = 0 ; 
	for(int i = 0 ; i < n ; i ++ ) 
	{
		if(i) upd(i+1,i+2,get(i)+(a[i]==0)) ;
		int x = (a[i]==1) ; 
		if(x) upd(0,i+1) ; 
	//	cout << i << endl ; 
	//	for(int j = 0 ; j <= i ; j ++ ) upd(j,j+1,get(j)+x) ; 
		vector<pair<pair<int,int>,int>> vv ;
		for(auto u : v[i])
		{
			int v = get(u) ; 
			ans = max(ans,v) ;
			vv.pb({{u,i+2},v}) ;
		}
		for(auto u : vv)
			upd(u.F.F,u.F.S,u.S) ; 

	}
	cout << n - get(n) << endl ; 
} 

### Prompt

Your task is to create 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 <bits/stdc++.h>

//#define cin fin
//#define cout fout
#define ld long double 
#define pb push_back
#define F first
#define S second 
#define ll long long

using namespace std;

const int N = 2e5 + 1e3 , SQ = 440 ; 

int a[N] , ps[N] ; 

vector<int> v[N] ; 

int dp[N] ; 

int add[N] , mx[N] ; 

void rebuild(int id)
{
	for(int i = id * SQ ; i < (id+1) * SQ ; i ++ ) 
		dp[i] = max(dp[i]+add[id],mx[id]) ; 
	add[id] = 0 ; 
	mx[id] = 0 ;
}

void upd(int l , int r , int x)
{
	rebuild(l/SQ) ; 
	rebuild(r/SQ) ;
	for( ; l % SQ != 0 && l < r ; l ++ ) 
		dp[l] = max(dp[l],x) ; 
	if(l==r) return ; 
	for( ; r % SQ != 0 ; r -- ) 
		dp[r-1] = max(dp[r-1],x) ; 
	for(int id = l / SQ ; id <= r / SQ ; id ++ ) 
		mx[id] = max(mx[id],x) ; 
}

void upd(int l , int r)
{
	rebuild(l/SQ) ; 
	rebuild(r/SQ) ;
	for( ; l % SQ != 0 && l < r ; l ++ ) 
		dp[l] ++  ; 
	if(l==r) return ; 
	for( ; r % SQ != 0 ; r -- ) 
		dp[r-1] ++  ; 
	for(int id = l / SQ ; id < r / SQ ; id ++ ) 
		mx[id] ++ , add[id] ++  ; 
}

int get(int i)
{
	rebuild(i/SQ) ; 
	return dp[i] ; 
}

int32_t main()
{
	ios::sync_with_stdio(0) ; cin.tie(0) ; 
	int n ; 
	cin >> n ; 
	for(int i = 0 ; i < n ; i ++ ) cin >> a[i] , ps[i+1] = ps[i] + a[i] ; 
	int m ; 
	cin >> m ; 
	for(int i = 0 ; i < m ; i ++ ) 
	{
		int l , r ; 
		cin >> l >> r ; 
		l -- , r -- ; 
		v[r].pb(l) ; 
	}
	
	upd(1,2,(a[0]==0)) ; 
	int ans = 0 ; 
	for(int i = 0 ; i < n ; i ++ ) 
	{
		if(i) upd(i+1,i+2,get(i)+(a[i]==0)) ;
		int x = (a[i]==1) ; 
		if(x) upd(0,i+1) ; 
	//	cout << i << endl ; 
	//	for(int j = 0 ; j <= i ; j ++ ) upd(j,j+1,get(j)+x) ; 
		vector<pair<pair<int,int>,int>> vv ;
		for(auto u : v[i])
		{
			int v = get(u) ; 
			ans = max(ans,v) ;
			vv.pb({{u,i+2},v}) ;
		}
		for(auto u : vv)
			upd(u.F.F,u.F.S,u.S) ; 

	}
	cout << n - get(n) << endl ; 
} 

```