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

const int INF = 1e6;

struct RMQ {
	using type = int;
	static type id() { return INT_MAX; }
	static type op(const type& l, const type & r) { return min(l, r); }
};

template <typename M>
class starry_sky_tree {
	using T = typename M::type;
	const int n;
	vector<T> data, lazy;
	int size(int x) {
		int res = 1;
		while (res < x) res <<= 1;
		return res;
	}
	T sub(int l, int r, int node, int lb, int ub) {
		if (r <= lb || ub <= l) return M::id();
		if (l <= lb && ub <= r) return data[node];
		return M::op(sub(l, r, node << 1, lb, (lb + ub) >> 1)
			, sub(l, r, (node << 1) | 1, (lb + ub) >> 1, ub)) + lazy[node];
	}
	void suc(int l, int r, int node, int lb, int ub, T val) {
		if (ub <= l || r <= lb) return;
		if (l <= lb && ub <= r) {
			data[node] += val;
			lazy[node] += val;
			return;
		}
		suc(l, r, node << 1, lb, (lb + ub) >> 1, val);
		suc(l, r, (node << 1) | 1, (lb + ub) >> 1, ub, val);
		data[node] = M::op(data[node << 1], data[(node << 1) | 1]) + lazy[node];
	}
public:
	starry_sky_tree(int n_) : n(size(n_)), data(n << 1), lazy(n << 1) {}
	void add(int l, int r, T val) {
		suc(l, r, 1, 0, n, val);
	}
	T find(int l, int r) {
		return sub(l, r, 1, 0, n);
	}
};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	vector<int> b(N);
	for (int i = 0; i < N; i++) {
		cin >> b[i];
	}
	int Q;
	cin >> Q;
	vector<pair<int, int>> lr;
	for (int i = 0; i < Q; i++) {
		int l, r;
		cin >> l >> r; --l;
		lr.emplace_back(l, r);
	}
	sort(lr.begin(), lr.end());
	starry_sky_tree<RMQ> dp(N + 1);
	int it = 0;
	dp.add(1, N + 1, INF);
	for (int i = 0; i < N; i++) {
		while (it < Q && lr[it].first == i) {
			int l, r;
			tie(l, r) = lr[it++];
			int x = dp.find(0, r);
			int v = dp.find(r, r + 1);
			dp.add(r, r + 1, min(0, x - v));
		}
		if (b[i]) {
			dp.add(0, i + 1, 1);
		}
		else {
			dp.add(i + 1, N + 1, 1);
		}
	}
	cout << dp.find(0, N + 1) << endl;
	return 0;
}

### 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 INF = 1e6;

struct RMQ {
	using type = int;
	static type id() { return INT_MAX; }
	static type op(const type& l, const type & r) { return min(l, r); }
};

template <typename M>
class starry_sky_tree {
	using T = typename M::type;
	const int n;
	vector<T> data, lazy;
	int size(int x) {
		int res = 1;
		while (res < x) res <<= 1;
		return res;
	}
	T sub(int l, int r, int node, int lb, int ub) {
		if (r <= lb || ub <= l) return M::id();
		if (l <= lb && ub <= r) return data[node];
		return M::op(sub(l, r, node << 1, lb, (lb + ub) >> 1)
			, sub(l, r, (node << 1) | 1, (lb + ub) >> 1, ub)) + lazy[node];
	}
	void suc(int l, int r, int node, int lb, int ub, T val) {
		if (ub <= l || r <= lb) return;
		if (l <= lb && ub <= r) {
			data[node] += val;
			lazy[node] += val;
			return;
		}
		suc(l, r, node << 1, lb, (lb + ub) >> 1, val);
		suc(l, r, (node << 1) | 1, (lb + ub) >> 1, ub, val);
		data[node] = M::op(data[node << 1], data[(node << 1) | 1]) + lazy[node];
	}
public:
	starry_sky_tree(int n_) : n(size(n_)), data(n << 1), lazy(n << 1) {}
	void add(int l, int r, T val) {
		suc(l, r, 1, 0, n, val);
	}
	T find(int l, int r) {
		return sub(l, r, 1, 0, n);
	}
};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	vector<int> b(N);
	for (int i = 0; i < N; i++) {
		cin >> b[i];
	}
	int Q;
	cin >> Q;
	vector<pair<int, int>> lr;
	for (int i = 0; i < Q; i++) {
		int l, r;
		cin >> l >> r; --l;
		lr.emplace_back(l, r);
	}
	sort(lr.begin(), lr.end());
	starry_sky_tree<RMQ> dp(N + 1);
	int it = 0;
	dp.add(1, N + 1, INF);
	for (int i = 0; i < N; i++) {
		while (it < Q && lr[it].first == i) {
			int l, r;
			tie(l, r) = lr[it++];
			int x = dp.find(0, r);
			int v = dp.find(r, r + 1);
			dp.add(r, r + 1, min(0, x - v));
		}
		if (b[i]) {
			dp.add(0, i + 1, 1);
		}
		else {
			dp.add(i + 1, N + 1, 1);
		}
	}
	cout << dp.find(0, N + 1) << endl;
	return 0;
}

```