#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const ll inf = 998244353;
#define all(a) a.begin(), a.end()

int t[(int)8e5 + 5];
vector<ll> h, c;
int n;

inline int Max(int a, int b){
    return (h[a] > h[b] ? a : b);
}

void build (int v, int tl, int tr) {
	if (tl == tr)
		t[v] = tl;
	else {
		int tm = (tl + tr) / 2;
		build (v*2, tl, tm);
		build (v*2+1, tm+1, tr);
		t[v] = Max(t[v*2], t[v*2+1]);
	}
}

int get_max (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return n;
	if (l == tl && r == tr)
		return t[v];
	int tm = (tl + tr) / 2;
	return Max (
		get_max (v*2, tl, tm, l, min(r,tm)),
		get_max (v*2+1, tm+1, tr, max(l,tm+1), r)
	);
}

ll solve2(int l, int r) {
    if(r - l == -1){
        return 0;
    }
    if(r - l == 0){
        return c[l];
    }
    int p = get_max(1, 0, n - 1, l, r);
    ll x = solve2(l, p - 1);
    ll y = solve2(p + 1, r);
    return c[p] + min(x, y);
}


void solve(){
    cin >> n;
    h.resize(n);
    c.resize(n);
    for(ll & i :h)cin >> i;
    for(ll & i : c)cin >> i;
    h.push_back(-1e9);
    build(1, 0, n - 1);
    cout << solve2(0, n - 1) << '\n';



}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //cin >> k;
    for (int i = 0; i < t; ++i) {
        solve();
    }
}
