#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
using ll=long long;
const ll MOD = 1000000007;
ll INF = 90000000001000;

int N,k;
ll a[200005];
int h[200005];
ll bit[200005];
void update(int a,ll k){
    for(;a <=N;a+=a&-a) bit[a] = max(bit[a],k);
}

ll query(int a){
    ll res = 0;
    for(; a > 0;a -= a & -a) res = max(res,bit[a]);
    return res;
}

int main() {

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)
{
    update(h[i],query(h[i] )+ a[i]);
}

cout << query(N);
return 0;
}


