#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,sse,tune=native")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
const int maxn = 1<<18;

//implicit segtree
struct node {
	node *l, *r;
	int mn = 1<<30, p = -1;
};

//node allocator
static char buf[500 << 18];
void* operator new(size_t s) {
	static size_t i = sizeof buf;
	assert(s < i);
	return (void*)&buf[i -= s];
}
void operator delete(void*) {}
#define newnode new node
//node allocator

int get(node *v, int l, int r, int ql, int qr) {
	if(!v || r < ql || l > qr) return 1<<30;
	if(v->p >= 0) return (ql <= v->p && v->p <= qr) ? v->mn : 1<<30;
	if(ql <= l && r <= qr) return v->mn;
	int mid = (l + r)/2;
	return min(get(v->l, l, mid, ql, qr), get(v->r, mid+1, r, ql , qr));
}
node* upd(node *v, int l, int r, int p, int a) {
	if(p < l || p > r) return v;
	if(!v) v = newnode(), v->p -= l==r;
	if(l == r) {v->mn = min(v->mn, a); return v;}
	int mid = (l+r)/2;
	if(v->p >= 0) {
		if(v->p == p) v->mn = min(v->mn, a);
		if(v->p <= mid) v->l = newnode(), v->l->p = v->p, v->l->mn = v->mn;
		else v->r = newnode(), v->r->p = v->p, v->r->mn = v->mn;
		v->p = -2;
	}
	v->mn = min(v->mn, a);
	if(v->p == -1) {
		v->p = p;
		return v;
	}
	v->l = upd(v->l, l, mid, p, a);
	v->r = upd(v->r, mid+1, r, p, a);
	return v;
}

//implicit segtree
int sz;
node *tree[maxn];
void upd(int x, int y, int a) {
	for(++x; x <= sz; x += x&-x) tree[x] = upd(tree[x], 0, sz-1, y, a);
}
int get(int x, int yl, int yr) {
	int ans = 1<<30;
	for(++x; x; x -= x&-x) ans = min(ans, get(tree[x], 0, sz-1, yl, yr));
	return ans;
}

int lol[maxn];
int gett(int x) {
	int ans = 1<<30;
	for(++x; x; x -= x&-x) ans = min(ans,lol[x]);
	return ans;
}

void updd(int x, int a) {
	for(++x; x <= sz; x += x&-x) lol[x] = min(lol[x], a);
}

int n, q, dp[maxn], pr[maxn], ir[maxn];
vector<pair<int, int>> a;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	memset(lol, 0x3f, sizeof lol);
	memset(tree, 0, sizeof tree);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> pr[i];
		pr[i] += pr[i-1];
		ir[i] = i - pr[i];
	}
	//for(int i = 1; i <= n; i++) cout << pr[i] << " "; cout << '\n';
	//for(int i = 1; i <= n; i++) cout << ir[i] << " "; cout << '\n';
	cin >> q;
	a.resize(q);
	for(auto &i : a) cin >> i.first >> i.second;
	sort(all(a), [](auto a, auto b) {
		return a.second < b.second;
	});
	sz = n+3;
	//dp_i min= dp_j + invsum(r_j; r_i] for all j: l_j <= l_i, l_i <= r_j <= r_i //segtree 0
	//dp_i min= dp_j + prefsum(l_j; r_i] for all j: r_j < l_i //segtree 1
	int ans = pr[n];
	for(int i = 0; i < q; i++) {
		int l = a[i].first, r = a[i].second;
		dp[i] = pr[l-1] + ir[r] - ir[l-1];
		//cout << i << " " << l << ";" << r << " " << dp[i] << " " << pr[n]  << " " << -pr[r] << '\n';
		dp[i] = min(dp[i], get(l, l, r) + ir[r]);
		//cout << i << " " << l << ";" << r << " " << dp[i] << " " << pr[n]  << " " << -pr[r] << '\n';
		dp[i] = min(dp[i], gett(l-1) + pr[l-1] + ir[r] - ir[l-1]);
		//cout << i << " " << l << ";" << r << " " << dp[i] << " " << pr[n]  << " " << -pr[r] << '\n';
		upd(l, r, dp[i]-ir[r]);
		updd(r, dp[i]-pr[r]);
		ans = min(ans, dp[i] + pr[n] - pr[r]);
	}
	cout << ans;
}


Measured memory_bytes: 129664