#include <bits/stdc++.h>
#define MAX_N 200000
using namespace std;
typedef long long lnt;
template <class T> inline void read(T &x) {
	x = 0; int c = getchar(), f = 1;
	for (; !isdigit(c); c = getchar()) if (c == 45) f = -1;
	for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0');
}
int n, h[MAX_N+5], a[MAX_N+5]; lnt tr[MAX_N+5];
void upd(int p, lnt x) {for (; p <= n; p += (p&-p)) tr[p] = max(tr[p], x);}
lnt qry(int p) {lnt ret = 0; for (; p; p -= (p&-p)) ret = max(ret, tr[p]); return ret;}
int main() {
	read(n);
	for (int i = 1; i <= n; i++) read(h[i]);
	for (int i = 1; i <= n; i++) read(a[i]);
	for (int i = 1; i <= n; i++)
		upd(h[i], qry(h[i])+a[i]);
	return printf("%lld\n", qry(n)), 0;
}
