import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

enum mod = 1_000_000_007L;

struct Mint {
    long x;

    this(long a) {
        x = a % mod;
        if (x < 0) x += mod;
    }

    this(Mint a) {
        x = a.x;
    }

    ref Mint opAssign(long a) {
        this = Mint(a);
        return this;
    }

    ref Mint opOpAssign(string op)(Mint rhs) {
        static if (op == "+") {
            x += rhs.x;
            if (x >= mod) x -= mod;
        }
        static if (op == "-") {
            x -= rhs.x;
            if (x < 0) x += mod;
        }
        static if (op == "*") {
            (x *= rhs.x) %= mod;
        }
        static if (op == "/") {
            this *= rhs.inv();
        }
        return this;
    }

    ref Mint opOpAssign(string op)(long rhs) {
        static if (op == "^^") {
            this = powmod(this, rhs);
            return this;
        }
        else {
            return mixin("this " ~ op ~ "= Mint(rhs)");
        }
    }

    const Mint powmod(Mint a, long b) {
        Mint res = 1, p = a;
        while (b > 0) {
            if (b & 1) res *= p;
            p *= p;
            b /= 2;
        }
        return res;
    }

    const Mint inv() {
        return powmod(this, mod - 2);
    }

    Mint opUnary(string op)() if (s == "-") {
        return Mint(-x);
    }

    Mint opBinary(string op, T)(const T rhs) {
        return mixin("Mint(this) " ~ op ~ "= rhs");
    }

    Mint opBinaryRight(string op)(const long rhs) {
        return mixin("Mint(rhs) " ~ op ~ "= this");
    }

    bool opEquals(Mint a, Mint b) {
        return a.x == b.x;
    }

    bool opEquals(long rhs) {
        long y = rhs % mod;
        if (y < 0) y += mod;
        return x == y;
    }

    string toString() {
        import std.conv : to;
        return x.to!string;
    }
}

unittest {
    long powmod(long a, long b) {
        return b > 0 ? powmod(a, b / 2)^^2 % mod * a^^(b & 1) % mod : 1L;
    }
    auto a = Mint(2), b = Mint(3);
    assert(a + b == 5);
    assert(a + 5 == 7);
    assert(1 + b == 4);
    assert(a - b == mod - 1);
    assert(a * b == 6);
    assert(a / b == 2 * powmod(3, mod - 2));
    assert(a^^10 == 1024);

    Mint z;
    assert(z == 0);

    (z += 2) *= 3;
    assert(z == 6);
}

struct SegTree(T, alias op, T initValue = 0) {
    import std.functional : binaryFun;

    alias f = binaryFun!(op);

    T[] _data;
    int _treeSize, _lineSize = 1;

    // 配列の要素数 n だけ与えて初期化
    this(int n) {
        while (_lineSize < n) {
            _lineSize *= 2;
        }
        _treeSize = 2 * _lineSize - 1;
        _data = new T[](_treeSize);
        _data[] = initValue;
    }

    // 配列 a を与えて初期化
    this(T[] a) {
        auto n = a.length;
        while (_lineSize < n) {
            _lineSize *= 2;
        }
        _treeSize = 2 * _lineSize - 1;
        _data = new T[](_treeSize);
        _data[] = initValue;

        _data[_lineSize - 1 .. _lineSize - 1 + n] = a[];

        foreach_reverse (i; 0 .. _lineSize - 1) {
            _data[i] = f(_data[2 * i + 1], _data[2 * i + 2]);
        }
    }

    // a[i] = x
    void assign(int i, T x) {
        i += _lineSize - 1;
        _data[i] = x;

        while (i > 0) {
            i = (i - 1) / 2;
            _data[i] = f(_data[2 * i + 1], _data[2 * i + 2]);
        }
    }

    // a[i] = f(a[i], x)
    void act(int i, T x) {
        i += _lineSize - 1;
        _data[i] = f(_data[i], x);

        while (i > 0) {
            i = (i - 1) / 2;
            _data[i] = f(_data[2 * i + 1], _data[2 * i + 2]);
        }
    }

    alias add = act;

    // 要求区間 [a, b) に対して、対象区間 [l, r) の値を返す
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r == -1) {
            r = _lineSize;
        }
        if (a >= b) { // 要求区間が空である場合
            return initValue;
        }
        if (b <= l || r <= a) { // 要求区間が対象区間外の場合
            return initValue;
        }
        if (a <= l && r <= b) { // 要求区間が対象区間に含まれている場合
            return _data[k];
        }

        // どちらでもない場合は対象区間を２つに分けて再帰する

        auto leftValue = query(a, b, 2 * k + 1, l, l + (r - l) / 2);
        auto rightValue = query(a, b, 2 * k + 2, l + (r - l) / 2, r);

        return f(leftValue, rightValue);
    }
}

unittest {
    import std.stdio;
    import std.conv;
    import std.algorithm;
    import std.random;

    int[] a = [3, 1, 4, 1, 5, 9, 2, 6, 5];
    int n = a.length.to!int;

    // 和のセグメント木のテスト
    auto sumSegTree = SegTree!(int, "a + b", 0)(n);

    foreach (i; 0 .. n) {
        sumSegTree.add(i, a[i]);
    }

    foreach (i; 0 .. n) {
        foreach (j; i .. n + 1) {
            assert(sumSegTree.query(i, j) == a[i .. j].sum());
        }
    }

    // 適当に加算してみる
    foreach (i; 0 .. n) {
        int dif = uniform(0, 100);
        a[i] += dif;
        sumSegTree.add(i, dif);
    }

    foreach (i; 0 .. n) {
        foreach (j; i .. n + 1) {
            assert(sumSegTree.query(i, j) == a[i .. j].sum());
        }
    }

    // 配列を直接与えても初期化できる
    int[] b = [1, 1, 2, 3, 5, 8, 13, 21];

    auto sumSegTree2 = SegTree!(int, "a + b")(b);

    foreach (i; 0 .. b.length.to!int) {
        foreach (j; i .. b.length.to!int + 1) {
            assert(sumSegTree2.query(i, j) == b[i .. j].sum());
        }
    }

    // minのセグメント木のテスト
    auto minSegTree = SegTree!(int, min, 1 << 30)(n);

    foreach (i; 0 .. n) {
        minSegTree.assign(i, a[i]);
    }

    foreach (i; 0 .. n) {
        foreach (j; i + 1 .. n + 1) {
            assert(minSegTree.query(i, j) == a[i .. j].minElement);
        }
    }

    // 適当に変更してみる
    foreach (i; 0 .. n) {
        int v = uniform(0, 100);
        a[i] = v;
        minSegTree.assign(i, v);
    }

    foreach (i; 0 .. n) {
        foreach (j; i + 1 .. n + 1) {
            assert(minSegTree.query(i, j) == a[i .. j].minElement);
        }
    }
}


enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;


void main() {
    int N;
    scan(N);
    auto h = readln.split.to!(int[]);
    auto a = readln.split.to!(int[]);
    auto d = zatsu(h);
    auto st = SegTree!(long, max, 0)(N);

    auto dp = new long[](N + 1);
    long ans;
    foreach (i ; 0 .. N) {
        auto x = a[i] + st.query(0, d[i]);
        chmax(ans, a[i] + st.query(0, d[i]));
        st.assign(d[i], x);
        debug {
            writeln(st);
        }
    }

    writeln(ans);
}


int[] zatsu(int[] h) {
    int N = h.length.to!int;
    auto res = new int[](N);
    auto hs = h.enumerate.array.sort!"a.value < b.value"();
    foreach (i ; 0 .. N) {
        res[hs[i].index] = i;
    }
    return res;
}

Measured memory_bytes: 23696