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: Accepted)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <vector>
#include <cstdio>
#include <cctype>

using namespace std;

inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar();
	while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return x*f;
}

const int INF=INT_MAX>>1;
const int N=200005;
const int Q=200005;

inline int min(int x,int y){return x<y?x:y;}

struct segment_tree
{
	int mi[N<<2],tag[N<<2];

	inline void update(int x){mi[x]=min(mi[x<<1],mi[x<<1|1]);}

	inline void change(int x,int y){mi[x]=min(mi[x],y),tag[x]=min(tag[x],y);}

	inline void clear(int x,int l,int r)
	{
		if (tag[x]!=INF)
		{
			if (l!=r) change(x<<1,tag[x]),change(x<<1|1,tag[x]);
			tag[x]=INF;
		}
	}

	void modify(int x,int st,int en,int l,int r,int edit)
	{
		clear(x,l,r);
		if (st==l&&en==r)
		{
			change(x,edit);
			return;
		}
		int mid=l+r>>1;
		if (en<=mid) modify(x<<1,st,en,l,mid,edit);
		else if (mid+1<=st) modify(x<<1|1,st,en,mid+1,r,edit);
		else modify(x<<1,st,mid,l,mid,edit),modify(x<<1|1,mid+1,en,mid+1,r,edit);
		update(x);
	}

	int query(int x,int y,int l,int r)
	{
		clear(x,l,r);
		if (l==r) return mi[x];
		int mid=l+r>>1;
		if (y<=mid) return query(x<<1,y,l,mid);
		else return query(x<<1|1,y,mid+1,r);
	}

	void build(int x,int l,int r)
	{
		mi[x]=tag[x]=INF;
		if (l==r) return;
		int mid=l+r>>1;
		build(x<<1,l,mid),build(x<<1|1,mid+1,r);
	}
}t;

vector<int> list[N];
int f[N],s[N];
bool a[N];
int n,q;

inline void update(int &x,int y){x=x<y?x:y;}

void dp()
{
	t.build(1,1,n),t.modify(1,1,1,1,n,0);
	for (int i=1;i<=n;++i)
	{
		f[i]=f[i-1]+a[i];
		sort(list[i].begin(),list[i].end());
		for (auto x:list[i])
		{
			int cur=t.query(1,x,1,n);
			update(f[i],cur+s[i]),t.modify(1,x,i,1,n,cur);
		}
		if (i<n) t.modify(1,i+1,i+1,1,n,f[i]-s[i]);
	}
}

int main()
{
	//freopen("dis.in","r",stdin),freopen("dis.out","w",stdout);
	n=read();
	for (int i=1;i<=n;++i) a[i]=read(),s[i]=s[i-1]+!a[i];
	q=read();
	for (int i=1,l,r;i<=q;++i) l=read(),r=read(),list[r].push_back(l);
	dp(),printf("%d\n",f[n]);
	//fclose(stdin),fclose(stdout);
	return 0;
}