#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 2e5 + 10;
int n, a[maxn], h[maxn]; ll c[maxn], f[maxn];

void upd(int pos, ll x) {
	for (; pos <= n; pos += pos & -pos) {
		c[pos] = max(c[pos], x);
	}
}

ll query(int pos) {
	ll res = 0;
	for (; pos; pos &= pos - 1) {
		res = max(res, c[pos]);
	}
	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);
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ans = max(ans, (f[i] = query(h[i]) + a[i])), upd(h[i], f[i]);
	}
	printf("%lld", ans);
	return 0;
}