#include <vector>
#include <iostream>
#include <limits>
using ll = long long;
template <typename T>
constexpr T INF() { return std::numeric_limits<T>::max() / 16; }
template <typename Monoid>
class SegTree
{
    static std::size_t SZ(const std::size_t n)
    {
        std::size_t ans = 1;
        for (; ans < n; ans <<= 1) {}
        return ans;
    }

public:
    using BaseMonoid = Monoid;
    using T = typename Monoid::T;
    SegTree(const std::size_t n) : size(n), half(SZ(n)), value(half << 1, Monoid::id()) {}
    template <typename InIt>
    SegTree(const InIt first, const InIt last) : size(distance(first, last)), half(SZ(size)), value(half << 1, Monoid::id())
    {
        std::copy(first, last, value.begin() + half);
        for (std::size_t i = half - 1; i >= 1; i--) { up(i); }
    }
    T get(const std::size_t a) const { return value[a + half]; }
    void set(std::size_t a, const T& val)
    {
        value[a += half] = val;
        while (a >>= 1) { up(a); }
    }
    T accumulate(std::size_t L, std::size_t R) const
    {
        T accl = Monoid::id(), accr = Monoid::id();
        for (L += half, R += half; L < R; L >>= 1, R >>= 1) {
            if (L & 1) { accl = acc(accl, value[L++]); }
            if (R & 1) { accr = acc(value[--R], accr); }
        }
        return acc(accl, accr);
    }
    template <typename Pred>
    std::size_t partitionPoint(const std::size_t L, const std::size_t R, const Pred& pred) const
    {
        auto prec = [&](auto&& self, const std::size_t index, const std::size_t left, const std::size_t right, const T& offset) -> std::pair<T, std::size_t> {
            if (right <= L or R <= left or pred(acc(offset, value[index]))) { return {Monoid::id(), R}; }
            if (index >= half) { return {value[index], index - half}; }
            const std::pair<T, std::size_t> lans = self(self, index << 1, left, (left + right) >> 1, offset);
            if (lans.second != R) { return lans; }
            return self(self, index << 1 | 1, (left + right) >> 1, right, acc(offset, lans.first));
        };
        return prec(prec, 1, 0, half, Monoid::id()).second;
    }
    std::vector<T> data() const { return std::vector<T>(value.begin() + half, value.begin() + half + size); }

private:
    void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); }
    const std::size_t size, half;
    std::vector<T> value;
    const Monoid acc{};
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const SegTree<T>& seg)
{
    os << "[";
    for (const auto e : seg.data()) { os << e << ","; }
    return (os << "]\n");
}
struct Max
{
    using X = ll;
    using T = X;
    T operator()(const T& a, const T& b) const { return std::max(a, b); }
    static constexpr T id() { return -INF<T>(); }
};

int main()
{
    int N;
    std::cin >> N;
    std::vector<int> h(N);
    for (int i = 0; i < N; i++) { std::cin >> h[i]; }
    std::vector<ll> a(N);
    for (int i = 0; i < N; i++) { std::cin >> a[i]; }
    SegTree<Max> seg(200001);
    seg.set(0, 0);
    for (int i = 0; i < N; i++) { seg.set(h[i], seg.accumulate(0, h[i]) + a[i]); }
    std::cout << seg.accumulate(0, 200001) << std::endl;
    return 0;
}


Measured memory_bytes: 6656