// Original C++ solution (slower):
#include <bits/stdc++.h>     



//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4"



#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

#define all(x) (x).begin(), (x).end()

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)2e5+100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;



int n, dp[maxn],a[maxn],b[maxn], t[maxn];

vi g[maxn];

void upd(int pos,int val){

  for(;pos<maxn;pos=(pos|(pos+1)))

    t[pos]=max(t[pos],val);

}

int get(int r){

  int res=0;

  for(;r>=0;r=(r&(r+1))-1)

    res=max(res,t[r]);

  return res;

}

main () {

  cin>>n;

  forn(i,1,n) cin>>a[i];

  forn(i,1,n) cin>>b[i];

  forn(i,1,n){

    dp[i]=get(a[i]-1)+b[i];

    upd(a[i],dp[i]);

  }

  cout<<*max_element(dp+1,dp+n+1);

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f

#define INF 0x3f3f3f3f

#define pb push_back

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define ms(a, x) memset(a, x, sizeof(a))

#define for0(i, n) for(int i = 0; i < n; ++i)

#define for1(i, n) for(int i = 1; i <= n; ++i)

using namespace std;

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<long long, int> pli;

typedef pair<long long, long long> pll;

const int mod = 1e9 + 7;

const int N = 2e5+10;

const int M = 1e6+10;

/*=================================================================================*/

#define mid ((l+r)>>1)



struct node {

    int id,h;

    ll s;

}arr[N];

ll dp[N],maxx[N<<2];

void up(int rt) {

    maxx[rt] = max(maxx[rt<<1],maxx[rt<<1|1]);

}

void update(int rt,int l,int r,int x,ll v)

{

    if(l==r)

    {

        maxx[rt] = max(maxx[rt],v);

        return ;

    }

    if(x<=mid) update(rt<<1,l,mid,x,v);

    else update(rt<<1|1,mid+1,r,x,v);

    up(rt);

}

ll query(int rt,int l,int r,int x,int y)

{

    if(x>r||y<l) return 0;

    if(x<=l&&r<=y)

    {

        return maxx[rt];

    }

    if(x>mid) return query(rt<<1|1,mid+1,r,x,y);

    else if(y<=mid) return query(rt<<1,l,mid,x,y);

    else return max(query(rt<<1,l,mid,x,y),query(rt<<1|1,mid+1,r,x,y));

}

int main() {

    int n;

    ll ans = 0;

    scanf("%d", &n);

    for(int i = 1;i<=n;++i) scanf("%d",&arr[i].h);

    for(int i = 1;i<=n;++i) scanf("%lld",&arr[i].s),arr[i].id = i;

    for(int i = 1;i<=n;++i)

    {

        ll tmp = 0;

        if(arr[i].h>1)  tmp = query(1,1,n,1,arr[i].h-1);

        update(1,1,n,arr[i].h,tmp+arr[i].s);

    }

    printf("%lld\n",maxx[1]);

    return 0;

}

// Fastest known solution for this problem: