#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0,x,y;
    cin>>n>>a>>b;
    total=a+b;
    x=a;y=b;
    string str;
    cin>>str;
    bool st=0;
    for(int i=0;i<str.length();i++){
        int cnt=0;
        if(str[i]!='*'){
            //i++;
            while(str[i]!='*' && i<str.length()){
                cnt++;
                i++;
            }
            //i--;
        }
        //cout<<cnt<<endl;
        if(cnt%2!=0){
            if(a>b){
                a-=((cnt+1)/2);
                b-=(cnt/2);
            }
            else{
                b-=((cnt+1)/2);
                a-=(cnt/2);
            }
        }
        else{
            a-=(cnt/2);
            b-=(cnt/2);
        }
        if(a<0)a=0;
        if(b<0)b=0;
    }

    cout<<total-(a+b);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0,x,y;
    cin>>n>>a>>b;
    total=a+b;
    x=a;y=b;
    string str;
    cin>>str;
    bool st=0;
    for(int i=0;i<str.length();i++){
        if(a==0 && b==0)break;
        x=max(a,b),y=min(a,b);
        if(a>=b)st=1;
        for(int j=i,cn=0;j<str.length();cn++,j++){
            if(str[j]=='*'){
                i=j;
                break;
            }
            else if(cn%2==0 && x!=0){
                if(st && a==0){
                    continue;
                }
                else if(st==0 && b==0){
                    continue;
                }
                x--;
                if(st)a--;
                else b--;
            }
            else if(y!=0 && cn%2!=0){
                if(st && b==0){
                    continue;
                }
                else if(st==0 && a==0){
                    continue;
                }
                y--;
                if(st)b--;
                else a--;
            }
        }
        st=0;

    }
    cout<<total-(x+y);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0,x,y;
    cin>>n>>a>>b;
    total=a+b;
    x=a;y=b;
    string str;
    cin>>str;
    bool st=0;
    for(int i=0;i<str.length();i++){
        if(a==0 && b==0)break;
        x=max(a,b),y=min(a,b);
        if(a>=b)st=1;
        for(int j=i,cn=0;j<str.length();cn++,j++){
            if(str[j]=='*'){
                i=j;
                break;
            }
            if(cn%2==0 && x!=0){
                x--;
                if(st)a--;
                else b--;
            }
            else if(y!=0 && cn%2!=0){
                y--;
                if(st)b--;
                else a--;
            }
        }
        st=0;

    }
    cout<<total-(x+y);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0;
    cin>>n>>a>>b;
    total=a+b;
    string str;
    cin>>str;
    for(int i=0;i<str.length();i++){
        int x=max(a,b),y=min(a,b);
        for(int j=i,cn=0;j<str.length();cn++,j++){
            if(str[j]=='*'){
                i=j;
                break;
            }
            if(cn%2==0){
                x--;
                a--;
            }
            else{
                y--;
                b--;
            }
        }
    }
    cout<<total-(a+b);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0,x,y;
    cin>>n>>a>>b;
    total=a+b;
    x=a;y=b;
    string str;
    cin>>str;
    bool st=0;
    for(int i=0;i<str.length();i++){
        if(a==0 && b==0)break;
        x=max(a,b),y=min(a,b);
        if(a>b)st=1;
        for(int j=i,cn=0;j<str.length();cn++,j++){
            if(str[j]=='*'){
                i=j;
                break;
            }
            else if(cn%2==0 && x!=0){
                if(st && a==0){
                    continue;
                }
                else if(st==0 && b==0){
                    continue;
                }
                x--;
                if(st)a--;
                else b--;
            }
            else if(y!=0 && cn%2!=0){
                if(st && b==0){
                    continue;
                }
                else if(st==0 && a==0){
                    continue;
                }
                y--;
                if(st)b--;
                else a--;
            }
        }
        st=0;

    }
    x=max(a,b),y=min(a,b);
    cout<<total-(x+y);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0,x,y;
    cin>>n>>a>>b;
    total=a+b;
    x=a;y=b;
    string str;
    cin>>str;
    for(int i=0;i<str.length();i++){
        if(a==0 && b==0)break;
        x=max(a,b),y=min(a,b);
        for(int j=i,cn=0;j<str.length();cn++,j++){
            if(str[j]=='*'){
                i=j;
                break;
            }
            if(cn%2==0 && a!=0){
                x--;
                a--;
            }
            else if(b!=0 && cn%2!=0){
                y--;
                b--;
            }
        }
    }
    cout<<total-(x+y);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,total=0,x,y;
    cin>>n>>a>>b;
    total=a+b;
    x=a;y=b;
    string str;
    cin>>str;
    bool st=0;
    for(int i=0;i<str.length();i++){
        if(a==0 && b==0)break;
        x=max(a,b),y=min(a,b);
        if(a>b)st=1;
        for(int j=i,cn=0;j<str.length();cn++,j++){
            if(str[j]=='*'){
                i=j;
                break;
            }
            else if(cn%2==0 && x!=0){
                if(st && a==0){
                    continue;
                }
                else if(st==0 && b==0){
                    continue;
                }
                x--;
                if(st)a--;
                else b--;
            }
            else if(y!=0 && cn%2!=0){
                if(st && b==0){
                    continue;
                }
                else if(st==0 && a==0){
                    continue;
                }
                y--;
                if(st)b--;
                else a--;
            }
        }
        st=0;

    }
    cout<<total-(x+y);
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,s,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    x=a;y=b;
    vector<char>str(n+1);
    str[0]='*';
    for(i=1;i<=n;i++)
        cin>>str[i];
    for(i=0;i<n;i++)
    {
        if(str[i]=='.' && str[i-1]=='*')
        {
            if(a>=b && a>0)
            {
                str[i]='a';a--;
            }
            else if(b>0)
            {
                str[i]='b';b--;
            }
        }

        if(str[i]=='.' && str[i-1]=='a' && b>0) b-- , str[i]='b';
        else if(str[i]=='.' && str[i-1]=='a' && b==0) str[i]='*';
        else if(str[i]=='.' && str[i-1]=='b' && a>0) a-- , str[i]='a';
        else if(str[i]=='.' && str[i-1]=='b' && a>0) str[i]='*';

        if(a==0 && b==0)
            break;
    }
    cout<<x+y-(a+b)<<endl;
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,s,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    string str;
    cin>>str;
    cnt=0;
    if(str[0]=='.')
    {
        if(a>=b && a>0)
        {
            a--;
            x=1;
            cnt++;
        }
        else if(b>0)
        {
            b--;
            x=0;
            cnt++;
        }
    }
    for(i=1;i<n;i++)
    {
        if(str[i]=='.')
        {
            if(str[i-1]=='*' && (a>0 || b>0))
            {
                if(a>=b && a>0)
                {
                    a--;
                    x=1;
                    cnt++;
                }
                else if(b>0)
                {
                    b--;
                    x=0;
                    cnt++;
                }
            }
            else if(str[i-1]=='.' && x==1 && b>0)
            {
                b--;
                x=0;
                cnt++;
            }
            else if(str[i-1]=='.' && x==0 && a>0)
            {
                a--;
                x=1;
                cnt++;
            }
            if(a==0 && b==0)
                break;
        }
    }
    y=cnt;

    cnt=0;
    if(str[0]=='.')
    {
        if(b>=a && b>0)
        {
            b--;
            x=0;
            cnt++;
        }
        else if(a>0)
        {
            a--;
            x=1;
            cnt++;
        }
    }
    for(i=1;i<n;i++)
    {
        if(str[i]=='.')
        {
            if(str[i-1]=='*' && (a>0 || b>0))
            {
                if(b>=a && b>0)
                {
                    b--;
                    x=0;
                    cnt++;
                }
                else if(a>0)
                {
                    a--;
                    x=1;
                    cnt++;
                }
            }
            else if(str[i-1]=='.' && x==1 && b>0)
            {
                b--;
                x=0;
                cnt++;
            }
            else if(str[i-1]=='.' && x==0 && a>0)
            {
                a--;
                x=1;
                cnt++;
            }
            if(a==0 && b==0)
                break;
        }
    }
    y=max(y,cnt);
    p(y);
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,s,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    string str;
    cin>>str;
    cnt=0;
    if(str[0]=='.')
    {
        if(a>=b && a>0)
        {
            a--;
            x=1;
        }
        else
        {
            b--;
            x=0;
        }
        cnt++;
    }
    for(i=1;i<n;i++)
    {
        if(str[i]=='.')
        {
            if(str[i-1]=='*' && (a>0 || b>0))
            {
                if(a>=b && a>0)
                {
                    a--;
                    x=1;
                }
                else
                {
                    b--;
                    x=0;
                }
                cnt++;
            }
            else if(str[i-1]=='.' && x==1 && b>0)
            {
                b--;
                x^=1;
                cnt++;
            }
            else if(a>0)
            {
                a--;
                x^=1;
                cnt++;
            }
        }
        if(a==0 && b==0)
            break;
    }
    p(cnt);
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    x=a;y=b;
    vector<char>str(n+1);
    str[0]='*';
    for(i=1;i<=n;i++)
        cin>>str[i];
    for(i=1;i<=n;i++)
    {
        if(str[i]=='.')
        {
            if(str[i-1]=='*')
            {
                if(a>=b && a>0)
                {
                    str[i]='a',a--;
                }
                else if(b>0)
                {
                    str[i]='b',b--;
                }
            }

            if(str[i-1]=='a' && b>0) b-- , str[i]='b';
            else if(str[i-1]=='a' && b==0) str[i]='*';
            else if(str[i-1]=='b' && a>0) a-- , str[i]='a';
            else if(str[i-1]=='b' && a==0) str[i]='*';

            if(a==0 && b==0)
                break;
        }
    }
    cout<<x+y-(a+b)<<endl;
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,s,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    string str;
    cin>>str;
    cnt=0;
    if(str[0]=='.')
    {
        if(a>=b && a>0)
        {
            a--;
            x=1;
            cnt++;
        }
        else if(b>0)
        {
            b--;
            x=0;
            cnt++;
        }
    }
    for(i=1;i<n;i++)
    {
        if(str[i]=='.')
        {
            if(str[i-1]=='*' && (a>0 || b>0))
            {
                if(a>=b && a>0)
                {
                    a--;
                    x=1;
                    cnt++;
                }
                else if(b>0)
                {
                    b--;
                    x=0;
                    cnt++;
                }
            }
            else if(str[i-1]=='.' && x==1 && b>0)
            {
                b--;
                x=0;
                cnt++;
            }
            else if(str[i-1]=='.' && x==0 && a>0)
            {
                a--;
                x=1;
                cnt++;
            }
            if(a==0 && b==0)
                break;
        }
    }

    p(cnt);
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,s,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    x=a;y=b;
    vector<char>str(n+1);
    str[0]='*';
    for(i=1;i<=n;i++)
        cin>>str[i];
    for(i=0;i<n;i++)
    {
        if(str[i]=='.' && str[i-1]=='*')
        {
            if(a>=b && a>0)
            {
                str[i]='a';a--;
            }
            else if(b>0)
            {
                str[i]='b';b--;
            }
        }

        if(str[i]=='.' && str[i-1]=='a' && b>0) b--;str[i]=b;
        if(str[i]=='.' && str[i-1]=='a' && b==0) str[i]='*';
        if(str[i]=='.' && str[i-1]=='b' && a>0) a--; str[i]='a';
        if(str[i]=='.' && str[i-1]=='b' && a>0) str[i]='*';
    }
    cout<<x+y-(a+b)<<endl;
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lld long long int
#define ll long double
#define p(x) cout<<x<<endl
#define vect(x,n) vector<lld>x(n,0)
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<lld>())
#define all(v) v.begin(),v.end()
#define matrix(x,n,m) vector<vector<lld>>x(n,vector<lld>(m,0))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mkp make_pair
const lld N=1e9+7;
lld pow(lld x, lld y) { lld res = 1; while (y >(lld)0) { if (y &(lld)1) res = ((res) * (x)); y = y >>(lld)1; x = ((x) * (x)); } return res; }
lld powm(lld x, lld y,lld p) { lld res = 1; x = x % p; while (y >(lld)0) {if (y &(lld)1) res= (res * x) % p; y>>=(lld)1; x=(x*x) % p;}return res; }

void solve()
{
    lld i,j,k,ans,a,b,c,mx,mn,t,n,m,x,temp,p,q,y,z,l,r,g,h,s,flg,sum,d,sumf,odd,even,prev,cnt,diff;
    cin>>n>>a>>b;
    x=a;y=b;
    vector<char>str(n+1);
    str[0]='*';
    for(i=1;i<=n;i++)
        cin>>str[i];
    for(i=0;i<n;i++)
    {
        if(str[i]=='.' && str[i-1]=='*')
        {
            if(a>=b && a>0)
            {
                str[i]='a';a--;
            }
            else if(b>0)
            {
                str[i]='b';b--;
            }
        }

        if(str[i]=='.' && str[i-1]=='a' && b>0) b-- , str[i]=b;
        else if(str[i]=='.' && str[i-1]=='a' && b==0) str[i]='*';
        else if(str[i]=='.' && str[i-1]=='b' && a>0) a-- , str[i]='a';
        else if(str[i]=='.' && str[i-1]=='b' && a>0) str[i]='*';

        if(a==0 && b==0)
            break;
    }
    cout<<x+y-(a+b)<<endl;
    return;
}

int main()
{
    fast;
    lld t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#include <iostream>

int main(){
	int seats, programmers, athletes;
	char seating[seats];
	std::cin >> seats >> programmers >> athletes;
	std::cin >> seating;

	int usedSeats=0;
	for(int i=0; i<seats; i++){
		if(programmers==0 && athletes==0){break;}
		else if(seating[i]=='.'){
			if(i==0 || seating[i-1]=='*'){
				if(athletes>=programmers && athletes>0){
					seating[i]='A';
					athletes--;
					usedSeats++;
				}
				else if(programmers>athletes && programmers>0){
					seating[i]='B';
					programmers--;
					usedSeats++;
				}
			} else if(seating[i-1]=='A' && programmers>0){
				seating[i]='B';
				programmers--;
				usedSeats++;
			} else if(seating[i-1]=='B' && athletes>0){
				seating[i]='A';
				athletes--;
				usedSeats++;
			}
		}
	}
	std::cout<<usedSeats<std::endl;

	return 0;
}


#include <iostream>

int main(){
	int seats, programmers, athletes;
	char seating[seats];
	std::cin >> seats >> programmers >> athletes;
	std::cin >> seating;

	int usedSeats=0;
	for(int i=0; i<seats; i++){
		if(seating[i]=='.'){
			if(i==0 || seating[i-1]=='*'){
				if(athletes>=programmers && athletes>0){
					seating[i]='A';
					athletes--;
					usedSeats++;
				}
				else if(programmers>athletes && programmers>0){
					seating[i]='B';
					programmers--;
					usedSeats++;
				}
			} else if(seating[i-1]=='A' && programmers>0){
				seating[i]='B';
				programmers--;
				usedSeats++;
			} else if(seating[i-1]=='B' && athletes>0){
				seating[i]='A';
				athletes--;
				usedSeats++;
			}
		}
	}
	std::cout<<usedSeats;

	return 0;
}


#include <iostream>
#include <string>

int main(){
	int seats, programmers, athletes;
	//char seating[seats];
	std::string seating;
	std::cin >> seats >> programmers >> athletes;
	std::cin >> seating;

	int usedSeats=0;
	for(int i=0; i<seats; i++){
		if(programmers==0 && athletes==0){break;}
		else if(seating[i]=='.'){
			if(i==0 || seating[i-1]=='*'){
				if(athletes>=programmers && athletes>0){
					seating[i]='A';
					athletes--;
					usedSeats++;
				}
				else if(programmers>athletes && programmers>0){
					seating[i]='B';
					programmers--;
					usedSeats++;
				}
			} else if(seating[i-1]=='A' && programmers>0){
				seating[i]='B';
				programmers--;
				usedSeats++;
			} else if(seating[i-1]=='B' && athletes>0){
				seating[i]='A';
				athletes--;
				usedSeats++;
			}
		}
	}
	std::cout<<usedSeats<<std::endl;

	return 0;
}


#include <iostream>

int main(){
	int seats, programmers, athletes;
	char seating[seats];
	std::cin >> seats >> programmers >> athletes;
	std::cin >> seating;

	int usedSeats=0;
	for(int i=0; i<seats; i++){
		if(programmers==0 && athletes==0){break;}
		else if(seating[i]=='.'){
			if(i==0 || seating[i-1]=='*'){
				if(athletes>=programmers && athletes>0){
					seating[i]='A';
					athletes--;
					usedSeats++;
				}
				else if(programmers>athletes && programmers>0){
					seating[i]='B';
					programmers--;
					usedSeats++;
				}
			} else if(seating[i-1]=='A' && programmers>0){
				seating[i]='B';
				programmers--;
				usedSeats++;
			} else if(seating[i-1]=='B' && athletes>0){
				seating[i]='A';
				athletes--;
				usedSeats++;
			}
		}
	}
	std::cout<<usedSeats;

	return 0;
}


#include <iostream>
#include <string>

int main(){
	int seats, programmers, athletes;
	//char seating[seats];
	std::string seating;
	std::cin >> seats >> programmers >> athletes;
	std::cin >> seating;

	int usedSeats=0;
	for(int i=0; i<seats; i++){
		if(programmers==0 && athletes==0){break;}
		else if(seating[i]=='.'){
			if(i==0 || seating[i-1]=='*'){
				if(athletes>=programmers && athletes>0){
					seating[i]='A';
					athletes--;
					usedSeats++;
				}
				else if(programmers>athletes && programmers>0){
					seating[i]='B';
					programmers--;
					usedSeats++;
				}
			} else if((seating[i-1]=='A' || seating[i-1]=='.')&& programmers>0){
				seating[i]='B';
				programmers--;
				usedSeats++;
			} else if((seating[i-1]=='B' || seating[i-1]=='.')&& athletes>0){
				seating[i]='A';
				athletes--;
				usedSeats++;
			}
		}
	}
	std::cout<<usedSeats<<std::endl;

	return 0;
}


#include <iostream>

int main(){
	int seats, programmers, athletes;
	char seating[seats];
	std::cin >> seats >> programmers >> athletes;
	std::cin >> seating;

	int usedSeats=0;
	for(int i=0; i<seats; i++){
		if(programmers==0 && athletes==0){break;}
		else if(seating[i]=='.'){
			if(i==0 || seating[i-1]=='*'){
				if(athletes>=programmers && athletes>0){
					seating[i]='A';
					athletes--;
					usedSeats++;
				}
				else if(programmers>athletes && programmers>0){
					seating[i]='B';
					programmers--;
					usedSeats++;
				}
			} else if(seating[i-1]=='A' && programmers>0){
				seating[i]='B';
				programmers--;
				usedSeats++;
			} else if(seating[i-1]=='B' && athletes>0){
				seating[i]='A';
				athletes--;
				usedSeats++;
			}
		}
	}
	std::cout<<usedSeats<<std::endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main() {
	int n,a,b;
	scanf("%d%d%d", &n,&a,&b);
	/*cin >> n >> a >> b;*/
	char s[200000];
	scanf("%s", s);
	cin >> s;
	int counta=0, countb = 0;
	bool nexta=true;
	for (int i = 0; i < n; i++) {
		if (s[i] != '*') {
			if (nexta&&a>0) {
				counta++;
				a--;
				nexta = !nexta;
			}
			else if (!nexta&&b>0) {
				countb++;
				b--;
				nexta = !nexta;
			}
		}
		else {
			if (a > 0) nexta = true;
			else if (b > 0) nexta = false;
		}
	}

	}
	cout << counta + countb << endl;

	return 0;

}


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cmath>

using namespace std;

int main() {
	int n, a, b;
	scanf("%d%d%d", &n, &a, &b);
	/*cin >> n >> a >> b;*/
	if (b > a) {
		swap(a, b);
	}
	char s[200000];
	scanf("%s", s);
	vector<int>size;
	/*cin >> s;*/
	int counta = 0, countb = 0;
	int count = 0;
	for (int i = 0; i <= n; i++) {
		if (s[i] == '*' || i == n) {
			if (count != 0) size.push_back(count);
			count = 0;
		}
		else {
			count++;
		}
	};
	sort(size.begin(), size.end());
	for (int i = size.size() - 1; i >= 0; i--) {
		if (a == 0 && b == 0) break;
		int curGreater;
		int curLess;
		if (size[i] & 1 == 1) {
			curGreater = ceil((float)size[i] / 2);
			curLess = size[i] - curGreater;
		}
		else {
			curGreater = size[i] / 2;
			curLess = curGreater;
		}
		/*cout << curGreater << " " << curLess << endl;*/
		if (a > 0) {
			int value = curGreater ^ ((a^curGreater)&-(a < curGreater));
			a -= value;
			counta += value;
		}
		if (b > 0) {
			int value = curLess ^ ((b^curLess)&-(b < curLess));
			b -= value;
			countb += value;
		}
		if (b > a) {
			swap(a, b);
		}

	}
	cout << counta + countb << endl;
	return 0;

}


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdio>

using namespace std;

int main() {
	int n, a, b;
	scanf("%d%d%d", &n, &a, &b);
	/*cin >> n >> a >> b;*/
	if (b > a) {
		swap(a, b);
	}
	char s[200000];
	scanf("%s", s);
	vector<int>size;
	/*cin >> s;*/
	int counta = 0, countb = 0;
	int count = 0;
	for (int i = 0; i <= n; i++) {
		if (s[i] == '*' || i == n) {
			if (count != 0) size.push_back(count);
			count = 0;
		}
		else {
			count++;
		}
	};
	sort(size.begin(), size.end());
	for (int i = size.size() - 1; i >= 0; i--) {
		if (a == 0 && b == 0) break;
		int curGreater;
		int curLess;
		if (size[i] & 1 == 1) {
			curGreater = ceil((float)size[i] / 2);
			curLess = size[i] - curGreater;
		}
		else {
			curGreater = size[i] / 2;
			curLess = curGreater;
		}
		/*cout << curGreater << " " << curLess << endl;*/
		if (a > 0) {
			int value = curGreater ^ ((a^curGreater)&-(a < curGreater));
			a -= value;
			counta += value;
		}
		if (b > 0) {
			int value = curLess ^ ((b^curLess)&-(b < curLess));
			b -= value;
			countb += value;
		}
		if (b > a) {
			swap(a, b);
		}

	}
	cout << counta + countb << endl;
	return 0;

}


#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main() {
	int n,a,b;
	scanf("%d%d%d", &n,&a,&b);
	/*cin >> n >> a >> b;*/
	char s[200000];
	scanf("%s", s);
	cin >> s;
	int counta=0, countb = 0;
	bool nexta=true;
	for (int i = 0; i < n; i++) {
		if (s[i] != '*') {
			if (nexta&&a>0) {
				counta++;
				a--;
				nexta = !nexta;
			}
			else if (!nexta&&b>0) {
				countb++;
				b--;
				nexta = !nexta;
			}
		}
		else nexta = true;
	}
	cout << counta + countb << endl;

	return 0;

}


#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main() {
	int n,a,b;
	scanf("%d%d%d", &n,&a,&b);
	/*cin >> n >> a >> b;*/
	char s[200000];
	scanf("%s", s);
	cin >> s;
	int counta=0, countb = 0;
	bool nexta;
	if (a > 0) nexta = true;
	else nexta = false;
	for (int i = 0; i < n; i++) {
		if (s[i] != '*') {
			if (nexta&&a>0) {
				counta++;
				a--;
				nexta = false;
			}
			else if (!nexta&&b>0) {
				countb++;
				b--;
				nexta = true;
			}
		}
		else {
			if (a > 0) nexta = true;
			else if (b > 0) nexta = false;
		}
	}
	cout << counta + countb << endl;

	return 0;

}


#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main() {
	int n,a,b;
	scanf("%d%d%d", &n,&a,&b);
	/*cin >> n >> a >> b;*/
	char s[200000];
	scanf("%s", s);
	cin >> s;
	int counta=0, countb = 0;
	bool nexta=true;
	for (int i = 0; i < n; i++) {
		if (s[i] != '*') {
			if (nexta&&a>0) {
				counta++;
				a--;
			}
			else if(b>0){
				countb++;
				b--;
			}
		}
		else nexta = true;
	}
	cout << counta + countb << endl;
	return 0;

}


#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main() {
	int n,a,b;
	scanf("%d%d%d", &n,&a,&b);
	/*cin >> n >> a >> b;*/
	char s[200000];
	scanf("%s", s);
	cin >> s;
	int counta=0, countb = 0;
	bool nexta=true;
	for (int i = 0; i < n; i++) {
		if (s[i] != '*') {
			if (nexta&&a>0) {
				counta++;
				a--;
				nexta = !nexta;
			}
			else if (!nexta&&b>0) {
				countb++;
				b--;
				nexta = !nexta;
			}
		}
		else {
			if (a > 0) nexta = true;
			else if (b > 0) nexta = false;
		}
	}
	cout << counta + countb << endl;

	return 0;

}


#include<iostream>
#include<string.h>
#include<set>
#include<map>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<time.h>
#include<vector>
#include<iomanip>
using namespace std;
#define ll long long
#define FLAG if(flag)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define mp make_pair
#define pb push_back
#define fori(a,b) for(ll i=(a);i<=(b);++i)
#define forj(a,b) for(ll j=(a);j<=(b);++j)
#define fors(a,b) for(ll i=(a);i<(b);++i)
const ll mod=1e9+7;
//ll qpow(ll n,ll k) {ll ans=1;while(k>0){if(k&1) ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans;}
//ll INV(ll x){ return x==1?x:1LL*(mod-mod/x)*INV(mod%x)%mod; }
//ll gcd(ll a,ll b){if(a<b)swap(a,b);return a%b==0?b:gcd(b,a%b);}
const ll MAX=1e6+10;
ll a[MAX];
ll b[MAX];
int main(){
ll n,k,m,sum,t,len;
string s,d;
//freopen("Ain.txt","r",stdin);
while(cin>>n>>m>>k){
     int flag=1;
     sum=0;
     cin>>s;
     len=s.size();
     if(m<k)swap(m,k);
     fors(0,len){
     if(s[i]=='.'&&m>0&&s[i-1]!='1'&&s[i+1]!='1'){m--;s[i]='1';sum++;}
     if(s[i]=='.'&&k>0&&s[i-1]!='0'&&s[i+1]!='0'){k--;s[i]='0';sum++;}
     //cout<<s[i]<<' ';
     }
     //cout<<endl;
    cout<<sum<<endl;
 }
//能去重优化？n=1?n=0?特殊情况考虑一下？
return 0;
}


#include<iostream>
#include<string.h>
#include<set>
#include<map>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<time.h>
#include<vector>
#include<iomanip>
using namespace std;
#define ll long long
#define FLAG if(flag)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define mp make_pair
#define pb push_back
#define fori(a,b) for(ll i=(a);i<=(b);++i)
#define forj(a,b) for(ll j=(a);j<=(b);++j)
#define fors(a,b) for(ll i=(a);i<(b);++i)
const ll mod=1e9+7;
//ll qpow(ll n,ll k) {ll ans=1;while(k>0){if(k&1) ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans;}
//ll INV(ll x){ return x==1?x:1LL*(mod-mod/x)*INV(mod%x)%mod; }
//ll gcd(ll a,ll b){if(a<b)swap(a,b);return a%b==0?b:gcd(b,a%b);}
const ll MAX=1e6+10;
ll a[MAX];
ll b[MAX];
int main(){
ll n,k,m,sum,t,len;
string s,d;
//freopen("Ain.txt","r",stdin);
while(cin>>n>>m>>k){
     int flag=1;
     sum=0;
     cin>>s;
     len=s.size();
     if(m>k)swap(m,k);
     fors(0,len){
     if(s[i]=='.'&&m>0&&s[i-1]!='1'&&s[i+1]!='1'){m--;s[i]='1';sum++;}
     if(s[i]=='.'&&k>0&&s[i-1]!='0'&&s[i+1]!='0'){k--;s[i]='0';sum++;}
     }
    cout<<sum<<endl;
 }
//能去重优化？n=1?n=0?特殊情况考虑一下？
return 0;
}


#include<iostream>
#include<string.h>
#include<set>
#include<map>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<time.h>
#include<vector>
#include<iomanip>
using namespace std;
#define ll long long
#define FLAG if(flag)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define mp make_pair
#define pb push_back
#define fori(a,b) for(ll i=(a);i<=(b);++i)
#define forj(a,b) for(ll j=(a);j<=(b);++j)
#define fors(a,b) for(ll i=(a);i<(b);++i)
const ll mod=1e9+7;
//ll qpow(ll n,ll k) {ll ans=1;while(k>0){if(k&1) ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans;}
//ll INV(ll x){ return x==1?x:1LL*(mod-mod/x)*INV(mod%x)%mod; }
//ll gcd(ll a,ll b){if(a<b)swap(a,b);return a%b==0?b:gcd(b,a%b);}
const ll MAX=1e6+10;
ll a[MAX];
ll b[MAX];
int main(){
ll n,k,m,sum,t,len;
string s,d;
//freopen("Ain.txt","r",stdin);
while(cin>>n>>m>>k){
     int flag=1;
     sum=0;
     cin>>s;
     len=s.size();
     fors(0,len){
         if(m<k)swap(m,k);
     if(s[i]=='.'&&m>0&&s[i-1]!='1'&&s[i-1]!='.'){m--;s[i]='1';sum++;}
     if(s[i]=='.'&&k>0&&s[i-1]!='0'&&s[i-1]!='.'){k--;s[i]='0';sum++;}
    //cout<<s[i]<<' ';
     }
     //cout<<endl;
    cout<<sum<<endl;
 }
//能去重优化？n=1?n=0?特殊情况考虑一下？
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define mod 1000000007
#define pb emplace_back
#define mp make_pair
#define fi first
#define sec second
typedef long long ll;
typedef long double  ld;
#define pii pair<ll,ll>
#define vi vector< ll >
#define vvi vector< vi >
#define vpi vector< pii >
#define vvpi vector< vpi >
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test(t) ll t;cin>>t;while(t--)
#define mem(a,b) memset(a,b,sizeof(a))
#define inn freopen("input.txt", "r", stdin)
#define outt freopen("output.txt", "w", stdout)
#define all(arr) arr.begin(),arr.end()
#define fr(i,n) for(ll i=0;i<(n);++i)
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define per(i,a,b) for(ll i=a;i>=b;i--)
#define remin(a,b) (a=min((a),(b)))
#define remax(a,b) (a=max((a),(b)))
#define debarr(a,n) cerr<<#a<<" : ";for(ll i=0;i<n;i++) cerr<<a[i]<<" "; cerr<<endl;
#define debmat(mat,row,col) cerr<<#mat<<" :\n";for(ll i=0;i<row;i++) {for(ll j=0;j<col;j++) cerr<<mat[i][j]<<" ";cerr<<endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {ll idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
ll power(ll x,ll y){ll res = 1;while(y){if(y&1) res = (res*x)%mod;y=y/2,x=(x*x)%mod;}return res%mod;}
typedef int fType;
typedef int cType;
const fType INF = 0x3f3f3f3f;
// check for flow in i ^ 1
struct edge {
    int u, v;fType flow;cType cost;
    edge() {}
    edge(int u, int v, fType f, cType c) : u(u), v(v), flow(f), cost(c) {}
};
struct MinCostMaxFlow {
    int N;vector < vector <int> > G;
    vector <edge> E;int numEdges;
    vector <int> found, dad;
    vector <cType> dist;

    MinCostMaxFlow(int N):
     N(N), G(N), numEdges(0), found(N), dad(N), dist(N)  {}

    void addEdge(int from, int to, fType capacity, cType cost) {
        // pr(from,to,capacity,cost);
        G[from].push_back(numEdges++);
        E.push_back(edge(from, to, capacity, cost));
        G[to].push_back(numEdges++);
        E.push_back(edge(to, from, 0, cType(-1) * cost));
    }
    bool spfa(int s, int t) {
        fill(dad.begin(), dad.end(), -1);
        fill(dist.begin(), dist.end(), INF);
        fill(found.begin(), found.end(), 0);
        queue <int> Q;dist[s] = 0;Q.push(s);found[s] = true;
        while(!Q.empty()) {
            int u = Q.front(); Q.pop();
            if(u == t) continue;
            for (int i = 0; i < (int)G[u].size(); ++i) {
                edge &pres = E[G[u][i]];
                int v = pres.v;
                if(pres.flow <= 0) continue;
                if(dist[u] + pres.cost < dist[v]) {
                    dad[v] = G[u][i];
                    dist[v] = dist[u] + pres.cost;
                    if(!found[v]) Q.push(v), found[v] = true;
                }
            }
            found[u] = false;
        }
        return (dad[t] != -1);
    }
    fType dfs(int t) {
        fType flow = INF;
        for(int i = dad[t]; i != -1; i = dad[E[i].u]) {
            if(E[i].flow < flow) flow = E[i].flow;
        }
        for(int i = dad[t]; i != -1; i = dad[E[i].u]) {
            E[i].flow -= flow;
            E[i ^ 1].flow += flow;
        }
        return flow;
    }
    pair <fType, cType> getMaxFlow(int s, int t) {
        fType totflow = 0;
        cType totcost = 0;
        while(spfa(s,t)) {
            fType amt = dfs(t);
            totflow += amt;
            totcost += dist[t] * (cType)amt;
        }
        return make_pair(totflow, totcost);
    }
};
signed main()
{
	fast;
	ll n,i,j,m;
	cin>>n;
	string s;
	cin>>s;
	cin>>m;
	string arr[m];
	ll val[m];
	fr(i,m)cin>>arr[i]>>val[i];
	ll x;
	cin>>x;
	ll source = n+1,sink = n;
	MinCostMaxFlow df(source+1);
	df.addEdge(source,0,x,0);
	for(i=0;i<n;i++)
		df.addEdge(i,i+1,x,0);
	for(i=0;i<m;i++)
	{
		for(j=0;j<=n-arr[i].length();j++)
		{
			ll k;
			for(k=j;k<j+arr[i].length();k++)
			{
				if(s[k]!=arr[i][k-j])
					break;
			}
			if(k==arr[i].length()+j)
				df.addEdge(j,k,1,val[i]*-1);
		}
	}
	cout<<df.getMaxFlow(source,sink).sec*-1;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string a;
  cin >> a;
  int m;
  cin >> m;
  vector<string> S(m);
  vi V(m);
  for (int i = 0; i < m; ++i) cin >> S[i] >> V[i];
  vvi B(m, vi(n, 0));
  for (int i = 0; i < m; ++i) {
    for (int j = (int)S[i].size() -1; j < n; ++j) {
      bool pot = true;
      int ind = j - (int)S[i].size()+1;
      for (int w = 0; w < S[i].size(); ++w) {
        if (S[i][w] != a[ind + w]) {
          pot = false;
          break;
        }
      }
      if (pot) B[i][j] = 1;
    }
  }
  int x;
  cin >> x;
  int res = 0;
  while (x--) {
    vi D(n+1, 0);
    vi T(n+1, -1);
    for (int i = 1; i <= n; ++i) {
      D[i] = D[i-1];
      for (int j = 0; j < m; ++j) {
        if (B[j][i-1]) {
          int i2 = i - (int)S[j].size();
          int val = D[i2] + V[j];
          if (D[i] < val) {
            D[i] = val;
            T[i] = j;
          }
        }
      }
    }
    res += D[n];
    int ind = n;
    while (ind) {
      if (T[ind] == -1) --ind;
      else {
        B[T[ind]][ind-1] = 0;
        ind -= (int)S[T[ind]].size();
      }
    }
  }
  cout << res << '\n';
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> PLI;

#define NN 600
#define pot(u,v) (pi[u]-pi[v])
#define REP(i,a) for(int i = 0; i < a; ++i)
int adj[NN][NN], deg[NN], padre[NN];
ll w[NN][NN], cap[NN][NN], pi[NN], d[NN], f[NN][NN], dist[NN];
int INF = 1000000000;
int N;
ll flow, cost;
bool dijkstra(int s, int t) {
  memset(padre, -1, sizeof(padre));
  REP(i,N) d[i] = INF;
  d[s] = 0;
  priority_queue<PLI> Q;
  Q.push(PLI(0,s));
  while (not Q.empty()) {
    int u = Q.top().second;
    ll dist = -Q.top().first;
    Q.pop();
    if (dist != d[u]) continue;
    REP(i,deg[u]) {
      int v = adj[u][i];
      if (f[u][v] >= 0 and cap[u][v] - f[u][v] > 0 and
          d[v] > d[u] + pot(u,v) + w[u][v]) {
        d[v] = d[u] + pot(u,v) + w[u][v];
        Q.push(PLI(-d[v], v));
        padre[v] = u;
      }
      else if (f[u][v] < 0 and d[v] > d[u] + pot(u,v) - w[v][u]) {
        d[v] = d[u] + pot(u,v) - w[v][u];
        Q.push(PLI(-d[v], v));
        padre[v] = u;
      }
    }
  }
  REP(i,N) if (pi[i] < INF) pi[i] += d[i];
  return padre[t] >= 0;
}
void maxmin(int s, int t) {
  memset(f, 0, sizeof(f));
  memset(pi, 0, sizeof(pi));
  flow = cost = 0;
  while (dijkstra(s, t)) {
    ll bot = INF;
    for (int v = t, u = padre[v]; u != -1; v = u, u = padre[u]) {
      if (f[u][v] >= 0) bot = min(cap[u][v] - f[u][v], bot);
      else bot = min(f[v][u], bot);
    }
    for (int v = t, u = padre[v]; u != -1; v = u, u = padre[u]) {
      if (f[u][v] >= 0) cost += w[u][v]*bot;
      else cost -= w[v][u]*bot;
      f[u][v] += bot;
      f[v][u] -= bot;
    }
    flow += bot;
  }
}
void negative_edges(int s, int t) {
  for (int i = 0; i < N; ++i) dist[i] = INF;
  dist[s] = 0;
  for (int k = 0; k < N; ++k) {
    for (int x = 0; x < N; ++x) {
      for (int j = 0; j < deg[x]; ++j) {
        int y = adj[x][j];
        if (!cap[x][y]) continue;
        dist[y] = min(dist[x] + w[x][y], dist[y]);
      }}}
  for (int x = 0; x < N; ++x) {
    for (int j = 0; j < deg[x]; ++j) {
      int y = adj[x][j];
      if (!cap[x][y]) continue;
      w[x][y] += dist[x] - dist[y];
    }
  }
  maxmin(s, t);
  cost += flow*dist[t];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  N = n+2;
  string a;
  cin >> a;
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    string b;
    int val;
    cin >> b >> val;
    int sz = (int)b.size();
    for (int j = 0; j + sz <= n; ++j) {
      bool pot = true;
      for (int w = 0; w < sz; ++w) {
        if (a[j + w] != b[w]) {
          pot = false;
          break;
        }
      }
      if (pot) {
        int x = j+1;
        int y = x + sz;
        adj[x][deg[x]++] = y;
        adj[y][deg[y]++] = x;
        cap[x][y] = 1;
        w[x][y] = -val;
      }
    }
  }
  int x;
  cin >> x;
  for (int i = 0; i <= n; ++i) {
    adj[i][deg[i]++] = i+1;
    adj[i+1][deg[i+1]++] = i;
    cap[i][i+1] = x;
  }
  negative_edges(0, N-1);
  cout << -cost << '\n';
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string a;
  cin >> a;
  int m;
  cin >> m;
  vector<string> S(m);
  vi V(m);
  for (int i = 0; i < m; ++i) cin >> S[i] >> V[i];
  vvi B(m, vi(n, 0));
  for (int i = 0; i < m; ++i) {
    for (int j = (int)S[i].size() -1; j < n; ++j) {
      bool pot = true;
      int ind = j - (int)S[i].size()+1;
      for (int w = 0; w < S[i].size(); ++w) {
        if (S[i][w] != a[ind + w]) {
          pot = false;
          break;
        }
      }
      if (pot) B[i][j] = 1;
    }
  }
  int x;
  cin >> x;
  vi D(n*x+1, 0);
  for (int i = 0; i < m; ++i) {
    for (int w = 0; w < n; ++w) {
      if (B[i][w] == 0) continue;
      for (int j = n*(x-1) + w; j >= 0; j -= n) {
        if (B[i][j%n]) D[j+1] = max(D[j+1], D[j - (int)S[i].size() + 1] + V[i]);
      }
      for (int j = 1; j <= n*x; ++j) D[j] = max(D[j], D[j-1]);
    }
  }
  cout << D[n*x] << '\n';
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;

int inc;

struct cosa {
  int val;
  cosa* f[5];
  cosa() {
    val = inc++;
    for (int i = 0; i < 5; ++i) f[i] = nullptr;
  }
};

const int N = 6000000;
int V[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  inc = 0;
  cosa* root = new cosa();
  for (int i = 0; i < n; ++i) {
    string a;
    cin >> a;
    cosa* p = root;
    for (auto c : a) {
      int t = c - 'a';
      if (p->f[t] == nullptr) {
        p->f[t] = new cosa();
      }
      p = p->f[t];
    }
    ++V[p->val];
  }
  for (int i = 0; i < m; ++i) {
    string a;
    cin >> a;
    int conta = 0;
    for (auto c : a) if (c == '?') ++conta;
    int val = 1;
    for (int j = 0; j < conta; ++j) val *= 6;
    set<int> S;
    for (int mask = 0; mask < val; ++mask) {
      int m = mask;
      cosa* p = root;
      bool pot = true;
      for (auto c : a) {
        int t = c - 'a';
        if (c == '?') {
          t = m%6;
          m /= 6;
          if (t == 5) continue;
        }
        if (p->f[t] == nullptr) {
          pot = false;
          break;
        }
        p = p->f[t];
      }
      if (pot) S.insert(p->val);
    }
    int res = 0;
    for (auto x : S) res += V[x];
    cout << res << '\n';
  }
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<ll, ll> pli;

const ll INF = 100000000000ll;

#define VEI(w,e) ((E[e].u == w)? E[e].v : E[e].u)
#define CAP(w,e) ((E[e].u == w)? E[e].cap - E[e].flow : E[e].flow)
#define COST(w,e) ((E[e].u == w)? E[e].cost : -E[e].cost)
#define POT(u,v) (pot[u] - pot[v])
#define ADD(w,e,f) E[e].flow += ((E[e].u==w)? (f) : (-(f)))

struct Edge {
  int u, v;
  ll cap;
  ll cost;
  ll flow;
};

vi pare;
vll d, pot;

bool dijkstra(int s, int t, vvi& adj, vector<Edge>& E) {
  pare = vi(adj.size(), -1);
  d = vll(adj.size(), INF);
  d[s] = 0;
  priority_queue<pli> Q;
  Q.push(pli(0,s));
  while (not Q.empty()) {
    int u = (int)Q.top().second;
    ll dist = -Q.top().first;
    Q.pop();
    if (dist != d[u]) continue;
    for (int i = 0; i < (int)adj[u].size(); ++i) {
      int e = adj[u][i], v = VEI(u, e);
      if (CAP(u,e) and d[v] > d[u] + POT(u,v) + COST(u,e)) {
        d[v] = d[u] + POT(u,v) + COST(u,e);
        Q.push(pli(-d[v], v));
        pare[v] = e;
      }
    }
  }
  for (int i = 0; i < (int)adj.size(); ++i)
    if (pot[i] < INF) pot[i] += d[i];
  return pare[t] >= 0;
}
pli maxmin(int s, int t, vvi& adj, vector<Edge>& E) {
  ll flow = 0, cost = 0;
  for (int i = 0; i < (int)E.size(); ++i) E[i].flow = 0;
  pot = vll(adj.size(), 0);
  while (dijkstra(s, t, adj, E)) {
    ll bot = INF;
    int v = t;
    while (pare[v] != -1) {
      int e = pare[v], u = VEI(v,e);
      bot = min(CAP(u,e), bot);
      v = u;
    }
    v = t;
    while (pare[v] != -1) {
      int e = pare[v], u = VEI(v,e);
      ADD(u,e,bot);
      cost += bot*COST(u,e);
      v = u;
    }
    flow += bot;
  }
  return pli(flow, cost);
}
pli negative_edges(int s, int t, vvi& adj, vector<Edge>& E) {
  for (int i = 0; i < (int)E.size(); ++i) E[i].flow = 0;
  vll dist(adj.size(), INF);
  dist[s] = 0;
  for (int k = 0; k < (int)adj.size(); ++k) {
    for (int x = 0; x < (int)adj.size(); ++x) {
      for (int j = 0; j < (int)adj[x].size(); ++j) {
        int e = adj[x][j], y = VEI(x,e);
        if (CAP(x,e) == 0) continue;
        dist[y] = min(dist[x] + COST(x,e), dist[y]);
      }}}
  for (int x = 0; x < (int)adj.size(); ++x) {
    for (int j = 0; j < (int)adj[x].size(); ++j) {
      int e = adj[x][j], y = VEI(x,e);
      if (CAP(x,e) == 0) continue;
      E[e].cost += dist[x] - dist[y];
    }
  }
  ll flow, cost;
  tie(flow, cost) = maxmin(s, t, adj, E);
  cost += flow*dist[t];
  return pli(flow, cost);
}

void addEdge(int u, int v, ll cap, ll cost, vvi& adj, vector<Edge>& E) {
  Edge e;
  e.u = u, e.v = v, e.cap = cap, e.cost = cost;
  adj[u].push_back((int)E.size());
  adj[v].push_back((int)E.size());
  E.push_back(e);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string a;
  cin >> a;
  int m;
  cin >> m;
  vector<Edge> E;
  vvi adj(n+2);
  for (int i = 0; i < m; ++i) {
    string b;
    int val;
    cin >> b >> val;
    int sz = (int)b.size();
    for (int j = 0; j + sz <= n; ++j) {
      bool pot = true;
      for (int w = 0; w < sz; ++w) {
        if (a[j + w] != b[w]) {
          pot = false;
          break;
        }
      }
      if (pot) {
        int x = j+1;
        int y = x + sz;
        addEdge(x,y,1,-val, adj, E);
      }
    }
  }
  int x;
  cin >> x;
  for (int i = 0; i <= n; ++i) {
    addEdge(i,i+1,x,0,adj, E);
  }
  auto res = negative_edges(0, n+1, adj, E);
  assert(res.first == x);
  cout << -res.second << '\n';
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> PLI;

#define NN 600
#define pot(u,v) (pi[u]-pi[v])
#define REP(i,a) for(int i = 0; i < a; ++i)
int adj[NN][NN], deg[NN], padre[NN];
ll w[NN][NN], cap[NN][NN], pi[NN], d[NN], f[NN][NN], dist[NN];
int INF = 1000000000;
int N;
ll flow, cost;
bool dijkstra(int s, int t) {
  memset(padre, -1, sizeof(padre));
  REP(i,N) d[i] = INF;
  d[s] = 0;
  priority_queue<PLI> Q;
  Q.push(PLI(0,s));
  while (not Q.empty()) {
    int u = Q.top().second;
    ll dist = -Q.top().first;
    Q.pop();
    if (dist != d[u]) continue;
    REP(i,deg[u]) {
      int v = adj[u][i];
      if (f[u][v] >= 0 and cap[u][v] - f[u][v] > 0 and
          d[v] > d[u] + pot(u,v) + w[u][v]) {
        d[v] = d[u] + pot(u,v) + w[u][v];
        Q.push(PLI(-d[v], v));
        padre[v] = u;
      }
      else if (f[u][v] < 0 and d[v] > d[u] + pot(u,v) - w[v][u]) {
        d[v] = d[u] + pot(u,v) - w[v][u];
        Q.push(PLI(-d[v], v));
        padre[v] = u;
      }
    }
  }
  REP(i,N) if (pi[i] < INF) pi[i] += d[i];
  return padre[t] >= 0;
}
void maxmin(int s, int t) {
  memset(f, 0, sizeof(f));
  memset(pi, 0, sizeof(pi));
  flow = cost = 0;
  while (dijkstra(s, t)) {
    ll bot = INF;
    for (int v = t, u = padre[v]; u != -1; v = u, u = padre[u]) {
      if (f[u][v] >= 0) bot = min(cap[u][v] - f[u][v], bot);
      else bot = min(f[v][u], bot);
    }
    for (int v = t, u = padre[v]; u != -1; v = u, u = padre[u]) {
      if (f[u][v] >= 0) cost += w[u][v]*bot;
      else cost -= w[v][u]*bot;
      f[u][v] += bot;
      f[v][u] -= bot;
    }
    flow += bot;
  }
}
void negative_edges(int s, int t) {
  for (int i = 0; i < N; ++i) dist[i] = INF;
  dist[s] = 0;
  for (int k = 0; k < N; ++k) {
    for (int x = 0; x < N; ++x) {
      for (int j = 0; j < deg[x]; ++j) {
        int y = adj[x][j];
        if (!cap[x][y]) continue;
        dist[y] = min(dist[x] + w[x][y], dist[y]);
      }}}
  for (int x = 0; x < N; ++x) {
    for (int j = 0; j < deg[x]; ++j) {
      int y = adj[x][j];
      if (!cap[x][y]) continue;
      w[x][y] += dist[x] - dist[y];
    }
  }
  maxmin(s, t);
  cost += flow*dist[t];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  N = n+2;
  string a;
  cin >> a;
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    string b;
    int val;
    cin >> b >> val;
    int sz = (int)b.size();
    for (int j = 0; j + sz <= n; ++j) {
      bool pot = true;
      for (int w = 0; w < sz; ++w) {
        if (a[j + w] != b[w]) {
          pot = false;
          break;
        }
      }
      if (pot) {
        int x = j+1;
        int y = x + sz;
        adj[x][deg[x]++] = y;
        adj[y][deg[y]++] = x;
        cap[x][y] = 1;
        w[x][y] = -val;
      }
    }
  }
  int x;
  cin >> x;
  for (int i = 0; i <= n; ++i) {
    adj[i][deg[i]++] = i+1;
    adj[i+1][deg[i+1]++] = i;
    cap[i][i+1] = x;
  }
  negative_edges(0, N-1);
  assert(flow == x);
  cout << -cost << '\n';
}


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

#define MAXN 200100
#define MAXM 2000200
#define INF 0x3f3f3f3f

struct ZKW_flow{
    int S, T, ecnt;
    int head[MAXN];
    int c[MAXM], w[MAXM], to[MAXM], next[MAXM];
    ZKW_flow(){
        memset(head,0,sizeof(head));
        ecnt = 2;
    }

    void add_edge(int u, int v, int _c, int ww){
        c[ecnt] = _c; w[ecnt] = ww; to[ecnt] = v;
        next[ecnt] = head[u]; head[u] = ecnt++;
        c[ecnt] = 0; w[ecnt] = -ww; to[ecnt] = u;
        next[ecnt] = head[v]; head[v] = ecnt++;
    }

    int dis[MAXN];

    void SPFA(){
        for(int i = 1; i <= T; ++i) dis[i] = INF;
        priority_queue<pair<int, int> > Q;
        dis[S] = 0;
        Q.push(make_pair(0, S));
        while(!Q.empty()){
            int u = Q.top().second, d = -Q.top().first;
            Q.pop();
            if(dis[u] != d) continue;
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && dis[v] > d + w[p]){
                    dis[v] = d + w[p];
                    Q.push(make_pair(-dis[v], v));
                }
            }
        }
        for(int i = 1; i <= T; ++i) dis[i] = dis[T] - dis[i];
    }

    int ans;
    bool use[MAXN];

    int add_flow(int u, int flow){
        if(u == T){
            ans += dis[S] * flow;
            return flow;
        }
        use[u] = true;
        int now = flow;
        for(int p = head[u]; p; p = next[p]){
            int &v = to[p];
            if(c[p] && !use[v] && dis[u] == dis[v] + w[p]){
                int tmp = add_flow(v, min(now, c[p]));
                c[p] -= tmp;
                c[p^1] += tmp;
                now -= tmp;
                if(!now) break;
            }
        }
        return flow - now;
    }

    bool modify_label(){
        int d = INF;
        for(int u = 1; u <= T; ++u) if(use[u])
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && !use[v]) d = min(d, dis[v] + w[p] - dis[u]);
            }
        if(d == INF) return false;
        for(int i = 1; i <= T; ++i) if(use[i]) dis[i] += d;
        return true;
    }

    int solve(){
        SPFA();
        while(true){
            while(true){
                for(int i = 1; i <= T; ++i) use[i] = 0;
                if(!add_flow(S, INF)) break;
            }
            if(!modify_label()) break;
        }
        return ans;
    }
} MCMF;

const int N = 550;

char str[N], pat[N][N];
int p[N];

struct Data {
	int id, l, r, val;
	Data(int id = 0, int l = 0, int r = 0, int val = 0) : id(id), l(l), r(r), val(val) {}
	bool operator < (const Data &d) const {
		if (l != d.l) return l < d.l;
		if (r != d.r) return r < d.r;
		if (id != d.id) return id < d.id;
		return val < d.val;
	}
} P[N * N];

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n >> str + 1;
	int m; cin >> m;
	for (int i = 1; i <= m; i++) cin >> pat[i] >> p[i];
	int k; cin >> k;

	int inc = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) if (!strncmp(str + i, pat[j], strlen(pat[j]))) {
			inc++;
			P[inc] = Data(inc, i, i + strlen(pat[j]) - 1, p[j]);
		}
	}
//	cerr << strncmp(str + 4, pat[4], 2) << "OK" << endl;
	sort(P + 1, P + inc + 1);
	int src = inc + inc + 1;
	MCMF.S = src + 1, MCMF.T = MCMF.S + 1;
	for (int i = 1; i <= inc; i++) {
		int u = P[i].id, v = 0;
		auto it = lower_bound(P + 1, P + inc + 1, Data(0, P[i].r + 1, P[i].r + 1, 0));
// 		int cnt = k;
		while (it < P + inc + 1) {
			v = it -> id;
//			cerr << u << " " << P[i].l << " " << P[i].r << " -> " << v << " " << it -> l << " " << it -> r << endl;
			MCMF.add_edge(u + inc, v, 1, 0);
			it++;
		}
	}
	for (int i = 1; i <= inc; i++) MCMF.add_edge(P[i].id, P[i].id + inc, 1, -P[i].val);
	for (int i = 1; i <= inc; i++) MCMF.add_edge(src, P[i].id, 1, 0), MCMF.add_edge(P[i].id + inc, MCMF.T, 1, 0);
	MCMF.add_edge(MCMF.S, src, k, 0);

	cout << -MCMF.solve() << endl;

	return 0;
}


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

#define MAXN 200100
#define MAXM 2000200
#define INF 0x3f3f3f3f

struct ZKW_flow{
    int S, T, ecnt;
    int head[MAXN];
    int c[MAXM], w[MAXM], to[MAXM], next[MAXM];
    ZKW_flow(){
        memset(head,0,sizeof(head));
        ecnt = 2;
    }

    void add_edge(int u, int v, int _c, int ww){
        c[ecnt] = _c; w[ecnt] = ww; to[ecnt] = v;
        next[ecnt] = head[u]; head[u] = ecnt++;
        c[ecnt] = 0; w[ecnt] = -ww; to[ecnt] = u;
        next[ecnt] = head[v]; head[v] = ecnt++;
    }

    int dis[MAXN];

    void SPFA(){
        for(int i = 1; i <= T; ++i) dis[i] = INF;
        priority_queue<pair<int, int> > Q;
        dis[S] = 0;
        Q.push(make_pair(0, S));
        while(!Q.empty()){
            int u = Q.top().second, d = -Q.top().first;
            Q.pop();
            if(dis[u] != d) continue;
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && dis[v] > d + w[p]){
                    dis[v] = d + w[p];
                    Q.push(make_pair(-dis[v], v));
                }
            }
        }
        for(int i = 1; i <= T; ++i) dis[i] = dis[T] - dis[i];
    }

    int ans;
    bool use[MAXN];

    int add_flow(int u, int flow){
        if(u == T){
            ans += dis[S] * flow;
            return flow;
        }
        use[u] = true;
        int now = flow;
        for(int p = head[u]; p; p = next[p]){
            int &v = to[p];
            if(c[p] && !use[v] && dis[u] == dis[v] + w[p]){
                int tmp = add_flow(v, min(now, c[p]));
                c[p] -= tmp;
                c[p^1] += tmp;
                now -= tmp;
                if(!now) break;
            }
        }
        return flow - now;
    }

    bool modify_label(){
        int d = INF;
        for(int u = 1; u <= T; ++u) if(use[u])
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && !use[v]) d = min(d, dis[v] + w[p] - dis[u]);
            }
        if(d == INF) return false;
        for(int i = 1; i <= T; ++i) if(use[i]) dis[i] += d;
        return true;
    }

    int solve(){
        SPFA();
        while(true){
            while(true){
                for(int i = 1; i <= T; ++i) use[i] = 0;
                if(!add_flow(S, INF)) break;
            }
            if(!modify_label()) break;
        }
        return ans;
    }
} MCMF;

const int N = 550;

char str[N], pat[N][N];
int p[N];

struct Data {
	int id, l, r, val;
	Data(int id = 0, int l = 0, int r = 0, int val = 0) : id(id), l(l), r(r), val(val) {}
	bool operator < (const Data &d) const {
		if (l != d.l) return l < d.l;
		if (r != d.r) return r < d.r;
		if (id != d.id) return id < d.id;
		return val < d.val;
	}
} P[N * N];

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n >> str + 1;
	int m; cin >> m;
	for (int i = 1; i <= m; i++) cin >> pat[i] >> p[i];
	int k; cin >> k;

	int inc = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) if (!strncmp(str + i, pat[j], strlen(pat[j]))) {
			P[++inc] = Data(inc, i, i + strlen(pat[j]) - 1, p[j]);
		}
	}

	sort(P + 1, P + inc + 1);
	int src = inc + inc + 1;
	MCMF.S = src + 1, MCMF.T = MCMF.S + 1;
	for (int i = 1; i <= inc; i++) {
		int u = P[i].id, v = 0;
		auto it = lower_bound(P + 1, P + inc + 1, Data(0, P[i].r + 1, P[i].r + 1, 0));
		if (it < P + inc + 1) {
			v = it -> id;
			MCMF.add_edge(u + inc, v, 1, 0);
		}
	}
	for (int i = 1; i <= inc; i++) MCMF.add_edge(P[i].id, P[i].id + inc, 1, -P[i].val);
	for (int i = 1; i <= inc; i++) MCMF.add_edge(src, P[i].id, 1, 0), MCMF.add_edge(P[i].id + inc, MCMF.T, 1, 0);
	MCMF.add_edge(MCMF.S, src, k, 0);

	cout << -MCMF.solve() << endl;

	return 0;
}


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

#define MAXN 200100
#define MAXM 2000200
#define INF 0x3f3f3f3f

struct ZKW_flow{
    int S, T, ecnt;
    int head[MAXN];
    int c[MAXM], w[MAXM], to[MAXM], next[MAXM];
    ZKW_flow(){
        memset(head,0,sizeof(head));
        ecnt = 2;
    }

    void add_edge(int u, int v, int _c, int ww){
        c[ecnt] = _c; w[ecnt] = ww; to[ecnt] = v;
        next[ecnt] = head[u]; head[u] = ecnt++;
        c[ecnt] = 0; w[ecnt] = -ww; to[ecnt] = u;
        next[ecnt] = head[v]; head[v] = ecnt++;
    }

    int dis[MAXN];

    void SPFA(){
        for(int i = 1; i <= T; ++i) dis[i] = INF;
        priority_queue<pair<int, int> > Q;
        dis[S] = 0;
        Q.push(make_pair(0, S));
        while(!Q.empty()){
            int u = Q.top().second, d = -Q.top().first;
            Q.pop();
            if(dis[u] != d) continue;
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && dis[v] > d + w[p]){
                    dis[v] = d + w[p];
                    Q.push(make_pair(-dis[v], v));
                }
            }
        }
        for(int i = 1; i <= T; ++i) dis[i] = dis[T] - dis[i];
    }

    int ans;
    bool use[MAXN];

    int add_flow(int u, int flow){
        if(u == T){
            ans += dis[S] * flow;
            return flow;
        }
        use[u] = true;
        int now = flow;
        for(int p = head[u]; p; p = next[p]){
            int &v = to[p];
            if(c[p] && !use[v] && dis[u] == dis[v] + w[p]){
                int tmp = add_flow(v, min(now, c[p]));
                c[p] -= tmp;
                c[p^1] += tmp;
                now -= tmp;
                if(!now) break;
            }
        }
        return flow - now;
    }

    bool modify_label(){
        int d = INF;
        for(int u = 1; u <= T; ++u) if(use[u])
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && !use[v]) d = min(d, dis[v] + w[p] - dis[u]);
            }
        if(d == INF) return false;
        for(int i = 1; i <= T; ++i) if(use[i]) dis[i] += d;
        return true;
    }

    int solve(){
        SPFA();
        while(true){
            while(true){
                for(int i = 1; i <= T; ++i) use[i] = 0;
                if(!add_flow(S, INF)) break;
            }
            if(!modify_label()) break;
        }
        return ans;
    }
} MCMF;

const int N = 550;

char str[N], pat[N][N];
int p[N];

struct Data {
	int id, l, r, val;
	Data(int id = 0, int l = 0, int r = 0, int val = 0) : id(id), l(l), r(r), val(val) {}
	bool operator < (const Data &d) const {
		if (l != d.l) return l < d.l;
		if (r != d.r) return r < d.r;
		if (id != d.id) return id < d.id;
		return val < d.val;
	}
} P[N * N];

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n >> str + 1;
	int m; cin >> m;
	for (int i = 1; i <= m; i++) cin >> pat[i] >> p[i];
	int k; cin >> k;

	int inc = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) if (!strncmp(str + i, pat[j], strlen(pat[j]))) {
			inc++;
			P[inc] = Data(inc, i, i + strlen(pat[j]) - 1, p[j]);
		}
	}
//	cerr << strncmp(str + 4, pat[4], 2) << "OK" << endl;
	sort(P + 1, P + inc + 1);
	int src = inc + inc + 1;
	MCMF.S = src + 1, MCMF.T = MCMF.S + 1;
	for (int i = 1; i <= inc; i++) {
		int u = P[i].id, v = 0;
		auto it = lower_bound(P + 1, P + inc + 1, Data(0, P[i].r + 1, P[i].r + 1, 0));
		int cnt = k;
		while (it < P + inc + 1 && cnt--) {
			v = it -> id;
//			cerr << u << " " << P[i].l << " " << P[i].r << " -> " << v << " " << it -> l << " " << it -> r << endl;
			MCMF.add_edge(u + inc, v, 1, 0);
			it++;
		}
	}
	for (int i = 1; i <= inc; i++) MCMF.add_edge(P[i].id, P[i].id + inc, 1, -P[i].val);
	for (int i = 1; i <= inc; i++) MCMF.add_edge(src, P[i].id, 1, 0), MCMF.add_edge(P[i].id + inc, MCMF.T, 1, 0);
	MCMF.add_edge(MCMF.S, src, k, 0);

	cout << -MCMF.solve() << endl;

	return 0;
}


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

#define MAXN 200100
#define MAXM 2000200
#define INF 0x3f3f3f3f

struct ZKW_flow{
    int S, T, ecnt;
    int head[MAXN];
    int c[MAXM], w[MAXM], to[MAXM], next[MAXM];
    ZKW_flow(){
        memset(head,0,sizeof(head));
        ecnt = 2;
    }

    void add_edge(int u, int v, int _c, int ww){
        c[ecnt] = _c; w[ecnt] = ww; to[ecnt] = v;
        next[ecnt] = head[u]; head[u] = ecnt++;
        c[ecnt] = 0; w[ecnt] = -ww; to[ecnt] = u;
        next[ecnt] = head[v]; head[v] = ecnt++;
    }

    int dis[MAXN];

    void SPFA(){
        for(int i = 1; i <= T; ++i) dis[i] = INF;
        priority_queue<pair<int, int> > Q;
        dis[S] = 0;
        Q.push(make_pair(0, S));
        while(!Q.empty()){
            int u = Q.top().second, d = -Q.top().first;
            Q.pop();
            if(dis[u] != d) continue;
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && dis[v] > d + w[p]){
                    dis[v] = d + w[p];
                    Q.push(make_pair(-dis[v], v));
                }
            }
        }
        for(int i = 1; i <= T; ++i) dis[i] = dis[T] - dis[i];
    }

    int ans;
    bool use[MAXN];

    int add_flow(int u, int flow){
        if(u == T){
            ans += dis[S] * flow;
            return flow;
        }
        use[u] = true;
        int now = flow;
        for(int p = head[u]; p; p = next[p]){
            int &v = to[p];
            if(c[p] && !use[v] && dis[u] == dis[v] + w[p]){
                int tmp = add_flow(v, min(now, c[p]));
                c[p] -= tmp;
                c[p^1] += tmp;
                now -= tmp;
                if(!now) break;
            }
        }
        return flow - now;
    }

    bool modify_label(){
        int d = INF;
        for(int u = 1; u <= T; ++u) if(use[u])
            for(int p = head[u]; p; p = next[p]){
                int &v = to[p];
                if(c[p] && !use[v]) d = min(d, dis[v] + w[p] - dis[u]);
            }
        if(d == INF) return false;
        for(int i = 1; i <= T; ++i) if(use[i]) dis[i] += d;
        return true;
    }

    int solve(){
        SPFA();
        while(true){
            while(true){
                for(int i = 1; i <= T; ++i) use[i] = 0;
                if(!add_flow(S, INF)) break;
            }
            if(!modify_label()) break;
        }
        return ans;
    }
} MCMF;

const int N = 550;

char str[N], pat[N][N];
int p[N];

struct Data {
	int id, l, r, val;
	Data(int id = 0, int l = 0, int r = 0, int val = 0) : id(id), l(l), r(r), val(val) {}
	bool operator < (const Data &d) const {
		if (l != d.l) return l < d.l;
		if (r != d.r) return r < d.r;
		if (id != d.id) return id < d.id;
		return val < d.val;
	}
} P[N * N];

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n >> str + 1;
	int m; cin >> m;
	for (int i = 1; i <= m; i++) cin >> pat[i] >> p[i];
	int k; cin >> k;

	int inc = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) if (!strncmp(str + i, pat[j], strlen(pat[j]))) {
		    inc++;
			P[inc] = Data(inc, i, i + strlen(pat[j]) - 1, p[j]);
		}
	}

	sort(P + 1, P + inc + 1);
	int src = inc + inc + 1;
	MCMF.S = src + 1, MCMF.T = MCMF.S + 1;
	for (int i = 1; i <= inc; i++) {
		int u = P[i].id, v = 0;
		auto it = lower_bound(P + 1, P + inc + 1, Data(0, P[i].r + 1, P[i].r + 1, 0));
		if (it < P + inc + 1) {
			v = it -> id;
			MCMF.add_edge(u + inc, v, 1, 0);
		}
	}
	for (int i = 1; i <= inc; i++) MCMF.add_edge(P[i].id, P[i].id + inc, 1, -P[i].val);
	for (int i = 1; i <= inc; i++) MCMF.add_edge(src, P[i].id, 1, 0), MCMF.add_edge(P[i].id + inc, MCMF.T, 1, 0);
	MCMF.add_edge(MCMF.S, src, k, 0);

	cout << -MCMF.solve() << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
#else
#define trace(...) 1
#endif
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout<<setprecision(25);
#define endl '\n'
#define F first
#define S second
#define I insert
#define pb push_back
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpll vector<pll>

// code credits -->> https://www.codechef.com/viewsolution/13525207 (jtnydv25)

//Works for negative costs, but does not work for negative cycles
//Complexity: O(min(E^2 *V log V, E logV * flow))

// to use -> graph G(n), G.add_edge(u,v,cap,cost), G.min_cost_max_flow(s,t)
const long long INF = 99999999; // ********* INF is used in both flow_type and cost_type so change accordingly

// vertices are 0-indexed
struct graph {
  typedef ll flow_type;
  typedef ll cost_type;
  struct edge {
    int src, dst;
    flow_type capacity, flow;
    cost_type cost;
    size_t rev;
  };
  vector<edge> edges;
  void add_edge(int src, int dst, flow_type cap, cost_type cost) {
    adj[src].push_back({src, dst, cap, 0, cost, adj[dst].size()});
    adj[dst].push_back({dst, src, 0, 0, -cost, adj[src].size()-1});
  }
  int n;
  vector<vector<edge>> adj;
  graph(int n) : n(n), adj(n) { }

  pair<flow_type, cost_type> min_cost_max_flow(int s, int t) {
    flow_type flow = 0;
    cost_type cost = 0;

    for (int u = 0; u < n; ++u) // initialize
      for (auto &e: adj[u]) e.flow = 0;

    vector<cost_type> p(n, 0);

    auto rcost = [&](edge e) { return e.cost + p[e.src] - p[e.dst]; };
    for (int iter = 0; ; ++iter) {
      vector<int> prev(n, -1); prev[s] = 0;
      vector<cost_type> dist(n, INF); dist[s] = 0;
      if (iter == 0) { // use Bellman-Ford to remove negative cost edges
        vector<int> count(n); count[s] = 1;
        queue<int> que;
        for (que.push(s); !que.empty(); ) {
          int u = que.front(); que.pop();
          count[u] = -count[u];
          for (auto &e: adj[u]) {
            if (e.capacity > e.flow && dist[e.dst] > dist[e.src] + rcost(e)) {
              dist[e.dst] = dist[e.src] + rcost(e);
              prev[e.dst] = e.rev;
              if (count[e.dst] <= 0) {
                count[e.dst] = -count[e.dst] + 1;
                que.push(e.dst);
              }
            }
          }
        }
        for(int i=0;i<n;i++) p[i] = dist[i];
      } else { // use Dijkstra
        typedef pair<cost_type, int> node;
        priority_queue<node, vector<node>, greater<node>> que;
        que.push({0, s});
        while (!que.empty()) {
          node a = que.top(); que.pop();
          if (a.S == t) break;
          if (dist[a.S] > a.F) continue;
          for (auto e: adj[a.S]) {
            if (e.capacity > e.flow && dist[e.dst] > a.F + rcost(e)) {
              dist[e.dst] = dist[e.src] + rcost(e);
              prev[e.dst] = e.rev;
              que.push({dist[e.dst], e.dst});
            }
          }
        }
      }
      if (prev[t] == -1) break;

      for (int u = 0; u < n; ++u)
        if (dist[u] < dist[t]) p[u] += dist[u] - dist[t];

      function<flow_type(int,flow_type)> augment = [&](int u, flow_type cur) {
        if (u == s) return cur;
        edge &r = adj[u][prev[u]], &e = adj[r.dst][r.rev];
        flow_type f = augment(e.src, min(e.capacity - e.flow, cur));
        e.flow += f; r.flow -= f;
        return f;
      };
      flow_type f = augment(t, INF);
      flow += f;
      cost += f * (p[t] - p[s]);
    }
    return {flow, cost};
  }
};

const ll N = 1e5+5;
const ll M = 320;
string ta[105];
ll pa[105];
int main()
{
    IOS
	ll n,m,p,x; string s,t; cin>>n>>s;
	graph g(n+2); cin>>m; for(ll i=0;i<m;i++) cin>>ta[i]>>pa[i]; cin>>x;
	for(ll i=1;i<=n+1;i++){
		g.add_edge(i-1,i,x,0);
	}
	for(ll k=0;k<m;k++){
		t = ta[k], p = pa[k]; ll l = t.length();
		trace(t,p);
		for(ll i=0;i<n-l+1;i++){
			ll mt = 1;
			for(ll j=0;j<l;j++){
				if(t[j] != s[j+i]) {mt = 0; break;}
			}
			if(mt) g.add_edge(i+1,i+l+1,1,-p), trace(i+1,i+l+1,-p);
		}
	}
	ll mf,mc; tie(mf,mc) = g.min_cost_max_flow(0,n+1);
	trace(mf,mc);
	cout<<-mc<<endl;
}


#include <bits/stdc++.h>
using namespace std;
// #define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
#else
#define trace(...) 1
#endif
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout<<setprecision(25);
#define endl '\n'
#define F first
#define S second
#define I insert
#define pb push_back
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpll vector<pll>

// code credits -->> https://github.com/Ashishgup1/Competitive-Coding/blob/master/Min%20Cost%20Max%20Flow%20-%20Dijkstra

//Works for negative costs, but does not work for negative cycles
//Complexity: O(min(E^2 *V log V, E logV * flow))
// vertices are 0-indexed
struct edge
{
  ll to, flow, cap, cost, rev;
};

struct MinCostMaxFlow
{
  ll nodes;
  vector<ll> prio, curflow, prevedge, prevnode, q, pot;
  vector<bool> inqueue;
  vector<vector<edge> > graph;
  MinCostMaxFlow() {}

  MinCostMaxFlow(ll n): nodes(n), prio(n, 0), curflow(n, 0),
  prevedge(n, 0), prevnode(n, 0), q(n, 0), pot(n, 0), inqueue(n, 0), graph(n) {}

  void addEdge(ll source, ll to, ll capacity, ll cost)
  {
    edge a = {to, 0, capacity, cost, (ll)graph[to].size()};
    edge b = {source, 0, 0, -cost, (ll)graph[source].size()};
    graph[source].push_back(a);
    graph[to].push_back(b);
  }

  void bellman_ford(ll source, vector<ll> &dist)
  {
    fill(dist.begin(), dist.end(), LLONG_MAX);
    dist[source] = 0;
    ll qt=0;
    q[qt++] = source;
    for(ll qh=0;(qh-qt)%nodes!=0;qh++)
    {
      ll u = q[qh%nodes];
      inqueue[u] = false;
      for(auto &e : graph[u])
      {
        if(e.flow >= e.cap)
          continue;
        ll v = e.to;
        ll newDist = dist[u] + e.cost;
        if(dist[v] > newDist)
        {
          dist[v] = newDist;
          if(!inqueue[v])
          {
            inqueue[v] = true;
            q[qt++ % nodes] = v;
          }
        }
      }
    }
  }

  pair<ll, ll> minCostFlow(ll source, ll dest, ll maxflow)
  {
    bellman_ford(source, pot);
    ll flow = 0;
    ll flow_cost = 0;
    while(flow < maxflow)
    {
      priority_queue<pair<ll, ll>, vector<pair<ll, ll> >, greater<pair<ll, ll> > > q;
      q.push({0, source});
      fill(prio.begin(), prio.end(), LLONG_MAX);
      prio[source] = 0;
      curflow[source] = LLONG_MAX;
      while(!q.empty())
      {
        ll d = q.top().first;
        ll u = q.top().second;
        q.pop();
        if(d != prio[u])
          continue;
        for(ll i=0;i<graph[u].size();i++)
        {
          edge &e=graph[u][i];
          ll v = e.to;
          if(e.flow >= e.cap)
            continue;
          ll newPrio = prio[u] + e.cost + pot[u] - pot[v];
          if(prio[v] > newPrio)
          {
            prio[v] = newPrio;
            q.push({newPrio, v});
            prevnode[v] = u;
            prevedge[v] = i;
            curflow[v] = min(curflow[u], e.cap - e.flow);
          }
        }
      }
      if(prio[dest] == LLONG_MAX)
        break;
      for(ll i=0;i<nodes;i++)
        pot[i]+=prio[i];
      ll df = min(curflow[dest], maxflow - flow);
      flow += df;
      for(ll v=dest;v!=source;v=prevnode[v])
      {
        edge &e = graph[prevnode[v]][prevedge[v]];
        e.flow += df;
        graph[v][e.rev].flow -= df;
        flow_cost += df * e.cost;
      }
    }
    return {flow, flow_cost};
  }
};

//Problem 1: https://www.spoj.com/problems/GREED/
//Solution 1: http://p.ip.fi/ODRk

//Problem 2 (Double Cost): https://codeforces.com/contest/277/problem/E
//Solution 2: https://codeforces.com/contest/277/submission/43180845

string ta[105];
ll pa[105];
int main()
{
    IOS
	ll n,m,p,x; string s,t; cin>>n>>s;
	MinCostMaxFlow g(n+2); cin>>m; for(ll i=0;i<m;i++) cin>>ta[i]>>pa[i]; cin>>x;
	for(ll i=1;i<=n+1;i++){
		g.addEdge(i-1,i,x,0);
	}
	for(ll k=0;k<m;k++){
		t = ta[k], p = pa[k]; ll l = t.length();
		trace(t,p);
		for(ll i=0;i<n-l+1;i++){
			ll mt = 1;
			for(ll j=0;j<l;j++){
				if(t[j] != s[j+i]) {mt = 0; break;}
			}
			if(mt) g.addEdge(i+1,i+l+1,1,-p), trace(i+1,i+l+1,-p);
		}
	}
	ll mf,mc; tie(mf,mc) = g.minCostFlow(0,n+1,(1e9));
	trace(mf,mc);
	cout<<-mc<<endl;
}


#include <bits/stdc++.h>
using namespace std;
struct Edge{
	int to, cap, cost, next;
};
Edge E[880301];
int head[555], ne = 1;
inline void addEdge(int from, int to, int cap, int cost) {
	E[++ne] = {to, cap, cost, head[from]}, head[from] = ne;
	E[++ne] = {from, 0, -cost, head[to]}, head[to] = ne;
}
int d[555], p[555];
bool inqueue[555];
pair<int, int> mcmf(int S, int T) {
	int f = 0, c = 0;
	while(1) {
		for(int i = S; i <= T; ++i) d[i] = 1e9;
		queue<int> Q;
		Q.push(S); d[S] = 0;
		while(!Q.empty()) {
			auto v = Q.front(); Q.pop();
			inqueue[v] = 0;
			for(int x = head[v]; x; x = E[x].next) {
				int &u = E[x].to, &cap = E[x].cap, &cost = E[x].cost;
				if(cap > 0 and d[v] + cost < d[u]) {
					d[u] = d[v] + cost;
					p[u] = x;
					if(!inqueue[u]) Q.push(u), inqueue[u] = 1;
				}
			}
		}
		if(d[T] == 1e9) break;
		int df = 1e9;
		for(int i = T; i != S; i = E[p[i]^1].to)
			df = min(df, E[p[i]].cap);
		for(int i = T; i != S; i = E[p[i]^1].to)
			E[p[i]].cap -= df, E[p[i]^1].cap += df;
		f += df, c += df * d[T];
	}
	return {f, c};
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n, m, x;
	string crossword;
	cin >> n >> crossword;
	cin >> m;
	int S = 0, T = n+1;
	for(int i = 1; i <= m; ++i) {
		string word;
		int p;
		cin >> word >> p;
		int pos = 0;
		while(1) {
			pos = crossword.find(word, pos);
			if(pos == string::npos) break;
			addEdge(pos+1, pos+1+word.size(), 1, -p);
			pos++;
		}
	}
	cin >> x;
	for(int i = 0; i <= n; ++i)
		addEdge(i, i+1, x, 0);
	auto ans = mcmf(S, T);
	cout << -ans.second << endl;
	return 0;
}


// oct code title CF/CF717G.cpp

#ifndef OCT_CODE_INSERT_PREx2dDOCUMENT  // oct code insert pre-document
#define OCT_CODE_INSERT_PREx2dDOCUMENT

#ifdef ONLINE_JUDGEs
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#ifndef STL_BITS__2F_STDCx2Bx2Bx2FH
#define STL_BITS__2F_STDCx2Bx2Bx2FH
#include <bits/stdc++.h>
#endif

#define debug std::cerr << "Debug(" << __LINE__ << "): "
#define pub push_back
#define pob pop_back
#define pup push
#define mkp make_pair
#define fir first
#define sec second
#define ite iterator
#define lob lower_bound
#define upb upper_bound
#define reg register

using uint8 = unsigned char;
using uint16 = unsigned short int;
using uint32 = unsigned int;
using uint64 = unsigned long long;
using int8 = signed char;
using int16 = short int;
using int32 = int;
using int64 = long long;
using pi32 = std::pair<int32, int32>;
using pi64 = std::pair<int64, int64>;

namespace oct {

/* Array tn4 is the 4-direction changes in coordinate system.
 * The directions in order is {right, up, left, down}.
 */
const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                       {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const double exp = 1e-8;

void sync(int pre = 8);
template <typename _Tp>
_Tp &mad(_Tp &x, _Tp y);
template <typename _Tp>
_Tp &mid(_Tp &x, _Tp y);
template <typename _Tp>
bool in(_Tp x, _Tp l, _Tp r);
template <typename _Tp>
bool in(_Tp x, _Tp y, _Tp l, _Tp r);
template <typename _Tp>
_Tp sqr(_Tp x);
template <typename _Tp>
_Tp power(_Tp x, int64 m);
template <typename _Tp>
void sort(_Tp &x, _Tp &y);
template <typename _Tp1, typename _Tp2>
std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                std::pair<_Tp1, _Tp2> y);
template <typename _Tp>
_Tp &operator+=(_Tp &x, _Tp y);
template <typename _Tp>
_Tp gcd(_Tp &x, _Tp &y);

inline void sync(int pre) {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    std::cout.flags(std::ios::fixed);
    std::cout.precision(pre);
    std::cout.setf(std::ios::showpoint);
}
template <typename _Tp>
inline _Tp &mad(_Tp &x, _Tp y) {
    return x = std::max(x, y);
}
template <typename _Tp>
inline _Tp &mid(_Tp &x, _Tp y) {
    return x = std::min(x, y);
}
template <typename _Tp>
inline bool in(_Tp x, _Tp l, _Tp r) {
    return l <= x && x <= r;
}
template <typename _Tp>
inline bool in(_Tp x, _Tp y, _Tp l, _Tp r) {
    return l <= x && y <= r;
}
template <typename _Tp>
inline _Tp sqr(_Tp x) {
    return x * x;
}
template <typename _Tp>
inline _Tp power(_Tp x, int64 m) {
    return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2));
}
template <typename _Tp>
inline void sort(_Tp &x, _Tp &y) {
    if (x > y) std::swap(x, y);
}
template <typename _Tp1, typename _Tp2>
inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x,
                                       std::pair<_Tp1, _Tp2> y) {
    return x.fir += y.fir, x.sec += y.sec, x;
}
template <typename _Tp>
inline _Tp &operator+=(_Tp &x, _Tp y) {
    return x = x + y;
}
template <typename _Tp>
inline _Tp gcd(_Tp &x, _Tp &y) {
    return std::__gcd(x, y);
}

}  // namespace oct

#endif  // oct code end pre-document

const int C = 600;
const int N = 100005;

void insert(int u, int v, int c, int f);
void addedge(int u, int v, int c, int f);
int bfs();

char s1[C], s2[C];
int n, m, cnt = 1, S, T, ans;
int to[N], next[N], head[C], cost[N], flow[N], pe[C], pv[C], dis[C], inq[C];
std::queue<int> q;

inline void insert(int u, int v, int c, int f) {
    next[++cnt] = head[u], head[u] = cnt;
    to[cnt] = v, cost[cnt] = c, flow[cnt] = f;
}
inline void addedge(int u, int v, int c, int f) {
    insert(u, v, c, f);
    insert(v, u, -c, 0);
}
inline int bfs() {
    memset(dis, 0xc0, sizeof(dis));
    dis[S] = 0, q.push(S);
    int i, u;
    while (!q.empty()) {
        u = q.front(), inq[u] = 0, q.pop();
        for (i = head[u]; i; i = next[i]) {
            if (dis[to[i]] < dis[u] + cost[i] && flow[i]) {
                dis[to[i]] = dis[u] + cost[i], pe[to[i]] = i, pv[to[i]] = u;
                if (!inq[to[i]]) q.push(to[i]), inq[to[i]] = 1;
            }
        }
    }
    return dis[T] > 0;
}

int main() {
    oct::sync();
    std::cin >> n;
    std::cin >> s1 + 1;
    std::cin >> m;
    int i, j, k, a, b;
    for (i = 1; i <= m; i++) {
        std::cin >> s2 >> b, a = strlen(s2);
        for (j = 1; j + a - 1 <= n; j++) {
            for (k = 0; k < a; k++) {
                if (s1[j + k] != s2[k]) break;
            }
            if (k == a) addedge(j, j + a, b, 1);
        }
    }
    std::cin >> a;
    for (i = 0; i <= n; i++) addedge(i, i + 1, 0, a);
    S = 0, T = n + 1;
    while (bfs()) {
        ans += dis[T];
        for (i = T; i != S; i = pv[i]) flow[pe[i]]--, flow[pe[i] ^ 1]++;
    }
    std::cout << ans << std::endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")

#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2

#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;

template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 502;
int n, m, c, s;
int x[N];

string t;
string p[N];

struct Edge {
    int cap;
    int flw;
    int wgt;
} edge[N][N];

struct Graph {
    int dst[N];
    int par[N];
    vec <int> adj[N];
} graph;

void input() {
    cin >> n;
    cin >> t;
    cin >> m;
    for(int i = 0; i < m; ++i) {
        cin >> p[i];
        cin >> x[i];
    }
    cin >> c;
}

void add_edge(int u, int v, int c, int w) {
    graph.adj[u].pb(v);
    graph.adj[v].pb(u);
    edge[u][v].cap = c;
    edge[u][v].flw = 0;
    edge[u][v].wgt = -w;
    edge[v][u].cap = c;
    edge[v][u].flw = c;
    edge[v][u].wgt = w;
}

int edmonds_karp() {
    int res = 0;
    int cur = 0;
    if(n == 350) {
        cout << c << "<<";
    }
    while(true) {
        fill(graph.dst, graph.dst + N, inf);
        graph.dst[s] = 0;
        queue <int> q; q.push(s);
        while(!q.empty()) {
            int u = q.front(); q.pop();
            for(int v: graph.adj[u]) {
                int w = edge[u][v].wgt;
                if(edge[u][v].cap > edge[u][v].flw && graph.dst[v] > graph.dst[u] + w) {
                    graph.dst[v] = graph.dst[u] + w;
                    graph.par[v] = u;
                    q.push(v);
                }
            }
        }
        if(n == 350) {
            cout << edge[12][13].flw << " <<\n";
        }
        if(graph.dst[n] == inf) {
            return res;
        }
        for(int v = n; v != s; ) {
            int u = graph.par[v];
            edge[u][v].flw++;
            edge[v][u].flw--;
            v = u;
        }
        cur -= graph.dst[n];
        res = max(res, cur);
    }
    return res;
}

void solve() {
    s = N - 1;
    add_edge(s, 0, c, 0);
    for(int i = 0; i < n; ++i) {
        add_edge(i, i + 1, c, 0);
    }
    for(int i = 0; i < m; ++i) {
        int l = len(p[i]);
        for(int j = 0; j <= n - l; ++j) {
            if(t.substr(j, l) == p[i]) {
                add_edge(j, j + l, 1, x[i]);
            }
        }
    }
    cout << edmonds_karp() << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    input();
    solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")

#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2

#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;

template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 501;
int n, m, c;
int x[N];

string t;
string p[N];

struct Edge {
    int cap;
    int flw;
    int wgt;
} edge[N][N];

struct Graph {
    int dst[N];
    int par[N];
    vec <int> adj[N];
} graph;

void input() {
    cin >> n;
    cin >> t;
    cin >> m;
    for(int i = 0; i < m; ++i) {
        cin >> p[i];
        cin >> x[i];
    }
    cin >> c;
}

void add_edge(int u, int v, int c, int w) {
    graph.adj[u].pb(v);
    graph.adj[v].pb(u);
    edge[u][v].cap = c;
    edge[u][v].flw = 0;
    edge[u][v].wgt = -w;
    edge[v][u].cap = c;
    edge[v][u].flw = c;
    edge[v][u].wgt = w;
}

int edmonds_karp() {
    int res = 0;
    int cur = 0;
    for(int i = 1; i <= c; ++i) {
        fill(graph.dst, graph.dst + N, inf);
        graph.dst[0] = 0;
        queue <int> q; q.push(0);
        while(!q.empty()) {
            int u = q.front(); q.pop();
            for(int v: graph.adj[u]) {
                int w = edge[u][v].wgt;
                if(edge[u][v].cap > edge[u][v].flw && graph.dst[v] > graph.dst[u] + w) {
                    graph.dst[v] = graph.dst[u] + w;
                    graph.par[v] = u;
                    q.push(v);
                }
            }
        }
        if(graph.dst[n] == inf) {
            return res;
        }
        int dif = inf;
        for(int v = n; v != 0; ) {
            int u = graph.par[v];
            dif = min(dif, edge[u][v].cap - edge[u][v].flw);
            v = u;
        }
        for(int v = n; v != 0; ) {
            int u = graph.par[v];
            edge[u][v].flw += dif;
            edge[v][u].flw -= dif;
            v = u;
        }
        cur -= graph.dst[n];
        res = max(res, cur);
    }
    return res;
}

void solve() {
    for(int i = 0; i < n; ++i) {
        add_edge(i, i + 1, c, 0);
    }
    for(int i = 0; i < m; ++i) {
        int l = len(p[i]);
        for(int j = 0; j <= n - l; ++j) {
            if(t.substr(j, l) == p[i]) {
                add_edge(j, j + l, 1, x[i]);
            }
        }
    }
    cout << edmonds_karp() << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    input();
    solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")

#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2

#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;

template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 502;
int n, m, c, s;
int x[N];

string t;
string p[N];

struct Edge {
    int cap;
    int flw;
    int wgt;
} edge[N][N];

struct Graph {
    int dst[N];
    int par[N];
    vec <int> adj[N];
} graph;

void input() {
    cin >> n;
    cin >> t;
    cin >> m;
    for(int i = 0; i < m; ++i) {
        cin >> p[i];
        cin >> x[i];
    }
    cin >> c;
}

void add_edge(int u, int v, int c, int w) {
    graph.adj[u].pb(v);
    graph.adj[v].pb(u);
    edge[u][v].cap = c;
    edge[u][v].flw = 0;
    edge[u][v].wgt = -w;
    edge[v][u].cap = c;
    edge[v][u].flw = c;
    edge[v][u].wgt = w;
}

int edmonds_karp() {
    int res = 0;
    int cur = 0;
    if(n == 350) {
        cout << c << "<<";
    }
    while(true) {
        fill(graph.dst, graph.dst + N, inf);
        graph.dst[s] = 0;
        queue <int> q; q.push(s);
        while(!q.empty()) {
            int u = q.front(); q.pop();
            for(int v: graph.adj[u]) {
                int w = edge[u][v].wgt;
                if(edge[u][v].cap > edge[u][v].flw && graph.dst[v] > graph.dst[u] + w) {
                    graph.dst[v] = graph.dst[u] + w;
                    graph.par[v] = u;
                    q.push(v);
                }
            }
        }
        if(graph.dst[n] == inf) {
            return res;
        }
        for(int v = n; v != s; ) {
            int u = graph.par[v];
            edge[u][v].flw++;
            edge[v][u].flw--;
            v = u;
        }
        cur -= graph.dst[n];
        res = max(res, cur);
        if(n == 350)
        cout << " >> " << res << "\n";
    }
    return res;
}

void solve() {
    s = N - 1;
    add_edge(s, 0, c, 0);
    for(int i = 0; i < n; ++i) {
        add_edge(i, i + 1, c, 0);
    }
    for(int i = 0; i < m; ++i) {
        int l = len(p[i]);
        for(int j = 0; j <= n - l; ++j) {
            // if(n == 350) {
            //     continue;
            // }
            if(t.substr(j, l) == p[i]) {
                add_edge(j, j + l, 1, x[i]);
            }
        }
    }
    cout << edmonds_karp() << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    input();
    solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")

#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2

#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;

template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 501;
int n, m, c;
int x[N];

string t;
string p[N];

struct Edge {
    int cap;
    int flw;
    int wgt;
} edge[N][N];

struct Graph {
    int dst[N];
    int par[N];
    vec <int> adj[N];
} graph;

void input() {
    cin >> n;
    cin >> t;
    cin >> m;
    for(int i = 0; i < m; ++i) {
        cin >> p[i];
        cin >> x[i];
    }
    cin >> c;
}

void add_edge(int u, int v, int c, int w) {
    graph.adj[u].pb(v);
    graph.adj[v].pb(u);
    edge[u][v].cap = c;
    edge[u][v].flw = 0;
    edge[u][v].wgt = -w;
    edge[v][u].cap = c;
    edge[v][u].flw = c;
    edge[v][u].wgt = w;
}

int edmonds_karp() {
    int res = 0;
    int cur = 0;
    for(int i = 1; i <= c; ++i) {
        fill(graph.dst, graph.dst + N, inf);
        graph.dst[0] = 0;
        queue <int> q; q.push(0);
        while(!q.empty()) {
            int u = q.front(); q.pop();
            for(int v: graph.adj[u]) {
                int w = edge[u][v].wgt;
                if(edge[u][v].cap > edge[u][v].flw && graph.dst[v] > graph.dst[u] + w) {
                    graph.dst[v] = graph.dst[u] + w;
                    graph.par[v] = u;
                    q.push(v);
                }
            }
        }
        if(graph.dst[n] == inf) {
            return res;
        }
        int dif = inf;
        for(int v = n; v != 0; ) {
            int u = graph.par[v];
            edge[u][v].flw++;
            edge[v][u].flw--;
            v = u;
        }
        cur -= graph.dst[n];
        res = max(res, cur);
    }
    return res;
}

void solve() {
    for(int i = 0; i < n; ++i) {
        add_edge(i, i + 1, c, 0);
    }
    for(int i = 0; i < m; ++i) {
        int l = len(p[i]);
        for(int j = 0; j <= n - l; ++j) {
            if(t.substr(j, l) == p[i]) {
                add_edge(j, j + l, 1, x[i]);
            }
        }
    }
    cout << edmonds_karp() << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    input();
    solve();
}