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

Solution (in JAVA):

Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

class Main{

    static class DP{
        int l,r, cost, useCost;
        DP(int l,int r){this.l=l;this.r=r;}
    }
    static DP[] dp;
    static int[] sum;
    static class RMQ{
        int data1[];
        int data2[];
        int N;
        RMQ(int n){
            N=1;
            while(N<n)N*=2;
            data1 = new int[2*N];
            data2 = new int[2*N];
            Arrays.fill(data1, Integer.MAX_VALUE/10);
            Arrays.fill(data2, Integer.MAX_VALUE/10);
        }
        void set1(int i, int x){
            i+=N;
            data1[i]=Math.min(data1[i], x);
            for(i=i/2;i>0;i/=2)data1[i]=Math.min(data1[2*i], data1[2*i+1]);
        }
        void set2(int i, int x){
            i+=N;
            data2[i]=Math.min(data2[i], x);
            for(i=i/2;i>0;i/=2)data2[i]=Math.min(data2[2*i], data2[2*i+1]);
        }
        //[l, r)
        int getMin1(int l, int r){return getMin(l, r, 1, 0, N, data1);}
        int getMin2(int l, int r){return getMin(l, r, 1, 0, N, data2);}
        int getMin(int l, int r, int k, int a,int b, int[] data){
            if(b<=l || r<=a)return Integer.MAX_VALUE/10;
            if(l<=a && b<=r)return data[k];
            return Math.min(getMin(l, r, 2*k, a, (a+b)/2, data), getMin(l, r, 2*k+1, (a+b)/2, b, data));
        }
    }
    
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        sum = new int[N+1];
        for(int i=0;i<N;++i)sum[i+1]=sum[i]+scan.nextInt();
        int Q = scan.nextInt();
        dp = new DP[Q+1];
        dp[0] = new DP(0, 0);
        // [l, r]　1-index
        for(int i=0;i<Q;++i)dp[i+1] = new DP(scan.nextInt(), scan.nextInt());
        Arrays.sort(dp, (a,b)->a.l==b.l ? b.r-a.r : a.l-b.l);
        RMQ rmq = new RMQ(N+2);
        dp[0].useCost =0;
        rmq.set1(dp[0].r, 0);
        rmq.set2(dp[0].r, 0);
        for(int i=1;i<=Q;++i){
            // li<rk<ri
            dp[i].useCost = Math.min(dp[i].r-sum[dp[i].r] + rmq.getMin1(dp[i].l, dp[i].r), 2*sum[dp[i].l-1] + dp[i].r -(dp[i].l-1) -sum[dp[i].r] + rmq.getMin2(0, dp[i].l));
            rmq.set1(dp[i].r, dp[i].useCost -dp[i].r + sum[dp[i].r]);
            rmq.set2(dp[i].r, dp[i].useCost - sum[dp[i].r]);
        }
        // for(int i=0;i<=Q;++i)System.out.println("("+dp[i].l+", "+dp[i].r+") : "+dp[i].useCost);
        // System.out.println();
        // for(int i=0;i<=N;++i)System.out.println(i+" "+rmq.getMin1(i, i+1) + " "+rmq.getMin2(i, i+1));
        int ans = Integer.MAX_VALUE;
        for(int i=0;i<=Q;++i)ans=Math.min(ans, dp[i].useCost + sum[N]-sum[dp[i].r]);
        System.out.println(ans);
    }
}