#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> a, weight;
vector<pair<int, int>> st;

void build(int v, int tl, int tr) {
    if (tl + 1 == tr) {
        st[v] = {a[tl], tl};
        return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm, tr);
    st[v] = max(st[v * 2], st[v * 2 + 1]);
}

pair<int, int> get(int l, int r, int v, int tl, int tr) {
    if (l >= tr || r <= tl)
        return {-1, -1};
    if (l <= tl && tr <= r)
        return st[v];
    int tm = (tl + tr) / 2;
    return max(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm, tr));
}
long long f(int l, int r) {
    if (l == r) return 0;
    if (r == l + 1) {return weight[l];}
    int ind = get(l, r, 1, 0, n).second;
    return weight[ind] + min(f(l, ind), f(ind + 1, r));
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    a.resize(n); weight.resize(n);
    st.resize(5 * n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i=0; i<n;i++)
        cin >> weight[i];
    build(1, 0, n);

    cout << f(0, n);
}