#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#define pb push_back
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);cout<<fixed<<setprecision(16);
#define ll long long int
#define ld long double
#define el '\n'
#define El '\n'
#define PI (ld)3.141592653589793238462643383279502884197169399375105820974944
#define inf (ll)1000000000
#define mod (ll)1000000007
#define mod1 (ll)998244353
#define fo(i, n) for (long long i = 0; i < n; i++)
#define pll pair<ll, ll>
#define ml map<ll,ll>
#define vpl vector<pll>
#define vvl vector<vector<ll> >
#define vvpl vector< vector<pll> >
#define ff first
#define ss second
#define pqueue priority_queue< ll >
#define pdqueue priority_queue< ll,vl ,greater< ll > >
#define mem(a,b) memset(a,b,sizeof(a));
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define sz(a) (ll)a.size()
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fd(i,a,b) for(ll i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define vl vector<ll>
//#define x ff
//#define y ss
#define pt(a) for(auto it:a)cout<<it<<" ";cout<<endl;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
using namespace std;

// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << t<<" "; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << v.first << " " << v.second ; }

double gcd(double a, double b) { return a < 0.01 ? b : gcd(fmod(b, a), a); }

long long bpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)

      res = res * a % m;
      a = a * a % m;
      b >>= 1;
  }
  return res;
}


ll kv;
ll t,n,x,y,h,k,z,p,q,w;
string s,s1,s2;
//vvl v;
//vl vis,d;

struct stree
{
    ll n;
    vector<ll>tree;

    void set(vl v){
        n=sz(v);
        tree.assign(8*n,0);
        build(v,1,0,n-1);
    }

    ll combine(ll a,ll b){
        return max(a,b);
    }

    void build(vl &a, ll v, ll tl, ll tr){
        if(tl==tr)tree[v]=a[tl];
        else{
            ll tm=(tl+tr)/2;
            build(a,2*v,tl,tm);
            build(a,2*v+1,tm+1,tr);
            tree[v]=combine(tree[2*v],tree[2*v+1]);
        }
    }

    void up(ll pos,ll val){update(1,1,n,pos,val);}

    void update(ll v, ll tl, ll tr, ll pos, ll new_val) {
        if (tl == tr)tree[v] = new_val;
        else {
            int tm = (tl + tr) / 2;
            if (pos <= tm)update(v*2, tl, tm, pos, new_val);
            else update(v*2+1, tm+1, tr, pos, new_val);
            tree[v] = combine(tree[v*2],tree[v*2+1]);
        }
    }

    ll que(ll l,ll r){return sum(1,1,n,l,r);}

    ll sum(ll v, ll tl, ll tr, ll l, ll r) {
        if (l > r)return 0;
        if (l == tl && r == tr)return tree[v];
        ll tm = (tl + tr) / 2;
        return combine(sum(v*2, tl, tm, l, min(r, tm))
           ,sum(v*2+1, tm+1, tr, max(l, tm+1), r));
    }

};

int main() {
    IOS;
    cin>>n;
    vl c(n),b(n),dp(n,0),a(n,0);
    cin>>c>>b;
    stree st;st.set(a);
    dp[0]=b[0];
    st.up(c[0],b[0]);
    for(int i=1;i<n;i++){
            dp[i]=b[i]+st.que(1,c[i]-1);
            st.up(c[i],dp[i]);
            //cout<<dp[i]<<el;
    }
    cout<<st.que(1,n)<<el;
    return 0;
}


Measured memory_bytes: 20608