#include <stdio.h>

int main()
{
    int n, a, b, max = 0, min = 1024 * 1024 * 1024, indexMax = 0, indexMin = 0, index = 0;

    scanf("%d", &n);

    while(n > 0){
        scanf("%d%d", &a, &b);
        n--;
        index++;

        if(a < min){
            min = a;
            indexMin = index;
            if(b == max){
                indexMax = index;
                continue;
            }
        }else if(a == min && b > max){
            max = b;
            indexMin = indexMax = index;
            continue;
        }

        if(b > max){
            max = b;
            indexMax = index;
            continue;
        }

        if(a == min && b == max && indexMin != indexMax){
            indexMax = indexMin = index;
        }
    }

    if(indexMax == indexMin)printf("%d\n", indexMin);
    else printf("-1\n");

    return 0;
}


#include <stdio.h>

int main()
{
	int n, a, b, max = 0, min = 1024 * 1024 * 1024, indexMax = 0, indexMin = 0, index = 0;

	scanf("%d", &n);

	while(n > 0){
		scanf("%d%d", &a, &b);
		n--;
		index++;

		if(a < min){
			min = a;
			indexMin = index;
		}else if(a == min && b > max){
			indexMin = index;
		}
		if(a == min){
			if(b >= max){
			max = b;
			indexMax = index;
			}
		}
	}

	if(indexMax == indexMin)printf("%d\n", indexMin);
	else printf("-1\n");

	return 0;
}


#include <stdio.h>

int main()
{
	int n, a, b, max = 0, min = 1024 * 1024 * 1024, indexMax = 0, indexMin = 0, index = 0;

	scanf("%d", &n);

	while(n > 0){
		scanf("%d%d", &a, &b);
		n--;
		index++;

		if(a < min){
			min = a;
			indexMin = index;
		}else if(a == min && b > max){
			indexMin = index;
		}

		if(b > max){
			max = b;
			indexMax = index;
		}else if(b == max && a == min){
			indexMax = index;
		}
	}

	if(indexMax == indexMin)printf("%d\n", indexMin);
	else printf("-1\n");

	return 0;
}


#include <stdio.h>
int main(){
    int n,l,r,lmi,rma,le,p,i;
    scanf("%d",&n);
    scanf("%d",&l);
    scanf("%d",&r);
    lmi=l;
    rma=r;
    le=r-l;
    p=1;
    for (i=2;i<=n;i++){
        scanf("%d",&l);
        scanf("%d",&r);
        if(l<lmi){
            lmi=l;
        }
        if(r>rma){
            rma=r;
        }
        if((r-l)>le){
            le=r-l;
            p=i;
        }
    }
    if((rma-lmi)==le){
        printf("%d",p);
    }else{
        printf("-1");
    }
    return 0;
}


#include <stdio.h>

int main()
{
	int a, c, d, e, f, g, h, i, j;

	scanf("%d", &a);
	scanf("%d%d", &c, &d);
	e = 1;
	f = c;
	g = d;
	for (h = 2; h <= a; ++h) {
		scanf("%d%d", &i, &j);
		if (i < f)
			f = i;
		if (g < j)
			g = j;
		if (e < 0 || c > f || d < g) {
			if (i == f && j == g) {
				c = i;
				d = j;
				e = h;
			} else {
				e = -1;
			}
		}
	}
	printf("%d\n", e);

	return 0;
}


#include <stdio.h>

int main()
{
	int a, b, c, d, e, f, g, h;

	scanf("%d", &a);
	scanf("%d%d", &e, &f);
	g = 1;
	h = 0;
	for (b = 2; b <= a; ++b) {
		scanf("%d%d", &c, &d);
		if (c == e && d > f) {
			e = c;
			f = d;
			g = b;
			h = 0;
		} else if (c < e) {
			e = c;
			f = d;
			g = b;
			if (d >= f)
				h = 0;
			else
				h = 1;
		} else if (c > e && d > f) {
			h = 1;
		}
	}
	if (h)
		printf("-1\n");
	else
		printf("%d\n", g);

	return 0;
}


#include <stdio.h>

int main(){
    int n, a, b, c, d, position;
    int i;
    int ban = 0;

    scanf("%d", &n);

    for(i=0; i<n; i++){
        scanf("%d %d", &c, &d);
        if(i==0){
            a = c;
            b = d;
            position = i+1;
        }else{
            if(c <= a){
                if(d >= b){
                    a = c;
                    b = d;
                    position = i+1;
                }
            }else{
                if(d > b ){
                    ban = 1;
                }

            }

        }

    }

    if(ban==1){
        printf("-1");
    }else{
        printf("%d", position);
    }
    return 0;
}


#include <stdio.h>

int main(){
    int n, a, b, c, d, position;
    int x, y, min, max;
    int i;
    int ban = 0;
    int segment1X, segment1Y;

    scanf("%d", &n);

    for(i=0; i<n; i++){
        scanf("%d %d", &x, &y);
        if(i==0){
            min = x;
            max = y;
            position = i+1;
            segment1X = x;
            segment1Y = y;
        }else{
            if(x <= min){
                min = x;
            }
            if(y >= max){
                max = y;
            }
            if(x <= min && y >= max){
                position = i+1;
                ban = 1;
            }
        }

    }

    if(ban==0 && segment1X>=x){
        printf("-1");
    }else{
        printf("%d", position);
    }
    return 0;
}


#include <stdio.h>

int main(){
    int n, a, b, c, d, position;
    int x, y, min, max;
    int i;
    int ban = 0;

    scanf("%d", &n);

    for(i=0; i<n; i++){
        scanf("%d %d", &x, &y);
        if(i==0){
            min = x;
            max = y;
            position = i+1;
        }else{
            if(x <= min){
                min = x;
            }
            if(y >= max){
                max = y;
            }
            if(x <= min && y >= max){
                position = i+1;
                ban=1;
            }
        }

    }

    if(ban==0){
        printf("-1");
    }else{
        printf("%d", position);
    }
    return 0;
}


#include <stdio.h>

int main(){
    int n, i, x, y, min, max, position, ban=0;

    scanf("%d", &n);

    int segments[n][2];

    for(i=0; i<n; i++){
        scanf("%d %d", &x, &y);
        if(i==0){
            min = x;
            max = y;
        }
        if(x < min){
            min = x;
        }
        if(y > max){
            max = y;
        }
        segments[i][0] = x;
        segments[i][1] = y;
    }

    for(i=0; i<n; i++){
        if(segments[i][0] <= min && segments[i][1] >= max){
            position = i+1;
            ban = 1;
        }
    }

    if(ban == 0){
        printf("-1");
    }else{
        printf("%d", position);
    }

    return 0;
}


#include <stdio.h>

int main(){
    int n, a, b, c, d, position;
    int x, y, min, max;
    int i;
    int ban = 0;
    int segment1X, segment1Y;

    scanf("%d", &n);

    for(i=0; i<n; i++){
        scanf("%d %d", &x, &y);
        if(i==0){
            min = x;
            max = y;
            position = i+1;
            segment1X = x;
            segment1Y = y;
        }else{
            if(x <= min){
                min = x;
            }
            if(y >= max){
                max = y;
            }
            if(x <= min && y >= max){
                position = i+1;
                ban = 1;
            }
        }

    }

    if (ban==1){
        printf("%d", position);
    }else{
    if(ban==0 && segment1X>x || segment1Y<y){
        printf("-1");
    }else{
        printf("%d",position);
        }
    }
    return 0;
}


#include <stdio.h>

int main(){
    int n, a, b, c, d, position;
    int x, y, min, max;
    int i;
    int ban = 0;
    int segment1X, segment1Y;

    scanf("%d", &n);

    for(i=0; i<n; i++){
        scanf("%d %d", &x, &y);
        if(i==0){
            min = x;
            max = y;
            position = i+1;
            segment1X = x;
            segment1Y = y;
        }else{
            if(x <= min){
                min = x;
            }
            if(y >= max){
                max = y;
            }
            if(x <= min && y >= max){
                position = i+1;
                ban = 1;
            }
        }

    }

    if (ban==1){
        printf("%d", position);
    }else{
    if(ban==0 && segment1X>x || segment1Y<y){
        printf("-1");
    }else{
        printf("%d",position);
        }
    return 0;
}}


#include <stdio.h>

int max_i(int b[], int i, int j){
    if(b[i] > b[j]){
        return i;
    }else{
        return j;
    }
}

int min_i(int b[], int i, int j){
    if(b[i] < b[j]){
        return i;
    }else{
        return j;
    }
}

int main(){
    int i, j, k, l, m, n;

    scanf("%d", &n);

    int a[n], b[n];
    for(i = 0; i < n; i++){
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }

    k = 0; j = 0;
    for(i = 1; i < n; i++){
        if(a[i] <= a[j] && b[k] <= b[i]){
            k = i;
            j = i;
        }else if(a[i] < a[j]){
            j = i;
        }else if(b[k] < b[i]){
            k = i;
        }else if(a[i] == a[j]){
            j = max_i(b, i, j);
        }else if(b[i] == b[k]){
            k = min_i(a, i, j);
        }
    }

    if(j == k){
        printf("%d", j + 1);
    }else{
        printf("-1\n");
    }

    return 0;
}


#include <stdio.h>

int max_i(int b[], int i, int j){
    if(b[i] > b[j]){
        return i;
    }else{
        return j;
    }
}

int min_i(int b[], int i, int j){
    if(b[i] < b[j]){
        return i;
    }else{
        return j;
    }
}

int main(){
    int i, j, k, l, m, n;

    scanf("%d", &n);

    int a[n], b[n];
    for(i = 0; i < n; i++){
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }

    k = 0; j = 0;
    for(i = 1; i < n; i++){
        if(a[i] <= a[j] && b[k] <= b[i]){
            k = i;
            j = i;
        }else if(a[i] < a[j]){
            j = i;
        }else if(b[k] < b[i]){
            k = i;
        }else if(a[i] == a[j]){
            j = max_i(b, i, j);
        }else if(b[i] == b[k]){
            k = min_i(a, i, k);
        }
    }

    if(j == k){
        printf("%d", j + 1);
    }else{
        printf("-1\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

void quicksort(int m[][3],int ini, int fin)
{
    int pivote,central,i,j,aux[3];

    central=(ini+fin)/2;
    pivote=m[central][0];
    i=ini;
    j=fin;

    do{
        while(m[i][0]<pivote)i++;
        while(m[j][0]>pivote)j--;

        if(i<=j)
        {
           aux[0]=m[i][0];
           aux[1]=m[i][1];
           aux[2]=m[i][2];
           m[i][0]=m[j][0];
           m[i][1]=m[j][1];
           m[i][2]=m[j][2];
           m[j][0]=aux[0];
           m[j][1]=aux[1];
           m[j][2]=aux[2];
           i++;
           j--;
        }
    }while(i<=j);

    if(ini<j)
        quicksort(m,ini,j);
    if(i<fin)
        quicksort(m,i,fin);
}

main()
{
    int i,a,b,f,pos=0,ban=0,n;

    scanf("%d",&n);

    n+=1;
    int seg[n][3];

    for(i=1;i<n;i++)
    {
        scanf("%d%d",&seg[i][0],&seg[i][1]);
        seg[i][2]=i;
    }

    quicksort(seg,1,n-1);

    i=1;
    pos=seg[1][2];
    a=seg[1][0];
    b=seg[1][1];

    while(seg[i][0]==seg[1][0])
    {

       if(seg[i][1]>b)
       {
            a=seg[i][0];
            b=seg[i][1];
            pos=seg[i][2];
       }
        i++;
    }

    ban=1;
    i=2;

    while(ban==1 && i<n)
    {
        if(a>seg[i][0] || b<seg[i][1])
            ban=0;

        i++;
    }

    if(ban)
        printf("%d",pos);
    else
        printf("-1");

    return 0;
}


#include <stdio.h>

int main()
{
    int i,posmay,posmen,n;

    scanf("%d",&n);

    int seg[n][2];

    scanf("%d%d",&seg[0][0],&seg[0][1]);
    posmen=posmay=0;
    for(i=1;i<n;i++)
    {
        scanf("%d%d",&seg[i][0],&seg[i][1]);
        if(seg[i][0]<seg[posmen][0])
            posmen=i;

        if(seg[i][1]>seg[posmay][1])
            posmay=i;
    }

    if(posmen==posmay)
        printf("%d",posmen+1);
    else
        printf("-1");

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

void quicksort(int m[][3],int ini, int fin)
{
    int pivote,central,i,j,aux[3];

    central=(ini+fin)/2;
    pivote=m[central][0];
    i=ini;
    j=fin;

    do{
        while(m[i][0]<pivote)i++;
        while(m[j][0]>pivote)j--;

        if(i<=j)
        {
            aux[0]=m[i][0];
            aux[1]=m[i][1];
            aux[2]=m[i][2];
            m[i][0]=m[j][0];
            m[i][1]=m[j][1];
            m[i][2]=m[j][2];
            m[j][0]=aux[0];
            m[j][1]=aux[1];
            m[j][2]=aux[2];
            i++;
            j--;
        }
    }while(i<=j);

    if(ini<j)
        quicksort(m,ini,j);
    if(i<fin)
        quicksort(m,i,fin);
}

int main()
{
    int i,a,b,j,pos=0,ban,n;

    /*Se pide la cantidad de segmentos*/
    do{
        scanf("%d",&n);
    }while(n<1 || n>100000);

    int seg[n][3];

    /*Se pide el primer segmento*/
    do{
        scanf("%d%d",&seg[0][0],&seg[0][1]);
    }while(seg[0][0]<1 || seg[0][0]>1000000000 || seg[0][1]<1 || seg[0][1]>1000000000 || seg[0][0]>seg[0][1]);

    seg[0][2]=1;    //Posicion original del segmento

    /*Se piden los demas segmentos*/
    for(i=1;i<n;i++)
    {
        /*Se verifica que el segmento ingresado no coincide con los anteriores*/
        do{
            j=0;
            ban=1;
            scanf("%d%d",&seg[i][0],&seg[i][1]);
            while(ban==1 && j<i)
            {
                if(seg[i][0]==seg[j][0] && seg[i][1]==seg[j][1])
                    ban=0;
                j++;
            }
            if(!ban)
                printf("Has ingresado un segmento que coincide con otro ya ingresado.\n");

        }while(seg[i][0]<1 || seg[i][0]>1000000000 || seg[i][1]<1 || seg[i][1]>1000000000 || seg[i][0]>seg[i][1] || ban==0);

        seg[i][2]=i+1; //Posicion original del i-esimo segmento
    }

    quicksort(seg,0,n-1); //Se ordena la lista de segmentos

    i=1;
    pos=seg[0][2];
    a=seg[0][0];
    b=seg[0][1];

    /*Se busca el mayor segmento entre los de menor primer extremo*/
    while(seg[i][0]==seg[0][0])
    {

       if(seg[i][1]>b)
       {
            a=seg[i][0];
            b=seg[i][1];
            pos=seg[i][2];
       }
        i++;
    }

    ban=1;
    i=0;

    /*Se verifica que el mayor segmento encontrado cubre a todos los demas*/
    while(ban==1 && i<n)
    {
        if(a>seg[i][0] || b<seg[i][1])
            ban=0;      //Si se encuetra un segmento fuera del rango del mayor termina el ciclo

        i++;
    }

    /*Se imprime el resultado si hubiera*/
    if(ban)
        printf("%d\n",pos);
    else
        printf("-1");

    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
inline char tc(void){
	static char fl[10000],*A=fl,*B=fl;
	return A==B&&(B=(A=fl)+fread(fl,1,10000,stdin),A==B)?EOF:*A++;
}
inline int read(void){
	int a=0;char c;
	while((c=tc())<'0'||c>'9');
	while(c>='0'&&c<='9')a=a*10+c-'0',c=tc();
	return a;
}
struct Y{
	int h,t;
	inline bool operator<(const Y&a)const{
		return t<a.t||t==a.t&&h>a.h;
	}
}s[10005];
int n,l,r,sumh,sum[10005],ans;
bool b[10005];
int main(void){
	register int i,j,x;
	n=read(),l=read(),r=read();
	for(i=1;i<=n;++i)sumh+=s[i].h=read();
	for(i=1;i<=n;++i)s[i].t=read();
	sort(s+1,s+n+1);
	x=l,l=sumh-r,r=sumh-x,b[0]=1;
	for(i=1;i<=n;++i)
		for(j=sumh-s[i].h;~j;--j)
			if(b[j]){
				b[j+s[i].h]=1;
				sum[j+s[i].h]=max(sum[j+s[i].h],sum[j]+(s[i].t==1&&l<=j+s[i].h&&r>=j+s[i].h));
				if(j+s[i].h<=r&&j+s[i].h>=l)ans=max(ans,sum[j+s[i].h]);
			}
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
inline char tc(void){
	static char fl[10000],*A=fl,*B=fl;
	return A==B&&(B=(A=fl)+fread(fl,1,10000,stdin),A==B)?EOF:*A++;
}
inline int read(void){
	int a=0;char c;
	while((c=tc())<'0'||c>'9');
	while(c>='0'&&c<='9')a=a*10+c-'0',c=tc();
	return a;
}
struct Y{
	int h,t;
	inline bool operator<(const Y&a)const{
		return t<a.t||t==a.t&&h<a.h;
	}
}s[10005];
int n,l,r,sumh,sum[10005],ans;
bool b[10005];
int main(void){
	register int i,j,x;
	n=read(),l=read(),r=read();
	for(i=1;i<=n;++i)sumh+=s[i].h=read();
	for(i=1;i<=n;++i)s[i].t=read();
	sort(s+1,s+n+1);
	x=l,l=sumh-r,r=sumh-x,b[0]=1;
	for(i=1;i<=n;++i)
		for(j=sumh-s[i].h;~j;--j)
			if(b[j]){
				b[j+s[i].h]=1;
				sum[j+s[i].h]=max(sum[j+s[i].h],sum[j]+(s[i].t==1&&l<=j+s[i].h&&r>=j+s[i].h));
				ans=max(ans,sum[j+s[i].h]);
			}
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
inline char tc(void){
	static char fl[10000],*A=fl,*B=fl;
	return A==B&&(B=(A=fl)+fread(fl,1,10000,stdin),A==B)?EOF:*A++;
}
inline int read(void){
	int a=0;char c;
	while((c=tc())<'0'||c>'9');
	while(c>='0'&&c<='9')a=a*10+c-'0',c=tc();
	return a;
}
struct Y{
	int h,t;
	inline bool operator<(const Y&a)const{
		return t<a.t||t==a.t&&h>a.h;
	}
}s[10005];
int n,l,r,sumh,sum[10005],ans;
bool b[10005];
int main(void){
	register int i,j,x;
	n=read(),l=read(),r=read();
	for(i=1;i<=n;++i)sumh+=s[i].h=read();
	for(i=1;i<=n;++i)s[i].t=read();
	sort(s+1,s+n+1);
	x=l,l=sumh-r,r=sumh-x,b[0]=1;
	for(i=1;i<=n;++i)
		for(j=sumh-s[i].h;~j;--j)
			if(b[j]){
				b[j+s[i].h]=1;
				sum[j+s[i].h]=max(sum[j+s[i].h],sum[j]+(s[i].t==1&&l<=j+s[i].h&&r>=j+s[i].h));
				if(j<=r&&j>=l)ans=max(ans,sum[j+s[i].h]);
			}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,l,r,f[10003];
struct data{
	int x,y;
}a[10003];
inline bool cmp(data a,data b){
	return a.y<b.y||a.y==b.y&&a.x>b.x;
}
int main(){
	cin>>n>>l>>r;
	int s=0;
	for(int i=1;i<=n;i++){
		cin>>a[i].x,s+=a[i].x;
	}
	for(int i=1;i<=n;i++){
		cin>>a[i].y;
	}
	sort(a+1,a+n+1,cmp);
	f[0]=1;
	for(int i=1;i<=n;i++){
		for(int j=s;j>=a[i].x;j--){
        	if(f[j-a[i].x]){
        		f[j]=max(f[j],f[j-a[i].x]+a[i].y*(j>=s-r&&j<=s-l));
			}
		}
	}
	int ans=0;
	for(int i=0;i<=s;i++){
		ans=max(ans,f[i]);
	}
	cout<<ans-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
const int N=10005;
int dp[N],n,L,R,s;
struct note{
	int h,w;
}a[N];
bool cmp(note x,note y){
	return x.w==y.w?x.h>y.h:x.w<y.w;
}
int main(){
	scanf("%d%d%d",&n,&L,&R);
	for (int i=1;i<=n;i++) scanf("%d",&a[i].h),s+=a[i].h;
	for (int i=1;i<=n;i++) scanf("%d",&a[i].w);
	int t=L;L=s-R,R=s-t;
	std::sort(a+1,a+n+1,cmp);
	memset(dp,-1,sizeof(dp));
	dp[0]=0;
	for (int i=1;i<=n;i++){
		for (int j=R;j>=a[i].h;j--)
			if (dp[j-a[i].h]!=-1) dp[j]=std::max(dp[j-a[i].h]+a[i].w*(j>=L && j<=R),dp[j]);
	}
	int ans=0;
	for (int i=L;i<=R;i++) ans=std::max(ans,dp[i]);
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
const int N=1005;
int dp[N],n,L,R,s;
struct note{
	int h,w;
}a[N];
bool cmp(note x,note y){
	return x.w==y.w?x.h>y.h:x.w<y.w;
}
int main(){
	scanf("%d%d%d",&n,&L,&R);
	for (int i=1;i<=n;i++) scanf("%d",&a[i].h),s+=a[i].h;
	for (int i=1;i<=n;i++) scanf("%d",&a[i].w);
	int t=L;L=s-R,R=s-t;
	std::sort(a+1,a+n+1,cmp);
	memset(dp,-1,sizeof(dp));
	dp[0]=0;
	for (int i=1;i<=n;i++){
		for (int j=R;j>=a[i].h;j--)
			if (dp[j-a[i].h]!=-1) dp[j]=std::max(dp[j-a[i].h]+a[i].w*(j>=L && j<=R),dp[j]);
	}
	int ans=0;
	for (int i=L;i<=R;i++) ans=std::max(ans,dp[i]);
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
const int N=10005;
int dp[N],n,L,R,s;
struct note{
	int h,w;
}a[N];
bool cmp(note x,note y){
	return x.w==y.w?x.h>y.h:x.w<y.w;
}
int main(){
	scanf("%d%d%d",&n,&L,&R);
	for (int i=1;i<=n;i++) scanf("%d",&a[i].h),s+=a[i].h;
	if (R>s) R=s;
	for (int i=1;i<=n;i++) scanf("%d",&a[i].w);
	int t=L;L=s-R,R=s-t;
	std::sort(a+1,a+n+1,cmp);
	memset(dp,-1,sizeof(dp));
	dp[0]=0;
	for (int i=1;i<=n;i++){
		for (int j=R;j>=a[i].h;j--)
			if (dp[j-a[i].h]!=-1) dp[j]=std::max(dp[j-a[i].h]+a[i].w*(j>=L && j<=R),dp[j]);
	}
	int ans=0;
	for (int i=L;i<=R;i++) ans=std::max(ans,dp[i]);
	printf("%d\n",ans);
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inline  __attribute__((always_inline))
using namespace std;
int read()
{
	char ch=getchar();int f=0,x=1;
	while(ch<'0'||ch>'9'){if(ch=='-') x=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}
	return f*x;
}
struct data
{
	int h,t;
}b[100005];
bool operator < (const data &x,const data &y)
{
	if(x.t!=y.t) return x.t<y.t;
	///return x.h>y.h;
}
int n,l,r,ans[100005];
const int inf=1000000000;
int main()
{
	n=read();l=read();r=read();
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		b[i].h=read();
		sum+=b[i].h;
	}
	int templ=l,tempr=r;
	l=sum-tempr,r=sum-templ;
	for(int i=1;i<=n;i++)
	{
		b[i].t=read();
	}
	sort(b+1,b+n+1);
	for(int i=1;i<=sum;i++) ans[i]=-inf;
	for(int i=1;i<=n;i++)
	{
		for(int h=sum-b[i].h;h>=0;h--)
		{
			ans[h+b[i].h]=max(ans[h+b[i].h],ans[h]+b[i].t*(h+b[i].h>=l&&h+b[i].h<=r));
		}
	}
	cout<<*max_element(ans,ans+sum+1)<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inline  __attribute__((always_inline))
using namespace std;
int read()
{
	char ch=getchar();int f=0,x=1;
	while(ch<'0'||ch>'9'){if(ch=='-') x=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}
	return f*x;
}
struct data
{
	int h,t;
}b[100005];
bool operator < (const data &x,const data &y)
{
	if(x.t!=y.t) return x.t<y.t;
	return x.h>y.h;
}
int n,l,r,ans[100005];
const int inf=1000000000;
int main()
{
	n=read();l=read();r=read();
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		b[i].h=read();
		sum+=b[i].h;
	}
	int templ=l,tempr=r;
	l=sum-tempr,r=sum-templ;
	for(int i=1;i<=n;i++)
	{
		b[i].t=read();
	}
	sort(b+1,b+n+1);
	for(int i=1;i<=sum;i++) ans[i]=-inf;
	for(int i=1;i<=n;i++)
	{
		for(int h=sum-b[i].h;h>=0;h--)
		{
			ans[h+b[i].h]=max(ans[h+b[i].h],ans[h]+b[i].t*(h+b[i].h>=l&&h+b[i].h<=r));
		}
	}
	cout<<*max_element(ans,ans+sum+1)<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inline  __attribute__((always_inline))
using namespace std;
int read()
{
	char ch=getchar();int f=0,x=1;
	while(ch<'0'||ch>'9'){if(ch=='-') x=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}
	return f*x;
}
struct data
{
	int h,t;
}b[100005];
bool operator < (const data &x,const data &y)
{
	if(x.t!=y.t) return x.t<y.t;
	//return x.h>y.h;
}
int n,l,r,ans[100005];
const int inf=1000000000;
int main()
{
	n=read();l=read();r=read();
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		b[i].h=read();
		sum+=b[i].h;
	}
	int templ=l,tempr=r;
	l=sum-tempr,r=sum-templ;
	for(int i=1;i<=n;i++)
	{
		b[i].t=read();
	}
	sort(b+1,b+n+1);
	for(int i=1;i<=sum;i++) ans[i]=-inf;
	for(int i=1;i<=n;i++)
	{
		for(int h=sum-b[i].h;h>=0;h--)
		{
			ans[h+b[i].h]=max(ans[h+b[i].h],ans[h]+b[i].t*(h+b[i].h>=l&&h+b[i].h<=r));
		}
	}
	cout<<*max_element(ans,ans+sum+1)<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define EPS 1e-9
#define FOR(x,n) for(int x=0; (x)<(n); (x)++)
#define FOR1(x,n) for(int x=1; (x)<=(n); (x)++)
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int, int> ii;
typedef long long ll;

ii arr[MAXN];
int N, L, R, H;
int dp[2][MAXN];

bool comp(ii a, ii b) {
	if (a.second != b.second) return a.second < b.second;
	return a.first > b.second;
}

int main() {
	scanf("%d %d %d", &N, &L, &R);
	H = 0;
	FOR(i, N) {
		scanf("%d", &arr[i].first);
		H += arr[i].first;
	}
	FOR(i, N) {
		scanf("%d", &arr[i].second);
	}

	L = H - L;
	R = H - R;
	swap(L, R);
	sort(arr, arr+N, comp);

	for(int i = N; i >= 0; i--) {
		for(int h = H; h >= 0; h--) {
			if (i == N || h + arr[i].first > H) {
				dp[i%2][h] = 0;
			}
			else {
				int cur = 0;
				if (h + arr[i].first >= L && h + arr[i].first <= R && arr[i].second == 1) cur = 1;
				dp[i%2][h] = max(dp[(i+1)%2][h], cur + dp[(i+1)%2][h+arr[i].first]);
			}
		}
	}

	printf("%d\n", dp[0][0]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define EPS 1e-9
#define FOR(x,n) for(int x=0; (x)<(n); (x)++)
#define FOR1(x,n) for(int x=1; (x)<=(n); (x)++)
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int, int> ii;
typedef long long ll;

struct box {
	int a, b;
} arr[MAXN];
int N, L, R, H;
int dp[2][MAXN];

bool comp(box a, box b) {
	if (a.b != b.b) return a.b < b.b;
	return a.a > b.a;
}

int main() {
	scanf("%d %d %d", &N, &L, &R);
	H = 0;
	FOR(i, N) {
		scanf("%d", &arr[i].a);
		H += arr[i].a;
	}
	FOR(i, N) {
		scanf("%d", &arr[i].b);
	}

	L = H - L;
	R = H - R;
	swap(L, R);
	sort(arr, arr+N, comp);

	for(int i = N; i >= 0; i--) {
		for(int h = H; h >= 0; h--) {
			if (i == N || h + arr[i].a > H) {
				dp[i%2][h] = 0;
			}
			else {
				int cur = 0;
				if (h + arr[i].a >= L && h + arr[i].a <= R && arr[i].b == 1) cur = 1;
				dp[i%2][h] = max(dp[(i+1)%2][h], cur + dp[(i+1)%2][h+arr[i].a]);
			}
		}
	}

	printf("%d\n", dp[0][0]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 10009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define EPS 1e-9
#define FOR(x,n) for(int x=0; (x)<(n); (x)++)
#define FOR1(x,n) for(int x=1; (x)<=(n); (x)++)
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int, int> ii;
typedef long long ll;

ii arr[MAXN];
int N, L, R, H;
int dp[2][MAXN];

bool comp(ii a, ii b) {
	if (a.second != b.second) return a.second < b.second;
	return a.first > b.second;
}

int main() {
	scanf("%d %d %d", &N, &L, &R);
	H = 0;
	FOR(i, N) {
		scanf("%d", &arr[i].first);
		H += arr[i].first;
	}
	FOR(i, N) {
		scanf("%d", &arr[i].second);
	}

	L = H - L;
	R = H - R;
	swap(L, R);
	sort(arr, arr+N, comp);

	for(int i = N; i >= 0; i--) {
		for(int h = H; h >= 0; h--) {
			if (h + arr[i].first > H || i == N) {
				dp[i%2][h] = 0;
			}
			else {
				int cur = 0;
				if (h + arr[i].first >= L && h + arr[i].first <= R && arr[i].second == 1) cur = 1;
				dp[i%2][h] = max(dp[(i+1)%2][h], cur + dp[(i+1)%2][h+arr[i].first]);
			}
		}
	}

	printf("%d\n", dp[0][0]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 10009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define EPS 1e-9
#define FOR(x,n) for(int x=0; (x)<(n); (x)++)
#define FOR1(x,n) for(int x=1; (x)<=(n); (x)++)
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int, int> ii;
typedef long long ll;

ii arr[MAXN];
int N, L, R, H;
int dp[2][MAXN];

bool comp(ii a, ii b) {
	if (a.second != b.second) return a.second < b.second;
	return a.first > b.second;
}

int main() {
	scanf("%d %d %d", &N, &L, &R);
	H = 0;
	FOR(i, N) {
		scanf("%d", &arr[i].first);
		H += arr[i].first;
	}
	FOR(i, N) {
		scanf("%d", &arr[i].second);
	}

	L = H - L;
	R = H - R;
	swap(L, R);
	sort(arr, arr+N, comp);

	for(int i = N; i >= 0; i--) {
		for(int h = H; h >= 0; h--) {
			if (i == N || h + arr[i].first > H) {
				dp[i%2][h] = 0;
			}
			else {
				int cur = 0;
				if (h + arr[i].first >= L && h + arr[i].first <= R && arr[i].second == 1) cur = 1;
				dp[i%2][h] = max(dp[(i+1)%2][h], cur + dp[(i+1)%2][h+arr[i].first]);
			}
		}
	}

	printf("%d\n", dp[0][0]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 10009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define EPS 1e-9
#define FOR(x,n) for(int x=0; (x)<(n); (x)++)
#define FOR1(x,n) for(int x=1; (x)<=(n); (x)++)
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int, int> ii;
typedef long long ll;

ii arr[MAXN];
int N, L, R, H;
int dp[MAXN][MAXN];

bool comp(ii a, ii b) {
	if (a.second != b.second) return a.second < b.second;
	return a.first > b.second;
}

int main() {
	scanf("%d %d %d", &N, &L, &R);
	H = 0;
	FOR(i, N) {
		scanf("%d", &arr[i].first);
		H += arr[i].first;
	}
	FOR(i, N) {
		scanf("%d", &arr[i].second);
	}

	L = H - L;
	R = H - R;
	swap(L, R);
	sort(arr, arr+N, comp);

	for(int i = N; i >= 0; i--) {
		for(int h = H; h >= 0; h--) {
			if (h + arr[i].first > H || i == N) {
				dp[i][h] = 0;
			}
			else {
				int cur = 0;
				if (h + arr[i].first >= L && h + arr[i].first <= R && arr[i].second == 1) cur = 1;
				dp[i][h] = max(dp[i+1][h], cur + dp[i+1][h+arr[i].first]);
			}
		}
	}

	printf("%d\n", dp[0][0]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 10009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define EPS 1e-9
#define FOR(x,n) for(int x=0; (x)<(n); (x)++)
#define FOR1(x,n) for(int x=1; (x)<=(n); (x)++)
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int, int> ii;
typedef long long ll;

vector<ii> arr;
int N, L, R, H;
int dp[2][MAXN];

bool comp(ii a, ii b) {
	if (a.second != b.second) return a.second < b.second;
	return a.first > b.second;
}

int main() {
	scanf("%d %d %d", &N, &L, &R);
	H = 0;
	arr.resize(N);
	FOR(i, N) {
		scanf("%d", &arr[i].first);
		H += arr[i].first;
	}
	FOR(i, N) {
		scanf("%d", &arr[i].second);
	}

	L = H - L;
	R = H - R;
	swap(L, R);
	sort(all(arr), comp);

	for(int i = N; i >= 0; i--) {
		for(int h = H; h >= 0; h--) {
			if (i == N || h + arr[i].first > H) {
				dp[i%2][h] = 0;
			}
			else {
				int cur = 0;
				if (h + arr[i].first >= L && h + arr[i].first <= R && arr[i].second == 1) cur = 1;
				dp[i%2][h] = max(dp[(i+1)%2][h], cur + dp[(i+1)%2][h+arr[i].first]);
			}
		}
	}

	printf("%d\n", dp[0][0]);
	return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define N 10100

int n,l,r;
int a[N],imp[N],un[N],f[N],impCnt,unCnt,sum;

template<typename T>
inline void nextInt(T &x)
{
  	x=0;
    char c=getchar();
    int sign=1;
	for(;(c<'0')||(c>'9');c=getchar())if(c=='-')sign=-1;
	for(;(c>='0')&&(c<='9');c=getchar())x=x*10+(c-'0');
	x*=sign;
}

bool cmp(const int& x,const int& y)
{
	return x>y;
}

int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	nextInt(n);nextInt(l);nextInt(r);
	for(int i=1;i<=n;i++)nextInt(a[i]);
	for(int i=1;i<=n;i++)
	{
		sum+=a[i];
		int tmp;
		nextInt(tmp);
		if(tmp==1)imp[++impCnt]=a[i];
		else un[++unCnt]=a[i];
	}
	int tmpR=sum-l;l=sum-r;r=tmpR;
	l=std::max(l,0);r=std::min(r,sum);
	memset(f,128,sizeof(f));
	f[0]=0;
	for(int i=1;i<=unCnt;i++)
		for(int j=r;j>=un[i];j--)f[j]=std::max(f[j],f[j-un[i]]);

	std::sort(imp+1,imp+impCnt+1,cmp);
	for(int i=1;i<=impCnt;i++)
		for(int j=r;j>=imp[i];j--)f[j]=std::max(f[j],f[j-imp[i]]+(j>=l&&j<=r? 1:0));
	//for(int i=0;i<=sum;i++)printf("%d ",f[i]);
	int ans=0;
	for(int i=l;i<=r;i++)ans=std::max(ans,f[i]);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define N 10100

int n,l,r;
int a[N],imp[N],un[N],f[N],impCnt,unCnt,sum;

template<typename T>
inline void nextInt(T &x)
{
  	x=0;
    char c=getchar();
    int sign=1;
	for(;(c<'0')||(c>'9');c=getchar())if(c=='-')sign=-1;
	for(;(c>='0')&&(c<='9');c=getchar())x=x*10+(c-'0');
	x*=sign;
}

bool cmp(const int& x,const int& y)
{
	return x>y;
}

int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	nextInt(n);nextInt(l);nextInt(r);
	for(int i=1;i<=n;i++)nextInt(a[i]);
	for(int i=1;i<=n;i++)
	{
		sum+=a[i];
		int tmp;
		nextInt(tmp);
		if(tmp==1)imp[++impCnt]=a[i];
		else un[++unCnt]=a[i];
	}
	int tmpR=sum-l;l=sum-r;r=tmpR;
	memset(f,128,sizeof(f));
	f[0]=0;
	for(int i=1;i<=unCnt;i++)
		for(int j=r;j>=un[i];j--)f[j]=std::max(f[j],f[j-un[i]]);

	std::sort(imp+1,imp+impCnt+1,cmp);
	for(int i=1;i<=impCnt;i++)
		for(int j=r;j>=imp[i];j--)f[j]=std::max(f[j],f[j-imp[i]]+(j>=l&&j<=r? 1:0));
	//for(int i=0;i<=sum;i++)printf("%d ",f[i]);
	int ans=0;
	for(int i=l;i<=r;i++)ans=std::max(ans,f[i]);
	printf("%d\n",ans);
	return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < int , pp > > Q[N];

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r+1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q[t].push(mp(1,mp(0,0)));
	for(; t ;){
        if(Q[t].size() == 0){
            t--;  continue;
        }
	    i = Q[t].front().st;
        a = Q[t].front().nd.st;
		b = Q[t].front().nd.nd;
		Q[t].pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i <= n){
    		if(b < zz) Q[ t-(!A[i].st && b+A[i].nd<=zz) ].push(mp(i+1,mp(a,b+A[i].nd)));
		    if(a < l)  Q[ t-!A[i].st ].push(mp(i+1,mp(a+A[i].nd,b)));
        }
    }

    cout << 0;
    return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < int , pp > > Q[N];

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r-1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q[t].push(mp(1,mp(0,0)));
	for(; t ;){
        i = Q[t].front().st;
        a = Q[t].front().nd.st;
		b = Q[t].front().nd.nd;
		Q[t].pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i <= n){
    		if(b < zz) Q[ t-(!A[i].st && b+A[i].nd<zz) ].push(mp(i+1,mp(a,b+A[i].nd)));
		  if(a < l) Q[ t-!A[i].st ].push(mp(i+1,mp(a+A[i].nd,b)));
        }
        if(Q[t].size() == 0) t--;
	}

    cout << 0;
    return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < int , pp > > Q[N];

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r; l--;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q[t].push(mp(1,mp(0,0)));
	for(; t ;){
        if(Q[t].size() == 0){
            t--;  continue;
        }
	    i = Q[t].front().st;
        a = Q[t].front().nd.st;
		b = Q[t].front().nd.nd;
		Q[t].pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i <= n){
    		if(b < zz) Q[ t-(!A[i].st && b+A[i].nd<=zz) ].push(mp(i+1,mp(a,b+A[i].nd)));
		    if(a < l)  Q[ t-!A[i].st ].push(mp(i+1,mp(a+A[i].nd,b)));
        }
    }

    cout << 0;
    return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < int , pp > > Q[N];

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q[t].push(mp(1,mp(0,0)));
	for(; t ;){
        if(Q[t].size() == 0){
            t--;  continue;
        }
	    i = Q[t].front().st;
        a = Q[t].front().nd.st;
		b = Q[t].front().nd.nd;
		Q[t].pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i <= n){
    		if(b < zz) Q[ t-(!A[i].st && b+A[i].nd<=zz) ].push(mp(i+1,mp(a,b+A[i].nd)));
		    if(a < l) Q[ t-!A[i].st ].push(mp(i+1,mp(a+A[i].nd,b)));
        }
    }

    cout << 0;
    return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < pp , pp > > Q;

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r-1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q.push(mp(mp(1,t),mp(0,0)));
	for(; Q.size() ;){
		i = Q.front().st.st;
        t = Q.front().st.nd;
		a = Q.front().nd.st;
		b = Q.front().nd.nd;
		Q.pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
		if(b < zz) Q.push(mp(mp(i+1,t-(!A[i].st && b+A[i].nd<zz)),mp(a,b+A[i].nd)));
		if(a < l) Q.push(mp(mp(i+1,t-!A[i].st),mp(a+A[i].nd,b)));
	}

	cout << -1;
	return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < pp , pp > > Q;

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r-1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q.push(mp(mp(1,t),mp(0,0)));
	for(; Q.size() ;){
		i = Q.front().st.st;
        t = Q.front().st.nd;
		a = Q.front().nd.st;
		b = Q.front().nd.nd;
		Q.pop();
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
		if(b < zz) Q.push(mp(mp(i+1,t-(!A[i].st && b+A[i].nd<zz)),mp(a,b+A[i].nd)));
		if(a < l) Q.push(mp(mp(i+1,t-!A[i].st),mp(a+A[i].nd,b)));
	}

	cout << -1;
	return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < pp , pp > > Q;

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r-1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q.push(mp(mp(1,t),mp(0,0)));
	for(; Q.size() ;){
		i = Q.front().st.st;
        t = Q.front().st.nd;
		a = Q.front().nd.st;
		b = Q.front().nd.nd;
		Q.pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i == n+1) continue;
		if(b < zz) Q.push(mp(mp(i+1,t-(!A[i].st && b+A[i].nd<zz)),mp(a,b+A[i].nd)));
		if(a < l) Q.push(mp(mp(i+1,t-!A[i].st),mp(a+A[i].nd,b)));
	}

	cout << -1;
	return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < int , pp > > Q[N];

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r-1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q[t].push(mp(1,mp(0,0)));
	for(; t>=0 ;){
		i = Q[t].front().st;
        a = Q[t].front().nd.st;
		b = Q[t].front().nd.nd;
		Q[t].pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i == n+1) continue;
		if(b < zz) Q[ t-(!A[i].st && b+A[i].nd<zz) ].push(mp(i+1,mp(a,b+A[i].nd)));
		if(a < l) Q[ t-!A[i].st ].push(mp(i+1,mp(a+A[i].nd,b)));
        if(Q[t].size() == 0) t--;
	}

    assert(0);
    return 0;
}


/*
E. Wardrobe
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olya wants to buy a custom wardrobe. It should have n boxes with heights a1, a2, ..., an, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length ai, and all these segments should form a single segment from 0 to  without any overlaps.

Some of the boxes are important (in this case bi = 1), others are not (then bi = 0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights l and r, inclusive.

You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.

Input
The first line contains three integers n, l and r (1 ≤ n ≤ 10 000, 0 ≤ l ≤ r ≤ 10 000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10 000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10 000: Olya is not very tall and will not be able to reach any higher.

The second line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 1), where bi equals 1 if the i-th box is important, and 0 otherwise.

Output
Print a single integer — the maximum possible convenience of the wardrobe.
*/

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

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 1e4 + 4;

bitset < N > H[N];
pp A[N];
int n,l,r,i,t,a,b,zz;
queue < pair < int , pp > > Q[N];

int main(){
	scanf("%d%d%d",&n,&l,&r);
	for(i=1;i<=n;i++) scanf("%d",&A[i].nd), zz += A[i].nd;
    for(i=1;i<=n;i++) scanf("%d",&A[i].st), t += A[i].st = !A[i].st;
	zz -= r-1;
    t = n-t;
	sort(A+1,A+n+1);
	reverse(A+1 , A+n+1);

	Q[t].push(mp(1,mp(0,0)));
	for(; t ;){
        if(Q[t].size() == 0){
            t--;  continue;
        }
	    i = Q[t].front().st;
        a = Q[t].front().nd.st;
		b = Q[t].front().nd.nd;
		Q[t].pop();
        if(H[i][a]) continue;
		H[i][a] = 1;
		if(a >= l && b >= zz){
			printf("%d", t);
			return 0;
		}
        if(i <= n){
    		if(b < zz) Q[ t-(!A[i].st && b+A[i].nd<zz) ].push(mp(i+1,mp(a,b+A[i].nd)));
		  if(a < l) Q[ t-!A[i].st ].push(mp(i+1,mp(a+A[i].nd,b)));
        }
    }

    printf("%d", 0);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fin(name) freopen(name, "r", stdin)
#define fout(name) freopen(name, "w", stdout)
#define I(x, a) for(auto x : a)
#define F(i, l, r) for(auto i = l; i < r; ++i)
#define E(i, l, r) for(auto i = l; i <= r; ++i)
#define DF(i, l, r) for(auto i = l; i >= r; --i)
#define clean(a) memset((a),0,sizeof (a))
#define sync ios_base::sync_with_stdio(0);cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define ret return
#define cont continue
#define brk break
#define ins insert
#define sz(a) ((int)(a).size())

typedef long long ll;
typedef unsigned long long ull;
typedef long double dbl;
typedef pair <int, int> pii;

const int inf = (int)1e9;
const ll linf = (ll)1e18;
const int mod = (int)1e9 + 7;
const dbl eps = (dbl)1e-8;
const int maxn = (int)2e4 + 5;
const dbl pi = acos(-1);

int ans = 0;
int n, l, r;
pii a[maxn];
vector <int> v0, v1;
int d[maxn];

void prodv(int& u, int& sumh, int h, int gr, int dop = 0) {
	if (h < 0 || u > gr + 1)
		return;
	while (sumh <= h && u <= gr) {
		sumh += a[v1[u]].X;
		++u;
	}
	if (sumh <= h)
		ans = max(ans, u + dop);
	else
		ans = max(ans, u);
}

int main() {
//    fin("t.in");
    sync;
    cin >> n >> l >> r;
    F(i, 0, n) {
    	cin >> a[i].X;
    }
    F(i, 0, n) {
    	cin >> a[i].Y;
    }
    sort(a, a + n);
    F(i, 0, n) {
    	if (a[i].Y == 0)
    		v0.pb(i);
    	else
    		v1.pb(i);
    }
    int u0 = 0;
    int sum0 = 0;
    int u1 = 0;
    int sum1 = 0;
   	E(i, 0, r)
		d[i] = inf;
	d[0] = 0;
	I(i, v0) {
		DF(j, r, 0) {
			if (d[j] == inf)
				cont;
			d[j + a[i].X] = 0;
		}
	}
	int minl = r + 1;
	int minl2 = r + 1;
	E(i, l, r)
		if (d[i] != inf) {
			minl = i;
			break;
		}
	DF(i1, sz(v1) - 1, 0) {
		int i = v1[i1];
		E(j, l, r) {
			if (d[j] != inf) {
				minl = j;
				break;
			}
		}
		E(j, minl, r) {
			if (d[j] != inf && d[j] < sz(v1) - i1 - 1) {
				minl2 = j;
				break;
			}
		}
		prodv(u0, sum0, r - minl, i1);
		prodv(u1, sum1, r - minl2, i1, 1);

		DF(j, r, 0) {
			if (d[j] == inf)
				cont;
			d[j + a[i].X] = min(d[j + a[i].X], d[j] + 1);
		}
//		cout << i << " " << a[i].X << " " << d[0] << " " << d[3] << endl;
	}
//	exit(0);
	E(j, l, r) {
		if (d[j] != -1 && d[j] < sz(v1)) {
			minl2 = j;
			break;
		}

	}
	/*
	cout << minl2 << endl;
	cout << sum1 << endl;
	cout << u1 << endl;
	cout << r - minl2 << endl;
	*/
	prodv(u1, sum1, r - minl2, -1, 1);
	cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fin(name) freopen(name, "r", stdin)
#define fout(name) freopen(name, "w", stdout)
#define I(x, a) for(auto x : a)
#define F(i, l, r) for(auto i = l; i < r; ++i)
#define E(i, l, r) for(auto i = l; i <= r; ++i)
#define DF(i, l, r) for(auto i = l; i >= r; --i)
#define clean(a) memset((a),0,sizeof (a))
#define sync ios_base::sync_with_stdio(0);cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define ret return
#define cont continue
#define brk break
#define ins insert
#define sz(a) ((int)(a).size())

typedef long long ll;
typedef unsigned long long ull;
typedef long double dbl;
typedef pair <int, int> pii;

const int inf = (int)1e9;
const ll linf = (ll)1e18;
const int mod = (int)1e9 + 7;
const dbl eps = (dbl)1e-8;
const int maxn = (int)2e4 + 5;
const dbl pi = acos(-1);

int ans = 0;
int n, l, r;
pii a[maxn];
vector <int> v0, v1;
int d[maxn];

void prodv(int& u, int& sumh, int h, int gr, int dop = 0) {
	if (h == -1 || u > gr + 1)
		return;
	while (sumh <= h && u <= gr) {
		sumh += a[v1[u]].X;
		++u;
	}
	if (sumh <= h)
		ans = max(ans, u + dop);
	else
		ans = max(ans, u);
}

int main() {
//    fin("t.in");
    sync;
    cin >> n >> l >> r;
    F(i, 0, n) {
    	cin >> a[i].X;
    }
    F(i, 0, n) {
    	cin >> a[i].Y;
    }
    sort(a, a + n);
    F(i, 0, n) {
    	if (a[i].Y == 0)
    		v0.pb(i);
    	else
    		v1.pb(i);
    }
    int u0 = 0;
    int sum0 = 0;
    int u1 = 0;
    int sum1 = 0;
   	E(i, 0, r)
		d[i] = inf;
	d[0] = 0;
	I(i, v0) {
		F(j, 0, r) {
			if (d[j] == inf)
				cont;
			d[j + a[i].X] = 0;
		}
	}
	int minl = r + 1;
	int minl2 = r + 1;
	E(i, l, r)
		if (d[i] != inf) {
			minl = i;
			break;
		}
	DF(i1, sz(v1) - 1, 0) {
		int i = v1[i1];
		E(j, l, r) {
			if (d[j] != inf) {
				minl = j;
				break;
			}
		}
		E(j, minl, r) {
			if (d[j] != inf && d[j] < sz(v1) - i1 - 1) {
				minl2 = j;
				break;
			}
		}
		/*
		cout << minl << " " << minl2 << endl;
		cout << d[3] << endl;
		cout << d[0] << endl;
		cout << a[i].X << endl;
		*/
		prodv(u0, sum0, r - minl, i1);
		prodv(u1, sum1, r - minl2, i1, 1);

		F(j, 0, r) {
			if (d[j] == inf)
				cont;
			d[j + a[i].X] = min(d[j + a[i].X], d[j] + 1);
		}
//		cout << i << " " << a[i].X << " " << d[0] << " " << d[3] << endl;
	}
//	exit(0);
	E(j, l, r) {
		if (d[j] != -1 && d[j] < sz(v1)) {
			minl2 = j;
			break;
		}

	}
	/*
	cout << minl2 << endl;
	cout << sum1 << endl;
	cout << u1 << endl;
	cout << r - minl2 << endl;
	*/
	prodv(u1, sum1, r - minl2, -1, 1);
	cout << ans << endl;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int MAXN=20005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int f[MAXN];
PR a[MAXN];
signed main()
{
	int n=read(),l=read(),r=read(),sum=0;
	for (int i=1;i<=n;i++) a[i].fi=read(),sum+=a[i].fi;
	for (int i=1;i<=n;i++) a[i].se=read();
	sort(a+1,a+n+1,[&](PR x,PR y){ return (x.se<y.se)||(x.se==y.se&&x.fi>y.fi); });
	f[0]=0;
	for (int i=1;i<=sum;i++) f[i]=-INF;
	for (int i=1;i<=n;i++)
		for (int j=sum-l;j>=a[i].fi;j--) upmax(f[j],f[j-a[i].fi]+(a[i].se&&j>=sum-r));
	int ans=0;
	for (int j=0;j<=sum-l;j++) upmax(ans,f[j]);
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
using std::swap;
inline int read()
{
	int res=0,f=0;
	char x;
	while((x=getchar())<'0'||x>'9')
		f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())
		res=res*10+x-'0';
	return f?-res:res;
}
const int N=1e4+5,inf=0x3f3f3f3f;
int n,h,l,r,ans;
int atot,btot;
int a[N],b[N],c[N];
int dp[N];
signed main()
{
	register int i,j;
	n=read();l=read();r=read();
	for(i=1;i<=n;i++)
		h+=(c[i]=read());
	swap(l,r);l=h-l;r=h-r;
	for(i=1;i<=n;i++)
		(read()?b[++btot]:a[++atot])=c[i];
	for(i=1;i<=h;i++)
		dp[i]=-inf;
	for(i=1;i<=atot;i++)
		for(j=h;j>=a[i];j--)
			if(dp[j-a[i]]>dp[j])
				dp[j]=dp[j-a[i]];
	for(i=1;i<=btot;i++)
		for(j=h;j>=b[i];j--)
			if(dp[j-b[i]]+(j>=l&&j<=r)>dp[j])
				dp[j]=dp[j-b[i]]+(j>=l&&j<=r);
	for(i=0;i<=h;i++)
		if(dp[i]>ans)
			ans=dp[i];
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
using std::swap;
using std::greater;
using std::sort;
inline int read()
{
	int res=0,f=0;
	char x;
	while((x=getchar())<'0'||x>'9')
		f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())
		res=res*10+x-'0';
	return f?-res:res;
}
const int N=1e4+5,inf=0x3f3f3f3f;
int n,h,l,r,ans;
int atot,btot;
int a[N],b[N],c[N];
int dp[N];
signed main()
{
	register int i,j;
	n=read();l=read();r=read();
	for(i=1;i<=n;i++)
		h+=(c[i]=read());
	swap(l,r);l=h-l;r=h-r;
	for(i=1;i<=n;i++)
		(read()?b[++btot]:a[++atot])=c[i];
	for(i=1;i<=h;i++)
		dp[i]=-inf;
	for(i=1;i<=atot;i++)
		for(j=h;j>=a[i];j--)
			if(dp[j-a[i]]>dp[j])
				dp[j]=dp[j-a[i]];
	sort(b+1,b+btot+1,greater<int>());
	for(i=1;i<=btot;i++)
		for(j=h;j>=b[i];j--)
			if(dp[j-b[i]]+(j>=l&&j<=r)>dp[j])
				dp[j]=dp[j-b[i]]+(j>=l&&j<=r);
	for(i=0;i<=h;i++)
		if(dp[i]>ans)
			ans=dp[i];
	printf("%d\n",ans);
	return 0;
}


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

const double eps = 1e-9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define sz(c) int((c).size())
#define all(c) (c).begin(), (c).end()
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define FORD(i,a,b) for (int i = int(b)-1; i >= (a); i--)
#define FORIT(i,c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define mscanf(...) if(0 == scanf(__VA_ARGS__)){fprintf(stderr, "Could not parse arguments\n");}

const int MAXN = 10100;
int N,L,U,A;

int a[MAXN],b[MAXN];
int t[MAXN], T;
int s[MAXN], S;

int dp[MAXN];

int calc(){
    int X = A-L+1;
    FOR(i,0,X)dp[i] = 0;
    dp[0] = 1;
    FOR(i,0,S){
        int x = s[i];
        FORD(j,x,X)dp[j] |= dp[j-x];
    }
    int sumT = 0;
    FOR(i,0,T)sumT += t[i];
    FORD(i,0,T){
        int x = t[i];
        FOR(j,max(0,A-U-x),A-L-sumT+1)if(dp[j])return i+1;
        FORD(j,x,X){
            dp[j] |= dp[j-x] | ((dp[j-x]<<1)&3);
        }
        sumT -= x;
        FOR(j,max(0,A-U),A-L-sumT+1)if(dp[j]&2)return i+1;
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin >> N >> L >> U;
    FOR(i,0,N)cin >> a[i];
    FOR(i,0,N)cin >> b[i];
    T = S = A = 0;
    FOR(i,0,N)A += a[i];
    FOR(i,0,N){
        if(b[i])t[T++] = a[i];
        else s[S++] = a[i];
    }
    sort(s,s+S);
    sort(t,t+T);
    cout << calc() << endl;
	return 0;
}


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

const double eps = 1e-9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define sz(c) int((c).size())
#define all(c) (c).begin(), (c).end()
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define FORD(i,a,b) for (int i = int(b)-1; i >= (a); i--)
#define FORIT(i,c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define mscanf(...) if(0 == scanf(__VA_ARGS__)){fprintf(stderr, "Could not parse arguments\n");}

const int MAXN = 10100;
int N,L,U,A;

int a[MAXN],b[MAXN];
int t[MAXN], T;
int s[MAXN], S;

int dp[MAXN];

int calc(){
    int X = A-L+1;
    FOR(i,0,X)dp[i] = 0;
    dp[0] = 1;
    FOR(i,0,S){
        int x = s[i];
        FORD(j,x,X)dp[j] |= dp[j-x];
    }
    int sumT = 0;
    FOR(i,0,T)sumT += t[i];
    FORD(i,0,T){
        int x = t[i];
        FOR(j,A-U-x,A-L-sumT+1)if(dp[j])return i+1;
        FORD(j,x,X){
            dp[j] |= dp[j-x] | ((dp[j-x]<<1)&3);
        }
        sumT -= x;
        FOR(j,A-U,A-L-sumT+1)if(dp[j]&2)return i+1;
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin >> N >> L >> U;
    FOR(i,0,N)cin >> a[i];
    FOR(i,0,N)cin >> b[i];
    T = S = A = 0;
    FOR(i,0,N)A += a[i];
    FOR(i,0,N){
        if(b[i])t[T++] = a[i];
        else s[S++] = a[i];
    }
    sort(s,s+S);
    sort(t,t+T);
    cout << calc() << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <map>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#define dd c = getchar()
#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

const int maxn = 10005;
const int inf = 0x3f3f3f3f;

typedef long long LL;

struct bb {
	int x, b;
	friend bool operator < (bb a, bb b) {
		return a.b < b.b;
	}
} x[maxn];

int f[maxn];

int main() {
	int n, l, r, s = 0;
	read(n), read(l), read(r);
	swap(l, r);
	for (int i = 1; i <= n; ++i) {
		read(x[i].x);
		s += x[i].x;
	}
	l = s - l, r = s - r;
	for (int i = 1; i <= n; ++i) {
		read(x[i].b);
	}
	// puts("bla");
	sort(x + 1, x + n + 1);
	for (int i = 1; i <= s; ++i) {
		f[i] = -inf;
	}
	// puts("alb");
	for (int i = 1; i <= n; ++i) {
		for (int j = s; j >= x[i].x; --j) {
			f[j] = max(f[j], f[j - x[i].x] + (x[i].b && l <= j && j <= r));
		}
	}
	// puts("baogao");
	int ans = -inf;
	for (int i = 0; i <= s; ++i) {
		ans = max(ans, f[i]);
	}
	write(ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <map>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#define dd c = getchar()
#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

const int maxn = 10005;
const int inf = 0x3f3f3f3f;

typedef long long LL;

struct bb {
	int x, b;
} x[maxn];

int f[maxn];

int main() {
	int n, l, r, s = 0;
	read(n), read(l), read(r);
	swap(l, r);
	for (int i = 1; i <= n; ++i) {
		read(x[i].x);
		s += x[i].x;
	}
	l = s - l, r = s - r;
	for (int i = 1; i <= n; ++i) {
		read(x[i].b);
	}
	// puts("bla");
	sort(x + 1, x + n + 1, [](bb a, bb b) { return a.b < b.b || (a.b == b.b && a.x > b.x); });
	for (int i = 1; i <= s; ++i) {
		f[i] = -inf;
	}
	// puts("alb");
	for (int i = 1; i <= n; ++i) {
		for (int j = s; j >= x[i].x; --j) {
			f[j] = max(f[j], f[j - x[i].x] + (x[i].b && l <= j && j <= r));
		}
	}
	int ans = -inf;
	for (int i = 0; i <= s; ++i) {
		ans = max(ans, f[i]);
	}
	write(ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct _{
	int val,op;
	bool operator<(const _ &ob)const{
		if(op!=ob.op) return op<ob.op;
		return val>ob.val;
	}
}a[10010];
int dp[10010];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	int l,r;
	cin>>l>>r;
	int i;
	for(i=1;i<=n;i++){
		cin>>a[i].val;
	}
	for(i=1;i<=n;i++){
		cin>>a[i].op;
	}
	long long sum=0;
	for(i=1;i<=n;i++){
		sum+=a[i].val;
	}
	int tmp=l;
	l=sum-r;
	r=sum-tmp;
	l=max(l,0);
	sort(a+1,a+1+n);
	memset(dp,250,sizeof(dp));
	dp[0]=0;
	int j;
	for(i=1;i<=n;i++){
		for(j=sum-a[i].val;j>=0;j--){
			if(j+a[i].val>=l&&j+a[i].val<=r&&a[i].op) dp[j+a[i].val]=max(dp[j+a[i].val],dp[j]+1);
			else dp[j+a[i].val]=max(dp[j+a[i].val],dp[j]);
		}
	}
	int ans=0;
	for(i=l;i<=r;i++){
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct _{
	int val,op;
	bool operator<(const _ &ob)const{
		if(op!=ob.op) return op<ob.op;
		return val>ob.val;
	}
}a[10010];
int dp[10010];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	int l,r;
	cin>>l>>r;
	int i;
	for(i=1;i<=n;i++){
		cin>>a[i].val;
	}
	for(i=1;i<=n;i++){
		cin>>a[i].op;
	}
	long long sum=0;
	for(i=1;i<=n;i++){
		sum+=a[i].val;
	}
	int tmp=l;
	l=sum-r;
	r=sum-tmp;
	sort(a+1,a+1+n);
	dp[0]=1;
	int j;
	for(i=1;i<=n;i++){
		for(j=sum-a[i].val;j>=0;j--){
			if(j+a[i].val>=l&&j+a[i].val<=r&&a[i].op) dp[j+a[i].val]=max(dp[j+a[i].val],dp[j]+1);
			else dp[j+a[i].val]=max(dp[j+a[i].val],dp[j]);
		}
	}
	int ans=0;
	for(i=l;i<=r;i++){
		ans=max(ans,dp[i]);
	}
	cout<<ans-1<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct _{
	int val,op;
	bool operator<(const _ &ob)const{
		if(op!=ob.op) return op<ob.op;
		return val>ob.val;
	}
}a[10010];
int dp[10010];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	int l,r;
	cin>>l>>r;
	int i;
	for(i=1;i<=n;i++){
		cin>>a[i].val;
	}
	for(i=1;i<=n;i++){
		cin>>a[i].op;
	}
	long long sum=0;
	for(i=1;i<=n;i++){
		sum+=a[i].val;
	}
	int tmp=l;
	l=sum-r;
	r=sum-tmp;
	l=max(l,0);
	sort(a+1,a+1+n);
	dp[0]=1;
	int j;
	for(i=1;i<=n;i++){
		for(j=sum-a[i].val;j>=0;j--){
			if(j+a[i].val>=l&&j+a[i].val<=r&&a[i].op) dp[j+a[i].val]=max(dp[j+a[i].val],dp[j]+1);
			else dp[j+a[i].val]=max(dp[j+a[i].val],dp[j]);
		}
	}
	int ans=0;
	for(i=l;i<=r;i++){
		ans=max(ans,dp[i]);
	}
	cout<<ans-1<<endl;
	return 0;
}


#if 1

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
#include <string.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

const int MAX = 1e4+1;
int dp[MAX];

void solve()
{
	ll a,b,c;
	cin>>a>>b>>c;

	vector<pair<int,int>> x(a);
	int l,r;
	for(int i=0;i<a;++i)
	{
		cin>>x[i].second;
	}

	for(int i=0;i<a;++i)
	{
		cin>>x[i].first;
	}

	sort(all(x));

	int idx=0;
	for(int i=0;i<a;++i)
	{
		if(x[i].first)
		{
			idx = i;
			break;
		}
	}

	int frst=-1;

	int res=0;
	int cur=0;

	dp[0]=1;

	for(int i=0;i<idx-2;++i)
	{
		for(int j=c;j>=x[i].second;--j)
		{
			dp[j]+=dp[j-x[i].second];
		}
	}

	for(int i=a-1;i>idx-2;++i)
	{
		cur=0;
		frst=-1;
		for(int j=c;j>=x[i].second;--j)
		{
			dp[j]+=dp[j-x[i].second];
		}

		for(int j=b;j<=c;++j)
		{
			if(dp[j])
			{
				frst=c-j;
				break;
			}
		}

		if(frst==-1)
		{
			continue;
		}

		for(int j=max(idx,i+1);j<a;++j)
		{
			cur++;
			frst-=x[i].second;
			if(frst<=0)
			{
				break;
			}

		}
		res=max(res,cur);
	}

	cout<<res<<endl;
}

#define name "lepus"

int main() {
	//ios_base::sync_with_stdio(false);
	//freopen(name".in","r",stdin); freopen(name".out","w",stdout);
	cout.setf(ios::fixed);
	cout.precision(9);
	solve();

	return 0;
}
#endif


#if 1

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
#include <string.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

const int MAX = 1e4+1;
int dp[MAX];

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

void solve()
{
	ll a,b,c;
	cin>>a>>b>>c;

	vector<pair<int,int>> x(a);
	int l,r;
	int sum=0;

	for(int i=0;i<a;++i)
	{
		cin>>x[i].second;
		sum+=x[i].second;
	}
	int buf = sum-c;
	c = sum -b;
	b= buf;
	for(int i=0;i<a;++i)
	{
		cin>>x[i].first;
	}

	sort(all(x),cmp);

	int res=0;

	for(int i=0;i<=c;++i)
	{
		dp[i]=-1e9;
	}

	dp[0]=0;

	for(int i=0;i<a;++i)
	{
		for(int j=c;j>=x[i].second;--j)
		{
			dp[j]=max(dp[j],dp[j-x[i].second]+x[i].first*(j<=c && j>=b));
		}
	}

	for(int i=0;i<=c;++i)
	{
		res=max(res,dp[i]);
	}

	cout<<res<<endl;
}

#define name "lepus"

int main() {
	//ios_base::sync_with_stdio(false);
	//freopen(name".in","r",stdin); freopen(name".out","w",stdout);
	cout.setf(ios::fixed);
	cout.precision(9);
	solve();

	return 0;
}
#endif


#if 1

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
#include <string.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

const int MAX = 1e4+1;
int dp[MAX];

void solve()
{
	ll a,b,c;
	cin>>a>>b>>c;

	vector<pair<int,int>> x(a);
	int l,r;
	for(int i=0;i<a;++i)
	{
		cin>>x[i].second;
	}

	for(int i=0;i<a;++i)
	{
		cin>>x[i].first;
	}

	sort(all(x));

	int idx=0;
	for(int i=0;i<a;++i)
	{
		if(x[i].first)
		{
			idx = i;
			break;
		}
	}

	int frst=-1;

	int res=0;
	int cur=0;

	dp[0]=1;

	for(int i=0;i<idx-1;++i)
	{
		for(int j=c;j>=x[i].second;--j)
		{
			dp[j]+=dp[j-x[i].second];
		}
	}

	for(int j=b;j<=c;++j)
	{
		if(dp[j])
		{
			frst=c-j;
			break;
		}
	}

	if(frst!=-1)
	{
		for(int j=idx;j<a;++j)
		{
			cur++;
			frst-=x[j].second;
			if(frst<=0)
			{
				break;
			}

		}
	}
	res=max(res,cur);

	for(int i=a-1;i>=idx;--i)
	{
		cur=0;
		frst=-1;
		for(int j=c;j>=x[i].second;--j)
		{
			dp[j]+=dp[j-x[i].second];
		}

		for(int j=b;j<=c;++j)
		{
			if(dp[j])
			{
				frst=c-j;
				break;
			}
		}

		if(frst==-1)
		{
			continue;
		}

		for(int j=max(idx,i+1);j<a;++j)
		{
			cur++;
			frst-=x[j].second;
			if(frst<=0)
			{
				break;
			}

		}
		res=max(res,cur);
	}

	cout<<res<<endl;
}

#define name "lepus"

int main() {
	//ios_base::sync_with_stdio(false);
	//freopen(name".in","r",stdin); freopen(name".out","w",stdout);
	cout.setf(ios::fixed);
	cout.precision(9);
	solve();

	return 0;
}
#endif


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#include <complex>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
const int maxn = 10005;
int a[maxn], b[maxn];
vector<int> v;
bool f1[maxn], f2[maxn];
int q[maxn];
int main()
{
	std::ios::sync_with_stdio(false);
	int n, l, r;
	cin >> n >> l >> r;
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	for(int i = 1; i <= n; i ++)
		cin >> b[i];
	f1[0] = 1;
	int pos1 = l == 0 ? 0 : maxn, pos2 = maxn;
	for(int i = 1; i <= n; i ++)
	{
		if(b[i] == 0)
		{
			for(int j = r - a[i]; j >= 0; j --)
				if(f1[j])
				{
					f1[j + a[i]] = 1;
					if(j + a[i] >= l && j + a[i] < pos1)
						pos1 = j + a[i];
				}
		}
		else
			v.push_back(a[i]);
	}
	sort(v.begin(), v.end());
	for(int i = 1; i <= v.size(); i ++)
		q[i] = q[i - 1] + v[i - 1];
	for(int i = (int)v.size() - 1; i >= 0; i --)
	{
		if(pos2 + q[i + 1] <= r)
		{
			cout << i + 2 << endl;
			return 0;
		}
		if(pos1 + q[i + 1] <= r)
		{
			cout << i + 1 << endl;
			return 0;
		}
		for(int j = r - v[i]; j >= 0; j --)
			if(f2[j])
			{
				f2[j + v[i]] = 1;
				if(j + v[i] >= l && j + v[i] < pos2)
					pos2 = j + v[i];
			}
		for(int j = r; j >= 0; j --)
			if(f1[j])
			{
				f2[j] = 1;
				if(j >= l && j < pos2)
					pos2 = j;
			}
		for(int j = r - v[i]; j >= 0; j --)
			if(f1[j])
			{
				f1[j + v[i]] = 1;
				if(j + v[i] >= l && j + v[i] < pos1)
					pos1 = j + v[i];
			}
	}
	cout << (pos2 <= r) << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
int n,dp[N],l,r;
struct Box {int v,tp;} a[N];
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n >> l >> r;int sum = 0;
    for (int i = 1;i <= n;i++) cin >> a[i].v,sum += a[i].v;
    for (int i = 1;i <= n;i++) cin >> a[i].tp;
    sort(a + 1,a + n + 1,
    [](Box a,Box b) {return a.tp < b.tp || (a.tp == b.tp && a.v < b.v);});
    int tmp = l;l = sum - r;r = sum - tmp;
    for (int i = 1;i <= sum;i++) dp[i] = INT_MIN >> 2;
    dp[0] = 0;
    for (int i = 1;i <= n;i++)
        for (int j = sum;j >= a[i].v;--j)
            dp[j] = max(dp[j],dp[j - a[i].v] + a[i].tp * (l <= j && j <= r));
    cout << *max_element(dp + 1,dp + sum + 1) << endl;
    return 0;
}
//关于题解里的 "standard optimization for the knapsack",
//可以把01背包转成多重背包再二进制分组最后转回来，
//貌似时间复杂度是O(Nsqrt(N))(N为背包容量，且本题中物品的体积和也为N)??
//纯属瞎猜，未经证明...


#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
int n,dp[N],l,r;
struct Box {int v,tp;} a[N];
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n >> l >> r;int sum = 0;
    for (int i = 1;i <= n;i++) cin >> a[i].v,sum += a[i].v;
    for (int i = 1;i <= n;i++) cin >> a[i].tp;
    sort(a + 1,a + n + 1,
    [](Box a,Box b) {return a.tp < b.tp || (a.tp == b.tp && a.v > b.v);});
    int tmp = l;l = sum - r;r = sum - tmp;
    for (int i = 1;i <= sum;i++) dp[i] = INT_MIN >> 2;
    dp[0] = 0;
    for (int i = 1;i <= n;i++)
        for (int j = sum;j >= a[i].v;--j)
            dp[j] = max(dp[j],dp[j - a[i].v] + a[i].tp * (l <= j && j <= r));
    cout << *max_element(dp + 1,dp + sum + 1) << endl;
    return 0;
}
//关于题解里的 "standard optimization for the knapsack",
//可以把01背包转成多重背包再二进制分组最后转回来，
//貌似时间复杂度是O(Nsqrt(N))(N为背包容量，且本题中物品的体积和也为N)??
//纯属瞎猜，未经证明...


#include<bits/stdc++.h>
#define rg register
#define fp( i , x , y ) for( rg int i=(x); i<=(y); ++i )
#define fq( i , x , y ) for( rg int i=(y); i>=(x); --i )
using namespace std ;
const int N = 20005 ;
int n , l , r , s , f[N] ;
struct node {
  int hi , tpy ;
  friend bool operator < ( node A , node B ) {
    if( A.tpy != B.tpy ) return A.tpy < B.tpy ;
    return A.hi > B.hi ;
  }
} a[N] ;
signed main( ) {
  ios::sync_with_stdio(false) ;
  cin.tie(0) ;
  cin >> n >> l >> r ;
  fp( i , 1 , n ) cin >> a[i].hi , s += a[i].hi ;
  fp( i , 1 , n ) cin >> a[i].tpy ;
  sort( a+1 , a+1+n ) ;
  int tr=r ;
  r=s-l ; l=s-tr ;
  memset( f , -1 , sizeof(f) ) ;
  f[0] = 0 ;
  fp( i , 1 , n ) {
    fq( j , a[i].hi , s )
      if( f[j-a[i].hi] != -1 )
       f[j] = max( f[j] , f[j-a[i].hi] + (l<=j&&j<=r&&a[i].tpy) )  ;
  }
  int ans = 0 ;
  fp( i , 0 , s )
    ans = max( ans , f[i] ) ;
  cout << ans << '\n' ;
  return 0 ;
}


#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){int x=0,f=1,ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}
inline void write(int x){if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10); putchar(x%10+'0');}
inline void writeln(int x){write(x); puts("");}
const int N=1e4+5,INF=1e9;
struct node{
	int x,y;
}a[N];
int ans,n,l,r,f[N],m;
inline bool cmp(node A,node B){
	return A.x<B.x||(A.x==B.x&&A.y>B.y);
}
inline void init(){
	n=read(); l=read(); r=read();
	for (int i=1;i<=n;i++) a[i].y=read(),m+=a[i].y;
	l=m-l; r=m-r; swap(l,r);
	for (int i=1;i<=n;i++) a[i].x=read();
	sort(a+1,a+1+n,cmp);
}
inline void solve(){
	for (int i=1;i<=m;i++) f[i]=-INF;
	for (int i=1;i<=n;i++){
		for (int j=m-a[i].y;j>=0;j--){
			f[j+a[i].y]=max(f[j+a[i].y],f[j]+(a[i].x&&j+a[i].y>=l&&j+a[i].y<=r));
		}
	}
	for (int i=0;i<=m;i++) ans=max(ans,f[i]);
	writeln(ans);
}
int main(){
	init();
	solve();
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){int x=0,f=1,ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}
inline void write(int x){if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10); putchar(x%10+'0');}
inline void writeln(int x){write(x); puts("");}
const int N=1e4+5,INF=1e9;
struct node{
	int x,y;
}a[N];
int ans,n,l,r,f[N],m;
inline bool cmp(node A,node B){
	return A.x<B.x||(A.x==B.x&&A.y<B.y);
}
inline void init(){
	n=read(); l=read(); r=read();
	for (int i=1;i<=n;i++) a[i].y=read(),m+=a[i].y;
	l=m-l; r=m-r; swap(l,r);
	for (int i=1;i<=n;i++) a[i].x=read();
	sort(a+1,a+1+n,cmp);
}
inline void solve(){
	for (int i=1;i<=m;i++) f[i]=-INF;
	for (int i=1;i<=n;i++){
		for (int j=m-a[i].y;j>=0;j--){
			f[j+a[i].y]=max(f[j+a[i].y],f[j]+(a[i].x&&j+a[i].y>=l&&j+a[i].y<=r));
		}
	}
	for (int i=0;i<=m;i++) ans=max(ans,f[i]);
	writeln(ans);
}
int main(){
	init();
	solve();
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long a[N], b[N], d[100][N], n, l, r, so, p, t = 1;

main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];

    fo(i,1,n) fo(j,0,N - 1)
    {
        d[1][j] = d[0][j];
        if (j - a[i] >= 0)
        {
            p = (j - a[i] + 1 <= r && j - a[i] + 1 >= l) & b[i];
            d[1][j] = max(d[1][j], d[i - 1][j - a[i]] + p);
            so = max(so, d[1][j]);
        }
        if (j == r)
        {
            fo(k,0,n) d[0][j] = d[1][j];
        }
    }
    db(so);
    //fo(i,1,n) fo(j,1,8) cout << d[i][j] << " \n"[j == 8];
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long a[N], b[N], d[100][N], n, l, r, so, p, t = 1;

main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];

    fo(i,1,n) fo(j,0,N - 1)
    {
        d[1][j] = d[0][j];
        if (j - a[i] >= 0)
        {
            p = (j - a[i] <= r && j - a[i] >= l) & b[i];
            d[1][j] = max(d[1][j], d[i - 1][j - a[i]] + p);
            so = max(so, d[1][j]);
        }
        if (j == r)
        {
            fo(k,0,n) d[0][j] = d[1][j];
        }
    }
    db(so);
    //fo(i,1,n) fo(j,1,8) cout << d[i][j] << " \n"[j == 8];
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long a[N], b[N], c[100][N], d[100][N], n, l, r, so, p, t = 1;

main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];

    d[0][0] = 1;
    fo(i,1,n) fu(j,N - 1,0)
    {
        if (j + a[i] <= N - 1 && d[0][j])
        {
            d[1][j + a[i]] = d[0][j + a[i]];
            p = (l <= j && j <= r) & b[i];
            d[1][j + a[i]] = max(d[1][j + a[i]], d[0][j] + p);
            so = max(so, d[1][j + a[i]]);
        }
        //if (j <= 15) db(d[1][j]);
        //if (j == 15) de("");

        if (j == 0)
        {
            fo(k,0,N - 1) d[0][k] = d[1][k];
        }
    }
    db(so - 1);
    //fo(i,1,n) fo(j,1,20) cout << c[i][j] << " \n"[j == 20];
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long a[N], b[N], d[2][N], n, l, r, so, p, t = 1;

main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];

    d[0][0] = 1;
    fo(i,1,n) fu(j,N - 1,0)
    {
        if (j + a[i] <= N - 1 && d[0][j])
        {
            d[1][j + a[i]] = d[0][j + a[i]];
            p = (l <= j && j <= r) & b[i];
            d[1][j + a[i]] = max(d[1][j + a[i]], d[0][j] + p);
            so = max(so, d[1][j + a[i]]);
        }
        //if (j <= 15) db(d[1][j]);
        //if (j == 15) de("");

        if (j == 0)
        {
            fo(k,0,N - 1) d[0][k] = d[1][k];
        }
    }
    db(so - 1);
    //fo(i,1,n) fo(j,1,20) cout << c[i][j] << " \n"[j == 20];
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long a[N], b[N], d[N][N], n, l, r, so, p;

main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];
    fo(i,1,n) fo(j,0,r)
    {
        d[i][j] = d[i - 1][j];
        if (j - a[i] >= 0)
        {
            p = (j - a[i] + 1 <= r && j - a[i] + 1 >= l);
            d[i][j] = d[i - 1][j - a[i]] + p;
            so = max(so, d[i][j]);
        }
    }
    db(so);
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long a[N], b[N], c[100][N], d[100][N], n, l, r, so, p, t = 1;

main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];

    d[0][0] = 1;
    fo(i,1,n) fo(j,0,N - 1)
    {
        d[1][j] = d[0][j];
        //c[i][j] = c[i - 1][j];
        if (j - a[i] >= 0 && d[0][j - a[i]])
        {
            p = (j - a[i] <= r && j - a[i] >= l) & b[i];
            d[1][j] = max(d[1][j], d[0][j - a[i]] + p);
            //c[i][j] = max(c[i][j], c[i - 1][j - a[i]] + p);
            so = max(so, d[1][j]);
        }
        //if (j <= 9) db(d[1][j]);
        //if (j == 9) de("");

        if (j == r)
        {
            fo(k,0,N - 1) d[0][k] = d[1][k];
        }
    }
    db(so - 1);
    //fo(i,1,n) fo(j,1,20) cout << c[i][j] << " \n"[j == 20];
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long b[N], d[N], n, l, r, so, p, h, x, y, q;
PLL a[N];

long cmp (PLL& a, PLL& b)
{
	if (a.y != b.y) return a.y < b.y;
		else return a.x > b.x;
}
main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i].x, h += a[i].x;
    fo(i,1,n) cin >> a[i].y;
    sort(a + 1, a + 1 + n, cmp);

	x = h - r;
	y = h - l;
	l = x;
	r = y;

	//fo(i,1,n) db(a[i].x), de(a[i].y);
	fo(i,1,h) d[i] = -1e9;
	d[0] = 0;

    fo(i,1,n)
    {
        fu(j,h - a[i].x,0)
        {
			q = j + a[i].x;
            p = (q <= r && q >= l) * a[i].y;
            d[q] = max(d[q], d[j] + p);
			//if (q == 1) db(q), db(l), db(r), de(p);
        }
    }
	fo(i,0,h) so = max(so, d[i]);
	db(so);
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <bitset>
#include <algorithm>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <iomanip>
using namespace std;

#define task "storm"
#define ini freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout)
#define long long long
#define fo(i,a,b) for ( long i = (long) a; i <= (long) b; i++)
#define fu(i,a,b) for ( long i = (long) a; i >= (long) b; i--)
#define fe(ia,a) for(__typeof((a).begin())ia=(a).begin();ia!=(a).end();ia++)
#define PLL pair<long,long>
#define x first
#define y second
#define xx begin()
#define yy end()
#define endl "\n"
#define db(a) cout << a << " "
#define de(a) cout << a << endl
#define dv(a) fe(ia,a) db(*ia); de("");
#define vi vector<long>
#define aw(a) (a).begin(), (a).end()

const long N = 1e4 + 1e3;
long b[N], d[N], n, l, r, so, p, t = 1, h;
PLL a[N];

long cmp (PLL& a, PLL& b)
{
	if (a.y == b.y) return a.x < b.x;
		else return a.y > b.y;
}
main()
{
    cin >> n >> l >> r;
    fo(i,1,n) cin >> a[i].x;
    fo(i,1,n) cin >> a[i].y;
    sort(a + 1, a + 1 + n, cmp);

    fo(i,1,n)
    {
        fu(j,h - a[i].x,0)
        {
            p = (h + a[i].x <= r && h + a[i].x >= l) & a[i].y;
            d[j + a[i].x] = max(d[j + a[i].x], d[j] + p);
        }
    }

	fo(i,0,h) so = max(so, d[i]);
}


#include<bits/stdc++.h>
using namespace std;
const int N=10050;
struct node{
	int p,w;
}a[N];
bool cmp(node x,node y){
	if (x.p==y.p) return x.w>y.w;
	return x.p<y.p;
}
int n,l,r,sum;
int dp[N],ans;
int main(){
	scanf("%d%d%d",&n,&l,&r);
	for (int i=1;i<=n;++i){
		scanf("%d",&a[i].w);
		sum+=a[i].w;
	}
	for (int i=1;i<=n;++i)
		scanf("%d",&a[i].p);
	sort(a+1,a+n+1,cmp);
	int L=sum-r;
	int R=sum-l;
	l=L;
	r=R;
	memset(dp,128,sizeof dp);
	dp[0]=0;
	for (int i=1;i<=n;++i)
		for (int j=sum;j>=a[i].w;--j){
			if (l<=j&&r>=j) dp[j]=max(dp[j],dp[j-a[i].w]+a[i].p);
			else dp[j]=max(dp[j],dp[j-a[i].w]);
		}

	for (int i=1;i<=sum;++i)
		if (dp[i]>ans) ans=dp[i];

	printf("%d\n",ans);
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <cmath>
#include <iomanip>
using namespace std;

typedef int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define per(i,a,b) for(int i = a; i >= b; i--)
#define inf 1000000000000000000
#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define trav(a,x) for(auto& a : x)

vi a,b,ind;

bool mysort(int i, int j){
  if (b[i]){
    if (b[j]){
      return a[i]>a[j];
    } else {
      return 0;
    }
  } else if (b[j]){
    return 1;
  } else {
    return a[i] > a[j];
  }
}

vi _;
vector<vi> dp;

int main(){
  _.assign(10002,0);
  ll n,l,r,hi,lo,cnt = 0; cin >> n >> l >> r;
  a.assign(n,0); b.assign(n,0);
  rep(i,0,n){ cin >> a[i]; }
  rep(i,0,n){ cin >> b[i]; cnt+=b[i]; }
  rep(i,0,n){ ind.push_back(i); }
  lo = 1; hi = cnt+1;
  sort(ind.begin(),ind.end(),mysort);

  dp.assign(n+2,_);
  rep(i,0,n+1) dp[i][0] = 1;
  rep(j,0,10001){
    rep(i,0,n){
      if (dp[i][j]) {
        dp[i+1][j+a[ind[i]]] = 1;
        dp[i+1][j] = 1;
      }
    }
  }
  ll check = 0,ds = 0,cc = 1;
  while (!check){
    if (cc > cnt) {cout << 0 << endl; exit(0); }
    if (r-ds < 0) { cout << 0 << endl; exit(0); }
    rep(i,max(l-ds,ll(0)),r+1-ds){
      if (dp[n-cc][i]) { check = 1; break; }
    }
    ds += a[ind[n-cc]]; cc++;
  }
/*
  rep(i,0,n){
    cout << ind[i] << " " << a[ind[i]] << " " << b[ind[i]] << endl;
  }
  cout << endl;
*/
  while (lo + 1 < hi){
    ll m = (hi+lo)/2;
    ll s = 0, lim;
    per(i,n-1,n-m+1) s += a[ind[i]];
    lim = r-s;
    //cout << m << " " << s << " " << lim << endl;
    if (lim < l){
      hi = m;
    } else {
      dp.assign(n-m+2,_);
      bool check = 0;
      rep(i,0,n-m+1) dp[i][0] = 1;
      rep(j,0,10001){
        rep(i,0,n-m){
          if (dp[i][j]) {
            dp[i+1][j+a[ind[i]]] = 1;
            dp[i+1][j] = 1;
          }
        }
      }
      rep(j,l,lim+1){
        if (dp[n-m][j]) { check = 1; break; }
      }
      if (check){
        lo = m;
      } else {
        hi = m;
      }
    }
  }
  cout << lo << endl;
  return 0;
}