#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
 
int n;
int a[2000005], b[2000005];

pair < int, int > t[4 * 2000005];

pair < int, int > mrg(pair < int, int > x, pair < int, int > y){
	pair < int, int > c;
	c.first = max(x.first, y.first);
	if(x.first == c.first) c.second = x.second;
	else c.second = y.second;
	return c; 
}

void build(int v, int l, int r){
	if(l == r){
		t[v] = {a[l], l};
		return;
	}
	int m = ((l + r) >> 1);
	build((v << 1), l, m);
	build((v << 1) + 1, m + 1, r);
	t[v] = mrg(t[(v << 1)], t[(v << 1) + 1]);
}

pair < int, int > get(int v, int l, int r, int tl, int tr){
	if(r < tl || tr < l) return {0, 0};
	if(tl <= l && r <= tr) return t[v];
	int m = ((l + r) >> 1);
	return mrg(get((v << 1), l, m, tl, tr),
			   get((v << 1) + 1, m + 1, r, tl, tr));
}
 
ll dnc(int l, int r){
    if(l > r) return 0;
    if(l == r) return b[l];
    int id = get(1, 1, n, l, r).second;
    return b[id] + min(dnc(l, id - 1), dnc(id + 1, r)); 
}
 
void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];
    build(1, 1, n);
    cout << dnc(1, n);
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}