import std.stdio;
import std.algorithm;
import core.stdc.stdio;

int[] ds;
int[] as;

int[][] dim2arr(int w,int h){
	int[] buf = new int[w*h];
	int[][] ret = new int[][h];
	for(int i=0;i<h;i++){
		ret[i] = buf[i*w..(i+1)*w];
	}
	return ret;
}

int factorial(int a){
	if(a <= 1)
		return 1;
	else
		return a * (a-1).factorial;
}

int unUsed(bool[] used,int idx){
	foreach(i,ref b;used){
		if(!b){
			if(idx-- == 0){
				b = true;
				return i;
			}
		}
	}
	assert(false);
}

int dMax=0;

bool[] used;
void sequence(int[] dest,int t){
	used[] = false;
	for(int i=0;i<dMax;i++){
		dest[i] = used.unUsed(t % (dMax-i));
		t /= (dMax-i);
	}
}

int[] seqTmp;
int seqIdx(int[] seq){
	used[] = false;
	for(int i=0;i<dMax;i++){
		int c=0;
		for(int j=0;j<dMax;j++){
			if(!used[j]){
				if(j == seq[i]){
					seqTmp[i] = c;
					used[j] = true;
					break;
				}
				c++;
			}
		}
	}
	int res = 0;
	for(int i=dMax-1;i>=0;i--){
		res *= (dMax-i);
		res += seqTmp[i];
	}
	return res;
}

void step_insert(int[] seq,int t,int idx){
	int c=0;
	for(int i=0;i<dMax;i++){
		if(seq[i] == 0){
			if(i < idx){
				idx--;
			}
			continue;
		}
		seq[c++] = seq[i]-1; 
	}
	for(int i=idx;i<dMax;i++){
		swap(seq[i],t);
	}
}

int[][] dp;

void main(){
	int n;
	scanf("%d",&n);
	ds = new int[n];
	as = new int[n];
	
	for(int i=0;i<n;i++){
		scanf("%d",&ds[i]);
		dMax = max(dMax,ds[i]);
	}
	used = new bool[dMax];
	seqTmp = new int[dMax];
	
	for(int i=0;i<n;i++){
		scanf("%d",&as[i]);
	}
	
	int dFact = dMax.factorial;
	
	dp = dim2arr(dFact,n);
	
	
	int[] seq = new int[dMax];
	bool[] ate = new bool[dMax];
	
	for(int k=0;k<dFact;k++){
		
		sequence(seq,k);
		ate[] = false;
		int sum=0;
		foreach(i;seq){
			ate[i] = true;
			for(int j=max(0,i-ds[i]);j<min(dMax,i+ds[i]+1);j++){
				if(!ate[j])
					sum += as[i];
			}
		}
		dp[0][k] = sum;
	}
	
	for(int i;i<n-dMax;i++){
		for(int j=0;j<dFact;j++){
			for(int k=0;k<dMax+1;k++){
				int s = dp[i][j];
				sequence(seq,j);
				int c=0;
				foreach(l;seq[0..k]){
					if(dMax-l <= ds[i+l]){
						s += as[i+l];
					}
				}
				foreach(l;seq[k..dMax]){
					if(dMax-l <= ds[i+dMax]){
						s += as[i+dMax];
					}
				}
				seq.step_insert(dMax-1,k);
				int si = seq.seqIdx;
				dp[i+1][si] = max(dp[i+1][si],s);
			}
		}
	}
	
	int ri = max(0,n-dMax);
	int ans = 0;
	for(int i=0;i<dFact;i++){
		ans = max(dp[ri][i],ans);
	}
	
	printf("%d\n",ans);
}