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

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
 
////////////////////////////////////////////////////////////////////
 
const int N = 2e5 + 5;

bool B[N];
int n, A[N], SEG[N << 2];
vector<int> Q[N];

#define md ((s + e) >> 1)
#define lc (id << 1)
#define rc (lc ^ 1)
void Update(int p, int x, int id = 1, int s = 0, int e = n + 1) {
	if (e - s < 2) { SEG[id] = min(SEG[id], x); return; }
	if (p < md) Update(p, x, lc, s, md);
	else Update(p, x, rc, md, e);
	SEG[id] = min(SEG[lc], SEG[rc]);
}
int Get(int l, int r, int id = 1, int s = 0, int e = n + 1) {
	if (l >= e || s >= r) return INF;
	if (l <= s && e <= r) return SEG[id];
	return min(Get(l, r, lc, s, md), Get(l, r, rc, md, e));
}

int main() {
 
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	mt19937 Rnd(time(0));
	
	int zero = 0; cin >> n;
	for (int i = 1; i <= n; i++) cin >> B[i], A[i] = (B[i] ? +1 : -1), zero += !B[i];
	int q; cin >> q;
	while (q--) {
		int l, r; cin >> l >> r;
		Q[l].push_back(r);
	}
	memset(SEG, 63, sizeof SEG);
	// DP[l + 1][r] = min(DP[l][l], DP[l][l + 1], ... DP[l][r]);
	// DP[i + 1][i + 1] = min(DP[i][i + 1], DP[i][i] + (B[i + 1] ? +1 : -1));
	Update(0, 0);
	for (int i = 1; i <= n; i++) {
		sort(all(Q[i])), reverse(all(Q[i]));
		for (int r : Q[i]) Update(r, Get(i - 1, r + 1));
		Update(i, Get(i - 1, i) + A[i]);
	}
	cout << zero + Get(n, n + 1) << endl;

	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
// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
 
////////////////////////////////////////////////////////////////////
 
const int N = 2e5 + 5;

bool B[N];
int n, A[N], SEG[N << 2];
vector<int> Q[N];

#define md ((s + e) >> 1)
#define lc (id << 1)
#define rc (lc ^ 1)
void Update(int p, int x, int id = 1, int s = 0, int e = n + 1) {
	if (e - s < 2) { SEG[id] = min(SEG[id], x); return; }
	if (p < md) Update(p, x, lc, s, md);
	else Update(p, x, rc, md, e);
	SEG[id] = min(SEG[lc], SEG[rc]);
}
int Get(int l, int r, int id = 1, int s = 0, int e = n + 1) {
	if (l >= e || s >= r) return INF;
	if (l <= s && e <= r) return SEG[id];
	return min(Get(l, r, lc, s, md), Get(l, r, rc, md, e));
}

int main() {
 
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	mt19937 Rnd(time(0));
	
	int zero = 0; cin >> n;
	for (int i = 1; i <= n; i++) cin >> B[i], A[i] = (B[i] ? +1 : -1), zero += !B[i];
	int q; cin >> q;
	while (q--) {
		int l, r; cin >> l >> r;
		Q[l].push_back(r);
	}
	memset(SEG, 63, sizeof SEG);
	// DP[l + 1][r] = min(DP[l][l], DP[l][l + 1], ... DP[l][r]);
	// DP[i + 1][i + 1] = min(DP[i][i + 1], DP[i][i] + (B[i + 1] ? +1 : -1));
	Update(0, 0);
	for (int i = 1; i <= n; i++) {
		sort(all(Q[i])), reverse(all(Q[i]));
		for (int r : Q[i]) Update(r, Get(i - 1, r + 1));
		Update(i, Get(i - 1, i) + A[i]);
	}
	cout << zero + Get(n, n + 1) << endl;

	return 0;
}

```