#include <cstring>
#include <cassert>
#include <utility>
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <bitset>
#include <chrono>
#include <cstdlib>
#include <functional>
#include <tuple>
#include <climits>
#include <limits>
#include <deque>
#include <list>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <complex>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll              long long
#define pi              pair<int,int>
#define pll             pair<ll,ll>
#define pb              push_back
#define F               first
#define S               second
#define vi              vector<int>
#define vll             vector<ll>
#define endl            '\n'
#define all(v)          (v).begin(),(v).end()
#define FOR(i,a,b)      for(int i=a;i<b;i++)
#define MP(a,b)         make_pair(a,b)
#define debug(x)        cerr<< #x << " : " << x << endl;
#define debugp(p)       cerr<< #p << " : " << "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v)       cerr<< #v << " : " << "["; for(int i = 0; i < (v).size(); i++) cerr << v[i] << " "; cerr<<"]\n";
#define debugs(m)       cerr<< #m << " : [ "; for(auto itr = m.begin(); itr != m.end(); itr++) cerr <<*itr << " "; cerr << "]\n";
#define debugm(m)       cerr<< #m << " : [ "; for(auto itr = m.begin(); itr != m.end(); itr++) cerr <<"(" << itr->first << "," << itr->second << ") "; cerr << "]\n";

/* ordered set: 
tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>
member functions :
1. order_of_key(k) : number of elements strictly lesser than k
2. find_by_order(k) : k-th element in the set
*/

inline ll modexp(ll x, ll ex , ll md) {
    ll ans = 1ll;
    while (ex > 0) {
        if (ex & 1ll) ans = (ans * x) % md;
        ex >>= 1ll;
        x = (x * x) % md;
    }
    return ans;
}

const int inf = 1e9 + 7;

class node{
    public:
    ll mx;
    node (){
        node (0);
    }
    node (ll mx){
        this->mx = mx;
    }
};
 
template<typename T>
class segtree{
    public:
    vector<node> tree;
    segtree(vector<T> &v){
        int n = int (v.size());
        tree.resize (4 * n);
        build (0 , 0 , n - 1 , v);
    }
    node unite (node i , node j){
        return node (max (i.mx , j.mx));
    }
    void f (node &o , T v){
        o = node (v);
    }
    node bottom (T v){
        return node (v);
    }
    void build (int x , int s , int e , vector<T> &v){
        if (s == e){
            tree[x] = bottom(v[s]);
            return;
        }
        int m = (s + e) >> 1;
        build (x + x + 1 , s , m, v);
        build (x + x + 2 , m + 1 , e, v);
        tree[x] = unite (tree[x + x + 1] , tree[x + x + 2]);
    }
    void update (int x , int s , int e , int pos , T v){
        if (pos > e || pos < s) return;
        if (s == e){
            assert (s == pos);
            f(tree[x] , v);
            return;
        }
        int m = (s + e) >> 1;
        update (x + x + 1 , s , m , pos , v);
        update (x + x + 2 , m + 1 , e , pos , v);
        tree[x] = unite (tree[x + x + 1] , tree[x + x + 2]);
    }
    node query (int x , int s , int e, int l , int r){
        assert (r >= l);
        assert (!(e < l || s > r));
        if (s >= l && e <= r){
            return tree[x];
        }
        assert (s != e);
        int m = (s + e) >> 1;
        if (m < l || s > r) return query (x + x + 2 , m + 1 , e , l , r);
        else if (m + 1 > r || e < l) return query (x + x + 1, s , m , l , r);
        else{
            node lef = query (x + x + 1 , s , m , l , r);
            node rig = query (x + x + 2 , m + 1, e , l , r);
            return unite (lef , rig);
        }
    }
};
 

void solve(){
  int n;
  cin >> n;
  vector<int> h(n);
  vector<ll> a(n);
  for (int i = 0; i < n; i++){
    cin >> h[i];
    --h[i];
  }
  for (int i = 0; i < n; i++){
    cin >> a[i];
  }
  vector<ll> dp(n , 0);
  vector<ll> v(n , 0);
  segtree<ll> st(v);
  for (int i = 0; i < n; i++){
    dp[i] = a[i];
    ll mx = 0;
    if (h[i] >= 1)
      mx = st.query(0 , 0 , n - 1, 0 , h[i] - 1).mx;
    dp[i] = max (dp[i] , mx + a[i]);
    st.update (0 , 0 , n - 1, h[i] , dp[i]);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++){
    ans = max (ans , dp[i]);
  }
  cout << ans << endl;
}
int main(){
    ios :: sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr);
    
    int T = 1;
    // cin >> T;
    for(int tc = 1; tc <= T; tc++){
        // cout << "Case #" << tc << ": ";
        solve();
    }
    #ifdef LOCAL
    cerr << "Time elapsed\t: " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";       
    #endif
}



Measured memory_bytes: 12704