//#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>
using namespace std;

    typedef long long ll;
    typedef long double ld;

vector<ll> tr;

void upd(int l, int r, int i, int idx, ll x) {
    if (l + 1 == r) {
        tr[i] = x;
        return;
    }
    int mid = (l + r) / 2;
    if (idx < mid) {
        upd(l, mid, i * 2 + 1, idx, x);
    } else {
        upd(mid, r, i * 2 + 2, idx, x);
    }
    tr[i] = min(tr[i * 2 + 1], tr[i * 2 + 2]);
}

ll get(int l, int r, int i, int sl, int sr) {
    if (l >= sr || sl >= r) return 1e18;
    if (sl <= l && r <= sr) {
        return tr[i];
    }
    int mid = (l + r) / 2;
    return min(get(l, mid, i * 2 + 1, sl, sr), get(mid, r, i * 2 + 2, sl, sr));
}

void solve() {
    int n;
    cin >> n;
    vector<int> h(n);
    map<int, int> idx;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
        idx[h[i]] = i + 1;
    }
    vector<ll> c(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    tr.resize(4 * (n + 1), 1e18);
    vector<ll> dp(n + 1, 1e18);
    dp[0] = 0;
    upd(0, n + 1, 0, 0, 0);
    vector<pair<int, ll>> stk;
    vector<pair<int, ll>> stk2;
    for (int i = 1; i <= n; i++) {
        while (!stk.empty() && stk.back().first < h[i - 1]) {
            stk.pop_back();
        }
        ll mn = 1e18;
        int last = 0;
        if (!stk.empty()) {
            mn = stk.back().second;
            dp[i] = mn;
            last = idx[stk.back().first];
        }
        //cout << last << '\n';
        stk.push_back({h[i - 1], min(dp[i] + c[i - 1], mn)});
        dp[i] = min(dp[i], get(0, n + 1, 0, last, i) + c[i - 1]);
        upd(0, n + 1, 0, i, dp[i]);
        //cout << dp[i] << '\n';
    }
    cout << dp.back() << '\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

    int t = 1;
    //cin >> t;

    while (t--) {
        solve();
    }
}
