#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define F first
#define S second
#define ll long long
#define N 200010
using namespace std;
ll n,h[N],a[N],dp[N],ans=0;
ll sm(ll x)
{
	ll ret=0;
	while(x>0)
	{
		ret=max(ret,dp[x]);
		x-=(x&(-x));
	}
	return ret;
}
void gb(ll x,ll val)
{
	while(x<=n)
	{
		dp[x]=max(dp[x],val);
		x+=(x&(-x));
	}
	return;
}
int main(){
	ll i,k;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>h[i];
	}
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	memset(dp,0,sizeof(dp));
	for(i=0;i<n;i++)
	{
		k=sm(h[i]);
		ans=max(ans,k+a[i]);
		gb(h[i],k+a[i]);
	}
	cout<<ans<<endl;
	return 0;
}