#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 1000005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define ll long long 
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
struct data
{
    //Use required attributes
    int mn,mx,sum;
 
    //Default Values
    data() : mn(INF), mx(-INF), sum(0) {};
};
 
struct SegTree
{
    int n;
    vector<data> st;
    vector<bool> cLazy;
    vector<int> lazy;
 
    void init(int a)
    {
        n = a;
        st.resize(4 * n + 5);
        cLazy.assign(4 * n + 5, false);
        lazy.assign(4 * n + 5, 0);
		vector<int> v(n+1,0);
        build(v);
    }
 
    //Write reqd merge functions
    void merge(data &cur, data &l, data &r) 
    {
        cur.mn = min(l.mn, r.mn);
        cur.mx = max(l.mx, r.mx);
        cur.sum = l.sum + r.sum;
    }
    
    //Handle lazy propagation appriopriately
    void pushdown(int node, int L, int R)
    {
        if(L != R)
        {
            cLazy[node*2] = 1;
            cLazy[node*2 + 1] = 1;
            lazy[node*2] = lazy[node];
            lazy[node*2 + 1] = lazy[node]; 
        }
        st[node].mn = lazy[node];
        st[node].mx = lazy[node];
        st[node].sum = (R-L+1)*lazy[node];
        lazy[node] = 0;
        cLazy[node] = 0;
    }
 
    void build(vector<int> &a, int node, int tl, int tr)
    {
        if(tl == tr)
        {
            st[node].mn = a[tl];
            st[node].mx = a[tl];
            st[node].sum = a[tl];
            return;
        }
        int mid=(tl + tr)/2;
        build(a, node*2, tl, mid);
        build(a, node*2 + 1, mid + 1, tr);
        merge(st[node], st[node*2], st[node*2+1]);
    }
 
    data Query(int node, int tl, int tr, int i, int j)
    {
        if(cLazy[node])
            pushdown(node, tl, tr);
        if(j<tl || i>tr)
            return data();
        if(i<=tl && tr<=j)
            return st[node];
        int mid = (tl + tr)/2;
        data left=Query(node*2, tl, mid, i, j);
        data right=Query(node*2 + 1, mid + 1, tr, i, j);
        data cur;
        merge(cur, left, right);
        return cur;
    } 
 
    void Update(int node, int tl, int tr, int i, int j, int val)
    {
        if(cLazy[node])
            pushdown(node, tl, tr);
        if(j<tl || i>tr)
            return;
        if(i<=tl && tr<=j)
        {
            cLazy[node] = 1;
            lazy[node] = val;
            pushdown(node,tl,tr);
            return;
        }
        int mid = (tl + tr)/2;
        Update(node*2, tl, mid, i, j, val);
        Update(node*2 + 1, mid + 1, tr, i, j, val);
        merge(st[node], st[node*2], st[node*2 + 1]);
    }
 
    data query(int pos)
    {
        return Query(1, 0, n, pos, pos);
    }
 
    data query(int l, int r)
    {
        return Query(1, 0, n, l, r);
    }
 
    void update(int pos, int val)
    {
        Update(1, 1, n, pos, pos, val);
    }
 
    void update(int l, int r, int val)
    {
        Update(1, 1, n, l, r, val);
    }
 
    void build(vector<int> &a){
        build(a,1,0,sz(a));
    }
};
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{   
		int n;
		cin >> n;
		int h[n];
		int a[n];
		rep(i,0,n) cin >> h[i];
		rep(i,0,n) cin >> a[i];
		SegTree st;
		st.init(n+1);
		rep(i,0,n){
			int dp = st.query(0,h[i]-1).mx;
			dp += a[i];
			// trace(h[i], dp);
			st.update(h[i], max(st.query(h[i]).mx, dp));
		}	
		cout<<st.query(0,n).mx;
	}
}

Measured memory_bytes: 30080