#include "bits/stdc++.h"
using namespace std;
#define PIGRECO 3.141592653589793

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
	~debug() { cerr << endl; }
	eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
	ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
	*this << "[";
	for (auto it = d.b; it != d.e; ++it)
		*this << ", " + 2 * (it == d.b) << *it;
	ris << "]";
}
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
#define int long long

#define ll long long

struct node {
	int s, e;
	ll mn, mx, sum;
	bool lset;
	ll add_val, set_val;
	node * l, *r;
	node (int _s, int _e, int A[] = NULL): s(_s), e(_e), mn(0), mx(0), sum(0), lset(0), add_val(0), set_val(0), l(NULL), r(NULL) {

		if (A == NULL) return;
		if (s == e) mn = mx = sum = A[s];
		else {
			l = new node(s, (s + e) >> 1, A), r = new node((s + e + 2) >> 1, e, A);
			combine();
		}
	}
	void create_children() {
		if (s == e) return;
		if (l != NULL) return;
		int m = (s + e) >> 1;
		l = new node(s, m);
		r = new node(m + 1, e);
	}
	void self_set(ll v) {
		lset = 1;
		mn = mx = set_val = v;
		sum = v * (e - s + 1);
		add_val = 0;
	}
	void self_add(ll v) {
		if (lset) { self_set(v + set_val); return; }
		mn += v, mx += v, add_val += v;
		sum += v * (e - s + 1);
	}
	void lazy_propagate() {
		if (e == s) return;
		if (lset) {
			l->self_set(set_val), r->self_set(set_val);
			lset = set_val = 0;
		}
		if (add_val != 0) {
			l->self_add(add_val), r->self_add(add_val);
			add_val = 0;
		}
	}
	void combine() {
		if (l == NULL) return;
		sum = l->sum + r->sum;
		mn = min(l->mn, r->mn);
		mx = max(l->mx, r->mx);
	}
	void set(int x, int y, ll v) {
		if (s == x && e == y) { self_set(v); return; }
		int m = (s + e) >> 1;
		create_children(); lazy_propagate();
		if (x <= m) l->set(x, min(y, m), v);
		if (y > m) r->set(max(x, m + 1), y, v);
		combine();
	}

	ll range_max(int x, int y) {
		if (s == x && e == y) return mx;
		if (l == NULL || lset) return mx;
		int m = (s + e) >> 1;
		lazy_propagate();
		if (y <= m) return l->range_max(x, y);
		if (x > m) return r->range_max(x, y);
		return max(l->range_max(x, m), r->range_max(m + 1, y));
	}
	~node() {
		if (l != NULL) delete l;
		if (r != NULL) delete r;
	}
};










int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector<int>h(n + 1);
	vector<int>a(n + 1);

	int dp[n + 1];

	memset(dp, 0, sizeof(dp));

	node segment_tree(1, n, dp);


	for (int i = 1; i <= n; i++)cin >> h[i];
	for (int i = 1; i <= n; i++)cin >> a[i];

	for (int i = 1; i <= n; i++) {

		dp[h[i]] += a[i];

		if (h[i] > 1)
			dp[h[i]] += segment_tree.range_max(1, h[i] - 1);

		segment_tree.set(h[i], h[i], dp[h[i]]);



	}

	int ans = 0;

	for (int i = 1; i <= n; i++) {
		ans = max(ans, dp[i]);
	}
	cout << ans << endl;

}

Measured memory_bytes: 45424