#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int MM = 200002;
int N, h[MM], a[MM]; ll bit[MM], ans;
ll qry(int x){
    ll ret = 0;
    for(int i=x; i; i-=i&-i) ret = max(ret, bit[i]);
    return ret;
}
void upd(int x, ll val){
    for(int i=x; i<MM; i+=i&-i) bit[i] = max(bit[i], val);
}
int main(){
    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%d", &h[i]);
    for(int i=1; i<=N; i++){
        scanf("%d", &a[i]); ll mx = qry(h[i]); upd(h[i], mx + a[i]); ans = max(ans, mx + a[i]);
    }
    printf("%lld\n", ans);
}