#include<iostream>
#include<algorithm>
using namespace std;
//1-indexed
#include<vector>
template<typename T>
struct BIT{
	int n;
	vector<T>bit;
	BIT(int n_=0,T a=0):n(n_),bit(n_+1,a){}
	T sum(int i)
	{
		T ans=0;
		for(;i>0;i-=i&-i)ans=max(ans,bit[i]);
		return ans;
	}
	void add(int i,T a)
	{
		if(i==0)return;
		for(;i<=n;i+=i&-i)bit[i]=max(bit[i],a);
	}
};
int n,h[2<<17],a[2<<17];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)cin>>h[i];
	BIT<long>P(n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		P.add(h[i],P.sum(h[i])+a[i]);
	}
	cout<<P.sum(n)<<endl;
}