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

int maxk (vector <int> a, int x, int y);
int ppp(vector <int> high, vector <int> pri, int x, int y);

int main(){
	int n;
	cin>>n;
	vector <int> high(n);
	vector <int> pri (n);
	for(int i=0; i<n; i++){
		cin>>high[i];
	}
	for(int i=0; i<n; i++){
		cin>>pri[i];
	}
	if(n==1){
		cout<<pri[0];
		return 0;
	}
	int f = ppp(high, pri, 0, n-1);
	cout<<f;
	return 0;
}

int maxk (vector <int> a, int x, int y){
	int max = a[x];
	int maxi=x;
	for(int i =x+1; i<y+1; i++){
		if(a[i]> max){
			max=a[i];
			maxi=i;
		}
	}
	return maxi;
}

int ppp(vector <int> high, vector <int> pri, int x, int y){
	int maxc = maxk(high,x,y);
	int res=0;
	if(maxc==y || maxc==x){
		res= pri[maxc];
		return res;
	}
	if(ppp(high, pri, x, y-maxc) > ppp(high, pri, maxc+1, y)){
		res= pri[maxc]+ppp(high, pri, maxc+1, y);
		return res;
	}else{
		res = pri[maxc]+ppp(high, pri, x, y-maxc);
		return res;
	}
}


