#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 h, c;
vector<vector<ll>> g(N);
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);
		}
		pll mx1 = tree[2*x+1];
		pll mx2 = tree[2*x+2];
		if(mx1.first == mx2.first){
			if(mx1.second > mx2.second){
				tree[x] = mx2;
			}
			else{
				tree[x] = mx1;
			}
		}
		else{
			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;
int n;
void build(ll x, ll l, ll r){
	if(l == r)return;
	pll mx = st.get(l, r);
	ll id = mx.second;
	if(id == 0){
		return;
	}
	if(id == n-1){
		return;
	}
	if(l != id){
		pll mx1 = st.get(l, id);
		g[mx.second].pb(mx1.second);
		build(mx1.second, l, id);	
	}
	if(r != id+1){
		pll mx2 = st.get(id+1, r);
		g[mx.second].pb(mx2.second);
		build(mx2.second, id+1, r);	
	}
}
vll d(N);
void dfs(ll v = 0){
	d[v] = c[v];
	ll x = INF;
	for(auto u : g[v]){
		dfs(u);
		x = min(x, d[u]);
	}
	if(x != INF){
		d[v] += x;
	}
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	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);
	pll mx = st.get(0, n);
	dfs(mx.second);
	cout << d[mx.second] << '\n';
	return 0;
}