// BEST TEACHERS: NAZAROV SHARAF and USMONZODA SOMON
//                littbird           another_love
#include <bits/stdc++.h>
#define ONLINE_JUDGE
#define fi first
#define se second
#define pb push_back
#define yes cout << "YES"
#define no cout << "NO"
#define int long long
#define in insert
#define endl '\n'
#define ld long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
struct segtree{
    vector< pair<int,int> >tree;
    int size;
    void init(int n){
        size = 1;
        while(size < n)size *=2;
        tree.assign(size * 2 - 1,make_pair(LLONG_MIN,-1));
    }
    void build(vector<int> &a,int x,int lx,int rx){
        if(rx - lx == 1){
            tree[x].se = lx;
            if(lx < a.size()){
                tree[x].fi = a[lx];
            }
        }
        else{
            int m = (lx + rx) / 2;
            build(a,x * 2 + 1,lx,m);
            build(a,x * 2 + 2,m,rx);
            tree[x] = combine(tree[x * 2 + 1],tree[x * 2 + 2]);
        }
    }
    void build(vector<int> &a){
        init(a.size());
        build(a,0,0,size);
    }
    pair<int,int> combine(pair<int,int>a,pair<int,int>b) {
        if(a.fi > b.fi) {
            return a;
        }
        else{
            return b;
        }
    }
    pair<int,int> mn(int l, int r,int x,int lx,int rx){
        if(lx >= r or l >= rx){
            return make_pair(LLONG_MIN,-1);
        }
        if(lx >= l and rx <= r){
            return tree[x];
        }
        int m = (lx + rx) / 2;
        auto s1 = mn(l,r,x * 2 + 1,lx,m);
        auto s2 = mn(l,r,x * 2 + 2,m,rx);
        return combine(s1,s2);
    }
    int mn(int l,int r){
        return mn(l,r,0,0,size).se;
    }
};
int n;
vector<int>c;
vector<int>a;
segtree st;
int f(int l,int r) {
    if(r - l == 1){
        return c[l];
    } 
    else if(r - l == 0) {
        return 0;
    }
    int mx = a[l];
    int pos = st.mn(l,r);
    int lx = f(l,pos);
    int rx = f(pos + 1, r);
    return max(lx,rx) + c[pos];
}
void solve() {
    cin >> n;
    st = segtree();
    c.assign(n,0);
    a.assign(n,0);
    for(int i =0 ;i < n;i ++){
        cin >> a[i];
    }
    for(int i =0 ;i < n;i ++){
        cin >> c[i];
    }
    st.init(a.size());
    st.build(a);
    cout <<f(0,n);
}
signed main() {
#ifndef ONLINE_JUDGE
    // For getting input from input.txt file
    freopen("input.txt", "r", stdin);
    // Printing the Output to output.txt file
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
   // cin >> tt;
    for (int q = 1; q <= tt; q++) {
        solve();
        cout << endl;
    }
}
