#include <cstdio>
#include <algorithm>
using namespace std;
int h[200200], v[200200];
const int N = 1 << 18;
long long b[N+N];
void update(int p, long long x) {
	p += N;
	b[p] = x;
	while (p > 1) {
		p >>= 1;
		b[p] = max(b[p+p], b[p+p+1]);
	}
}
long long get(int x) {
	long long r = 0;
	int p = 1;
	for (int i = 17; i >= 0; i--) {
		if (x >> i & 1) {
			r = max(b[p+p], r);
			p = p + p + 1;
		} else {
			p += p;
		}
	}
	return r;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &h[i]);
		--h[i];
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &v[i]);
		update(h[i], get(h[i]) + v[i]);
	}
	printf("%lld\n", *max_element(b + N, b + N + n));
	return 0;
}
