#include <bits/stdc++.h>

using namespace std;

using ll = long long;

void solve()
{
    ll n;
    cin >> n;
    vector <ll> h(n);
    vector <ll> c(n);
    for(ll i = 0; i < n; i++)
    {
        cin >> h[i];
    }
    for(ll i = 0; i < n; i++)
    {
        cin >> c[i];
    }
    vector <ll> prev(n);
    vector <ll> stk;
    for(ll i = 0; i < n; i++)
    {
        while(stk.size() > 0 && h[stk[stk.size() - 1]] < h[i])
        {
            stk.pop_back();
        }
        if(stk.size() == 0)
        {
            prev[i] = -1;
        }
        else
        {
            prev[i] = stk[stk.size() - 1];
        }
        stk.push_back(i);
    }
    vector <ll> dp_l(n);
    vector <ll> dp_r(n);
    for(ll i = 0; i < n; i++)
    {
        if(prev[i] == -1)
        {
            dp_l[i] = c[i];
        }
        else
        {
            dp_l[i] = c[i] + min(dp_l[prev[i]], dp_r[prev[i]]);
        }
        if(prev[i] == -1)
        {
            dp_r[i] = c[i];
            if(i > 0)
            {
                dp_r[i] += min(dp_l[i - 1], dp_r[i - 1]);
            }
        }
        else
        {
            dp_r[i] = dp_r[prev[i]];
        }
    }
    cout << min(dp_l[n - 1], dp_r[n - 1]);
}

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

    //freopen("input.txt", "r", stdin);

    int t = 1;
    //cin >> t;
    while (t--)
        solve();
}
