#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937_64 rnd(time(0));
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()

const ll N = 2e5 + 5;
const ll INF = 1e15;

double binpow(double n, ll p) {
    if (p == 1) return n;
    if (p % 2 == 0) {
        ll f = binpow(n, p / 2);
        return f * f;
    }
    return binpow(n, p - 1);
}

void solve() {
    ll n; cin >> n;
    vector<ll> h(n + 1);
    for (int i = 1; i <= n; i++) cin >> h[i];
    vector<ll> c(n + 1);
    for (int i = 1; i <= n; i++) cin >> c[i];

    vector<ll> prev(n + 1), nxt(n + 1);
    vector<pair<ll, ll>> nw = {{INF, 0}};
    for (int i = 1; i <= n; i++) {
        while (nw.back().first < h[i]) {
            nw.pop_back();
        }
        prev[i] = nw.back().second;
        nw.push_back({h[i], i});
    }
    nw.clear();
    nw.push_back({INF, n + 1});
    for (ll i = n; i >= 1; i--) {
        while (nw.back().first < h[i]) {
            nw.pop_back();
        }
        nxt[i] = nw.back().second;
        nw.push_back({h[i], i});
    }

    vector<ll> dp(n + 1, INF);
    multiset<ll> open;
    vector<vector<ll>> to_close(n + 2);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = min(dp[i], dp[prev[i]] + c[i]);

        for (auto x : to_close[i]) {
            open.erase(open.find(x));
        }
        if (!open.empty()) {
            dp[i] = min(dp[i], *open.begin());
        }
        open.insert(dp[i - 1] + c[i]);
        to_close[nxt[i]].push_back(dp[i - 1] + c[i]);
        /*
        for (int j = i; j >= 1; j--) {
            if (h[j] > h[i] || (h[j] == h[i] && j != i)) break;
            dp[i] = min(dp[i], dp[j - 1] + c[i]);
        }
        for (int j = i; j <= n; j++) {
            if (h[j] > h[i] || (h[j] == h[i] && j != i)) break;
            dp[j] = min(dp[j], dp[i - 1] + c[i]);
        }
         */
        // cout << i << " " << dp[i] << '\n';
    }
    cout << dp[n] << '\n';
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    solve();

    return 0;
}