#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n;
int h[200100];
const int mx = 1 << 18;
ll bit[mx];
void ins(int idx, ll val)
{
    while (idx < mx)
    {
        bit[idx] = max(bit[idx], val);
        idx += idx & -idx;
    }
}
ll gt(int idx)
{
    ll ret = 0;
    while (idx)
    {
        ret = max(ret, bit[idx]);
        idx -= idx & -idx;
    }
    return ret;
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &h[i]);
    }
    for (int i = 0, x; i < n; ++i)
    {
        scanf ("%d", &x);
        ins(h[i], x + gt(h[i]-1));
    }
    printf("%lld\n", gt(mx-1));

    return 0;
}
