#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

//namespaces
using namespace __gnu_pbds; 
using namespace std;

//MACROS
#define ld long double
#define pb push_back
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ull unsigned long long 
#define For(i,s,e) for (int i=(s); i<(e); i++)
#define Fod(i,s,e) for (int i=(s); i>(e); i--)
#define pii pair<ll,ll>
#define f first
#define s second
#define endl "\n"
#define all(container) container.begin(), container.end()
#define mp make_pair

//debug
#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

//Templates
template <typename T>
using ordered_set =tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }

//globals
ll const prime1 = 304933;
ll const prime2 = 15486277;
ll const mod = 1e9+7;
ll const inf = 1e18;
ll const size_1d = 1e6 + 7;
ll const size_2d = 1e3 + 7;

//functions
ll power(ll x, ll y, ll mod = 2e18){ ll ans = 1;x %= mod;while(y){if(y&1)ans = (x * ans) % mod;x = (x * x) % mod;y >>= 1;}return ans;}
ll modInverse(ll a, ll m){ll m0 = m;ll y = 0, x = 1;if (m == 1) return 0;while (a > 1){ ll q = a / m;ll t = m;m = a % m,a = t;t = y;y = x - q * y;x = t;}if(x < 0) x += m0;return x;} 
ll gcdext(ll a,ll b,ll *x = 0, ll *y = 0){if(a == 0){*x = 0;*y = 1;return b;}ll x1,y1;ll gcd1 = gcdext(b%a,a,&x1,&y1);*x = y1 - (b/a)*x1;*y = x1;return gcd1;}
int randomize(){return (rand()%10000);} 

// CODE STARTS HERE
struct Node
{
    ll x = 0;
    Node() {}
    Node(ll x) : x(x) {}
};
 
ll n;
Node seg[4*size_1d];

void merge(int ti){
    if(seg[ti*2 + 1].x >= seg[ti*2 + 2].x)
        seg[ti] = seg[ti*2 + 1];
    else seg[ti] = seg[ti*2 + 2];
}
Node check(Node t1, Node t2){
    if(t1.x >= t2.x) return t1;
    return t2;
}
void build(ll ti,ll l,ll r,std::vector<ll> &v){
    if(l==r){
        seg[ti] = v[l];
        return;
    }
    ll mid = (l+r)/2;
    build(2*ti + 1,l,mid,v);
    build(2*ti + 2,mid+1,r,v);
    merge(ti);
    return;
}
Node query(ll ti,ll l,ll r,ll left,ll right){
    if(l>right || r<left) return Node(0);
    if(l>=left && r<=right) return seg[ti];
    ll mid = (l+r)/2;
    Node t1 = query(2*ti + 1,l,mid,left,right);
    Node t2 = query(2*ti + 2,mid+1,r,left,right);
    return check(t1,t2);
}
void update(ll ti,ll l,ll r,ll index,Node x){
    if(l==r){
        seg[ti]=x;
        return;
    }
    ll mid = (l+r)/2;
    if(index<=mid) update(2*ti + 1,l,mid,index,x);
    else update(2*ti+2,mid+1,r,index,x);
    merge(ti);
    return ;
}
int main(){
    IOS
    cin >> n;
    std::vector<int> h(n);
    For(i,0,n){
        cin >> h[i];
        h[i]--;
    }
    std::vector<int> v(n);
    For(i,0,n) cin >> v[i];
    ll ans = 0;
    For(i,0,n){
        Node max1 = query(0,0,n-1,0,h[i] - 1);
        update(0,0,n-1,h[i],Node(v[i] + max1.x));
        ans = max(v[i] + max1.x,ans);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 33152