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

typedef long long Int;

Int* tree;
void update(int node, Int val) {
	while (node < tree[0]) 
		tree[node] = max(tree[node], val),
		node += node & -node;
}

Int get(int node) {
	Int res = 0;
	while (node) 
		res = max(res, tree[node]),
		node &= node - 1;
	return res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	vector<int> h(n);
	vector<Int> a(n);
	for (auto&x : h) cin >> x;
	for (auto&x : a) cin >> x;
	tree = new Int[n + 1];
	tree[0] = n + 1;
	for (int i = 1; i <= n; i++) tree[i] = 0;
	
	Int answer = 0;
	for (int i = 0; i < n; i++) {
		Int f_i = get(h[i]) + a[i];
		answer = max(answer, f_i);
		update(h[i], f_i);
	} 
	cout << answer;
}