#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	int range,add;
};

int N;
int table_index,max_digit;
int num_array[7],xiao[7],work[8],order[7],calc[7];
int table[5040][7];
int dp[100][5040];
bool used[7];
Info info[100];
map<int,int>MAP;

//num_arrayとusedはグローバルに持つ
void makeTable(int index){

	if(index == max_digit){
		int tmp = 0;
		for(int i = 0; i < max_digit; i++){
			table[table_index][i] = num_array[i];
			tmp = 10*tmp+num_array[i];
		}
		MAP[tmp] = table_index;
		table_index++;
		return;
	}
	for(int i = 0; i < max_digit; i++){
		if(used[i] == false){
			num_array[index] = i;
			used[i] = true;
			makeTable(index+1);
			used[i] = false;
		}
	}
}

int main(){

	scanf("%d",&N);

	for(int i = 0; i < N; i++){
		scanf("%d",&info[i].range);
	}
	for(int i = 0; i < N; i++){
		scanf("%d",&info[i].add);
	}

	//順列テーブルを作成する
	table_index = 0;
	max_digit = min(7,N);

	for(int i = 0; i < max_digit; i++)used[i] = false;
	makeTable(0);

	for(int i = 0; i < N; i++){
		for(int state = 0; state < table_index; state++)dp[i][state] = 0;
	}

	int next_state,next_value,index,tmp_num;

	//左端の計算
	for(int state = 0; state < table_index; state++){

		for(int k = 0; k < max_digit; k++)order[k] = table[state][k]; //順番の取得
		//小籠包を順番に並べる
		for(int a = 0; a < max_digit; a++){
			work[a] = order[a];
		}
		next_value = 0;
		for(int a = 0; a < max_digit-1; a++){ //汁を飛び散らせる小籠包のループ
			for(int b = a+1; b < max_digit; b++){ //汁を浴びる小籠包のループ
				if(abs(work[a]-work[b]) <= info[work[a]].range){
					next_value += info[work[a]].add;
				}
			}
		}
		dp[max_digit-1][state] = next_value;
		if(next_value == 0){
			printf("state:%d\n",state);
		}
	}

	if(N <= 7){

		int ans = 0;
		for(int state = 0; state < table_index; state++){
			ans = max(ans,dp[max_digit-1][state]);
		}

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

		return 0;
	}

	for(int loc = 7; loc <= N-1; loc++){

		for(int k = 0; k < 7; k++)xiao[k] = loc-(7-k); //作業量配列で扱う小籠包のインデックス

		for(int state = 0; state < 5040; state++){ //左7個の状態の走査

			for(int k = 0; k < 7; k++)order[k] = table[state][k]; //順番の取得

			//★locの小籠包の、相対順位のループ
			for(int pos = 0; pos <= 7; pos++){
				//小籠包を順番に並べる
				for(int a = 0; a <= 7; a++)work[a] = -1;
				work[pos] = loc;
				index = 0;
				for(int a = 0; a <= 7; a++){
					if(work[a] == -1){
						work[a] = xiao[order[index++]];
					}
				}
				//おいしさの増加分を計算
				next_value = dp[loc-1][state];

				//locの増加分(locより前に食べられた小籠包で、汁がlocにかかるものを求める)
				for(int a = 0; a < pos; a++){
					if(abs(work[a]-loc) <= info[work[a]].range){
						next_value += info[work[a]].add;
					}
				}
				//locの汁が飛び散ることによる増加分
				for(int a = pos+1; a <= 7; a++){
					if(abs(work[a]-loc) <= info[loc].range){
						next_value += info[loc].add;
					}
				}

				//新しい状態を計算する(loc-7の小籠包を除いた順位を計算する)
				index = 0;

				for(int a = 0; a <= 7; a++){
					if(work[a] == loc-7)continue;
					calc[index++] = 6-(loc-work[a]);
				}
				tmp_num = 0;
				for(int i = 0; i < 7; i++)tmp_num = 10*tmp_num+calc[i];

				next_state = MAP[tmp_num];

				dp[loc][next_state] = max(dp[loc][next_state],next_value);
			}
		}
	}

	int ans = 0;
	for(int state = 0; state < 5040; state++){
		ans = max(ans,dp[N-1][state]);
	}

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

	return 0;
}



Measured memory_bytes: 5500