#include<bits/stdc++.h>

#define ll long long
#define int long long
#define pb push_back
#define vec vector
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

using namespace std;

const int MAX = 2e5 + 7;

ll dp[MAX * 4];
ll lazy[MAX * 4];


void push(int v, int tl, int tr) {
    dp[v] = min(dp[v], lazy[v]);
    if (tl == tr) {
        return;
    }
    lazy[v * 2] = min(lazy[v], lazy[v * 2]);
    lazy[v * 2 + 1] = min(lazy[v], lazy[v * 2 + 1]);
}

void upd(int v, int tl, int tr, int l, int r, ll val) {
    push(v, tl, tr);
    if (l > r) return;
    if (tl == l && tr == r) {
        lazy[v] = min(lazy[v], val);
        push(v, tl, tr);
        return;
    }
    int tm = (tl + tr) / 2;
    upd(v * 2, tl, tm, l, min(r, tm), val);
    upd(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
    dp[v] = min(dp[v * 2], dp[v * 2 + 1]);
}

ll oo = 2e18;

ll get(int v, int tl, int tr, int l, int r) {
    push(v, tl, tr);
    if (l > r) return oo;
    if (tl == l && tr == r) return dp[v];
    int tm = (tl + tr) / 2;
    return min(get(v * 2, tl, tm, l, min(r, tm)),
                get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void solve(){
    int n;
    cin >> n;
    for (int i = 0; i < MAX * 4; i++) {
        dp[i] = oo;
        lazy[i] = oo;
    }
    vector<ll> a(n + 1), c(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> c[i];
    vector<int> l(n + 1, 0), r(n + 1, n + 1);
    vector<int> st;
    for (int i = 1; i <= n; ++i) {
        while (sz(st) && a[st.back()] < a[i])
            st.pop_back();
        if (sz(st))
            l[i] = st.back();
        st.push_back(i);
    }
    st.clear();
    for (int i = n; i >= 1; --i) {
        while (sz(st) && a[st.back()] < a[i])
            st.pop_back();
        if (sz(st))
            r[i] = st.back();
        st.push_back(i);
    }
    upd(1, 0, n, 0, 0, 0);
    for (int i = 1; i <= n; i++) {
        //l
        int R = r[i] - 1;
        ll val = get(1, 0, n, i - 1, i - 1);
        val += c[i];
        upd(1, 0, n, i, R, val);
       // cout << i << ' ' << R << ' ' << val << endl;
        //r
        int L = l[i];
        val = get(1, 0, n, L, i - 1);
        val += c[i];
        upd(1, 0, n, i, i, val);
       // cout << i << ' ' << i << ' ' << val << endl;
    }
    ll res = get(1, 0, n, n, n);
    cout << res;
}


int32_t main () {
    // freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    solve();
}
