//weighter LIS
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
typedef long long LL;
int main()
{
	int n;
	int h[maxn], a[maxn];
	scanf("%d",&n);
	for(int i=0; i<n; i++) scanf("%d",&h[i]);
	for(int i=0; i<n; i++) scanf("%d",&a[i]);
	map<int,LL> m;
	m[0] = 0;
	for(int i=0; i<n; i++)
	{
		map<int,LL>::iterator iter, iter_tmp;
		iter = m.lower_bound(h[i]);
		iter--;
		LL val = iter->second + a[i];
		iter++;
		while(iter!=m.end() && iter->second <= val)
		{
			iter_tmp = iter;
			iter_tmp++;
			m.erase(iter);
			iter = iter_tmp;
		}
		m[h[i]] = val;
	}
	printf("%lld\n",m.rbegin()->second);
	return 0;
}