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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using P = pair<int, int>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 29;
const ll INF64 = 1ll << 55;
const int mod = 1000000007;
//const int mod = 998244353;

template <class T>inline bool chmin(T &a, const T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline bool chmax(T &a, const T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint
{
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a)
  {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a)
  {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a)
  {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const
  {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const
  {
    mint res(*this);
    return res -= a;
  }
  mint &operator++()
  {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--()
  {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const
  {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const
  {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const
  {
    return pow(mod - 2);
  }
  mint &operator/=(const mint a)
  {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const
  {
    mint res(*this);
    return res /= a;
  }
};

//第二引数にfalseを入れると最大値取得
struct SegmentTree {
  private:
  int n;
  int sz;
  vector<ll> node;
  bool is_min;
  public:
  SegmentTree(vector<ll> v, bool flag = true) {
    sz = v.size();
    is_min = flag;
    n = 1; while(n < sz) n *= 2;
    node.resize(2*n-1, (is_min ? 1 : -1) * INF64);
    for(int i = 0; i < sz; ++i) node[i+n-1] = v[i];
    for(int i = n-2; i >= 0; --i) {
      if(is_min) node[i] = min(node[2*i+1], node[2*i+2]);
      else node[i] = max(node[2*i+1], node[2*i+2]);
    }
  }
  void update(int x, ll val) {
    x += (n-1);
    node[x] = val;
    while(x > 0) {
      x = (x-1)/2;
      if(is_min) node[x] = min(node[2*x+1], node[2*x+2]);
      else node[x] = max(node[2*x+1], node[2*x+2]);
  }
  }
  //[a, b) 区間の最大／最小値
  ll get_val(int a, int b, int k = 0, int l = 0, int r=-1) {
    if(r < 0) r = n;
    if(r <= a || b <= l) return (is_min ? 1 : -1) * INF64;
    if(a <= l && r <= b) return node[k];
    ll vl = get_val(a, b, 2*k+1, l, (l+r)/2);
    ll vr = get_val(a, b, 2*k+2, (l+r)/2, r);
    if(is_min) return min(vl, vr);
    else return max(vl, vr);
  }
  vector<ll> get_vec() {
    vector<ll> res;
    rep(i, sz) res.push_back(node[i+n-1]);
    return res;
  }
  ll get_node(int x) {
    return node[x+n-1];
  }
};

int main()
{
  int n; cin >> n;
  vector<ll> h(n), a(n);
  rep(i, n) cin >> h[i];
  rep(i, n) cin >> a[i];

  vector<ll> cell(n+1);
  SegmentTree dp(cell, false);

  ll res = 0;
  rep(i, n) {
    ll A = dp.get_val(0, h[i]);

    if(dp.get_val(h[i], h[i]+1) < A + a[i]) {
      dp.update(h[i], A+a[i]);
      chmax(res, A+a[i]);
    }
  }
  cout << res << endl;
}

Measured memory_bytes: 10624