#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <random>
#include <numeric>
#include <map>
#include <set>
#include <string>
#include <stack>

using namespace std;
using ll = long long;
using ld = long double;

#define deb(x) cerr << #x << ":\t" << x << '\n';
#define len(x) (ll) x.size()

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

int n;
vector <ll> h, c, dp, ind, tree, dpmn;

ll get(ll ql, ll qr, ll v = 0, ll l = 0, ll r = n) {
    if (r <= ql || qr <= l) return INF;
    if (l >= ql && r <= qr) return tree[v];
    return min(get(ql, qr, v * 2 + 1, l, (l + r) / 2), get(ql, qr, v * 2 + 2, (l + r) / 2, r));
}

void change(ll i, ll x, ll v = 0, ll l = 0, ll r = n) {
    if (i < l || i >= r) return;
    if (l == r - 1) {
        tree[v] = x;
        return;
    }
    change(i, x, v * 2 + 1, l, (l + r) / 2);
    change(i, x, v * 2 + 2, (l + r) / 2, r);
    tree[v] = min(tree[v * 2 + 1], tree[v * 2 + 2]);
}

void solve() {
    cin >> n;
    h.resize(n), c.resize(n), dp.resize(n, INF), ind.resize(n), tree.resize(4 * n, INF), dpmn.resize(n, INF);
    for (int i = 0; i < n; i++) cin >> h[i];
    for (int i = 0; i < n; i++) cin >> c[i];
    stack <int> x;
    for (int i = 0; i < n; i++) {
        while (x.size() && h[i] >= h[x.top()]) x.pop();
        if (x.size()) ind[i] = x.top();
        else ind[i] = -1;
        x.push(i);
    }
    dp[0] = c[0];
    dpmn[0] = c[0];
    change(0, 0);
    for (int i = 1; i < n; i++) {
        change(i, dp[i - 1]);
        ll mn = get(ind[i] + 1, i + 1) + c[i];
        if (ind[i] != -1) {
            mn = min(mn, dpmn[ind[i]]);
        }
        dpmn[i] = dp[i - 1] + c[i];
        if (ind[i] != -1) dpmn[i] = min(dpmn[ind[i]], dp[i - 1] + c[i]);
        dp[i] = min(dp[i], mn);
    }
    cout << dp[n - 1];
}

int main() {
    int T = 1;
    //    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
