#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int,int>
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define all(V) V.begin(),V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
using namespace std;
inline int gcd(int a, int b) {
	while (b) {
		int c = a;
		a = b; b = c % b;
	}
	return a;
}
inline int lcm(int a, int b) {
	return a / gcd(a, b) * b;
}
bool isprime(int n) {
	if (n == 1)return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0)return false;
	}
	return true;
}
int mypow(int a, int b) {
	if (!b)return 1;
	if (b & 1)return mypow(a, b - 1) * a;
	int memo = mypow(a, b >> 1);
	return memo * memo;
}
int modpow(int a, int b, int m = mod) {
	if (!b)return 1;
	if (b & 1)return modpow(a, b - 1, m) * a % m;
	int memo = modpow(a, b >> 1, m);
	return memo * memo % m;
}
template<typename T, typename U>
class SegTree {
	int n = 1;
	T* node = NULL;
	U* lazy = NULL;
	bool* lazyflag = NULL;
	T nodee;
	function<T(T, T)> nodef;
	function<U(U, U)> lazyf;
	function<T(T, U, int, int)> updf;
	void eval(int k, int l, int r) {
		if (lazyflag[k]) {
			node[k] = updf(node[k], lazy[k], l, r);
			if (r - l > 1) {
				if (lazyflag[2 * k + 1])lazy[2 * k + 1] = lazyf(lazy[2 * k + 1], lazy[k]);
				else lazy[2 * k + 1] = lazy[k];
				if (lazyflag[2 * k + 2])lazy[2 * k + 2] = lazyf(lazy[2 * k + 2], lazy[k]);
				else lazy[2 * k + 2] = lazy[k];
				lazyflag[2 * k + 1] = lazyflag[2 * k + 2] = true;
			}
			lazyflag[k] = false;
		}
	}
public:
	SegTree(int m, T init, T nodee, function<T(T, T)> nodef, function<U(U, U)> lazyf, function<T(T, U, int, int)> updf) :nodee(nodee), nodef(nodef), lazyf(lazyf), updf(updf) {
		delete[] node;
		delete[] lazy;
		while (n < m)n *= 2;
		node = new T[2 * n], lazy = new U[2 * n], lazyflag = new bool[2 * n];
		for (int i = n - 1; i < 2 * n - 1; i++)node[i] = init;
		for (int i = n - 2; i >= 0; i--)node[i] = nodef(node[2 * i + 1], node[2 * i + 2]);
		rep(i, 2 * n)lazyflag[i] = false;
	}
	SegTree(int m, T nodee, function<T(T, T)> nodef, function<U(U, U)> lazyf, function<T(T, U, int, int)> updf, vector<T> initvec) :nodee(nodee), nodef(nodef), lazyf(lazyf), updf(updf) {
		if (m != initvec.size()) {
			cerr << "Invalid Initialization of SegmentTree" << endl;
			exit(1);
		}
		delete[] node;
		delete[] lazy;
		while (n < m)n *= 2;
		node = new T[2 * n], lazy = new U[2 * n], lazyflag = new bool[2 * n];
		for (int i = n - 1; i < 2 * n - 1; i++) {
			if (i - n + 1 < m)node[i] = initvec[i - n + 1];
			else node[i] = nodee;
		}
		for (int i = n - 2; i >= 0; i--)node[i] = nodef(node[2 * i + 1], node[2 * i + 2]);
		rep(i, 2 * n)lazyflag[i] = false;
	}
	~SegTree() {
		delete[] node;
		delete[] lazy;
	}
	void update(int a, int b, U x, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = n;
		eval(k, l, r);
		if (b <= l || r <= a)return;
		if (a <= l && r <= b) {
			lazyflag[k] = true;
			lazy[k] = x;
			eval(k, l, r);
		}
		else {
			update(a, b, x, 2 * k + 1, l, (l + r) / 2);
			update(a, b, x, 2 * k + 2, (l + r) / 2, r);
			node[k] = nodef(node[2 * k + 1], node[2 * k + 2]);
		}
	}
	T query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = n;
		eval(k, l, r);
		if (b <= l || r <= a)return nodee;
		if (a <= l && r <= b)return node[k];
		T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
		T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
		return nodef(vl, vr);
	}
};
class RAQRSQ :public SegTree<int, int> {
public:
	RAQRSQ(int size, const int& def = 0) :SegTree<int, int>(size, def, 0, [](int a, int b) {return a + b; }, [](int a, int b) {return a + b; }, [](int a, int b, int l, int r) {return a + (r - l) * b; }) {};
};
class RAQRMiQ :public SegTree<int, int> {
	using base = SegTree<int, int>;
public:
	RAQRMiQ(int size, const int& def = 0) :base(size, def, INF, [](int a, int b) {return min(a, b); }, [](int a, int b) {return a + b; }, [](int a, int b, int l, int r) {return a + b; }) {};
};
class RAQRMaQ :public SegTree<int, int> {
	using base = SegTree<int, int>;
public:
	RAQRMaQ(int size, const int& def = 0) :base(size, def, -INF, [](int a, int b) {return max(a, b); }, [](int a, int b) {return a + b; }, [](int a, int b, int l, int r) {return a + b; }) {};
};
class RUQRSQ :public SegTree<int, int> {
	using base = SegTree<int, int>;
public:
	RUQRSQ(int size, const int& def = 0) :base(size, def, 0, [](int a, int b) {return a + b; }, [](int a, int b) {return b; }, [](int a, int b, int l, int r) {return (r - l) * b; }) {};
};
class RUQRMiQ :public SegTree<int, int> {
	using base = SegTree<int, int>;
public:
	RUQRMiQ(int size, const int& def = 0) :base(size, def, INF, [](int a, int b) {return min(a, b); }, [](int a, int b) {return b; }, [](int a, int b, int l, int r) {return b; }) {};
};
class RUQRMaQ :public SegTree<int, int> {
	using base = SegTree<int, int>;
public:
	RUQRMaQ(int size, const int& def = 0) :base(size, def, -INF, [](int a, int b) {return max(a, b); }, [](int a, int b) {return b; }, [](int a, int b, int l, int r) {return b; }) {};
};
int n, h[200010], a[200010];
int dp[200010];
signed main() {
	cin >> n;
	rep(i, n)cin >> h[i];
	rep(i, n)cin >> a[i];
	RUQRMaQ st(n + 1, 0);
	rep(i, n) {
		dp[i] = st.query(0, h[i]) + a[i];
		st.update(h[i], h[i] + 1, dp[i]);
	}
	int ans = 0;
	rep(i, n)chmax(ans, dp[i]);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 13056