Problem:
Score : 1000 points

Problem Statement

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.

Sample Input 1

3
1 0 1
1
1 3

Sample Output 1

1

If you choose to perform the operation, a will become \{1, 1, 1\}, for a hamming distance of 1.

Sample Input 2

3
1 0 1
2
1 1
3 3

Sample Output 2

0

If both operations are performed, a will become \{1, 0, 1\}, for a hamming distance of 0.

Sample Input 3

3
1 0 1
2
1 1
2 3

Sample Output 3

1

Sample Input 4

5
0 1 0 1 0
1
1 5

Sample Output 4

2

It may be optimal to perform no operation.

Sample Input 5

9
0 1 0 1 1 1 0 1 0
3
1 4
5 8
6 7

Sample Output 5

3

Sample Input 6

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

Sample Output 6

5

Sample Input 7

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

Sample Output 7

1

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i,n) for(auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,n) for(ll i=s, i##_len=(ll)(n); i<i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

const ll INF = 1e9;

// Segment Tree : 時間計算量 O(n log n), 空間計算量 O(n)
// require  : なし
// verified : 
//
// T はモノイド（単位元を持ち，結合則が成り立つ）であることが要求される
template <class T, class U = size_t>
struct SegTree {
	// !!! 実装時 t(lhs, rhs) の引数の順番に注意(意図せず可換則が要求されてしまう) !!!
	typedef typename T::V V;
	explicit SegTree(U _n = 0, const T &t = T()) : n(1), t(t) {
		while (n < _n) n *= 2;
		v.assign(n * 2 - 1, t.identity());
		if (n >= 2) {
			for (U i = n - 2;; --i) {
				v[i] = t(v[i * 2 + 1], v[i * 2 + 2]);
				if (i == 0) break; // Uが符号無し整数の可能性がある為こう実装している
			}
		}
	}
	V operator[](U i) const { return v[n - 1 + i]; }
	void set(U i, const V & x) {
		i += n - 1;
		v[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;
			v[i] = t(v[i * 2 + 1], v[i * 2 + 2]);
		}
	}
	// [a,b)の範囲についてのクエリを返す
	V get(U a, U b) const { return q(a, b, 0, 0, n); }

	// f(get(0, i), x) == trueを満たす最小のiを返す
	// f(get(i, n), x) == trueを満たす最大のiを返す場合はrev:のところを書き換える(未verify)．
	template<class F>
	U lower_bound(const V & x, F && f) const { return lb(x, t.identity(), 0, 0, n, f); }

	// f(get(l, i), x) == trueを満たす最小のiを返す
	// f(get(i, l), x) == trueを満たす最大のiを返す場合はrev:のところを書き換える(未verify)．
	// 木をジグザグに登っていったあとlb()で下っていくイメージ
	template<class F>
	U lower_bound(const V & x, U l, F && f) const {
		// rev: l--;
		U k = l + n - 1, len = 1;
		V nlv = v[k], lv = t.identity();
		while (!f(nlv, x)) {
			while (k % 2 == 0) {      // rev: k % 2 == 1
				if (k == 0) return 0; // rev: return n;
				k = (k - 1) / 2;
				l -= len; // rev: delete
				len *= 2;
			}
			k += 1;   // rev: k -= 1;
			l += len; // rev: l -= len;
			lv = nlv;
			nlv = t(nlv, v[k]); // rev: t(v[k], nlv)
		}
		return lb(x, lv, k, l, l + len, f);
	}
private:
	// t : traits のインスタンス
	// v : 完全二分木の配列表現
	// n : 完全二分木の葉の数
	T t;
	vector<V> v;
	U n;

	// [a,b)の範囲についてのクエリを返す
	// k     : 現在見ている接点の番号
	// [l,r) : kに対応した範囲 
	V q(U a, U b, U k, U l, U r) const {
		if (r <= a || b <= l) return t.identity(); // 範囲外
		if (a <= l && r <= b) return v[k];         // [l,r) ⊂ [a,b)

												   //[l,r)の一部が[a,b)に含まれる
		U c = (l + r) / 2;
		return t(
			q(a, b, k * 2 + 1, l, c),
			q(a, b, k * 2 + 2, c, r)
		);
	}

	template<class F>
	U lb(const V & x, const V & lv, U k, U l, U r, F && f) const {
		if (k >= n - 1) return f(t(lv, v[k]), x) ? r : 0; // rev: f(t(v[k], lv), x) ? l : n
		V nlv = t(lv, v[k * 2 + 1]); // rev: t(v[k * 2 + 2], lv);
		U c = (l + r) / 2;
		return f(nlv, x) ? lb(x, lv, 2 * k + 1, l, c, f)  // rev: lb(x,  lv, 2 * k + 2, c, r, f);
			: lb(x, nlv, 2 * k + 2, c, r, f); // rev: lb(x, nlv, 2 * k + 1, l, c, f);
	}
};

template <class T>
struct LCMTrait {
	typedef T V;
	V identity() const { return 1; }
	V operator()(const V &a, const V &b) const { return lcm(a, b); }
};

template <class T>
struct MinTrait {
	typedef T V;
	V identity() const { return TEN(10); }
	V operator()(const V &a, const V &b) const { return min(a, b); }
};

template <class T>
struct MaxTrait {
	typedef T V;
	V identity() const { return 0; }
	V operator()(const V &a, const V &b) const { return max(a, b); }
};

template <class T>
struct SumTrait {
	typedef T V;
	V identity() const { return 0; }
	V operator()(const V &a, const V &b) const { return a + b; }
};


int main() {
	#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
	#endif
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(50);

	ll n; cin >> n;
	SegTree<SumTrait<ll>> st(n);
	REP(i, n) {
		ll b; cin >> b;
		st.set(i, b);
	}

	ll q; cin >> q;
	vvl r(n);
	REP(i, q) {
		ll l, _;
		cin >> l >> _; l--, _--;
		r[_].push_back(l);
	}

	vl dp(n + 1, TEN(10));
	dp[0] = 0;
	FOR(i, 1, n + 1) {
		for (ll l : r[i - 1]) {
			dp[i] = min(dp[l] + (i - l) - st.get(l, i), dp[i]);
		}
		dp[i] = min(dp[i], dp[i - 1] + st[i - 1]);
	}

	cout << dp[n] << endl;
	return 0;
}