#include<bits/stdc++.h>
using namespace std;

// macros
#define int long long 
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define all(c) c.begin(), c.end()
#define PB push_back
#define MP make_pair
#define bitcount __builtin_popcount
#define watch(x) cerr<< (#x) << " is " << (x) <<"\n";
#define sz(x) ((int)((x).size()))
#define UNIQUE(c) (c).resize(unique(all(c)) - (c).begin())
#define pii2ll(p) ((ll)(p).first<<32 | (p).second)

// type definitions
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vi > vvi;
typedef pair<int, int> pii;
typedef long double ld;

// template functions
template < typename A, typename B >
ostream &operator << ( ostream& os, const pair< A, B > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}
template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "{";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}
template < typename A, typename B >
ostream &operator << ( ostream & os, const map< A, B > &v ) {
    os << "[";
    typename map< A, B >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << " -> " << it -> second ;
    }
    return os << "]";
}
template<typename T>
T gcd(T a, T b) { if(a==0 or b==0) return a+b; return gcd(b, a%b) ; }
template<typename T>
T lcm(T a, T b) { if(a==0 or b==0) return 0; return a/gcd(a, b)*b; }

// random number generation
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// constants
const long double eps = LDBL_EPSILON;
const int inf = 5e8;
const int modn = 1e9+7;
const int maxn = 2e5+3;

template<typename T>
class segTree
{
public:
    int n;
    vector<T> st;

    segTree() {}
    segTree(int _n) {
        n = _n;
        st.resize(4*n);
    }
    segTree(vector<T> a) {
        n = a.size();
        st.resize(4*n);
        for(int i=0; i<n; i++) {
            update(i, a[i]);
        }
    }

    T combine(T a, T b)
    {
        return max(a, b);
    }
    
    // void build(int v, int tl, int tr)
    // {
    //     if(tl==tr) {
    //         st[v]=something;
    //         return;
    //     }
        
    //     int tm=(tl+tr)/2;
    //     build(2*v+1, tl, tm);
    //     build(2*v+2, tm+1, tr);
    //     st[v]=combine(st[2*v+1], st[2*v+2]);
    // }

    auto query2(int v, int tl, int tr, int l, int r)
    {
        if(l>r) return (int)0;
        if(tl==l and tr==r) {
            return st[v];
        }
        int tm=(tl+tr)/2;
        auto ans1=query2(2*v+1, tl, tm, l, min(r, tm));
        auto ans2=query2(2*v+2, tm+1, tr, max(l, tm+1), r);
        return combine(ans1, ans2);
    }

    void update2(int v, int tl, int tr, int pos, T new_val)
    {
        if(tl==tr) {
            st[v] = new_val;
            return;
        }
        int tm=(tl+tr)/2;
        if(pos<=tm) {
            update2(2*v+1, tl, tm, pos, new_val);
        }
        else {
            update2(2*v+2, tm+1, tr, pos, new_val);
        }
        st[v]=combine(st[2*v+1], st[2*v+2]);
    }

    void update(int pos, T new_val) {
        update2(0, 0, n-1, pos, new_val);
    }

    auto query(int l, int r) {
        return query2(0, 0, n-1, l, r);
    }
};

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Your code here
    int n;
    cin>>n;
    vi h(n), val(n);
    for(int i=0; i<n; i++) {
        cin>>h[i];
    }
    for(int i=0; i<n; i++) {
        cin>>val[i];
    }
    segTree<int> st(n+1);
    vi dp(n);
    for(int i=0; i<n; i++) {
        int cur = st.query(1, h[i]-1);
        dp[i] = cur + val[i];
        st.update(h[i], dp[i]);
    }
    cout << *max_element(all(dp)) << "\n";
    return 0;
}



Measured memory_bytes: 11264