#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using ld = long double;
using ull = unsigned long long;
#define pb  push_back
#define pf  push_front
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const ll mod = 1e9 + 7;
const int N = 1e6;
const ll INF = 1e18;
vll tree1(N, -1), h, c;
int sz = 1;
struct segtree{
	vector<pair<ll, ll>> tree;
	void init(int n){
		while(sz < n){
			sz *= 2;
		}
		tree = vector<pll>(2*sz-1, {0, 0});
	}
	void set(int i, pll val, ll x = 0, ll lx = 0, ll rx = sz){
		if(rx - lx == 1){
			tree[x] = val;
			return;
		}
		ll m = (lx + rx)/2;
		if(i < m){
			set(i, val, 2*x+1, lx, m);
		}
		else{
			set(i, val, 2*x+2, m, rx);
		}
		tree[x] = max(tree[2*x+1], tree[2*x+2]);
 	}
 	pll get(int l, int r, ll x = 0, ll lx = 0, ll rx = sz){
 		if(lx >= r || rx <= l){
 			return {0, 0};
 		}
 		if(lx >= l && rx <= r){
 			return tree[x];
 		}
 		ll m = (lx + rx)/2;
 		return max(
 		get(l, r, 2*x+1, lx, m), 
 		get(l, r, 2*x+2, m, rx)
 		);
 	}
} st;

void build(ll x, ll l, ll r){
	if(l == r)return;
	pll mx = st.get(l, r);
	ll id = mx.second;
	tree1[x] = id;
	build(2*x+1, l, id);
	build(2*x+2, id+1, r);
}
vll d(N);
void dfs(ll v = 0){
	d[v] = c[tree1[v]];
	ll x1 = INF;
	ll x2 = INF;
	if(tree1[2*v+1] != -1){
		dfs(2*v+1);
		x1 = d[2*v+1];
	}
	if(tree1[2*v+2] != -1){
		dfs(2*v+2);
		x2 = d[2*v+2];
	}
	if(min(x1, x2) != INF){
		d[v] += min(x1, x2);
	}
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	h = vll(n);
	c = vll(n);
	st.init(n);
	for(int i = 0; i < n; i++){
		cin >> h[i];
		st.set(i, {h[i], i});
	}
	for(int i = 0; i < n; i++){
		cin >> c[i];
	}
	build(0, 0, n);
	dfs();
	cout << d[0] << '\n';
	return 0;
}