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

enum rangeOperaion{
    NUL,
    UPDATE,
    ADD
};
enum rangeOutput{
    MIN,
    MAX,
    SUM
};

class segTree{
    int N;
    rangeOutput output;
    function<int64_t(int64_t,int64_t)> func;

    int64_t *tree;
    pair<rangeOperaion, int64_t> *lazy;
    const pair<rangeOperaion, int64_t> nullopr=make_pair(NUL, 0);
    pair<int, int> *range;

    int getParent(int child){
        return child/2;
    }
    pair<int,int> getChildren(int parent){
        return make_pair(2*parent, 2*parent+1);
    }
    int lg(int64_t N){
        int lgN;
        for(lgN=0;(1<<lgN)<N;lgN++);
        return lgN;
    }
    inline int64_t defaultValue(rangeOutput o){
        if(o==MIN) return INT64_MAX;
        if(o==MAX) return INT64_MIN;
        if(o==SUM) return 0;
        else       return 0;
    }

    void setLazy(int pos, pair<rangeOperaion, int64_t> val){
        if(val.first==UPDATE) lazy[pos]=val;
        else if(lazy[pos].first==NUL) lazy[pos]=val;
        else lazy[pos].second+=val.second;
    }
    void eval(int pos){
        if(lazy[pos].first==NUL) return;
        if(pos<N){
            setLazy(getChildren(pos).first, lazy[pos]);
            setLazy(getChildren(pos).second, lazy[pos]);
        }
        if(lazy[pos].first==UPDATE){
            if(output==SUM) tree[pos]=lazy[pos].second*(range[pos].second-range[pos].first);
            else tree[pos]=lazy[pos].second;
        }else{
            if(output==SUM) tree[pos]+=lazy[pos].second*(range[pos].second-range[pos].first);
            else tree[pos]+=lazy[pos].second;
        }
        lazy[pos]=nullopr;
    }

    void operate(int64_t val, int pos, int left, int right, rangeOperaion op){
        eval(pos);
        if(right<=range[pos].first || range[pos].second<=left) return;
        if(left<=range[pos].first && range[pos].second<=right){
            setLazy(pos, make_pair(op, val));
            eval(pos);
            return;
        }
        operate(val, getChildren(pos).first, left, right, op);
        operate(val, getChildren(pos).second, left, right, op);
        tree[pos]=func(tree[getChildren(pos).first], tree[getChildren(pos).second]);
    }

    int64_t get(int pos, int left, int right){
        eval(pos);
        if(right<=range[pos].first || range[pos].second<=left) return defaultValue(output);
        if(left<=range[pos].first && range[pos].second<=right) return tree[pos];
        return func(get(getChildren(pos).first, left, right), get(getChildren(pos).second, left, right));
    }

    public:
    segTree(int n, rangeOutput o){
        N=(1LL<<lg(n));
        if(N<n) N*=2;
        tree=new int64_t[2*N];
        lazy=new pair<rangeOperaion,int64_t>[2*N];
        range=new pair<int,int>[2*N];

        output=o;
        for(int i=1;i<2*N;i++){
            tree[i]=defaultValue(o);
            lazy[i]=nullopr;
        }
        for(int i=2*N-1;i>0;i--){
            if(i>=N) range[i]=make_pair(i-N, i-N+1);
            else range[i]=make_pair(range[getChildren(i).first].first, range[getChildren(i).second].second);
        }
        
        switch(output){
            case MIN:
                func=[](int64_t a, int64_t b){ return min(a, b); };
                break;
            case MAX:
                func=[](int64_t a, int64_t b){ return max(a, b); };
                break;
            case SUM:
                func=[](int64_t a, int64_t b){ return a+b; };
                break;
        }
    }
    ~segTree(){
        delete[] tree;
        delete[] lazy;
        delete[] range;
    }
    void update(int64_t value, int left, int right){
        assert(0<=left && left<right && right<=N);
        operate(value, 1, left, right, UPDATE);
    }
    void add(int64_t value, int left, int right){
        assert(0<=left && left<right && right<=N);
        operate(value, 1, left, right, ADD);
    }
    void update(int64_t value, int pos){
        update(value, pos, pos+1);
    }
    void add(int64_t value, int pos){
        add(value, pos, pos+1);
    }
    int64_t queue(int left, int right){
        return get(1, left, right);
    }
    int64_t queue(int pos){
        return get(1, pos, pos+1);
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout.precision(12);
    cout.setf(ios_base::fixed, ios_base::floatfield);
    
    int n, h[200000];
    uint64_t a[200000];

    cin >> n;
    for(int i=0;i<n;i++) cin >> h[i];
    for(int i=0;i<n;i++) cin >> a[i];

    segTree st(200001, rangeOutput::MAX);
    st.update(0LL, 0);

    for(int i=0;i<n;i++){
        st.update(st.queue(0, h[i]) + a[i], h[i]);
    }
    cout << st.queue(0, n+1) << endl;
    
    return 0;
}

Measured memory_bytes: 18944