#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
#define int long long
//for set
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;   
// for multiset
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_multiset;

#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define PI 3.1415926535897932384626433832795
int MOD=1e9+7;
int powmod(int a,int l, int md){int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
int binpow(int a,int l){int res=1;while(l){if(l&1)res=res*a;l/=2;a=a*a;}return res;}
typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> ii; typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) {return b|(1<<i);} //set ith bit
int __unset(int b, int i) {return b&(~(1UL<<i));}
int __check(int b, int i) {return b&(1<<i);} //returns 0 if ith bit is 0
int mulmod(int a, int b, int md) {return ((a%md)*(b%md))%md;}
int addmod(int a, int b, int md) {return (a+b)%md;}
int submod(int a, int b, int md) {return (((a-b)%md)+md)%md;}
int divmod(int a, int b, int md) {return mulmod(a, powmod(b, md-2, md), md);} //if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
priority_queue<int, vector<int>, greater<int> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t time_p=clock();
void time()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

class SegmentTree {
private:
	int n; vii st; vi lz; vi a;
	void merge(int node) {
		if(st[2*node].ss >= st[2*node+1].ss)
			st[node]=st[2*node];
		else
			st[node]=st[2*node+1];
	}
	void build(int node, int lo, int hi) {
		if(lo==hi) {
			st[node]={lo,a[lo]};
			return;
		}
		build(2*node,lo,(lo+hi)/2);
		build(2*node+1,(lo+hi)/2+1,hi);
		merge(node);
	}
	void propagate(int node, int l, int r) {
		if(l!=r) {
			lz[2*node] += lz[node];
			lz[2*node+1] += lz[node];
		}
		st[node].ss += lz[node];
		lz[node]=0;
	}
	void update(int node, int l, int r, int lo, int hi, int val) {
		if(lz[node]!=0)
			propagate(node,l,r);
		if(hi < l || lo > r)
			return;
		if(hi >= r && lo <= l) {
			lz[node]+=val;
			propagate(node,l,r);
			return;
		}
		update(2*node,l,(l+r)/2,lo,hi,val);
		update(2*node+1,(l+r)/2+1,r,lo,hi,val);
		merge(node);
	}
	ii rmq(int node, int l, int r, int lo, int hi) {
		if(lz[node]!=0)
			propagate(node,l,r);
		if(hi < l || lo > r)
			return {-1, -1};
		if(hi >= r && lo <= l)
			return st[node];
		ii d1 = rmq(2*node, l, (l+r)/2, lo, hi);
		ii d2 = rmq(2*node+1, (l+r)/2+1, r, lo, hi);
		if(d1.ss >= d2.ss)
			return d1;
		return d2;
	}
public:
	SegmentTree(vi& __a) {
		n = __a.size();
		a=__a;
		st.assign(4*n,{0,0});
		lz.assign(4*n,0);
		build(1,0,n-1);
	}
	void Update(int l, int r, int val) {return update(1,0,n-1,l,r,val);}
	ii RMQ(int l, int r) {return rmq(1,0,n-1,l,r);}
};

int32_t main(void)
{
	IOS;
	int n; cin>>n;
	vii h(n);
	vi a(n);
	vector<pair<ii,int>> hai(n);
	for(int i = 0; i < n; i++) {
		cin>>h[i].ff;
		h[i].ss=i;
	}
	sort(all(h));
	map<int,int> m;
	for(int i = 0; i < n; i++)
		m[h[i].ss]=i;
	for(int j = 0; j < n; j++)
		cin>>a[j];
	vi tmp(n,0);
	SegmentTree st(tmp);
	for(int i = 0; i < n; i++) {
		int ind = m[i];
		// cout<<ind<<endl;
		if(ind==0) {
			st.Update(ind,ind,a[i]);
			continue;
		}
		int val = st.RMQ(0,ind-1).ss;
		st.Update(ind,ind,max(val+a[i],a[i]));
		// cout<<val<<" "<<max(val+a[i],a[i])<<endl;
	}
	cout<<st.RMQ(0,n-1).ss<<endl;
	time();	
	return 0;
}

Measured memory_bytes: 44032