#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll ft[200005], v[200005], h[200005], n;

void update(ll pos, ll x){
    for(ll i = pos; i <= n; i+=i&(-i))
        ft[i] = max(ft[i], x);
}

ll query(ll pos){
    ll res = 0;
    for(ll i = pos; i > 0; i-=i&(-i))
        res = max(res, ft[i]);
    return res;
}

int main(){
    
    scanf("%lld", &n);
    for(ll i = 1; i <= n; i++)
        scanf("%lld", &h[i]);
    for(ll i = 1; i <= n; i++)
        scanf("%lld", &v[i]);
    ll res = 0;
    for(ll i = 1; i <= n; i++){
        ll aux = query(h[i]-1) + v[i];
        update(h[i], aux);
        res = max(res, aux);
    }
    printf("%lld\n", res);
	return 0;
}
