// Slower C++ solution:
/*...Part - 01...*/



#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <string>

#include <cmath>

#include <vector>

#include <set>

#include <map>

#include <unordered_set>

#include <unordered_map>

#include <stack>

#include <queue>

#include <deque>

#include <iterator>

#include <bitset>

#include <assert.h>

#include <new>

#include <sstream>

//#include <bits/stdc++.h>

using namespace std ;



/*...Part - 02...*/



typedef long long               ll ;

typedef long double             ld ;

typedef unsigned long long      ull ;

typedef pair<int,int>           pii ;

typedef pair<ll,ll>             pll ;

typedef vector<int>             vi ;

typedef vector<ll>              vll ;

typedef vector<vector<int>>     vvi ;



int Int(){int x ; scanf("%d",&x) ; return x ;}

ll Long(){ll x ; scanf("%lld",&x) ; return x ;}



/*...Part - 03...*/

/*....Debugger....*/



#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {cout << endl ;}

template<typename T, typename... Args>

void err(istream_iterator<string> it, T a, Args... args) {

    cerr << *it << " = " << a << ' ' ;

    err(++it, args...);

}



/*...Part - 04...*/

/*...Needed to change according to problem requirements...*/



const int N          = (int)2e5 + 5 ;

const int maxN       = (int)1e6 + 6 ;

const ll  Mod        = (ll)1e9 + 7 ;

const int inf        = (int)2e9 ;

const ll  Inf        = (ll)1e18 ;



/*..........................................................*/

/*...Part - 05...*/



#define     debug(x)    cerr << #x << " = " << x << '\n' ;

#define     rep(i,b,e)  for(__typeof(e) i = (b) ; i != (e + 1) - 2 * ((b) > (e))  ; i += 1 - 2 * ((b) > (e)))

#define     Int         Int()

#define     Long        Long()

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

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

#define     ff          first

#define     ss          second

#define     pb          push_back

#define     eb          emplace_back

#define     mem(a)      memset(a , 0 ,sizeof a)

#define     memn(a)     memset(a , -1 ,sizeof a)



/*...Part - 06...*/

/*...... ! Code start from here ! ......*/



ll dp[N] ;



struct segment{

	ll tree[N << 2] ;

	segment(){

		mem(tree) ;

	}

	void upd(int i , int b , int e , int p , ll v){

		if(b > p or e < p)return ;

		if(b == e){

			tree[i] = v ;

			return ;

		}

		int mid = (b + e) >> 1 ;

		upd(i << 1 , b , mid , p , v) ;

		upd(i << 1 | 1 , mid + 1 , e , p , v) ;

		tree[i] = max(tree[i << 1], tree[i << 1 | 1]) ;

	}



	ll query(int i , int b , int e , int l , int r){

		if(b > r or e < l)return 0 ;

		if(l <= b and e <= r)return tree[i] ;

		int mid = (b + e) >> 1 ;

		return max(query(i << 1 , b , mid , l , r), query(i << 1 | 1 , mid + 1 , e , l , r)) ;

	}

}seg;







int main(){

    int test = 1 , tc = 0 ;

    while(test--){

        int n = Int ;

        ll res = 0 ;

        vi a(n + 1) ;

        for(int i = 1 ; i <= n ; ++i)a[i] = Int ;

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

        	int x = Int ;

        	ll k = seg.query(1, 1, n, 1, a[i]) + x ;

        	seg.upd(1, 1, n, a[i], k) ;

        	if(res < k)res = k ;

        }

        cout << res << '\n' ;

    }

    return 0 ;

}



/*...Always look at the part - 04...*/

/*...............END................*/






// Faster C++ solution for the same problem (measured speedup: 7.606635025963988x):
/*...Part - 01...*/



#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <string>

#include <cmath>

#include <vector>

#include <set>

#include <map>

#include <unordered_set>

#include <unordered_map>

#include <stack>

#include <queue>

#include <deque>

#include <iterator>

#include <bitset>

#include <assert.h>

#include <new>

#include <sstream>

//#include <bits/stdc++.h>

using namespace std ;



/*...Part - 02...*/



typedef long long               ll ;

typedef long double             ld ;

typedef unsigned long long      ull ;

typedef pair<int,int>           pii ;

typedef pair<ll,ll>             pll ;

typedef vector<int>             vi ;

typedef vector<ll>              vll ;

typedef vector<vector<int>>     vvi ;



int Int(){int x ; scanf("%d",&x) ; return x ;}

ll Long(){ll x ; scanf("%lld",&x) ; return x ;}



/*...Part - 03...*/

/*....Debugger....*/



#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {cout << endl ;}

template<typename T, typename... Args>

void err(istream_iterator<string> it, T a, Args... args) {

    cerr << *it << " = " << a << ' ' ;

    err(++it, args...);

}



/*...Part - 04...*/

/*...Needed to change according to problem requirements...*/



const int N          = (int)2e5 + 5 ;

const int maxN       = (int)1e6 + 6 ;

const ll  Mod        = (ll)1e9 + 7 ;

const int inf        = (int)2e9 ;

const ll  Inf        = (ll)1e18 ;



/*..........................................................*/

/*...Part - 05...*/



#define     debug(x)    cerr << #x << " = " << x << '\n' ;

#define     rep(i,b,e)  for(__typeof(e) i = (b) ; i != (e + 1) - 2 * ((b) > (e))  ; i += 1 - 2 * ((b) > (e)))

#define     Int         Int()

#define     Long        Long()

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

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

#define     ff          first

#define     ss          second

#define     pb          push_back

#define     eb          emplace_back

#define     mem(a)      memset(a , 0 ,sizeof a)

#define     memn(a)     memset(a , -1 ,sizeof a)



/*...Part - 06...*/

/*...... ! Code start from here ! ......*/



ll t[N] ;



void upd(int i, ll v, int n){

	while(i <= n){

		t[i] = max(t[i], v) ;

		i += (i & -i) ;

	}

}



ll query(int i){

	ll res = 0 ;

	while(i){

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

		i -= (i & -i) ;

	}

	return res ;

}







int main(){

    int test = 1 , tc = 0 ;

    while(test--){

        int n = Int ;

        ll res = 0 ;

        vi a(n + 1) ;

        for(int i = 1 ; i <= n ; ++i)a[i] = Int ;

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

        	int x = Int ;

        	ll k = query(a[i]) + x ;

        	upd(a[i], k, n) ;

        	if(res < k)res = k ;

        }

        cout << res << '\n' ;

    }

    return 0 ;

}



/*...Always look at the part - 04...*/

/*...............END................*/