#include <bits/stdc++.h>

using namespace std;

#define all(a) a.begin(),a.end()
#define pb push_back
#define vt vector
#define endl '\n'
typedef long long ll;

const int inf=1e9+1;
const int N=4e5+605;

int n,h[N],c[N];
pair<int,int>t[N*4];

void build(int tl=1,int tr=n,int v=0){
    if(tr-tl==0){
        t[v]={h[tr],tr};
        return;
    }
    int mid=(tl+tr)/2;
    build(tl,mid,v+v+1);
    build(mid+1,tr,v+v+2);
    t[v]=max(t[v+v+1],t[v+v+2]);
}

pair<int,int> get(int tl,int tr,int v,int L,int R){
    if(tr<L || tl>R) return {0,0};
    if(tl>=L && tr<=R) return t[v];
    int mid=(tl+tr)/2;
    return max(get(tl,mid,v+v+1,L,R),get(mid+1,tr,v+v+2,L,R));
}

int dnc(int l,int r){
    //cout<<l<<' '<<r<<endl;
    int centr=get(1,n,0,l,r).second;
    //cout<<centr<<endl;
    //return 0;
    if(l==r) return c[r];
    if(l>r) return 0;
    int val1=dnc(l,centr-1);
    int val2=dnc(centr+1,r);
    if(val1>val2) return val2+c[centr];
    if(val1<val2) return val1+c[centr];
}

void solve(){
    cin>>n;
    for(int i=1; i<=n; ++i){
        cin>>h[i];
    }
    for(int i=1; i<=n; ++i){
        cin>>c[i];
    }
    build();
    cout<<dnc(1,n);
}

int main(){
    srand(time(0));
 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 int tt=1;
 //cin>>tt;
 while(tt--) {
  solve();
 }
}