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

/* Requires GNU C++ */
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace __gnu_pbds;

/*
  typedef base_name<
  key,
  mapped,
  key_comp_func,
  tag_specifying_datastructure,
  updating_node_invariants_policy> my_name;


  my_name s;
  s.insert(x);
  s.find_by_order(i); // index->value
  s.order_of_key(x); // value->index
*/

namespace std {
  typedef trie<
    std::string,
    null_type,
    trie_string_access_traits<>,
    pat_trie_tag,
    trie_prefix_search_node_update> pref_trie;

  typedef tree<
    int,
    null_type,
    std::less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update> order_set;
}

#ifdef HACKERLIB
#include "../src/hackerlib.h"
#endif

#define ioFix ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define PI (2.0 * acos(0.0))
#define INF (int)1e9
#define LINF (long long)1e18
#define EPS 1e-9

#define ll long long
#define ull unsigned long long
#define ld long double

#define pii pair<int, int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define pcc pair<char, char>
#define pbb pair<bool, bool>

#define tiii tuple<int, int, int>
#define tlll tuple<long long, long long, long long>

#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vld vector<long double>
#define vvld vector<vector<long double>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vvs vector<vector<string>>

#define REP(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define FOR(i, from, to) for(int (i) = from; (i) < (to); (i)++)
#define RFOR(i, from, to) for(int (i) = to-1; (i) >= (from); (i)--)
#define RANGE(i, from, to) for(int (i) = from; (i) <= (to); (i)++)
#define RRANGE(i, from, to) for(int (i) = to; (i) >= (from); (i)--)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()

#define pb push_back
#define mp make_pair
#define mt make_tuple

#define pow2(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchVector(v) { for(int xvx = 0; xvx < (v).size(); xvx++) { cout << (v)[xvx] << " "; } cout << endl; }
#define watchArray(arr, n) { for(int xax = 0; xax < (n); xax++) { cout << (arr)[xax] << " "; } cout << endl; }
#define watchMatrix(x, rows, cols) for(int r = 0; r < (rows); r++) { for(int c = 0; c < (cols); c++) { cout << (x)[r][c] << " "; } cout << endl; }



struct lazy_max_seg_tree {
  std::vector<ll> tree, lazy;
  ll n, height;

  lazy_max_seg_tree(std::vector<ll> v) {
    tree = std::vector<ll>(2*(n = v.size()));
    lazy = std::vector<ll>(n);
    height = sizeof(ll) * 8 - __builtin_clz(n);

    // Fill leaves i.e. tree[n, 2n-1]
    for(ll i = 0; i < n; i++)
      tree[n+i] = v[i];

    // Compute parents i.e. tree[0, n-1]
    for(ll i = n-1; i > 0; i--)
      tree[i] = merge(tree[2*i], tree[2*i+1]);
  }

  void incr(ll l, ll r, ll val) {
    ll l0 = (l += n);
    ll r0 = (r += n+1);
    for(; l < r; l >>= 1, r >>= 1) {
      if(l&1) propagate(l++, val);
      if(r&1) propagate(--r, val);
    }
    updateParents(l0);
    updateParents(r0-1);
  }

  ll query(ll l, ll r) {
    l += n, r += n+1;
    push(l);
    push(r - 1);
    ll res = identifier();
    for(; l < r; l >>= 1, r >>= 1) {
      if(l&1) res = merge(res, tree[l++]);
      if(r&1) res = merge(res, tree[--r]);
    }
    return res;
  }

private:
  void propagate(ll p, ll val) {
    tree[p] += val;
    if(p < n) lazy[p] += val;
  }

  void updateParents(ll p) {
    while(p > 1)
      p >>= 1, tree[p] = merge(tree[2*p], tree[2*p+1]) + lazy[p];
  }

  void push(ll p) {
    for (ll s = height; s > 0; s--) {
      int i = p >> s;
      if(lazy[i] == 0) continue;
      propagate(2*i, lazy[i]);
      propagate(2*i+1, lazy[i]);
      lazy[i] = 0;
    }
  }

  ll merge(ll v1, ll v2) {
    return std::max(v1, v2);
  }

  ll identifier() {
    return -2e9;
  }

};


int main() {
  ioFix;
  ll N;
  cin >> N;
  vi H(N);
  REP(i, N)
    cin >> H[i];
  vi A(N);
  REP(i, N)
    cin >> A[i];

  vll T(N+5);
  lazy_max_seg_tree t(T);

  ll best = 0;
  REP(i, N) {
    ll upd = t.query(0, H[i]-1) + A[i];
    ll cur = t.query(H[i], H[i]);
    t.incr(H[i], H[i], upd - cur);
    best = max(best, upd - cur);
  }

  cout << best << endl;

}


Measured memory_bytes: 9728