#include "bits/stdc++.h"
using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define prvec(v)                                  lop(R15,0,v.size())cout << v[R15] <<" "; cout << endl;
#define prarr(arr,s1,e1)                          lop(R15,s1,e1)cout << arr[R15] << " "; cout << endl;

#define endl                                      '\n'
#define ff                                        first
#define ss                                        second
#define pii                                       pair<int,int>

#define pb                                        push_back
#define all(v)                                    v.begin(),v.end()         

#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
#define rtn                                       if(0)return;
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { rtn cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{     rtn 
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

inline void INP();
const int N = 3e5 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;

int n, Q;
int arr[N+2];


// Segment Tree

class Segtree{
public:
     
     // for '0' index  queries 

     int sz;
     int *segt;
     int *from;

     Segtree(int x, int *array)
     {  
        sz = x;
        segt = new int[4*sz];
        from = array;
        build();
     }

     int ff(int a,int b)
     {
          return max(a,b);        // change here
     }

     int queryUtil(int l, int r, int i, int L, int R)
     {    
          if( r < L or R < l ) return -inf;     // change here 
          if( L <= l && r <= R)   return segt[i];

          int mid = l + (r-l)/2;
          int a = queryUtil(l    , mid, 2*i + 1, L, R);
          int b = queryUtil(mid+1, r  , 2*i + 2, L, R);

          return ff(a,b);                    
     }

     int upd(int i)
     {    

         int a = segt[ 2*i + 1 ];
         int b = segt[ 2*i + 2 ];

         return ff(a,b);         
     }

     void build()
     {         

          buildUtil(0, sz-1, 0);                  // l, r, i
     }

     int query(int L, int R)
     {    
          return queryUtil(0, sz-1, 0, L, R);     // l, r, i, L, R
     }

     void update(int idx ,int val)
     {
          updateUtil(0, sz-1, 0, idx, val);       // l, r , i, idx , val
     }

     void buildUtil(int l, int r , int i)
     {    

          if( l == r )
          {    
               segt[i] = from[r];
               return;
          }

          int mid = l + (r-l)/2;
          buildUtil(l,   mid, 2*i + 1 );
          buildUtil(mid+1, r, 2*i + 2 );

          segt[i] = upd(i);

     }



     void updateUtil( int l, int r, int i, int idx, int val)
     {

          if( l > idx || r < idx ) return ;
          
          if( l == r and l == idx )
          {
               segt[i] = val;
               from[idx] = val;
               return;
          }

          int mid = l + (r-l)/2;
          updateUtil(l, mid, i*2 + 1, idx, val);
          updateUtil(mid+1, r, i*2 + 2, idx, val);

          segt[i] = upd(i);
     }

};



void solve()
{
      cin >> n;
      vector< pii > vec(n);
      lop(i,0,n) cin >> vec[i].ff;
      lop(i,0,n) cin >> vec[i].ss;
      int ans = 0;
      Segtree sgt(N-2, arr);

      lop(i,0,n)
      {
            int h = vec[i].ff;
            int b = vec[i].ss;
            int val = sgt.query(0, h-1);
            sgt.update(h, val+b);
            ans = max(ans, val + b);
      }

      cout << ans;
      
}


int32_t main()
{
      
      ios_base::sync_with_stdio(0);   cin.tie(0);   cout.tie(0);
      int t = 1;
      //cin >> t;
      while(t--) solve();

}











inline void INP()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
}


// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //






Measured memory_bytes: 14208