#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(x) (x).begin(),(x).end()
#define rrng(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x,y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cout << #x << " = " << (x) << ", " << #y << " = " << (y) <<  ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second);
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) find(rng(v),x)!=v.end()
#define incls(s,c) s.find(c)!=string::npos
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n,k) ((n>>k)&1) // nのk bit目
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(rng(x)),x.end())
#define SP << " " <<
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vs = vector<string>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
typedef vector<pii> vp;
typedef vector<tiii> vt;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = (1<<30)-1;
const ll INFLL = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int MAX_N = 262144;
// セグメント木を持つグローバル配列(n2は要素数を2のべき乗にするから)
int n2, dat[2*MAX_N - 1];
void init(int n_) {
  n2 = 1;
  while (n2 < n_) n2 *= 2;
  for (int i = 0; i < 2*n2-1; i++) dat[i] = -INFLL;
}
// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  // 葉の節点
  k += n2 - 1;
  dat[k] = a;
  // 登りながら更新
  while (k > 0) {
    k = (k-1)/2;
    dat[k] = max(dat[k*2+1], dat[k*2+2]);
  }
}
// 区間max: [a,b)の最大値を求める(k: 節点の番号、l、rはその節点が[l,r)に対応づいていることを表す
// query(a,b,0,0,n2)と呼ぶ
int query(int a, int b, int k=0, int l=0, int r=n2) {
  if (r <= a || b <= l) return -INFLL;
  if (a <= l && r <= b) return dat[k];
  else {
    int vl = query(a,b,k*2+1,l,(l+r)/2);
    int vr = query(a,b,k*2+2,(l+r)/2,r);
    return max(vl, vr);
  }
}

/*
int main () {
  int n;
  n = 6;
  vi a = {1,3,6,6,4,2};
  n2 = n;
  init(n2);
  rep(i,n2) update(i,a[i]);
  showt(dat,6);
  show(query(0,1));
  return 0;
}
*/

signed main () {
  int n;
  cin >> n;
  vi h(n);
  rep(i,n) cin >> h[i];
  vi a(n);
  rep(i,n) cin >> a[i];

  init(n+1);
  update(0,0);
  rep(i,n) {
    int temp = query(0,h[i]);
    update(h[i],temp+a[i]);
  }
  //show2(n,n2);
  //show2(MAX_N,2*MAX_N - 1);

  int ans = query(0,n+1);
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 7424