#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,bmi2,fma")

using namespace std;
//using namespace __gnu_pbds;

//gp_gash_table<int,int>mp;
#define int long long
#define pb push_back
#define ld long double
#define endl '\n'
#define all(a) a.begin(), a.end()

const int INF = 2e18, MOD = 1e9 + 7, MAXN = 2e5;

vector<int> h;
vector<int> c;

pair<int, int> tree[MAXN * 4];

void build(int v, int l, int r) {
    if (l == r) {
        tree[v] = {h[l], l};
        return;
    }
    int m = (l + r) / 2;
    build(v * 2, l, m);
    build(v * 2 + 1, m + 1, r);
    tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
}

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


int GOAT(int l, int r, int n) {
    if (l > r) {
        return 0;
    }
    if (r == l) {
        return c[l];
    }
    auto [mx, pos] = get(1, l, r, 0, n - 1);
    auto tl = GOAT(l, pos - 1, n);
    auto tr = GOAT(pos + 1, r, n);
    return min(tl, tr) + c[pos];
}


void solve() {
    int n;
    cin >> n;
    h.resize(n);
    c.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    build(1, 0, n - 1);
    cout << GOAT(0, n - 1, n);
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while(t--) {
        solve();
    }
}