#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
#include <tuple>
#include <cmath>
#include <complex>
#include <algorithm>
#include <utility>
#include <regex>
#include <cstdint>
#include <numeric>
#include <functional>
#include <cassert>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = numeric_limits<ll>::max() / 3;/* 1e+9a */
namespace utils{

    class Repeater{
        int st;
        int ed;
        int d;
    public:
        class rep_iterator{
            int i; int d;
        public:
            rep_iterator(int x, int pd) :i(x), d(pd) {}

            int& operator*(){ return i; }
            bool operator==(const rep_iterator& k) const { return i == k.i; }
            bool operator!=(const rep_iterator& k) const { return i != k.i; }
            rep_iterator& operator++(){ i+=d; return *this; }
        };

        explicit Repeater(int N) :st(0), ed(N), d(1) {}
        Repeater(int b, int e) :st(b), ed(e), d(1) {if(st > ed){ st--; ed--; d = -1; } }

        rep_iterator begin() const{ return rep_iterator(st, d); }
        rep_iterator end() const{ return rep_iterator(ed, d); }

    };

    template<class T, class Compare> using p_queue = priority_queue<T, vector<T>, Compare>;
    template<class T> using min_queue = p_queue<T, greater<T>>;
    template<class T> using max_queue = p_queue<T, less<T>>;

    template<class T> bool min_update(T& X, const T& A){ if(X > A) {X = A; return true;} return false; }
    template<class T> bool max_update(T& X, const T& A){ if(X < A) {X = A; return true;} return false; }
#define ALL(x) begin(x), end(x)
#define rALL(x) rbegin(x), rend(x)
}
using namespace utils;

template<class C = long long>
class segtree{
   vector<C> dat;
   ll N;
public:
    explicit segtree(long long n) :N(n), dat() {
       int  k = 1;
       while(k < N) k*=2;
       dat.resize(k*2, 0);
       N = k;
    }

    void update(int k, C a){
        k += N - 1;
        dat[k] = a;
        while(k > 0){
            k = (k-1) / 2;
            dat[k] = max(dat[k*2+1], dat[k*2+2]);
        }
    }

    C query(int a, int b, int k, int l, int r){
        if(r <= a or b <= l) return -INF;
        if(a <= l and r <= b) return dat[k];

        C v1 = query(a, b, 2*k+1, l, (l+r)/2);
        C v2 = query(a, b, 2*k+2, (l+r)/2, r);

        return max(v1, v2);
    }

    C maximum(int l, int r){
        return query(l, r, 0, 0, N);
    }

};


void sub_main(istream &is) {
    ll N;
    is >> N;
    if(!is) return;
    vector<ll> H(N), A(N);
    for (auto &&h : H) { is >> h; } // end h
    for (auto &&a : A) { is >> a; } // end a

    segtree<ll> sg(N+1);
    for (int i = 0; i < N; ++i) {
        auto pre = sg.maximum(0, H[i]);
        sg.update(H[i], pre + A[i]);
    } // end i

    cout << sg.maximum(0, N+1) << endl;

}

int main(int argc, char *argv[]) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(16) << scientific;
#ifdef DEBUG
    string test_cases = "test_Q.txt";
    cerr << "DEBUG MODE" <<  endl;
    cerr << test_cases << " -->" << endl;
    auto fs = fstream(test_cases, fstream::in);
    int lp = 0;
    while(fs) {
        lp++;
        cout << lp <<  "#------\n";
        sub_main(fs);
    }
    cout << "------#" << endl;
    if(lp <= 1) sub_main(cin);
#else
    sub_main(cin);
#endif
    return 0;
}


Measured memory_bytes: 7552