/*░   ░▄▀▀▀▄░Prashik░░ 
▄███▀░◐░░░▌░░░░░░░░░
░░░░▌░░░░░▐░░░░░░░░░ 
░░░░▐░░░░░▐░░░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄ 
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄ 
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄ 
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄ 
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄ 
░░░░░░░░░░░▌▌░▌▌░░░░░ 
░░░░░░░░░░░▌▌░▌▌░░░░░ 
░░░░░░░░░▄▄▌▌▄▌▌░░░░░*/

#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define tr(x,it) for(auto it = x.begin();it!=x.end();++it)
#define fpresent(x,c) ((c).find(x) != (c).end())  //set,map
#define present(c,x) (find(all(c),x) != (c).end()) //vector
#define pb push_back 
#define mp make_pair
#define setv(ar, val) memset(ar, val, sizeof(ar));
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}


vector<ll> tree(4000000);

ll n;

void buildtree(ll a[],ll node,ll left,ll right)
{
    if(left==right)
    {
        tree[node]=a[left];
    }
    else
    {
        ll mid=(left+right)/2;
        buildtree(a,2*node+1,left,mid);
        buildtree(a,2*node+2,mid+1,right);
        tree[node]=max(tree[2*node+1],tree[2*node+2]);//this tree is for the sum in the given range query , this line can be modified according to the need for
                                                //example, if we have to find the minimum in the given range we can accordingly change the line of command;
    }

}
void updateutil(ll a[],ll x,ll pos,ll left,ll right,ll node)//give the val to be updated and the pos where to update. pos should be with respect to the original array.
{
    if(left==right)
    {
        a[pos]=x;
        tree[node]=x;
    }
    else
    {
        ll mid=(left+right)/2;
        if(pos<=mid)
        {
            updateutil(a,x,pos,left,mid,2*node+1);
        }
        else
            updateutil(a,x,pos,mid+1,right,2*node+2);
        tree[node]=max(tree[2*node+1],tree[2*node+2]);//this line can be changed accordingly for a different query
    }
}
void update(ll a[],ll x,ll pos)
{
    updateutil(a,x,pos-1,0,n-1,0);
}
void prlltree()
{
    for(ll i=0;i<2*n-1;i++)
        cout<<tree[i]<<endl;
}
ll queryutil(ll left,ll right,ll l,ll r,ll node)
{

    if(left==l && right==r)
    {
        return tree[node];
    }
    else
    {
        if(left<=right)
        {
            ll mid=(l+r)/2;
            if(mid>=right)
            {
                return queryutil(left,right,l,mid,2*node+1);
            }
            if(left>mid)
            {
                 return queryutil(left,right,mid+1,r,2*node+2);
            }
            if(left<=mid && right>mid)
            {
                return max(queryutil(left,mid,l,mid,2*node+1),queryutil(mid+1,right,mid+1,r,2*node+2));//this line can be changed for minimum or maximum query

            }
        }
        else
            return 0;

    }
}
ll query(ll left,ll right)
{
    return queryutil(left-1,right-1,0,n-1,0);
}



int main()
{
    // freopen("input.in","r",stdin);
    // freopen("output.in","w",stdout);
    fastio;
    cin.tie(NULL);
    ll m,i,j,k,ans=0;
    cin>>n;
    ll h[n],a[n],rev[n+1];
   	for(i=0;i<n;i++)
   	{
   		cin>>h[i];
   		rev[h[i]]=i;
   	}
   	for(i=0;i<n;i++)
   		cin>>a[i];
   	ll val[n];
   	for(i=0;i<n;i++) val[i]=0;

    buildtree(val,0,0,n-1);
    for(i=1;i<=n;i++)
    {
    	ll pos = rev[i];
    	ll temp = query(1,pos+1) ;
    	ans = max(ans,temp + a[pos]);
    	update(val,a[pos] + temp,pos+1);
    	val[pos]=a[pos] + temp;
    	// for(j=0;j<n;j++)
    	// 	cout<<val[j]<<" ";
    	// cout<<endl;
    }

    cout<<ans<<endl;


    return 0;
}


Measured memory_bytes: 37760