#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define sz(x)(int) x.size()
#define ins insert
#define speed ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define pf push_front
#define pb push_back
#define all(x) x.begin(), x.end()
#define pii pair < int, int >
#define pll pair < ll, ll >
#define vi vector < int >
#define vll vector < long long >
#define vpii vector < pair < int, int >>
#define vpll vector < pair < long long, long long >>
#define mem memset
#define debug cerr << "OK\n";
 
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count());
 
const ll INF = (ll)1e18;
const ll INF1 = (ll)1e14 + 1;
const int N = (int)5e5 + 5;
const int MAT = 2005;
const ll mod = (ll)1e9 + 7;
const ll mod1 = (ll)1e9 + 9;
const ll MAX = (ll)15000000 + 5;
const int P = 31;
const int K = 17;
const long double eps = 1e-9;
long double binpow(double a, ll n) {
    if (n == 0) return 1;
    if (n == 1) return (a);
    long double r = binpow(a, n / 2);
    if (n & 1) return (((r * r)) * a);
    return (r * r);
}
 
void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
 
int dx[4] = {
    0,
    -1,
    0,
    1
};
 
int dy[4] = {
    1,
    0,
    -1,
    0
};
 
ll add(ll a, ll b) {
    return (((a + b) % mod) + mod) % mod;
}
 
ll mul(ll a, ll b) {
    return (a * b) % mod;
}
 
int n;
int h[N], c[N], dp[N], pv[N], t[N << 2], mn[N];
 
void build(int v = 1, int tl = 0, int tr = n) {
    if (tl == tr) {
        t[v] = INF1;
        return;
    }
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
}
 
void upd(int i, int val, int v = 1, int tl = 0, int tr = n) {
    if (tl == tr) {
        t[v] = val;
        return;
    }
    int tm = (tl + tr) / 2;
    if (i <= tm) upd(i, val, v * 2, tl, tm);
    else upd(i, val, v * 2 + 1, tm + 1, tr);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
}
 
int get(int l, int r, int v = 1, int tl = 0, int tr = n) {
    if (tr < l || tl > r) return INF1;
    if (l <= tl && tr <= r) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return min(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm + 1, tr));
}
 
void yela() {
    memset(dp, 0x3f, sizeof dp);
    memset(mn, 0x3f, sizeof mn);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    upd(0, 0);
    stack<int> st;
    st.push(0);
    h[0] = INF1;
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        while (h[i] > h[st.top()]) {
            st.pop();
        }
        pv[i] = st.top();
        st.push(i);
    }
    for (int i = 1; i <= n; i++) {
        dp[i] = get(pv[i], i - 1) + c[i];
        dp[i] = min(dp[i], mn[pv[i]]);
        mn[i] = min(mn[pv[i]], dp[i] + c[i]);
        upd(i, dp[i]);
    }
    cout << dp[n] << "\n";
}
 
signed main() {
    speed;
    int T = 1;
    //cin >> T;
    for (int i = 1; i <= T; i++) {
        yela();
    }
    #ifdef ONPC
        cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
    #endif
    return 0;
}
