#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int MN = 200010;
int n;
int h[MN], a[MN];
ll tree[MN];

void add(int k, ll v) {
    while (k <= n) {
        tree[k] = max(tree[k], v);
        k += k & -k;
    }
}

ll find_max(int k) {
    ll res = 0;
    while (k) {
        res = max(res, tree[k]);
        k -= k & -k;
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

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

    for (int i = 0; i < n; i++) {
        ll v = a[i] + find_max(h[i]);
        add(h[i], v);
    }
    cout << find_max(n) << endl;
    return 0;
}