/*||>>>> Praveen Ojha <<<<>>>> 29 August 2019 <<<<>>>> 21:31:11 <<<<||*/
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define mod 1000000007
#define inf (int)1e18+5
#define sz(x) (int)x.size()
#define PI 3.141592653589793238510
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define __ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define vi vector<int>
#define vpii vector<pair<int,int> > 
#define vvi vector<vector<int> >
#define PRINT_TIME cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." <<endl;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename   enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long double ld;
typedef pair<int,int> pii;
//Read Problems Carefully & Check for corner cases N=0,1 ?
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
const int N=200005;
int dp[N];
int n;
template <typename T>
class ST{
public:
  int n;
  vector <T> seg;
  vector <T> laz;
  ST(int x):n(4*x){
    seg.resize(n);
    build(1,x,1);
  }
  ST(int x,int is_laz):n(4*x){
    seg.resize(n);
    laz.resize(n);
    build(1,x,1);
  }
  void ini(T &tt){
  	tt=0;
    //Initialize the data to return at base cases;
  }
  T make_data(int st){
    T dt;
   	dt=dp[st];
    return dt;
  }
  T combine_data(T le,T ri){
    T par;
    par=max(le,ri);
    return par;
  }
  void build(int st,int en,int pos){
    if(st==en){
      seg[pos]=make_data(st);
      return;
    }
    int mid=(st+en)/2;
    build(st,mid,2*pos);
    build(mid+1,en,2*pos+1);
    seg[pos]=combine_data(seg[2*pos],seg[2*pos+1]);
  }
  void update(int st,int en,int ind,int val,int pos){
    if(st==en)
    {

      seg[pos]=val;
      return;
    }
    int mid=(st+en)/2;
    if(st<=ind && ind<=mid)
    update(st,mid,ind,val,2*pos);
	else
    update(mid+1,en,ind,val,2*pos+1);
    seg[pos]=combine_data(seg[2*pos],seg[2*pos+1]);
  }
  T query(int st,int en,int qst,int qen,int pos){
    if(en<qst || qen<st)
    {
      T dt;
      ini(dt);
      return dt;
    }
    if(qst<=st && en<=qen)
    {
      return seg[pos];
    }
    int mid=(st+en)/2;
    return combine_data( query(st,mid,qst,qen,2*pos) , query(mid+1,en,qst,qen,2*pos+1) );
  }
};
int32_t main(){__
	cin>>n;
	vi a(n),h(n);
	for(int i=0;i<n;i++)
		cin>>h[i];
	for(int i=0;i<n;i++)
		cin>>a[i];
	ST <int> st(n);
	for(int i=0;i<n;i++){
			dp[h[i]]=st.query(1,n,1,h[i],1)+a[i];
			st.update(1,n,h[i],dp[h[i]],1);
	}
	int ans=0;
	for(int i=0;i<n;i++){
		ans=max(ans,dp[h[i]]);
	}
	cout<<ans<<"\n";
return 0;
}

Measured memory_bytes: 11264