#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())

typedef long long ll;
typedef pair<int, int> point;

const int MAXN = 2e5 + 5, off = 1 << 18, inf = 1e9;

bool bio[2 * off];
vector<point> T[2 * off];

vector<point> Compress(vector<point> v) {
  vector<point> ret;
  sort(v.begin(), v.end());

  ret.push_back(v[0]);
  FOR(i, 1, sz(v)) {
    if(v[i].second < ret.back().second) {
      ret.push_back(v[i]);
    }
  }

  return ret;
}

int Query(int x, int a) {
  if(!bio[x]) {
    T[x] = Compress(T[x]);
    bio[x] = true;
  }

  int i = lower_bound(T[x].begin(), T[x].end(), point(a, -inf)) - T[x].begin(); --i;
  return T[x][i].second;
}

int Ask(int x, int lo, int hi, int a, int b) {
  if(lo >= b || hi <= a) {
    return inf;
  }
  if(lo >= a && hi <= b) {
    return Query(x, a);
  }

  int mid = (lo + hi) >> 1;

  return min(
             Ask(x * 2 + 0, lo, mid, a, b),
             Ask(x * 2 + 1, mid, hi, a, b)
             );
}

void upd(int i, point p) {
  i += off;
  for(; i; i /= 2) {
    T[i].push_back(p);
  }
}

int a[MAXN];
vector<int> l[MAXN];

int n;

int dp[MAXN];

void solve() {
  int global = 0, delta = 0;

  FOR(i, 1, n + 1) {
    delta = 1 - a[i];
    global += delta;

    for(auto x: l[i]) {
      int val = Ask(1, 0, off, x, i);
      val = min(val, dp[x - 1]);
      upd(i, {x, val});
    }

    upd(i, {i + 1, dp[i - 1] - delta + a[i]});
    upd(i, {0, inf});

    dp[i] = Query(i + off, MAXN);
  }

  cout << dp[n] + global << endl;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);

  cin >> n;
  REP(i, n) cin >> a[i + 1];

  int q; cin >> q;
  REP(i, q) {
    int x, y; cin >> x >> y;
    l[y].push_back(x);
  }

  solve();

  REP(i, 2 * off) {
    if(!sz(T[i])) continue;
    if(!bio[i]) T[i] = Compress(T[i]);
    FOR(j, 1, sz(T[i])) {
      assert(T[i][j].second < T[i][j - 1].second);
    }
  }
}

### Prompt

Please formulate a Cpp solution to the following 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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())

typedef long long ll;
typedef pair<int, int> point;

const int MAXN = 2e5 + 5, off = 1 << 18, inf = 1e9;

bool bio[2 * off];
vector<point> T[2 * off];

vector<point> Compress(vector<point> v) {
  vector<point> ret;
  sort(v.begin(), v.end());

  ret.push_back(v[0]);
  FOR(i, 1, sz(v)) {
    if(v[i].second < ret.back().second) {
      ret.push_back(v[i]);
    }
  }

  return ret;
}

int Query(int x, int a) {
  if(!bio[x]) {
    T[x] = Compress(T[x]);
    bio[x] = true;
  }

  int i = lower_bound(T[x].begin(), T[x].end(), point(a, -inf)) - T[x].begin(); --i;
  return T[x][i].second;
}

int Ask(int x, int lo, int hi, int a, int b) {
  if(lo >= b || hi <= a) {
    return inf;
  }
  if(lo >= a && hi <= b) {
    return Query(x, a);
  }

  int mid = (lo + hi) >> 1;

  return min(
             Ask(x * 2 + 0, lo, mid, a, b),
             Ask(x * 2 + 1, mid, hi, a, b)
             );
}

void upd(int i, point p) {
  i += off;
  for(; i; i /= 2) {
    T[i].push_back(p);
  }
}

int a[MAXN];
vector<int> l[MAXN];

int n;

int dp[MAXN];

void solve() {
  int global = 0, delta = 0;

  FOR(i, 1, n + 1) {
    delta = 1 - a[i];
    global += delta;

    for(auto x: l[i]) {
      int val = Ask(1, 0, off, x, i);
      val = min(val, dp[x - 1]);
      upd(i, {x, val});
    }

    upd(i, {i + 1, dp[i - 1] - delta + a[i]});
    upd(i, {0, inf});

    dp[i] = Query(i + off, MAXN);
  }

  cout << dp[n] + global << endl;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);

  cin >> n;
  REP(i, n) cin >> a[i + 1];

  int q; cin >> q;
  REP(i, q) {
    int x, y; cin >> x >> y;
    l[y].push_back(x);
  }

  solve();

  REP(i, 2 * off) {
    if(!sz(T[i])) continue;
    if(!bio[i]) T[i] = Compress(T[i]);
    FOR(j, 1, sz(T[i])) {
      assert(T[i][j].second < T[i][j - 1].second);
    }
  }
}

```