#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>

using namespace std;

const long long INF = 1e18;

int n, l[200001], r[200001], h[200001];
int t[800001];
long long ans[200001], c[200001];
pair <int, int> a[200001];
stack <int> st;

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

int get(int v, int tl, int tr, int l, int r) {
    if (r < tl || tr < l) 
        return -1;
    if (l <= tl && tr <= r)
        return t[v];
    int tm = (tl + tr) / 2;
    int L = get(v + v, tl, tm, l, r);
    int R = get(v + v + 1, tm + 1, tr, l, r);
    if (L == -1) 
        return R;
    if (R == -1)
        return L;
    if (h[L] > h[R])
        return L;
    return R;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> h[i];
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i <= n; i++)
        a[i].first = h[i], a[i].second = i;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        while (!st.empty() && h[st.top()] < h[i])
            st.pop();
        if (st.empty())
            l[i] = 1;
        else 
            l[i] = st.top() + 1;
        st.push(i);
    }
    while (!st.empty())
        st.pop();
    for (int i = n; i >= 1; i--) {
        while (!st.empty() && h[st.top()] < h[i]) 
            st.pop();
        if (st.empty())
            r[i] = n;
        else 
            r[i] = st.top() - 1;
        st.push(i);
    }
    build(1, 1, n);
    for (int i = 1; i <= n; i++) {
        int j = a[i].second;
        ans[j] = INF;
        if (l[j] != j) {
            int k = get(1, 1, n, l[j], j - 1);
            ans[j] = min(ans[j], c[j] + ans[k]);
        } else 
            ans[j] = min(ans[j], c[j]);
        if (r[j] != j) {
            int k = get(1, 1, n, j + 1, r[j]);
            ans[j] = min(ans[j], c[j] + ans[k]);
        } else 
            ans[j] = min(ans[j], c[j]);
    }
    cout << ans[a[n].second];
}

