#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,h[N],a[N];ll dp[N],t[N],ans;
inline int lowbit(int x){return x&-x;}
inline void update(int x,ll v){for(register int i=x;i<=n;i+=lowbit(i))t[i]=max(t[i],v);}
inline ll query(int x){ll sum=0;for(register int i=x;i;i-=lowbit(i))sum=max(sum,t[i]);return sum;}
int main(){
	scanf("%d",&n);
	for(register int i=1;i<=n;i++)scanf("%d",&h[i]);
	for(register int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(register int i=1;i<=n;i++)
		dp[i]=query(h[i]-1)+a[i],update(h[i],dp[i]),ans=max(ans,dp[i]);
	printf("%lld\n",ans);
	return 0;
}