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

const int N = 200005;
const int INF = 1e9;

int n, q, res;
int f[2][N];
int it[2][4 * N];
vector< pair<int, int> > b;

void upd(int t, int i, int l, int r, int p, int x) {
	if (l == r) {
		it[t][i] = min(it[t][i], x); return;
	}
	int mid = (l + r) >> 1;
	if (mid >= p) upd(t, i << 1, l, mid, p, x);
	else upd(t, i << 1 | 1, mid + 1, r, p, x);
	it[t][i] = min(it[t][i << 1], it[t][i << 1 | 1]);
}

int get(int t, int i, int l, int r, int u, int v) {
	if (l > v || u > r) return INF;
	if (u <= l && r <= v) return it[t][i];
	int mid = (l + r) >> 1;
	return min(get(t, i << 1, l, mid, u, v), get(t, i << 1 | 1, mid + 1, r, u, v));
} 

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		int x; cin >> x;
		for (int j = 0; j < 2; ++j) {
			f[j][i] = f[j][i - 1] + (x != j);
		}
	}
	cin >> q;
	for (int i = 1; i <= q; ++i) {
		int l, r; cin >> l >> r;
		b.push_back(make_pair(l, r));
	}
	sort(b.begin(), b.end());
	for (int i = 0; i < 4 * N; ++i) it[0][i] = it[1][i] = INF;
	upd(0, 1, 0, n, 0, 0);
	upd(1, 1, 0, n, 0, 0);
	res = f[0][n];
	for (int i = 0; i < q; ++i) {
		int tmp = INF;
		int l = b[i].first, r = b[i].second;
		tmp = min(tmp, get(0, 1, 0, n, l, r) + f[1][r]);
		tmp = min(tmp, get(1, 1, 0, n, 0, l - 1) + f[0][l - 1] + f[1][r] - f[1][l - 1]);
		upd(0, 1, 0, n, r, tmp - f[1][r]);
		upd(1, 1, 0, n, r, tmp - f[0][r]);
		res = min(res, tmp + f[0][n] - f[0][r]);
	}
	cout << res;
}
### Prompt

Your challenge is to write 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>
using namespace std;

const int N = 200005;
const int INF = 1e9;

int n, q, res;
int f[2][N];
int it[2][4 * N];
vector< pair<int, int> > b;

void upd(int t, int i, int l, int r, int p, int x) {
	if (l == r) {
		it[t][i] = min(it[t][i], x); return;
	}
	int mid = (l + r) >> 1;
	if (mid >= p) upd(t, i << 1, l, mid, p, x);
	else upd(t, i << 1 | 1, mid + 1, r, p, x);
	it[t][i] = min(it[t][i << 1], it[t][i << 1 | 1]);
}

int get(int t, int i, int l, int r, int u, int v) {
	if (l > v || u > r) return INF;
	if (u <= l && r <= v) return it[t][i];
	int mid = (l + r) >> 1;
	return min(get(t, i << 1, l, mid, u, v), get(t, i << 1 | 1, mid + 1, r, u, v));
} 

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		int x; cin >> x;
		for (int j = 0; j < 2; ++j) {
			f[j][i] = f[j][i - 1] + (x != j);
		}
	}
	cin >> q;
	for (int i = 1; i <= q; ++i) {
		int l, r; cin >> l >> r;
		b.push_back(make_pair(l, r));
	}
	sort(b.begin(), b.end());
	for (int i = 0; i < 4 * N; ++i) it[0][i] = it[1][i] = INF;
	upd(0, 1, 0, n, 0, 0);
	upd(1, 1, 0, n, 0, 0);
	res = f[0][n];
	for (int i = 0; i < q; ++i) {
		int tmp = INF;
		int l = b[i].first, r = b[i].second;
		tmp = min(tmp, get(0, 1, 0, n, l, r) + f[1][r]);
		tmp = min(tmp, get(1, 1, 0, n, 0, l - 1) + f[0][l - 1] + f[1][r] - f[1][l - 1]);
		upd(0, 1, 0, n, r, tmp - f[1][r]);
		upd(1, 1, 0, n, r, tmp - f[0][r]);
		res = min(res, tmp + f[0][n] - f[0][r]);
	}
	cout << res;
}
```