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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::{input, marker::Usize1};
// ordtools {{{
#[allow(dead_code)]
mod ordtools {
    pub trait Ordtools: PartialOrd + Clone + Sized {
        fn change_min(&mut self, rhs: Self) {
            if &rhs < self {
                *self = rhs.clone();
            }
        }

        fn change_max(&mut self, mut rhs: Self) {
            if self < &mut rhs {
                *self = rhs.clone();
            }
        }
    }

    impl<T: PartialOrd + Clone + Sized> Ordtools for T {}
}
// }}}
use ordtools::Ordtools;

// segtree {{{
#[allow(dead_code)]
mod segtree {
    pub trait SegValue: Clone + std::fmt::Debug {}

    impl<Value: Clone + std::fmt::Debug> SegValue for Value {}

    pub trait SegInfo {
        type Value: SegValue;

        fn id() -> Self::Value;

        fn op(x: &Self::Value, y: &Self::Value) -> Self::Value;

        fn op_assign(x: &mut Self::Value, y: &Self::Value) {
            *x = Self::op(x, y);
        }
    }

    pub trait Zero {
        fn zero() -> Self;
    }
    macro_rules! impl_zero {
        ($($ty: ty)+) => { $(impl Zero for $ty { fn zero() -> Self { 0 } })+ };
    }
    impl_zero! {
        u8 u16 u32 u64 u128 usize
        i8 i16 i32 i64 i128 isize
    }
    pub trait MinValue {
        fn min_value() -> Self;
    }
    macro_rules! impl_min {
        ($($ty: ident)+) => { $(impl MinValue for $ty { fn min_value() -> Self { std::$ty::MIN } })+ };
    }
    impl_min! {
        u8 u16 u32 u64 u128 usize
        i8 i16 i32 i64 i128 isize
    }

    pub trait MaxValue {
        fn max_value() -> Self;
    }
    macro_rules! impl_max {
        ($($ty: ident)+) => { $(impl MaxValue for $ty { fn max_value() -> Self { std::$ty::MAX } })+ };
    }
    impl_max! {
        u8 u16 u32 u64 u128 usize
        i8 i16 i32 i64 i128 isize
    }

    pub struct AddInfo<Value: SegValue + std::ops::Add> {
        _marker: std::marker::PhantomData<Value>,
    }

    impl<Value: SegValue + Zero + std::ops::Add<Output = Value> + std::ops::AddAssign> SegInfo
        for AddInfo<Value>
    {
        type Value = Value;
        fn id() -> Self::Value {
            Self::Value::zero()
        }
        fn op(x: &Self::Value, y: &Self::Value) -> Self::Value {
            x.clone() + y.clone()
        }
        fn op_assign(x: &mut Self::Value, y: &Self::Value) {
            *x += y.clone()
        }
    }
    pub struct MinInfo<Value: SegValue + MaxValue + std::cmp::Ord> {
        _marker: std::marker::PhantomData<Value>,
    }
    impl<Value: SegValue + MaxValue + std::cmp::Ord> SegInfo for MinInfo<Value> {
        type Value = Value;
        fn id() -> Self::Value {
            Self::Value::max_value()
        }
        fn op(x: &Self::Value, y: &Self::Value) -> Self::Value {
            x.clone().min(y.clone())
        }
    }
    pub struct MaxInfo<Value: SegValue + MinValue + std::cmp::Ord> {
        _marker: std::marker::PhantomData<Value>,
    }
    impl<Value: SegValue + MinValue + std::cmp::Ord> SegInfo for MaxInfo<Value> {
        type Value = Value;
        fn id() -> Self::Value {
            Self::Value::min_value()
        }
        fn op(x: &Self::Value, y: &Self::Value) -> Self::Value {
            x.clone().max(y.clone())
        }
    }

    #[derive(Clone)]
    pub struct Segtree<Info: SegInfo>(Vec<Info::Value>);

    impl<Info: SegInfo> Segtree<Info> {
        pub fn new(len: usize) -> Self {
            Self(vec![Info::id(); 2 * len])
        }

        pub fn is_empty(&self) -> bool {
            self.0.is_empty()
        }

        pub fn len(&self) -> usize {
            self.0.len() / 2
        }

        pub fn as_slice(&self) -> &[Info::Value] {
            &self.0[self.len()..self.0.len()]
        }

        pub fn iter(&self) -> std::slice::Iter<Info::Value> {
            self.0[self.len()..self.0.len()].iter()
        }

        pub fn vec(&self) -> Vec<Info::Value> {
            self.0[self.len()..self.0.len()].into()
        }

        pub fn set(&mut self, i: usize, x: Info::Value) {
            let i = i + self.len();
            self.0[i] = x;
            self.rebuild_one_leaf(i);
        }

        pub fn modify(&mut self, i: usize, f: impl Fn(&mut Info::Value)) {
            let i = i + self.len();
            f(&mut self.0[i]);
            self.rebuild_one_leaf(i);
        }

        pub fn fold(&self, mut l: usize, mut r: usize) -> Info::Value {
            assert!(l <= r && r <= self.len());
            l += self.len();
            r += self.len();
            let mut fl = Info::id();
            let mut fr = Info::id();
            while l < r {
                if l % 2 == 1 {
                    Info::op_assign(&mut fl, &self.0[l]);
                    l += 1;
                }
                if r % 2 == 1 {
                    r -= 1;
                    Info::op_assign(&mut fr, &self.0[r]);
                }
                l /= 2;
                r /= 2;
            }
            Info::op(&fl, &fr)
        }

        pub fn fold_all(&self) -> Info::Value {
            self.fold(0, self.len())
        }

        fn rebuild_one_leaf(&mut self, mut i: usize) {
            i /= 2;
            while 0 != i {
                self.0[i] = Info::op(&self.0[2 * i], &self.0[2 * i + 1]);
                i /= 2
            }
        }

        fn rebuild(&mut self) {
            for i in (1..self.len()).rev() {
                self.0[i] = Info::op(&self.0[2 * i], &self.0[2 * i + 1]);
            }
        }
    }

    impl<Info: SegInfo> std::convert::From<Vec<Info::Value>> for Segtree<Info> {
        fn from(vec: Vec<Info::Value>) -> Self {
            let mut vec1 = Vec::with_capacity(vec.len() * 2);
            vec1.resize(vec.len(), Info::id());
            vec1.extend(vec);
            let mut seg = Self(vec1);
            seg.rebuild();
            seg
        }
    }

    impl<Info: SegInfo, I: std::slice::SliceIndex<[Info::Value]>> std::ops::Index<I> for Segtree<Info> {
        type Output = I::Output;

        #[inline]
        fn index(&self, index: I) -> &Self::Output {
            std::ops::Index::index(self.as_slice(), index)
        }
    }

    impl<Info: SegInfo> std::fmt::Debug for Segtree<Info> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_list().entries(self.as_slice()).finish()
        }
    }

    #[macro_export]
    macro_rules! segtree {
        ($($elem:expr),*$(,)?) => {
            Segtree::from(vec![$($elem),*])
        };
        ($elem:expr; $n:expr) => {
            Segtree::from(vec![$elem; $n])
        };
    }
}
// }}}

use segtree::{MinInfo, Segtree};

fn main() {
    input!(n: usize, b: [i32; n], q: usize, mut lr: [(Usize1, usize); q]);
    lr.sort_by_key(|&(l, r)| (l, std::cmp::Reverse(r)));
    lr.dedup();

    let mut seg: Segtree<MinInfo<i32>> = segtree![std::i32::MAX; n+1];
    seg.set(0, n as i32 - b.iter().sum::<i32>());
    let mut pos = 0;
    for &(l, r) in lr.iter().chain(std::iter::once(&(n, n))) {
        while pos < l {
            let x = seg[pos];
            seg.modify(pos + 1, |y| y.change_min(x + 2 * b[pos] - 1));
            pos += 1;
        }
        let x = seg.fold(l, r);
        seg.modify(r, |y| y.change_min(x));
    }
    println!("{}", seg[n]);
}