// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 6.376234384572917x):
/*

    -ensure correct output format

    -ensure printing required output

    -reread the problem statement

  */



#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll,ll>pll;

typedef pair<ll,pair<ll,ll>>plll;

#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));

#define vll(v) v.begin(),v.end()

#define all(x) x.rbegin(),x.rend()

#define min3(a, b, c)   min(a, min(b, c))

#define max3(a, b, c)   max(a, max(b, c))

#define F first

#define S second

#define in  freopen("input.txt", "r", stdin)

#define out  freopen("output.txt", "w", stdout)

#define minheap int,vector<int>,greater<int>

#define pb push_back

#define eb emplace_back

#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))

#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))

#define bug cout<<"BUG"<<endl;

const int Max = 2e5 + 10;

const int Mod = 1e9 + 7;

const double PI  =3.141592653589793238463;

bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)

{

       return (a.first > b.first);

}

ll lcm(ll a,ll b)

{

    if(a==0 || b==0)return 0;



    return a/__gcd(a,b)*b;

}



void input(ll ara[],ll n)

{

    for(ll i=0; i<n; i++)cin>>ara[i];

}

ll tree[Max+5];



ll query(ll idx)

{

    ll sum=0;



    while(idx>0)

    {

        sum=max(tree[idx],sum);



        idx-= idx & (-idx);



    }



    return sum;

}



void update(ll idx,ll x,ll n)

{

    while(idx<=n)

    {

        tree[idx]=max(tree[idx],x);



        idx+= idx & (-idx);

    }

}



int main()

{



   fastread();



   ll i,j,n,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;



      bool flag=false;



      string str;



      cin>>n;



      ll ara[n+2],dp[n+2];







      ll mx=0;



       input(ara,n);



       for(i=0; i<n; i++)

       {

         cin>>a;



        a+=query(ara[i]-1);



          update(ara[i],a,Max);



          mx=max(mx,a);





       }



       cout<<mx<<endl;



}