#pragma region

#ifdef LOCAL
#define debug cout
#else
#define debug \
    if (0) cout
#endif

#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "[ ";
    for (auto e : v) os << e << " ";
    return os << "]";
}

template <typename T, typename V>
ostream& operator<<(ostream& os, set<T, V> v) {
    os << "{ ";
    for (auto e : v) os << e << " ";
    return os << "}";
}

template <typename T, typename V, typename S>
ostream& operator<<(ostream& os, map<T, V, S> v) {
    os << "{ ";
    for (auto e : v) os << e << " ";
    return os << "}";
}

template <typename T, typename V>
ostream& operator<<(ostream& os, pair<T, V> x) {
    return os << "( " << x.first << ", " << x.second << " )";
}

template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (auto& e : v) is >> e;
    return is;
}

template <typename T, typename V>
istream& operator>>(istream& is, pair<T, V>& x) {
    return is >> x.first >> x.second;
}

void solve();

int main() {
#ifdef LOCAL
    freopen("data.txt", "r", stdin);
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    solve();
}

#pragma endregion

template <typename T>
struct segtree {
    std::vector<T> t, a;

    /** Customize this! **/
    inline T combine(T a, T b) {
        return max(a, b);
    }

    segtree(int n) {
        a = std::vector<T>(n);
        t = std::vector<T>(4 * a.size());
        build(1, 0, a.size() - 1);
    }

    segtree(std::vector<T> a)
        : a(a) {
        t = std::vector<T>(4 * a.size());
        build(1, 0, a.size() - 1);
    }

    void build(int u, int l, int r) {
        if (l != r) {
            int m = (l + r) / 2;
            build(2 * u, l, m);
            build(2 * u + 1, m + 1, r);
            t[u] = combine(t[2 * u], t[2 * u + 1]);
        } else {
            t[u] = a[l];
        }
    }

    // Returns the answer for [l, r]
    T query(int l, int r) {
        if (l > r) return 0;
        return query(1, 0, a.size() - 1, l, r);
    }

    T query(int u, int tl, int tr, int l, int r) {
        if (l <= tl && tr <= r) {
            return t[u];
        }
        int tm = (tl + tr) / 2;
        if (l <= tm && tm < r) {
            return combine(query(2 * u, tl, tm, l, r), query(2 * u + 1, tm + 1, tr, l, r));
        } else if (tm < l) {
            return query(2 * u + 1, tm + 1, tr, l, r);
        } else {
            assert(tm >= r);
            return query(2 * u, tl, tm, l, r);
        }
    }

    void update(int i, T v) {
        if (v > a[i]) update(1, 0, a.size() - 1, i, v);
    }

    // Updates a[i] = v
    void update(int u, int tl, int tr, int i, T v) {
        if (tl == tr) {
            t[u] = v;
        } else {
            int tm = (tl + tr) / 2;
            if (i <= tm) {
                update(2 * u, tl, tm, i, v);
            } else {
                update(2 * u + 1, tm + 1, tr, i, v);
            }
            t[u] = combine(t[2 * u], t[2 * u + 1]);
        }
    }
};

void solve() {
    int n;
    cin >> n;
    vector<int> h(n);
    vector<ll> a(n);
    cin >> h >> a;
    for (int i = 0; i < n; ++i) --h[i];
    segtree<ll> tree(n);
    for (int i = 0; i < n; ++i) {
        ll k = 1LL * tree.query(0, h[i] - 1) + a[i];
        tree.update(h[i], k);
    }
    cout << tree.query(0, n - 1) << "\n";
}

Measured memory_bytes: 10368