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

const int N = (int)2e5+5;
int a[N], n, h[N];
long long segTree[4 * N], dp[N], ans;

void update(int node, int tl, int tr, int id, int val){
    if(tr == tl){
        segTree[node] = val;
        dp[id] = val;
    }
    else{
        int tm = (tl + tr) >> 1;
        if(tl <= id && id <= tm)
            update(node << 1, tl, tm, id, val);
        else
            update(node << 1 ^ 1, tm + 1, tr, id, val);
        segTree[node] = max(segTree[node << 1], segTree[node << 1 ^ 1]);
    }
}

long long query(int node, int tl, int tr, int l, int r){
    if(r < tl || l > tr)
        return 0;
    if(l <= tl && tr <= r)
        return segTree[node];
    int tm = (tl + tr) >> 1;
    long long r1 = query(node << 1, tl, tm, l, r);
    long long r2 = query(node << 1 ^ 1, tm + 1, tr, l, r);
    return max(r1, r2);
}

template <class Type>
class SegTree{
    Type T[N * 4], lazy[N * 4];
    Type defaultValue = 0;

    Type SegFunc(Type x, Type y){
        return max(x, y);
    }
    public :
    void update(int node, int treeLeft, int treeRight, int index, Type value){
        if(treeLeft == treeRight){
            T[node] = value;
        }
        else{
            int mid = (treeLeft + treeRight) >> 1;
            if(treeLeft <= index && index <= mid)
                update(node << 1, treeLeft, mid, index, value);
            else
                update(node << 1 ^ 1, mid + 1, treeRight, index, value);
            T[node] = SegFunc(T[node << 1], T[node << 1 ^ 1]);
        }
    }

    Type query(int node, int treeLeft, int treeRight, int queryLeft, int queryRight){
        if(queryRight < treeLeft || queryLeft > treeRight)
            return defaultValue;
        lazyupdate(node, treeLeft, treeRight);

        if(queryLeft <= treeLeft && treeRight <= queryRight)
            return T[node];

        int mid = (treeLeft + treeRight) >> 1;
        Type leftResult = query(node << 1, treeLeft, mid, queryLeft, queryRight);
        Type rightResult = query(node << 1 ^ 1, mid + 1, treeRight, queryLeft, queryRight);
        return SegFunc(leftResult, rightResult);
    }

    void lazyupdate(int node, int treeLeft, int treeRight){
        if(!lazy[node])
            return;
        T[node] += lazy[node] * (treeRight - treeLeft + 1);
        if(treeLeft != treeRight){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 ^ 1] += lazy[node];
        }
        lazy[node] = 0;
    }

    void rangeUpdate(int node, int treeLeft, int treeRight, int queryLeft, int queryRight, Type value){
        lazyupdate(node, treeLeft, treeRight);

        if(queryRight < treeLeft || queryLeft > treeRight)
            return;

        if(queryLeft <= treeLeft && treeRight <= queryRight){
            lazy[node] += value;
            lazyupdate(node, treeLeft, treeRight);
        }
        else{
            int mid = (treeLeft + treeRight) >> 1;
            rangeUpdate(node << 1, treeLeft, mid, queryLeft, queryRight, value);
            rangeUpdate(node << 1 ^ 1, mid + 1, treeRight, queryLeft, queryRight, value);
            T[node] = SegFunc(T[node << 1], T[node << 1 ^ 1]);
        }
    }
};

SegTree <long long> ST;

int main(){
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> h[i];
    }
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        long long resl = ST.query(1, 1, N - 5, 1, h[i]) + a[i];
        ST.update(1, 1, N - 5, h[i], resl);
        ans = max(ans, ST.query(1, 1, N - 5, h[i], h[i]));
    }
    cout << ans << '\n';
}


Measured memory_bytes: 11904