#include <bits/stdc++.h>

#define fi first
#define se second

#define pw(x) (1LL << x)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz(x) (int)(x).size()

using namespace std;

template < typename T >
bool umn (T& a, T  b) {return a > b ? a = b, 1 : 0;}
template < typename T >
bool umx (T& a, T  b) {return a < b ? a = b, 1 : 0;}

using ll = long long;
using ld = long double;

const int N = 5e5 + 5;

int n, a[N], c[N];
pair <int, int> t[4 * N];

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = {a[tl], tl};
        return;
    }
    int tm = (tl + tr) >> 1;
    build(v << 1, tl, tm);
    build(v << 1 | 1, tm + 1, tr);
    t[v] = max(t[v << 1], t[v << 1 | 1]);
}

pair <int, int> get(int v, int tl, int tr, int l, int r) {
    if (l > r) return {-2e9, -1};
    if (tl == l && tr == r) {
        return t[v];
    }
    int tm = (tl + tr) >> 1;
    return max(get(v << 1, tl, tm, l, min(tm, r)),
               get(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r));
}

vector <int> g[N];

void buildG(int v, int l, int r) {
    if (l >= r) return;

    int posL = get(1, 0, n - 1, l, v - 1).se;
    int posR = get(1, 0, n - 1, v + 1, r).se;

    if (posL != -1) {
        g[v].pb(posL);
    }

    if (posR != -1) {
        g[v].pb(posR);
    }

    buildG(posL, l, v - 1);
    buildG(posR, v + 1, r);
}

ll dp[N];

void dfs(int v) {
    if (sz(g[v]) == 0) {
        dp[v] = c[v];
        return;
    }

    if (sz(g[v]) == 1) {
        //dfs(g[v][0]);
        dp[v] = c[v];
        return;
    }

    if (sz(g[v]) == 2) {
        dfs(g[v][0]);
        dfs(g[v][1]);
        dp[v] = c[v] + min(dp[g[v][0]], dp[g[v][1]]);
        return;
    }
}

int main() {
#ifdef localy
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);
#endif // localy
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }

    build(1, 0, n - 1);
    int root = get(1, 0, n - 1, 0, n - 1).se;
    buildG(root, 0, n - 1);
    dfs(root);

    cout << dp[root] << "\n";

    return 0;
}
