#include<bits/stdc++.h>

using namespace std;
#define ll long long

const int sz = 200001;
int n, h[sz], a[sz];
ll dp[sz];

void update(int x, ll y) {
    while (x < sz){
        dp[x] = max(dp[x], y);
        x += x & -x;
    }
}

ll get(int x) {
    ll rt = -1;
    while(x>0){
        rt = max(rt, dp[x]);
        x -= (x & -x);
    }
    return rt;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    int i, j;
    for (i = 0; i < n; ++i) cin >> h[i];
    for (i = 0; i < n; ++i) cin >> a[i];
    for (i = 0; i < n; ++i) update(h[i], get(h[i]) + a[i]);
    cout << get(n) << endl;
}