#include <bits/stdc++.h>

#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("03")

#define frpn		freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define mm(x) 		memset(x, 0, sizeof(x))
#define all(x)		(x).begin(), (x).end()
#define rs(x) 		((x) << 1 | 1)
#define ls(x) 		((x) << 1)
#define pb			push_back
#define int			long long
#define sz			size()
#define S 			second
#define F 			first

using namespace std;

const int N = 1e5;

int intx () {
	int x;
	cin >> x;
	return x;
}

void solve() {
	int n, s = 1, ans = 0;
	cin >> n;
	int a[n + 3], c[n + 3];
	map <int, int> mp;
	map <int, int> mp2;
	vector <int> mxa;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		mxa.pb(a[i]);
		mp[a[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];			
	}
	sort (all(mxa));
	reverse (all(mxa));
	for (int i = 0; i < mxa.sz; i++) mp2[mxa[i]] = i;
	while (mxa.sz > 0) {
		int point = mp[mxa[0]];
		int hr = 0, hl = 0;
		int cr = 0, cl = 0;
		for (int i = point + 1; i <= n; i++) {
			if (a[i] > hr) {
				hr = a[i];
				cr = c[i];
			} 
		}
		for (int i = point - 1; i >= s; i--) {
			if (a[i] > hl) {
				hl = a[i];
				cl = c[i];
			} 
		}
		if (cr > cl) {
			ans += c[point];
			for (int i = point; i <= n; i++) {
				mxa.erase(mxa.begin() + mp2[a[i]] - 1, mxa.begin() + mp2[a[i]]);
			}
			n = point - 1;
		} else {
			ans += c[point];
			for (int i = point; i >= s; i--) {
				mxa.erase(mxa.begin() + mp2[a[i]] - 1, mxa.begin() + mp2[a[i]]);
			}
			s = point + 1;
		}
//		cout << 1;
//		mxa.erase(mxa.begin(), mxa.begin() + 1);
	}
	cout << ans;
}

signed main () {
//	ios_base::sync_with_stdio(false);
//	cin.tie(nullptr);
//	cout.tie(nullptr);

	int t = 1;
//	cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

