#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,j,n) for(int i=(j);i<=((int)n);++i)
#define rev(i,n,j) for(int i=(n);i>=((int)j);--i)
typedef long long int ll;
#define int long long int
const ll INFL=0x3f3f3f3f3f3f3f3fLL;
const int INF=0x3f3f3f3f;
const int mod=1000000007;
#define endl "\n"
#define mem(a,val) memset(a,val,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define pb push_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define readmat(a, n, m) int a[n + 5][m + 5] = {}; rep(i, 1, n) {rep(j, 1, m) cin >> a[i][j];}
#define printmat(a, n, m) rep (i, 1, n) {rep (j, 1, m) cout << a[i][j] << " "; cout << endl;} cout << endl;
#define printarr(a, n) rep(i, 1, n) cout << a[i] << " "; cout << endl;
typedef std::map< int,int> mii;
typedef std::vector< int > vi;
typedef std::vector< vi > vvi;
typedef std::pair< int,int > ii;
using namespace std;
#define cerr cout
#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) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);}
const int N = 1e6+5, M = N;
vector <int>g[N];

int tree[800005];
int a[200005];
// array is 0 indexed 
// segment tree values start from index 1

void build(int node,int start,int end)
{
	if (start==end)
	{
		tree[node]=a[start];
		return ;
	}
	else 
	{
		int mid=(start+end)/2;
		build(2*node,start,mid);
		build(2*node+1,mid+1,end);
		tree[node]=max(tree[2*node],tree[2*node+1]);
	}
}

void update(int node,int start,int end,int idx,int val)
{
	int mid=(start+end)/2;
	if (start==end)
	{
		tree[node]=val;
		a[idx]=val;
		return;
	}
	else if (idx>mid)
	{
		update(2*node+1,mid+1,end,idx,val);
		tree[node]=max(tree[2*node],tree[2*node+1]);	
	}
	else 
	{
		update(2*node,start,mid,idx,val);
		tree[node]=max(tree[2*node],tree[2*node+1]);	
	}
}

int query(int node,int start,int end,int left,int right)
{
	if (left<=start and end<=right)
		return (tree[node]);
	else if (right<start or left>end)
		return -INFL;
	else
	{
		int mid=(start+end)/2;
		int ans1=query(2*node,start,mid,left,right);
		int ans2=query(2*node+1,mid+1,end,left,right);
		return (max(ans2,ans1));
	}
}


void solve()
{
	int n;
	cin>>n;
	int h[n+5];
	int b[n+5];
	for(int i = 0; i <n; ++i) 
	{
		cin>>h[i];
	}

	for(int i = 0; i <n; ++i) 
	{
		cin>>b[i];
	}

	for(int i = 0; i <n+5; ++i) 
	{
		a[i]=-INFL;
	}
	build(1,0,n);

	for(int i = 0; i <n; ++i) 
	{
		int mx=query(1,0,n,0,h[i]-1);
		mx=max(mx,0ll);
		update(1,0,n,h[i],b[i]+mx);
	}
	cout<<query(1,0,n,0,n)<<endl;
  

}



signed main()
{
  FAST;
#ifdef LOCAL
    freopen("C:\\Users\\hp\\Documents\\input.txt", "r", stdin);
    freopen("C:\\Users\\hp\\Documents\\output.txt", "w", stdout);
    std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
    start = std::chrono::high_resolution_clock::now();
#endif

solve();

#ifdef LOCAL
    end = std::chrono::high_resolution_clock::now();
    ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
    cout<<endl;
    cout << "\nElapsed Time: " << elapsed_time << "ms\n";
#endif

return 0;

}

// vector string set map first second continue break return upper_bound lower_bound length void sort
// stack queue pop size erase empty insert
// #Hala BBCF



Measured memory_bytes: 34688