#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>
 
#define init ios::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL);
#define setpr(x) cout << setprecision((x))
#define PI atan(1)*4
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mk(a , b) { min((a) , (b)) , max((a) , (b)) }
#define flush cout.flush()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endc "\n"
#define MOD 1000000007
#define meme(x) memset((x), 0, sizeof((x)))
#define memo(x) memset((x),-1, sizeof((x)))
 
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;

// #define int ll

ll egcd(ll a, ll b, ll &x, ll &y){
    if (a == 0LL){  
        x = 0LL;  
        y = 1LL;  
        return b;  
    }
    ll xx, yy;
    ll val = egcd(b % a, a, xx, yy);  
    x = yy - (b / a) * xx;  
    y = xx;  
    return val;
}

ll gcd(ll a, ll b){
    while(true){
        if(a>b)
            swap(a,b);
        if (a == 0) 
            return b; 
        ll p = b % a  , q = a;
        a = p;
        b = q;
    }
}
 
ll powerMod(ll x,ll y){
    ll res = 1;
    x %= MOD;
    while(y > 0){
        if(y & 1)res = (res*x)%MOD;
        y = y >> 1;
        x = (x * x)%MOD;
    }
    return res%MOD;
}

//  ==========     //\\       //||     ||====//||
//      ||        //  \\        ||     ||   // ||
//      ||       //====\\       ||     ||  //  ||
//      ||      //      \\      ||     || //   ||
//  ========== //        \\  ========  ||//====|| 
//  code

const int SIZE = 4e6 + 5;
vector<ll> tree, lazy;
vector<ll> a;
int defval = -1;

void build(int start, int end, int node){
    if(start > end)
        return;
    if(start == end){
        tree[node] = a[start];
        return;
    }
    int mid = start + (end - start) / 2;
    build(start, mid, 2 * node + 1);
    build(mid + 1, end, 2 * node + 2);

    tree[node] = max(tree[2 * node + 1], tree[2 * node + 2]);
}

void upd(int start, int end, int node, int idx, ll val){
    if(start > end || idx < start || idx > end)
        return;

    if(start == end){
        assert(idx == start);
        a[idx] += val;
        tree[node] += val;
        return;
    }

    int mid = start + (end - start) / 2;
    if(idx >= start && idx <= mid)
        upd(start, mid, 2 * node + 1, idx, val);
    else
        upd(mid + 1, end, 2 * node + 2, idx, val);

    tree[node] = max(tree[2 * node + 1], tree[2 * node + 2]);
}

ll query(int start, int end, int node, int left, int right){
    if(start > end || left > end || right < start)
        return defval;
    if(left <= start && end <= right){
        return tree[node];
    }

    int mid = start + (end - start) / 2;
    ll ans1 = query(start, mid, 2 * node + 1, left, right);
    ll ans2 = query(mid + 1, end, 2 * node + 2, left, right);
    ll ans = max(ans1, ans2);
    return ans;
}

void clr(int node){
    tree[node] += lazy[node];
    if(2 * node + 2 < lazy.size()){
        lazy[2 * node + 1] += lazy[node];
        lazy[2 * node + 2] += lazy[node];
    }
    lazy[node] = 0;
}

void updr(int start, int end, int node, int left, int right, int val){
    if(start > end || left > end || right < start)
        return;

    clr(node);
    if(start >= left && end <= right){
        tree[node] += val;
        if(2 * node + 2 < lazy.size()){ 
            lazy[2 * node + 1] += val;
            lazy[2 * node + 2] += val;
        }
        return;
    }

    int mid = start + (end - start) / 2;
    updr(start, mid, 2 * node + 1, left, right, val);
    updr(mid + 1, end, 2 * node + 2, left, right, val);
}

void solve(){
    ll n,i,j,k,l,p;
    cin >> n;
    a.resize(n + 1, 0);
    tree.resize(SIZE);
    build(0, n, 0);

    vector<ll> heights(n), val(n);
    for(auto &i : heights){
        cin >> i;
    }

    for(auto &i : val){
        cin >> i;
    }

    for(i = 0; i < n; i++){
        ll ans = query(0, n, 0, 0, heights[i] - 1);
        upd(0, n, 0, heights[i], val[i] + ans);
    }

    cout << query(0, n, 0, 0, n) << endc;
}

int32_t main(){
    init;
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


Measured memory_bytes: 39100