//#include"pch.h"
#include<bits/stdc++.h>
using namespace std;
#define lol(i,n) for(int i=0;i<n;i++)
#define chmax(a,b) a=max(a,b);
typedef long long ll;
#define N 200010
class RAQxRMQ {
private:
	ll dat[2 * N], laz[2 * N];
	ll eval(ll k) {
		if (k < N) {
			laz[k * 2] += laz[k];
			laz[k * 2 + 1] += laz[k];
		}
		dat[k] += laz[k],laz[k]=0;
		return dat[k];
	}
public:
	void init() {
		lol(i, 2 * N)dat[i] = -1e17, laz[i] = 0;
	}
	void add(ll l, ll r, ll x) {
		l += N, r += N + 1;
		for (ll a = l, b = r; a < b; a >>= 1, b >>= 1) {
			if (a & 1)laz[a++] += x;
			if (b & 1)laz[--b] += x;
		}
		for (ll a = l, b = r; a + b; a >>= 1, b >>= 1) {
			dat[a / 2] = max(eval(a), eval(a ^ 1));
			dat[b / 2] = max(eval(b), eval(b ^ 1));
		}
	}
	ll maxi(ll l, ll r) {
		l += N, r += N + 1;
		for (ll d = 20; ~d; d--) {
			eval(l >> d); eval(r >> d);
		}
		ll res = -1e18;
		for (ll a = l, b = r; a < b; a >>= 1, b >>= 1) {
			if (a & 1)chmax(res, eval(a++));
			if (b & 1)chmax(res, eval(--b));
		}
		return res;
	}
	void upd(ll i, ll x) {
		ll val = maxi(i, i);
		add(i, i, max(0LL,x-val));
	}
};
ll n, d[N], q;
vector<ll> v[N];
void outs(ll x) {
	if (x <= -1e10)cout << "# ";
	else cout << x << " ";
}
int main() {
	cin >> n;
	lol(i, n)cin >> d[i];
	cin >> q;
	lol(i, q) {
		ll a, b; cin >> a >> b; a--;
		v[a].push_back(b);
	}
	RAQxRMQ seg; seg.init();
	seg.upd(0, 0);
	lol(i, n) {
		sort(v[i].begin(), v[i].end());
		for (auto to : v[i]) {
			seg.upd(to, seg.maxi(0, to - 1));
			//cout << to << " " << seg.maxi(0, to - 1) << endl;
		}
		//lol(i, 16)outs(seg.maxi(i, i)); cout << endl;
		ll val = (d[i] == 0 ? -1 : +1);
		seg.add(i +1, n+5, val);
	}
	ll ans = -seg.maxi(0, n+5);
	lol(i, n)ans += d[i];
	cout << ans << endl;
	return 0;
}
### Prompt

Please formulate 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"pch.h"
#include<bits/stdc++.h>
using namespace std;
#define lol(i,n) for(int i=0;i<n;i++)
#define chmax(a,b) a=max(a,b);
typedef long long ll;
#define N 200010
class RAQxRMQ {
private:
	ll dat[2 * N], laz[2 * N];
	ll eval(ll k) {
		if (k < N) {
			laz[k * 2] += laz[k];
			laz[k * 2 + 1] += laz[k];
		}
		dat[k] += laz[k],laz[k]=0;
		return dat[k];
	}
public:
	void init() {
		lol(i, 2 * N)dat[i] = -1e17, laz[i] = 0;
	}
	void add(ll l, ll r, ll x) {
		l += N, r += N + 1;
		for (ll a = l, b = r; a < b; a >>= 1, b >>= 1) {
			if (a & 1)laz[a++] += x;
			if (b & 1)laz[--b] += x;
		}
		for (ll a = l, b = r; a + b; a >>= 1, b >>= 1) {
			dat[a / 2] = max(eval(a), eval(a ^ 1));
			dat[b / 2] = max(eval(b), eval(b ^ 1));
		}
	}
	ll maxi(ll l, ll r) {
		l += N, r += N + 1;
		for (ll d = 20; ~d; d--) {
			eval(l >> d); eval(r >> d);
		}
		ll res = -1e18;
		for (ll a = l, b = r; a < b; a >>= 1, b >>= 1) {
			if (a & 1)chmax(res, eval(a++));
			if (b & 1)chmax(res, eval(--b));
		}
		return res;
	}
	void upd(ll i, ll x) {
		ll val = maxi(i, i);
		add(i, i, max(0LL,x-val));
	}
};
ll n, d[N], q;
vector<ll> v[N];
void outs(ll x) {
	if (x <= -1e10)cout << "# ";
	else cout << x << " ";
}
int main() {
	cin >> n;
	lol(i, n)cin >> d[i];
	cin >> q;
	lol(i, q) {
		ll a, b; cin >> a >> b; a--;
		v[a].push_back(b);
	}
	RAQxRMQ seg; seg.init();
	seg.upd(0, 0);
	lol(i, n) {
		sort(v[i].begin(), v[i].end());
		for (auto to : v[i]) {
			seg.upd(to, seg.maxi(0, to - 1));
			//cout << to << " " << seg.maxi(0, to - 1) << endl;
		}
		//lol(i, 16)outs(seg.maxi(i, i)); cout << endl;
		ll val = (d[i] == 0 ? -1 : +1);
		seg.add(i +1, n+5, val);
	}
	ll ans = -seg.maxi(0, n+5);
	lol(i, n)ans += d[i];
	cout << ans << endl;
	return 0;
}
```