#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

pp qr[200010];
int n, q;
int s[200010];

const int inf = 2e9;

struct Tree {
	static const int M = 262144;
	int T[M<<1];
	void init(){ for(int i=0; i<(M<<1); ++i) T[i]=inf; }
	void put(int p, int v){ for(p+=M; p; p>>=1) T[p]=min(T[p], v); }
	int range(int l, int r){
		int ret=inf;
		for(l+=M, r+=M; l<=r; l/=2, r/=2){
			if(l%2==1) ret=min(ret, T[l++]);
			if(r%2==0) ret=min(ret, T[r--]);
		}
		return ret;
	}
	void put(int l, int r, int v, int mp=1, int ml=0, int mr=M-1){
		if(r<ml || mr<l) return;
		if(l<=ml && mr<=r){ T[mp]=min(T[mp], v); return; }
		int mid=(ml+mr)/2;
		put(l, r, v, mp*2, ml, mid);
		put(l, r, v, mp*2+1, mid+1, mr);
	}
	int pick(int p){
		int ret=inf;
		for(p+=M; p; p>>=1) ret=min(ret, T[p]);
		return ret;
	}
} tt, t2;

int main()
{
	read(n);
	for(int i=1; i<=n; ++i){ read(s[i]); s[i]+=s[i-1]; }
	read(q);
	for(int i=1; i<=q; ++i) read(qr[i].x, qr[i].y);
	sort(qr+1, qr+q+1, [&](const pp& a, const pp& b){ return a.second < b.second; });
	
	tt.init(); t2.init();
	tt.put(0, 0);
	int ans=s[n];
	for(int i=1; i<=q; ++i){
		int l=qr[i].x, r=qr[i].y;
		int mydp = inf;
		
		mydp = min(mydp, t2.pick(l) + r - s[r]);
		mydp = min(mydp, tt.range(0, l-1) + r-l+1 - s[r]+2*s[l-1]);
		ans=min(ans, mydp+s[n]-s[r]);
		
		tt.put(r, mydp-s[r]);
		t2.put(l, r, mydp+s[r]-r);
	}
	printf("%d\n", ans);
	return 0;
}

### 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 long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

pp qr[200010];
int n, q;
int s[200010];

const int inf = 2e9;

struct Tree {
	static const int M = 262144;
	int T[M<<1];
	void init(){ for(int i=0; i<(M<<1); ++i) T[i]=inf; }
	void put(int p, int v){ for(p+=M; p; p>>=1) T[p]=min(T[p], v); }
	int range(int l, int r){
		int ret=inf;
		for(l+=M, r+=M; l<=r; l/=2, r/=2){
			if(l%2==1) ret=min(ret, T[l++]);
			if(r%2==0) ret=min(ret, T[r--]);
		}
		return ret;
	}
	void put(int l, int r, int v, int mp=1, int ml=0, int mr=M-1){
		if(r<ml || mr<l) return;
		if(l<=ml && mr<=r){ T[mp]=min(T[mp], v); return; }
		int mid=(ml+mr)/2;
		put(l, r, v, mp*2, ml, mid);
		put(l, r, v, mp*2+1, mid+1, mr);
	}
	int pick(int p){
		int ret=inf;
		for(p+=M; p; p>>=1) ret=min(ret, T[p]);
		return ret;
	}
} tt, t2;

int main()
{
	read(n);
	for(int i=1; i<=n; ++i){ read(s[i]); s[i]+=s[i-1]; }
	read(q);
	for(int i=1; i<=q; ++i) read(qr[i].x, qr[i].y);
	sort(qr+1, qr+q+1, [&](const pp& a, const pp& b){ return a.second < b.second; });
	
	tt.init(); t2.init();
	tt.put(0, 0);
	int ans=s[n];
	for(int i=1; i<=q; ++i){
		int l=qr[i].x, r=qr[i].y;
		int mydp = inf;
		
		mydp = min(mydp, t2.pick(l) + r - s[r]);
		mydp = min(mydp, tt.range(0, l-1) + r-l+1 - s[r]+2*s[l-1]);
		ans=min(ans, mydp+s[n]-s[r]);
		
		tt.put(r, mydp-s[r]);
		t2.put(l, r, mydp+s[r]-r);
	}
	printf("%d\n", ans);
	return 0;
}

```