/*
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
   🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊
*/

#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <iomanip>

using namespace std;

typedef long long ll;

mt19937 rnd(228);

const int N = 200000 + 7;
const int inf = 1e9;

int t[4 * N];
int d[4 * N];

void push(int v)
{
    if (d[v] != inf)
    {
        t[v * 2 + 1] = min(t[v * 2 + 1], d[v]);
        t[v * 2 + 2] = min(t[v * 2 + 2], d[v]);
        d[v * 2 + 1] = min(d[v * 2 + 1], d[v]);
        d[v * 2 + 2] = min(d[v * 2 + 2], d[v]);
        d[v] = inf;
    }
}

void upd(int v, int l, int r, int tl, int tr, int x)
{
    if (tl >= r || tr <= l)
    {
        return;
    }
    if (tl >= l && tr <= r)
    {
        d[v] = min(d[v], x);
        t[v] = min(t[v], x);
    }
    else
    {
        int tm = (tl + tr) / 2;
        push(v);
        upd(v * 2 + 1, l, r, tl, tm, x);
        upd(v * 2 + 2, l, r, tm, tr, x);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int l, int r, int tl, int tr)
{
    if (tl >= r || tr <= l)
    {
        return 1e9;
    }
    if (tl >= l && tr <= r)
    {
        return t[v];
    }
    else
    {
        int tm = (tl + tr) / 2;
        push(v);
        return min(get(v * 2 + 1, l, r, tl, tm), get(v * 2 + 2, l, r, tm, tr));
    }
}

bool cmp(pair <int, int> a, pair <int, int> b)
{
    if (a.second != b.second)
    {
        return a.second < b.second;
    }
    else
    {
        return a.first < b.first;
    }
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    for (int i = 0; i < 4 * N; i++)
    {
        t[i] = inf;
        d[i] = inf;
    }
    int n;
    scanf("%d", &n);
    vector <int> a(n);
    vector <int> s(n);
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        char c;
        scanf(" %c", &c);
        if (c == '0')
        {
            a[i] = 1;
        }
        else
        {
            a[i] = -1;
            cnt++;
        }
    }
    for (int i = n - 1; i >= 0; i--)
    {
        if (i != n - 1)
        {
            s[i] = a[i] + s[i + 1];
        }
        else
        {
            s[i] = a[i];
        }
    }
    int q;
    scanf("%d", &q);
    vector <int> pref(q);
    vector <pair <int, int> > e;
    for (int i = 0; i < q; i++)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        l--, r--;
        e.push_back({l, r});
    }
    sort(e.begin(), e.end(), cmp);
    int ans = 0;
    for (int i = 0; i < q; i++)
    {
        int l = e[i].first, r = e[i].second;
        int sum = s[l] - (r == n - 1 ? 0 : s[r + 1]);
        int dp = sum;
        int vl = -1, vr = -1;
        if (e[0].second < l)
        {
            vl = 0, vr = i;
            while (vl < vr - 1)
            {
                int vm = (vl + vr) / 2;
                if (e[vm].second < l)
                {
                    vl = vm;
                }
                else
                {
                    vr = vm;
                }
            }
            dp = min(dp, pref[vl] + sum);
        }
        dp = min(dp, get(0, l, l + 1, 0, n) - (r == n - 1 ? 0 : s[r + 1]));
        if (l != r)
        {
            upd(0, l + 1, r + 1, 0, n, dp + (r == n - 1 ? 0 : s[r + 1]));
        }
        ans = min(ans, dp);
        pref[i] = (i == 0 ? dp : min(pref[i - 1], dp));
    }
    cout << cnt + ans << '\n';
}


Measured memory_bytes: 12020