#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#define ll long long
#define lowbit(x) (x&-x)
using namespace std;
const int N = 2e5+7, p = 1e9+7;
ll tre[N];
int n, a[N], h[N];
void change(int p, ll v)
{
	for(int i = p; i <= n; i += lowbit(i))
	{
		tre[i] = max(tre[i], v);
	}
	return;
}
ll query(int p)
{
	ll res = 0;
	for(int i = p; i >= 1; i -= lowbit(i))
	{
		res = max(res, tre[i]);
	}
	return res;
}
int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &h[i]);
	}
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
	}
	for(int i = 1; i <= n; i++)
	{
		change(h[i], query(h[i])+a[i]);
	}
	printf("%lld\n", query(n));
	return 0;
}