// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define ent '\n'
#define all(x) x.begin(), x.end()
#define s second
#define f first
#define pb push_back
typedef long long ll;
using namespace std;
const int maxn = 3e6 + 250;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int K = 400;
const double Pi = acos(-1.0);
const long double eps = 1e-9;
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define int long long
int h[maxn], c[maxn], dp[maxn], pref[maxn];
void solve() {
    int n;
    cin >> n;
    for(int i = 1;i <= n; i++) {
        cin >> h[i];
        pref[i] = max(pref[i-1],h[i]);
    }
    for(int i = 1;i <= n; i++) {
        cin >> c[i];
    }
    int pos = 0;
    stack<int>st;
    multiset<int>t;
    for(int i = 1;i <= n; i++) {
        while(st.size()&&h[st.top()]<h[i]) {
            t.erase(t.find(dp[st.top()-1]+c[st.top()]));
            st.pop();
        }
        if(st.size()==0) {
            dp[i] = c[i];
        } else {
            int it = (*t.begin());
            dp[i] = min(it, dp[st.top()]+c[i]);
        }
        st.push(i);
        t.insert(dp[i-1]+c[i]);
    }
    cout << dp[n];
}
 
signed main () {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // freopen("position.in", "r" , stdin);
    // freopen("position.out", "w", stdout);
    int t=1;
    // cin >> t;
    for(int i = 1;i <= t; i++) {
        // cout << "Case " << i << ": ";
        solve();
    }
}