#include "bits/stdc++.h"
using namespace std;
#define pb          push_back
#define mp          make_pair
#define ll          long long
#define pii         pair<ll, ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll, ll>
#define mii         map<pii, ll>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define hell        1000000007
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define rev(i,a,b)  for(ll i=a;i>=b;i--)
#define lb          lower_bound
#define ub          upper_bound

#define bg(i)       i.begin()
#define fset(it,s)   for(auto it = s.begin(); it != s.end(); it++)
 
#define track(x)                 cerr<<#x<<": "<<x<<endl
#define track2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define track3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define track4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
 
#define ed(i)       i.end()
#define mem(a)	    memset(a, 0, sizeof(a))

#define MOD         1000000007
ll mult(ll a,ll b, ll p=MOD){return ((a%p)*(b%p))%p;}
ll add(ll a, ll b, ll p=MOD){return (a + b + p)%p;}
ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1LL) {if (k & 1LL) r = mult(r,n,p); n = mult(n,n,p);} return r;}
ll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
const ll INF = 1e9 + 5;
#define N	    	400001

typedef struct Node {
	ll val;
	ll l, r;
	struct Node *left, *right;

}node;
node *getNode(ll val, int l, int r) {
	node *tem = (node *)malloc(sizeof(node));
	tem->l = l; tem->r = r;
	tem->left = tem->right = NULL;
	tem->val = val;
	return tem;
}
node *build(ll val, int l, int r) {
	if(l == r) {
		return getNode(val, l, r);
	}
	node *root = getNode(val, l, r);
	root->left = build(val, l, (l + r) / 2);
	root->right = build(val, (l + r) / 2 + 1, r);
	return root;
}
void update(node *root, ll ind, ll change) {
	root->val = max(root->val, change);
	if(root->left == root->right)
		return;
	ll l = root->l, r = root->r;
	if(ind <= (l + r) / 2) {
		update(root->left, ind, change);
	}
	else {
		update(root->right, ind, change);
	}
}
ll getMax(node *root, int lf, int rf) {
	ll l = root->l, r = root->r;
	if(lf == l && rf == r)
		return root->val;
	if(rf <= (l + r) / 2) 
		return getMax(root->left, lf, rf);
	else if(lf >= (l + r) / 2 + 1) 
		return getMax(root->right, lf, rf);
	else 
		return max(getMax(root->left, lf, (l + r) / 2), getMax(root->right,(l + r) / 2 + 1 , rf));
}

void solve(){
    node *root = build(0, 0, N);
    ll n;
    cin>>n;
    int a[n+1];
    int val[n+1];
    rep(i,1,n+1) cin>>a[i];
    rep(i,1,n+1) cin>>val[i];
    ll dp[n+1];
    mem(dp);
    rep(i, 1, n+1) {
        ll pre = getMax(root, 0, a[i]-1);
        dp[a[i]] = pre + val[i];
        update(root, a[i], pre + val[i]);
    }
    ll ans = 0;
    rep(i, 1, n+1)
        ans = max(ans, dp[i]);
    cout<<ans<<endl;
}
int main() {
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
	cin.tie(0);
	cout.tie(0);
	ll t = 1;
//	cin>>t;
	while(t--){
		solve();
	}
}




Measured memory_bytes: 40960