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

typedef pair<int, int> pii;
typedef pair<short, short> pss;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

const int MX = 200010;

const int SZ = 1 << 18;

struct segtree {
	vector<int> dat;

	void init() {
		dat.resize(SZ * 2);
		rep(i, SZ * 2) dat[i] = INT_MIN;
	}

	void update(int pos, int x) {
		pos += SZ - 1;
		dat[pos] = x;

		while (pos > 0) {
			pos = (pos - 1) / 2;
			dat[pos] = max(dat[pos * 2 + 1], dat[pos * 2 + 2]);
		}
	}

	int get(int a, int b, int k = 0, int l = 0, int r = SZ) {
		if (b <= l || r <= a) return INT_MIN;
		if (a <= l && r <= b) return dat[k];
		return max(get(a, b, k * 2 + 1, l, (l + r) / 2), get(a, b, k * 2 + 2, (l + r) / 2, r));
	}
} seg, seg2;

int N, Q;
int b[MX];
int num[MX];
int ans;

int main() {
	seg.init();
	seg2.init();
	scanf("%d", &N);

	rep(i, N) {
		scanf("%d", &b[i]);
		num[i+1] = num[i] + !b[i];
	}

	scanf("%d", &Q);
	vector<pii> qr(Q);

	rep(i, Q) {
		int l, r;
		scanf("%d%d", &l, &r);
		qr[i] = mp(l, r);
	}

	sort(ALL(qr));

	for (auto p : qr) {
		int l = p.fi, r = p.se;
		int c = r - l + 1 - 2 * (num[r] - num[l-1]);

		int t = max(0, seg.get(0, l));
		t += c;

		int t2 = seg2.get(l, r+1);

		if (t2 != INT_MIN) {
			t2 += r - 2 * num[r];
		}

		int val = max(t, t2);

		ans = max(ans, val);

		seg.update(r, val);
		seg2.update(r, val - r + 2 * num[r]);
	}

	cout << N - num[N] - ans << endl;
	
	return 0;
}
### Prompt

Please create a solution in Cpp 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;

typedef pair<int, int> pii;
typedef pair<short, short> pss;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

const int MX = 200010;

const int SZ = 1 << 18;

struct segtree {
	vector<int> dat;

	void init() {
		dat.resize(SZ * 2);
		rep(i, SZ * 2) dat[i] = INT_MIN;
	}

	void update(int pos, int x) {
		pos += SZ - 1;
		dat[pos] = x;

		while (pos > 0) {
			pos = (pos - 1) / 2;
			dat[pos] = max(dat[pos * 2 + 1], dat[pos * 2 + 2]);
		}
	}

	int get(int a, int b, int k = 0, int l = 0, int r = SZ) {
		if (b <= l || r <= a) return INT_MIN;
		if (a <= l && r <= b) return dat[k];
		return max(get(a, b, k * 2 + 1, l, (l + r) / 2), get(a, b, k * 2 + 2, (l + r) / 2, r));
	}
} seg, seg2;

int N, Q;
int b[MX];
int num[MX];
int ans;

int main() {
	seg.init();
	seg2.init();
	scanf("%d", &N);

	rep(i, N) {
		scanf("%d", &b[i]);
		num[i+1] = num[i] + !b[i];
	}

	scanf("%d", &Q);
	vector<pii> qr(Q);

	rep(i, Q) {
		int l, r;
		scanf("%d%d", &l, &r);
		qr[i] = mp(l, r);
	}

	sort(ALL(qr));

	for (auto p : qr) {
		int l = p.fi, r = p.se;
		int c = r - l + 1 - 2 * (num[r] - num[l-1]);

		int t = max(0, seg.get(0, l));
		t += c;

		int t2 = seg2.get(l, r+1);

		if (t2 != INT_MIN) {
			t2 += r - 2 * num[r];
		}

		int val = max(t, t2);

		ans = max(ans, val);

		seg.update(r, val);
		seg2.update(r, val - r + 2 * num[r]);
	}

	cout << N - num[N] - ans << endl;
	
	return 0;
}
```