#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
typedef long long ll;
ll abi[maxn];
int N;
int h[maxn],b[maxn];
void update(int pos,ll v){
    for (;pos<= N;pos+=pos&-pos)
        abi[pos] = max(abi[pos],v);
}
ll query(int pos){
    ll sum = 0;
    for (;pos;pos-=pos&-pos)
        sum = max(abi[pos],sum);
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen(".in","r",stdin);
    cin >> N;
    for (int i = 1; i <= N;i++){
        cin >> h[i];
    }
    for (int i = 1; i <= N;i++){
        cin >> b[i];
    }
    for (int i = 1; i <= N;i++){
        ll x = query(h[i]-1)+b[i];
        update(h[i],x);
    }
    cout << query(N);
    return 0;
}
