// Slower C++ solution:
#include <bits/stdc++.h>     



//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4"



#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

#define all(x) (x).begin(), (x).end()

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)2e5+100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;



int n, dp[maxn],a[maxn],b[maxn], t[maxn];

vi g[maxn];

void upd(int pos,int val){

  for(;pos<maxn;pos=(pos|(pos+1)))

    t[pos]=max(t[pos],val);

}

int get(int r){

  int res=0;

  for(;r>=0;r=(r&(r+1))-1)

    res=max(res,t[r]);

  return res;

}

main () {

  cin>>n;

  forn(i,1,n) cin>>a[i];

  forn(i,1,n) cin>>b[i];

  forn(i,1,n){

    dp[i]=get(a[i]-1)+b[i];

    upd(a[i],dp[i]);

  }

  cout<<*max_element(dp+1,dp+n+1);

}

// Faster C++ solution for the same problem (measured speedup: 6.37519745595801x):
#include <bits/stdc++.h>



#define Solved int main()

#define pb push_back

#define mp make_pair

#define F first

#define S second

#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

#define forn(i, x) for(int i = 1; i <= x; i++)

#define rep(i, x, y) for(int i = x; i <= y; i++)

#define sz(x) (int)x.size()

#define pii pair<int, int>

#define pll pair<ll, ll>

#define ll long long

#define ull unsigned long long

#define ld long double

#define mii map<int, int>

#define all(x) x.begin(), x.end()

#define lb lower_bound

#define ub upper_bound

using namespace std;



inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}



const int maxn = 1e9;

const ll inf = 1e18;

//const int mod = 998244353;

const int mod = 1e9 + 7;

const int N = 2e5 + 1;



int n, h[N], a[N];

pll dp[N][2];

ll t[4 * N];



ll getmax(int v, int tl, int tr, int l, int r) {

    if (l > r) return 0;

    if (l == tl && r == tr) return t[v];



    int tm = (tl + tr) >> 1;

    return max(getmax(v + v, tl, tm, l, min(tm, r)), getmax(v + v + 1, tm + 1, tr, max(tm + 1, l), r));

}



void update(int v, int tl, int tr, int pos, ll x) {

    if (tl == tr) t[v] = x;

    else {

        int tm = (tl + tr) >> 1;

        if (pos <= tm)

            update(v + v, tl, tm, pos, x);

        else

            update(v + v + 1, tm + 1, tr, pos, x);

        t[v] = max(t[v + v], t[v + v + 1]);

    }

}



pll maxx(pll a, pll b) {

    if (a.F > b.F) return a;

    else if (a.F == b.F && a.S < b.S) return a;

    return b;

}



void solve() {

    cin >> n;



    for (int i = 1; i <= n; i++)

        cin >> h[i];



    for (int i = 1; i <= n; i++)

        cin >> a[i];



    dp[1][1] = {a[1], h[1]};

    update(1, 1, n, h[1], a[1]);

    for (int i = 2; i <= n; i++) {

        dp[i][0] = maxx(dp[i - 1][0], dp[i - 1][1]);

        ll k = getmax(1, 1, n, 1, h[i] - 1);

        dp[i][1] = {k + a[i], h[i]};

        update(1, 1, n, h[i], dp[i][1].F);

        //cout << dp[i].F << ' ' << dp[i].S << endl;

    }

    cout << max(dp[n][1].F, dp[n][0].F);

}



Solved {

    SpeedForce;

	int tt = 1;

    //cin >> tt;

	while (tt--) {

		solve();

		cout << '\n';

	}



}