atcoder#ARC085D. [ARC085F] NRE

[ARC085F] NRE

Score : 10001000 points

Problem Statement

You are given a sequence a={a1,...,aN}a = \{a_1, ..., a_N\} with all zeros, and a sequence b={b1,...,bN}b = \{b_1, ..., b_N\} consisting of 00 and 11. The length of both is NN.

You can perform QQ kinds of operations. The ii-th operation is as follows:

  • Replace each of ali,ali+1,...,aria_{l_i}, a_{l_i + 1}, ..., a_{r_i} with 11.

Minimize the hamming distance between aa and bb, that is, the number of ii such that aibia_i \neq b_i, by performing some of the QQ operations.

Constraints

  • 1N200,0001 \leq N \leq 200,000
  • bb consists of 00 and 11.
  • 1Q200,0001 \leq Q \leq 200,000
  • 1liriN1 \leq l_i \leq r_i \leq N
  • If iji \neq j, either liljl_i \neq l_j or rirjr_i \neq r_j.

Input

Input is given from Standard Input in the following format:

NN

b1b_1 b2b_2 ...... bNb_N

QQ

l1l_1 r1r_1

l2l_2 r2r_2

::

lQl_Q rQr_Q

Output

Print the minimum possible hamming distance.

3
1 0 1
1
1 3
1

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

3
1 0 1
2
1 1
3 3
0

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

3
1 0 1
2
1 1
2 3
1
5
0 1 0 1 0
1
1 5
2

It may be optimal to perform no operation.

9
0 1 0 1 1 1 0 1 0
3
1 4
5 8
6 7
3
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
5
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
1